using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using Kreta.BusinessLogic.HelperClasses; using Kreta.Core; using Kreta.Core.ConnectionType; using Kreta.Core.FileService; using Kreta.DataAccess.Interfaces; using Kreta.DataAccessManual; using Kreta.DataAccessManual.Interfaces; namespace Kreta.BusinessLogic.Helpers { public class MerohelyHelper : LogicBase { #region Constructor public MerohelyHelper(IConnectionType connectionType) : base(connectionType) { } #endregion #region Merohely public DataSet MerohelyKereses(string merohelyNev, int? merohelyTipusId, int? keresesMerohelyMukodesiHelyId, int? isMeroallasFrissiteseSzukseges) { return Dal.CustomConnection.Run(ConnectionType, helper => { IMerohelyDal dal = helper.MerohelyDal(); return dal.MerohelyKereses(TanevId, IntezmenyId, merohelyNev, merohelyTipusId, keresesMerohelyMukodesiHelyId, isMeroallasFrissiteseSzukseges); }); } public MerohelyCO GetMerohelyById(int id) { return Dal.CustomConnection.Run(ConnectionType, helper => { IMerohelyDal dal = helper.MerohelyDal(); IMerohely merohely = dal.GetMerohelyById(id); var merohelyCo = new MerohelyCO(); merohelyCo.Id = merohely.ID; merohelyCo.Nev = merohely.Nev; merohelyCo.MukodesiHelyId = merohely.MukodesiHelyId; merohelyCo.MerohelyTipusId = merohely.MerohelyTipusId; merohelyCo.MeroallasFrissiteseSzukseges = merohely.MeroallasFrissiteseSzukseges; return merohelyCo; }); } public DataSet GetNemToroltMerohelyek() { return Dal.CustomConnection.Run(ConnectionType, helper => { IMerohelyDal dal = helper.MerohelyDal(); return dal.GetNemToroltMerohelyek(TanevId, IntezmenyId); }); } #endregion #region Meroallas public DataSet GetMeroallasListByMerohelyId(int merohelyId) { return Dal.CustomConnection.Run(ConnectionType, helper => { IMerohelyDal dal = helper.MerohelyDal(); return dal.GetMeroallasListByMerohelyId(merohelyId); }); } public MeroallasCO GetMeroallasById(int id) { return Dal.CustomConnection.Run(ConnectionType, helper => { IMerohelyDal dal = helper.MerohelyDal(); IMeroallas meroallas = dal.GetMeroallasById(id); MeroallasCO meroallasCo = new MeroallasCO(); meroallasCo.Id = meroallas.ID; meroallasCo.MerohelyId = meroallas.MerohelyId; meroallasCo.MeroallasErtek = meroallas.Ertek; meroallasCo.LeolvasasDatuma = meroallas.LeolvasasDatuma; if (meroallas.FeltoltottFajl.Count != 0) { meroallasCo.KepUtvonal = meroallas.FeltoltottFajl.Single().Utvonal; meroallasCo.KepKiterjesztes = meroallas.FeltoltottFajl.Single().Kiterjesztes; meroallasCo.KepId = meroallas.FeltoltottFajl.Single().ID; } return meroallasCo; }); } public void DeleteMeroallasById(int id) { Dal.CustomConnection.Run(ConnectionType, helper => { IMeroallasDal dal = helper.MeroallasDal(); dal.Delete(id); }); } public void SaveMeroallas(MeroallasCO meroallasCo, IFileService fileService) { bool hasNewImage = false; Dal.CustomConnection.Run(ConnectionType, helper => { IMeroallasDal dal = helper.MeroallasDal(); IMeroallas meroallasEntity = !meroallasCo.Id.IsEntityId() ? dal.Get() : dal.Get(meroallasCo.Id.Value); meroallasEntity.Ertek = meroallasCo.MeroallasErtek; meroallasEntity.LeolvasasDatuma = meroallasCo.LeolvasasDatuma; meroallasEntity.MerohelyId = meroallasCo.MerohelyId; if (meroallasCo.Id.IsEntityId()) { hasNewImage = UpdateMeroallasImage(helper, meroallasCo, fileService); dal.FullUpdate(meroallasEntity); } else { if (!string.IsNullOrWhiteSpace(meroallasCo.MeroallasKep.ContentAsBase64EncodedString)) { hasNewImage = InsertMeroallasImage(meroallasCo, fileService); } dal.Insert(meroallasEntity); } if (hasNewImage && !meroallasCo.IsDeleteImage) { InsertMeroallasImageData(helper.FeltoltottFajlDAL(), meroallasCo, meroallasEntity); } }); } private bool InsertMeroallasImage(MeroallasCO meroallasCo, IFileService fileService) { return SaveImageAndSetCoData(meroallasCo, fileService); } private bool UpdateMeroallasImage(IDalHandler h, MeroallasCO meroallasCo, IFileService fileService) { bool needInsertImageData = false; var oldPicture = GetPicture(fileService, meroallasCo.Id); bool hasOldImage = oldPicture != null; bool hasNewImage = !string.IsNullOrWhiteSpace(meroallasCo.MeroallasKep.ContentAsBase64EncodedString); if (hasNewImage) { if (hasOldImage) { if (!meroallasCo.MeroallasKep.ContentAsBase64EncodedString.Equals(oldPicture.ContentAsBase64EncodedString)) { DeleteFileAndFileData(h, fileService, meroallasCo.KepId.Value); needInsertImageData = SaveImageAndSetCoData(meroallasCo, fileService); } } else { needInsertImageData = SaveImageAndSetCoData(meroallasCo, fileService); } } else if (hasOldImage && meroallasCo.IsDeleteImage) { DeleteFileAndFileData(h, fileService, meroallasCo.KepId.Value); } return needInsertImageData; } public string SaveFile(IFileService fileService, string fileContent) { return fileService.WriteToFile(fileContent, Constants.FileServiceStorageName.Default); } public void DeleteFileAndFileData(IFileService fileService, int feltoltottFileId) { Dal.CustomConnection.Run(ConnectionType, (h) => DeleteFileAndFileData(h, fileService, feltoltottFileId)); } private void DeleteFileAndFileData(IDalHandler h, IFileService fileService, int feltoltottFileId) { var dal = h.FeltoltottFajlDAL(); var feltoltottFile = dal.Get(feltoltottFileId); fileService.DeleteFile(feltoltottFile.Utvonal); dal.Delete(feltoltottFileId); } #endregion Meroallas public List GetPictureList(IFileService fileService, int? meroallasId) { MeroallasCO meroallasCo = GetMeroallasById(meroallasId.Value); if (!string.IsNullOrWhiteSpace(meroallasCo.KepUtvonal) && !string.IsNullOrWhiteSpace(meroallasCo.KepKiterjesztes)) { return new List { new MeroallasUploadedFileCO { ContentAsBase64EncodedString = fileService.ReadImage(meroallasCo.KepUtvonal, meroallasCo.KepKiterjesztes) } }; } return null; } public MeroallasUploadedFileCO GetPicture(IFileService fileService, int? meroallasId) { MeroallasCO meroallasCo = GetMeroallasById(meroallasId.Value); if (!string.IsNullOrWhiteSpace(meroallasCo.KepUtvonal) && !string.IsNullOrWhiteSpace(meroallasCo.KepKiterjesztes)) { return new MeroallasUploadedFileCO { ContentAsBase64EncodedString = fileService.ReadImage(meroallasCo.KepUtvonal, meroallasCo.KepKiterjesztes) }; } return null; } private bool SaveImageAndSetCoData(MeroallasCO meroallasCo, IFileService fileService) { var kepInfo = new FileInfo(meroallasCo.MeroallasKep.Name); meroallasCo.KepUtvonal = SaveFile(fileService, meroallasCo.MeroallasKep.ContentAsBase64EncodedString); meroallasCo.KepNev = Path.GetFileNameWithoutExtension(kepInfo.Name); meroallasCo.KepKiterjesztes = kepInfo.Extension.Replace(".", string.Empty); return true; } private void InsertMeroallasImageData(IFeltoltottFajlDAL feltoltottFajlDal, MeroallasCO meroallasCo, IMeroallas meroallasEntity) { IFeltoltottFajl feltoltottFajlEntity = feltoltottFajlDal.Get(); feltoltottFajlEntity.Nev = meroallasCo.KepNev; feltoltottFajlEntity.Kiterjesztes = meroallasCo.KepKiterjesztes; feltoltottFajlEntity.Utvonal = meroallasCo.KepUtvonal; feltoltottFajlEntity.FeltoltesDatuma = DateTime.Now; feltoltottFajlDal.InsertMeroallasImageData(feltoltottFajlEntity, meroallasEntity); } } }