using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sleis.Models; using NHibernate; using Sleis.ViewModels; using Sleis.Models.CustomFields; using Sleis.Utility; using Sleis.Models.ErrorHandling; namespace Sleis.Service { public class AgencyControlDeviceService : ControlDeviceService, IControlDeviceService { public override List GetControlDeviceListByReport(int reportId) { List list = ControlDeviceData.GetControlDevicesByReport(reportId); LoadSimpleLookupItems(list); foreach (var cd in list) { cd.Pollutants = ControlDevicePollutantData.Get(p => p.ControlDeviceId == cd.Id).ToList(); foreach (ControlDevicePollutant p in cd.Pollutants) { p.Pollutant = LookupUtility.Get(p.Code); } } return list; } public override List GetControlDeviceListByReport(int reportId, int pageNum, int pageSize) { FacilityModel facility = FacilityData.GetReportFacility(reportId); if (facility != null && facility.Id > 0) { return GetControlDeviceListByFacility(facility.Id, pageNum, pageSize); } return new List(); } public override List GetControlDeviceListByFacility(int facilityId, int pageNum, int pageSize) { List list = ControlDeviceData.Get(x => x.FacilityId == facilityId, o => o.Asc(d => d.Identifier), pageNum, pageSize).ToList(); LoadSimpleLookupItems(list); return list; } public override int CountControlDevicesByFacility(int facilityId) { return ControlDeviceData.Count(x => x.FacilityId == facilityId); } public override int CountControlDevicesByReport(int reportId) { FacilityModel facility = FacilityData.GetReportFacility(reportId); if (facility != null && facility.Id > 0) { return CountControlDevicesByFacility(facility.Id); } throw new Exception("No facility associated with report."); } public override List GetControlDeviceListByFacility(int facilityId) { List list = ControlDeviceData.GetControlDevicesByFacility(facilityId); LoadSimpleLookupItems(list); return list; } public override void Update(ControlDeviceModel controlDevice, List customFields) { bool isAdd = controlDevice.Id < 1; AgencyControlDevice pcd = controlDevice as AgencyControlDevice; ControlDeviceModel oldCd = controlDevice.Id > 0 ? GetControlDevice(controlDevice.Id) : new AgencyControlDevice(); using (ISession session = ControlDeviceData.GetSession()) { using (ITransaction trans = session.BeginTransaction()) { controlDevice.LastUpdated = DateTime.Now; controlDevice.LastUpdatedBy = SessionUtility.CurrentUser; string masterComment = ""; if (pcd.IsMaster && !String.IsNullOrWhiteSpace(pcd.Comment)) { masterComment = pcd.Comment; pcd.Comment = ""; //clear before save } if (!ControlDeviceData.IsIdentifierUnique(controlDevice)) { throw new Utility.UniqueIdentifierException("Control Device Identifier must be unique."); } trans.Begin(); ControlDeviceData.SaveOrUpdate(pcd, session); //delete all previously assoicated pollutants ControlDevicePollutantData.DeleteAllByControlDevice(controlDevice.Id, session); //save all pollutants as new records foreach (AgencyControlDevicePollutant pollutant in controlDevice.Pollutants) { pollutant.Id = 0; //clear Id so always save as new. pollutant.ControlDeviceId = controlDevice.Id; if (!String.IsNullOrEmpty(pollutant.Code)) { ControlDevicePollutantData.SaveOrUpdate(pollutant, session); } } /* //delete all previously stored Ids ControlDeviceIdentifierData.DeleteAllByControlDevice(controlDevice.Id, session); //save all ids as new if (((AgencyControlDevice)controlDevice).Identifiers != null) { foreach (AgencyControlDeviceIdentifier id in ((AgencyControlDevice)controlDevice).Identifiers) { id.Id = 0; //clear Id so always saves as new. id.ControlDeviceId = controlDevice.Id; ControlDeviceIdentifierData.SaveOrUpdate(id, session); } } * */ if (((AgencyControlDevice)controlDevice).IsMaster && isAdd) { AgencyControlDeviceIdentifier id = new AgencyControlDeviceIdentifier() { ControlDeviceId = controlDevice.Id, IdentifierText = controlDevice.Identifier, ProSysCode = "SLEIS", EffectiveDate = DateTime.Now }; ControlDeviceIdentifierData.SaveOrUpdate(id, session); } //Save comment if master //add comment. We never delete these. keep a running tally. if (pcd.IsMaster && !String.IsNullOrWhiteSpace(masterComment)) { AgencyControlDeviceComment comment = new AgencyControlDeviceComment(); comment.Text = masterComment; comment.ControlDeviceId = pcd.Id; comment.UserId = SessionUtility.CurrentUser.Id; comment.Date = DateTime.Now; ControlDeviceData.SaveOrUpdate(comment, session); pcd.Comment = ""; //clear value so it does not save. } //save custom fields SaveCustomFields(customFields, controlDevice.Id, session); trans.Commit(); } } CheckAndRecalculateEmissions(oldCd, controlDevice); } public override ControlDeviceModel GetControlDevice(int id) { AgencyControlDevice cd = new AgencyControlDevice(); if (id > 0) { cd = ControlDeviceData.GetSingle(c => c.Id == id); cd.Facility = FacilityData.GetById(cd.FacilityId); LoadSimpleLookupItems(cd); List pollutants = ControlDevicePollutantData.Get(p => p.ControlDeviceId == cd.Id).ToList(); if (pollutants.Count > 0) { cd.Pollutants = pollutants.ToList(); foreach (ControlDevicePollutant p in cd.Pollutants) { p.Pollutant = LookupUtility.Get(p.Code); } } LoadModel(cd); foreach (UnitProcess up in cd.RelatedUnitProcesses) { up.EmissionUnit = EmissionUnitData.GetById(up.EmissionUnitId); up.EmissionIdentifier = up.EmissionUnit.Identifier; } if (cd.LastUpdatedBy != null && cd.LastUpdatedBy.Id > 0) { cd.LastUpdatedBy = UserData.GetById(cd.LastUpdatedBy.Id); } if (cd.IsMaster) { LoadMasterData(cd); } } return cd; } public void LoadMasterData(AgencyControlDevice cd) { cd.Identifiers = ControlDeviceData.Get(i => i.ControlDeviceId == cd.Id).ToList(); cd.MasterRecordComments = ControlDeviceData.Get(c => c.ControlDeviceId == cd.Id).ToList(); foreach (Comment comment in cd.MasterRecordComments) { comment.User = UserData.GetSingle(u => u.Id == comment.UserId); } } public override void Delete(ControlDeviceModel controlDevice, List customFields) { if (!ControlDeviceData.CanDelete(controlDevice.Id) || controlDevice.MasterControlDeviceId.HasValue) { throw new CannotDeleteException(Properties.Get(Constants.AgencyControlDeviceCannotDeleteMessage)); } using (ISession session = ControlDeviceData.GetSession()) { using (ITransaction trans = session.BeginTransaction()) { //delete identifiers. ControlDeviceIdentifierData.DeleteAllByControlDevice(controlDevice.Id, session); if (((AgencyControlDevice)controlDevice).MasterRecordComments != null) { foreach (AgencyControlDeviceComment cmnt in ((AgencyControlDevice)controlDevice).MasterRecordComments) { ControlDeviceData.Delete(cmnt, session); } } //delete pollutant first foreach (AgencyControlDevicePollutant pollutant in controlDevice.Pollutants) { ControlDevicePollutantData.Delete(pollutant, session); } //delete custom field value foreach (CustomFieldView customFeild in customFields) { AgencyControlDeviceCustomFieldValue val = customFeild.Value.Convert(); val.EntityId = controlDevice.Id; val.FieldValue.CustomFieldId = customFeild.Field.Id; CustomFieldValueData.Delete(session, val); } ControlDeviceData.Delete(controlDevice, session); trans.Commit(); } } } } }