public int SaveAtivities(List<Activity> activities, List<iatiactivity> iatiActivities, tblFundSource fundSource) { foreach (var activity in activities) { var a = dbContext.Activities.FirstOrDefault(x => x.IatiIdentifier == activity.IatiIdentifier); if (a != null) { a.OrgId = activity.OrgId; a.IatiActivityPrev = a.IatiActivity; a.IatiActivity = activity.IatiActivity; a.Hierarchy = activity.Hierarchy; a.ParentHierarchy = activity.ParentHierarchy; a.DownloadDatePrev = a.DownloadDate; a.DownloadDate = DateTime.Now; //update aimsdb if (a.ProjectId > 0 || a.MappedProjectId > 0) { var aimsDAL = new AimsDAL(); //step 1: project structure var iactivities = new List<iatiactivity>(); if (a.Hierarchy == 1) iactivities = ImportLogic.LoadH1ActivitiesWithChild(iatiActivities); // here pass all activities to find out their child activities else iactivities = ImportLogic.LoadH2ActivitiesWithParent(iatiActivities); //step 2: get mapped iatiActivity and aimsProject var iatiActivity = iactivities.Find(f => f.IatiIdentifier == a.IatiIdentifier); // SetExchangedValues SetExchangedValues(iatiActivity); iatiActivity.childActivities.ForEach(ra => SetExchangedValues(ra)); var aimsProject = new iatiactivity(); if (a.ProjectId > 0) { aimsProject = aimsDAL.GetAIMSProjectInIATIFormat(a.ProjectId); if (aimsProject != null) { //step 3: get general preference var generalPreference = GetFieldMappingPreferenceGeneral(a.OrgId); //step 4: create a ProjectFieldMapModel using iatiActivity, aimsProject and generalPreference var ProjectFieldMapModel = new ProjectFieldMapModel(iatiActivity, aimsProject, generalPreference); //step 5: SetFieldMappingPreferences var ProjectFieldMapModels = new List<ProjectFieldMapModel>(); // here we make a list just to use existing method (e.g existing method require a List parameter) ProjectFieldMapModels.Add(ProjectFieldMapModel); ImportLogic.SetFieldMappingPreferences(ProjectFieldMapModels, ProjectFieldMapModel); //step 6: merge iatiActivity and aimsProject; and get an new merged activity var mergedActivities = ImportLogic.MergeProjects(ProjectFieldMapModels); //now it will allways return a list containing single activity mergedActivities.n(0).AllID = fundSource.Id + "~" + a.OrgId + "~" + (int)ExecutingAgencyType.DP + "~" + fundSource.FundSourceCategoryId; //step 7: update aims database with margedActivities aimsDAL.UpdateProjects(mergedActivities, "system"); } else { dbContext.Logs.Add(new Log { IatiIdentifier = activity.IatiIdentifier, OrgId = activity.OrgId, ProjectId = a.MappedProjectId, Message = "The mapped project is not found in AIMS database", LogType = (int)LogType.AimsProjectNotFound, DateTime = DateTime.Now }); } } else if (a.MappedProjectId > 0) //for co-finance projects { aimsProject = aimsDAL.GetAIMSProjectInIATIFormat(a.MappedProjectId); if (aimsProject != null) { iatiActivity.AllID = fundSource.Id + "~" + a.OrgId + "~" + (int)ExecutingAgencyType.DP + "~" + fundSource.FundSourceCategoryId; aimsProject.MatchedProjects.Add(iatiActivity); //step 7: update aims database with margedActivities aimsDAL.UpdateCofinanceProjects(new List<iatiactivity> { aimsProject }, "system"); } else { dbContext.Logs.Add(new Log { IatiIdentifier = activity.IatiIdentifier, OrgId = activity.OrgId, ProjectId = a.MappedProjectId, Message = "The mapped project is not found in AIMS database", LogType = (int)LogType.AimsProjectNotFound, DateTime = DateTime.Now }); } } } } else { activity.DownloadDate = DateTime.Now; dbContext.Activities.Add(activity); dbContext.Logs.Add(new Log { IatiIdentifier = activity.IatiIdentifier, OrgId = activity.OrgId, Message = "Imported new activity", LogType = (int)LogType.AddedNewActivity, DateTime = DateTime.Now }); } } return dbContext.SaveChanges(); }
private List<ProjectFieldMapModel> PrepareMappedActivities(List<iatiactivity> iatiActivities, Activity a, AimsDAL aimsDAL) { //step 1: project structure var iactivities = new List<iatiactivity>(); if (a.Hierarchy == 1) iactivities = ImportLogic.LoadH1ActivitiesWithChild(iatiActivities); // here pass all activities to find out their child activities else iactivities = ImportLogic.LoadH2ActivitiesWithParent(iatiActivities); //step 2: get mapped iatiActivity and aimsProject var iatiActivity = iactivities.Find(f => f.IatiIdentifier == a.IatiIdentifier); // SetExchangedValues SetExchangedValues(iatiActivity); iatiActivity.childActivities.ForEach(ra => SetExchangedValues(ra)); var aimsProject = new iatiactivity(); if (a.ProjectId > 0) { aimsProject = aimsDAL.GetAIMSProjectInIATIFormat(a.ProjectId); } else if (a.MappedProjectId > 0) //for co-finance projects { aimsProject = aimsDAL.GetAIMSProjectInIATIFormat(a.MappedProjectId); } //step 3: get general preference var generalPreference = GetFieldMappingPreferenceGeneral(a.OrgId); //step 4: create a ProjectFieldMapModel using iatiActivity, aimsProject and generalPreference var ProjectFieldMapModel = new ProjectFieldMapModel(iatiActivity, aimsProject, generalPreference); //step 5: SetFieldMappingPreferences var ProjectFieldMapModels = new List<ProjectFieldMapModel>(); // here we make a list just to use existing method (e.g existing method require a List parameter) ProjectFieldMapModels.Add(ProjectFieldMapModel); ImportLogic.SetFieldMappingPreferences(ProjectFieldMapModels, ProjectFieldMapModel); return ProjectFieldMapModels; }