using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sleis.Models; using Sleis.ViewModels; using NHibernate; using Sleis.Models.CustomFields; using Sleis.Utility; using Sleis.Models.ErrorHandling; namespace Sleis.Service { public class AgencyReleasePointService : ReleasePointService, IReleasePointService { public override List GetReleasePoints(int reportId) { List list = ReleasePointData.GetReleasePointsByReport(reportId); LoadModel(list); return list; } public override List GetReleasePoints(int reportId, int pageNum, int pageSize) { FacilityModel facility = FacilityData.GetReportFacility(reportId); if (facility != null && facility.Id > 0) { return GetReleasePointsByFacility(facility.Id, pageNum, pageSize); } return new List(); } public override List GetReleasePointsByFacility(int facilityId, int pageNum, int pageSize) { List list = ReleasePointData.Get(x => x.FacilityId == facilityId, o => o.Asc(rp => rp.Identifier), pageNum, pageSize).ToList(); LoadModel(list); return list; } public override int CountReleasePointsByFacility(int facilityId) { return ReleasePointData.Count(x => x.FacilityId == facilityId); } public override int CountReleasePointsByReport(int reportId) { FacilityModel facility = FacilityData.GetReportFacility(reportId); if (facility != null && facility.Id > 0) { return CountReleasePointsByFacility(facility.Id); } throw new Exception("No facility assoicated with current report."); } public override ReleasePoint GetReleasePoint(int reportId, int id) { AgencyReleasePoint rp = ReleasePointData.GetSingle(r => r.Id == id); if (rp != null && rp.Id > 0) { if (rp.LastUpdatedBy != null && rp.LastUpdatedBy.Id > 0) { rp.LastUpdatedBy = UserData.GetById(rp.LastUpdatedBy.Id); } //TODO: Get Releated Unit Processes Based on Context rp.RelatedUnitProcesses = UnitProcessData.GetReleasePointUnitProcesses(reportId, id); //TODO REFACTOR foreach (UnitProcess up in rp.RelatedUnitProcesses) { up.EmissionUnit = EmissionUnitData.GetById(up.EmissionUnitId); up.EmissionIdentifier = up.EmissionUnit.Identifier; } rp.Facility = FacilityData.GetSingle(f => f.Id == rp.FacilityId); if (rp.UsesFacilitySiteLocation) { rp.Location = FacilityData.GetById(rp.FacilityId).Location; } //if reportId is 0, must be a master... if (reportId == 0) { rp.MasterRecordComments = ReleasePointData.Get(c => c.ReleasePointId == rp.Id).ToList(); foreach (Comment comment in rp.MasterRecordComments) { comment.User = UserData.GetSingle(u => u.Id == comment.UserId); } rp.Identifiers = ReleasePointData.Get(i => i.ReleasePointId == rp.Id).ToList(); } rp.Status = LookupUtility.Get(rp.StatusCode); rp.Type = LookupUtility.Get(rp.TypeCode); if (rp.Location != null) { rp.Location.CollectionMethod = LookupUtility.Get(rp.Location.CollectionMethodCode); rp.Location.ReferencePoint = LookupUtility.Get(rp.Location.ReferencePointCode); rp.Location.ReferenceSystem = LookupUtility.Get(rp.Location.ReferenceSystemCode); } else { rp.Location = new GeographicLocation(); } } return rp;// != null ? rp : new AgencyReleasePoint(); } public override void AddReleasePoint(ReleasePoint releasePoint, List customFields) { UpdateReleasePoint(releasePoint, customFields); } public override bool UpdateReleasePoint(ReleasePoint releasePoint, List customFields) { bool isAdd = releasePoint.Id < 1; //massage the data CleanReleasePointValues(releasePoint); using (ISession session = ReleasePointData.GetSession()) { using (ITransaction trans = session.BeginTransaction()) { releasePoint.LastUpdated = DateTime.Now; releasePoint.LastUpdatedBy = SessionUtility.CurrentUser; string masterComment = ""; if (((AgencyReleasePoint)releasePoint).IsMaster && !String.IsNullOrWhiteSpace(releasePoint.Comment)) { masterComment = releasePoint.Comment; releasePoint.Comment = ""; } if (!ReleasePointData.IsIdentifierUnique(releasePoint)) { throw new Utility.UniqueIdentifierException("Release Point Identifier must be unique."); } trans.Begin(); ReleasePointData.SaveOrUpdate(releasePoint, session); /* //delete all previously stored Ids ReleasePointIdentifierData.DeleteAllByReleasePoint(releasePoint.Id, session); //save all ids as new if(((AgencyReleasePoint)releasePoint).Identifiers!=null) foreach (AgencyReleasePointIdentifier id in ((AgencyReleasePoint)releasePoint).Identifiers) { id.ReleasePointId = releasePoint.Id; ReleasePointData.SaveOrUpdate(id, session); } * */ if (((AgencyReleasePoint)releasePoint).IsMaster && isAdd) { AgencyReleasePointIdentifier id = new AgencyReleasePointIdentifier() { ReleasePointId = releasePoint.Id, IdentifierText = releasePoint.Identifier, ProSysCode = "SLEIS", EffectiveDate = DateTime.Now }; ReleasePointIdentifierData.SaveOrUpdate(id, session); } //add and save master comment if (((AgencyReleasePoint)releasePoint).IsMaster && !String.IsNullOrWhiteSpace(masterComment)) { AgencyReleasePointComment comment = new AgencyReleasePointComment(); comment.Text = masterComment; comment.ReleasePointId = releasePoint.Id; comment.UserId = SessionUtility.CurrentUser.Id; comment.Date = DateTime.Now; ReleasePointData.SaveOrUpdate(comment, session); } //save customFieldValue SaveCustomFields(customFields, releasePoint.Id, session); trans.Commit(); return trans.WasCommitted; } } } public override void DeleteReleasePoint(int id, List customFields) { if (!ReleasePointData.CanDelete(id) || ReleasePointData.GetById(id).MasterReleasePointId.HasValue) { throw new CannotDeleteException(Properties.Get(Constants.AgencyReleasePointCannotDeleteMessage)); } using (ISession session = ReleasePointData.GetSession()) { using (ITransaction trans = session.BeginTransaction()) { trans.Begin(); ReleasePointIdentifierData.DeleteAllByReleasePoint(id, session); CommentData.DeleteReleasePointComments(id, session); foreach (CustomFieldView customFeild in customFields) { AgencyReleasePointCustomFieldValue val = customFeild.Value.Convert(); val.EntityId = id; val.FieldValue.CustomFieldId = customFeild.Field.Id; CustomFieldValueData.Delete(session, val); } ReleasePointData.Delete(id, session); //onle one record should have been deleted trans.Commit(); } } } } }