using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; using EnVisage.Models; using EnVisage.Code.Integration; using IntergrationAccessInf; using System.Web.Script.Serialization; namespace EnVisage.Code.BLL { public class IntegrationManager : ManagerBase { public IntegrationManager(EnVisageEntities dbContext) : base(dbContext) { } public IntegrationConnectionModel getModel(IntergrationAccessType t) { Guid id = Guid.Empty; CustomerInfoControlAPIManager cicMan = new CustomerInfoControlAPIManager(); var model = cicMan.GetClientInfo(); if (model == null) return new IntegrationConnectionModel(); var rtModel = (IntegrationConnectionModel) DataTable.Where(x => x.Type ==(int)t).FirstOrDefault(); if (rtModel == null || rtModel.Id == Guid.Empty) Logger.Log(NLog.LogLevel.Debug, "No CRM Master record for " + model.Id); else Logger.Log(NLog.LogLevel.Debug, "CRM Master record found for " + model.Id); return rtModel; } public IntegrationConnectionModel getModel(IntergrationAccessType t,string hostUrl) { Guid id = Guid.Empty; CustomerInfoControlAPIManager cicMan = new CustomerInfoControlAPIManager(); var model = cicMan.GetClientInfo(hostUrl); if (model == null) return new IntegrationConnectionModel(); return (IntegrationConnectionModel)DataTable.Where(x => x.ClientInfoId == model.Id && x.Type == (int) t).FirstOrDefault(); } public List getFieldModelsForTable(string table,string action) { return this.DbContext.Integration2Prevu.Where(x => x.PrevuEntityName == table && x.TriggerType.ToLower().Trim() == action.ToLower().Trim()).ToList(); } public ProjectModel ImportProjectFromAPI(IntergrationAccessType type, IntegrationConnectionModel model, string remoteProjectId) { var rt = new Dictionary(); var apiClass = IntergrationHelper.GetIntergrationClass(type, model); apiClass.LogEvent += ApiClass_LogEvent; apiClass.LookUp += ApiClass_LookUp; var project = GetProjectImportDataFromApiClass(apiClass, model, remoteProjectId); return project; } private void ApiClass_LookUp(object sender, EventArgs e) { var evnt = (InfLookUpValue) e; if (evnt.PropertyName == "Company") evnt.result = this.DbContext.Companies.Where(x => x.Name == evnt.Key).Select(x => x.Id).FirstOrDefault(); if (evnt.PropertyName == "Client") evnt.result = this.DbContext.Clients.Where(x => x.Name == evnt.Key).Select(x => x.Id).FirstOrDefault(); if (evnt.PropertyName == "Type") evnt.result = this.DbContext.Types.Where(x => x.Name == evnt.Key).Select(x => x.Id).FirstOrDefault(); if (evnt.PropertyName == "Status") evnt.result = this.DbContext.Status.Where(x => x.Name == evnt.Key).Select(x => x.Id).FirstOrDefault(); if (evnt.PropertyName == "Team") evnt.result = this.DbContext.Teams.Where(x => x.Name == evnt.Key).Select(x => x.Id).FirstOrDefault(); if (evnt.PropertyName == "CompanyContact") evnt.result = this.DbContext.Contacts.Where(x => x.Email == evnt.Key && x.Type==(int)ContactType.CompanyContact).Select(x => x.Id).FirstOrDefault(); if (evnt.PropertyName == "CompanyFromTeam") evnt.result = this.DbContext.Teams.Where(x => x.Name == evnt.Key).Select(x => x.CompanyId).FirstOrDefault(); } private ProjectModel GetProjectImportDataFromApiClass( AccessInf apiClass, IntegrationConnectionModel model, string remoteProjectId) { var rt = apiClass.ImportProjectData(remoteProjectId); JavaScriptSerializer json_serializer = new JavaScriptSerializer(); return json_serializer.Deserialize(rt); } private void ApiClass_LogEvent(object sender, EventArgs e) { //add nlog info here.. } public override DbSet DataTable { get { return DbContext.IntegrationConnectionInfoes; } } } }