using AICore.Context.Summary; using BeWo.Data.Access; using BeWo.Data.Entities; using BeWo.Service.DCEntityMapper; using BeWo.Service.ServiceImplementations; using BS.Shared.DataContracts.Compact; using BS.Shared.DataContracts; using BS.Shared; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AICore.Context.Core; namespace BeWo.Service.ServiceUtils { public static class AiContextLoader { public static BaseContextSummary LoadContext(AiContextType uicontext, Dictionary context_reference) { switch (uicontext) { case AiContextType.SupportConceptNavigation: case AiContextType.CustomerNavigation: case AiContextType.PersonNavigation: case AiContextType.EmployeeNavigation: case AiContextType.OrganisationNavigation: return loadNavigationContext(uicontext, context_reference); case AiContextType.CustomerDetail: case AiContextType.PersonDetail: case AiContextType.EmployeeDetail: case AiContextType.OrganisationDetail: case AiContextType.SupportConceptDetail: return loadDetailContext(uicontext, context_reference); case AiContextType.ServiceRecord: return loadServiceRecordContext(context_reference); default: break; } throw new NotImplementedException(); } private static BaseContextSummary loadNavigationContext(AiContextType uicontext, Dictionary context_reference) { switch (uicontext) { case AiContextType.SupportConceptNavigation: var list = new CustomerServiceImp().GetAllAuthorizedCompactSupportConcepts(); var filter = list.Where(x => oidsContainsOid(context_reference[TableID.SupportConcept], x.SupportConceptOid)); return AiContextSummaryFactory.CreateSupportConceptNavigationContextSummary(filter); case AiContextType.CustomerNavigation: var list2 = new CustomerServiceImp().GetAllAuthorizedCompactCustomers(); var filter2 = list2.Where(x => oidsContainsOid(context_reference[TableID.Customer], x.CustomerOid)); return AiContextSummaryFactory.CreateCustomerNavigationContextSummary(filter2); case AiContextType.PersonNavigation: var list3 = new CustomerServiceImp().GetAllAuthorizedCompactPersons(); var filter3 = list3.Where(x => oidsContainsOid(context_reference[TableID.Person], x.PersonOid)); return AiContextSummaryFactory.CreatePersonNavigationContextSummary(filter3); case AiContextType.EmployeeNavigation: var list4 = new EmployeeServiceImp().GetAllCompactEmployees(); var filter4 = list4.Where(x => oidsContainsOid(context_reference[TableID.Employee], x.EmployeeOid)); return AiContextSummaryFactory.CreateEmployeeNavigationContextSummary(filter4); case AiContextType.OrganisationNavigation: var list5 = new CustomerServiceImp().GetAllOrganisationsCompact(); var filter5 = list5.Where(x => oidsContainsOid(context_reference[TableID.Organisation], x.OrganisationOid)); return AiContextSummaryFactory.CreateOrganisationNavigationContextSummary(filter5); } throw new NotImplementedException(); } private static BaseContextSummary loadDetailContext(AiContextType uicontext, Dictionary context_reference) { switch (uicontext) { case AiContextType.CustomerDetail: var oids = context_reference[TableID.Customer]; var dc = new CustomerServiceImp().LoadCustomer(oids[0]); return AiContextSummaryFactory.CreateCustomerDetailContextSummary(dc); case AiContextType.PersonDetail: var oids2 = context_reference[TableID.Person]; var dc2 = new CustomerServiceImp().LoadPerson(oids2[0]); return AiContextSummaryFactory.CreatePersonDetailContextSummary(dc2); case AiContextType.EmployeeDetail: var oids3 = context_reference[TableID.Employee]; var dc3 = new EmployeeServiceImp().LoadEmployee(oids3[0]); return AiContextSummaryFactory.CreateEmployeeDetailContextSummary(dc3); case AiContextType.OrganisationDetail: var oids4 = context_reference[TableID.Organisation]; var dc4 = new CustomerServiceImp().LoadOrganisation(oids4[0]); return AiContextSummaryFactory.CreateOrganisationDetailContextSummary(dc4); case AiContextType.SupportConceptDetail: var oids5 = context_reference[TableID.SupportConcept]; var dc5 = new CustomerServiceImp().LoadSupportConcept(oids5[0]); return AiContextSummaryFactory.CreateSupportConceptDetailContextSummary(dc5); } throw new NotImplementedException(); } private static BaseContextSummary loadServiceRecordContext(Dictionary context_reference) { var customer = loadCompactCustomerDC(context_reference); var serviceRecords = loadServiceRecordDCs(context_reference); var supportConcept = loadSupportConceptDC(context_reference); var costBearer = supportConcept.CostBearerList.First(x => x.CostBearerOid == context_reference[TableID.CostBearer][0]); return AiContextSummaryFactory.CreateServiceRecordNavigationContextSummary(serviceRecords, customer, costBearer, supportConcept); } private static bool oidsContainsOid(long[] oids, long oid) { if (oids == null || oids.Length == 0) return false; return oids.Contains(oid); } private static CompactCustomerDC loadCompactCustomerDC(Dictionary context_reference) => LoadDataContact(MapperFactory.CompactCustomerDC_Customer, context_reference); private static CompactSupportConceptDC loadSupportConceptDC(Dictionary context_reference) => LoadDataContact(MapperFactory.CompactSupportConceptDC_SupportConcept, context_reference); private static IEnumerable loadServiceRecordDCs(Dictionary context_reference) => LoadDataContacts(MapperFactory.ServiceRecordDC_ServiceRecord, context_reference); private static TDC LoadDataContact(TMapper mapper, Dictionary context_reference) where TMapper : AbstractIDCEntityMapper where TEntity : BeWoEntityBase, new() where TDC : class, new() { var example = new TEntity(); var tid = example.Tid; var oid = context_reference[tid][0]; var entity = DAOFactory.GenericDAO.LoadByID(oid); var dc = mapper.MapToNewDC(entity); return dc; } private static IEnumerable LoadDataContacts(TMapper mapper, Dictionary context_reference) where TMapper : AbstractIDCEntityMapper where TEntity : BeWoEntityBase, new() where TDC : class, new() { var example = new TEntity(); var tid = example.Tid; var oids = context_reference[tid]; var entities = DAOFactory.GenericDAO.LoadByIDs(oids); var dcs = mapper.MapToNewDCs(entities); return dcs; } } }