private int HandleComponentCondition(PathwayComponentCondition input, Pathway pathway, PathwayComponent component, ref SaveStatus status)
        {
            int           newId         = 0;
            List <string> messages      = new List <string>();
            string        statusMessage = "";

            input.ParentComponentId = component.Id;
            if (pccm.Save(input, ref messages))
            {
                newId = input.Id;
                activityMgr.SiteActivityAdd(new SiteActivity()
                {
                    ActivityType     = "PathwayComponent",
                    Activity         = "Import",
                    Event            = "Add",
                    Comment          = string.Format("Added PathwayComponentCondition via Import: '{0}' for Component: '{1}'", input.Name, component.Name),
                    ActivityObjectId = newId,
                });
            }
            else
            {
                status.AddErrorRange(messages);
            }

            if (newId == 0 || (!string.IsNullOrWhiteSpace(statusMessage) && statusMessage != "successful"))
            {
                status.AddError(string.Format("Row: Issue encountered updating pathway ComponentCondition: {0} for Component: '{1}': {2}", input.Name, component.Name, statusMessage));
                return(0);
            }
            //==================================================


            //handle target components - better organization to move this to HandleComponentCondition since all components should now exist
            List <PathwayComponent> profiles = new List <PathwayComponent>();

            messages = new List <string>();
            foreach (var tc in input.HasTargetComponentList)
            {
                var targetComponent = PathwayComponentManager.Get(tc);
                if (targetComponent == null || targetComponent.Id == 0)
                {
                    //shouldn't happen here - although the add attempt could have failed?
                    status.AddError(string.Format("The target pathway component: {0} for ConditionComponent: {1} was not found. This could have been due the an issue adding the component - which should have resulted in an earlier error message.", tc, input.Name));
                    continue;
                }
                profiles.Add(targetComponent);
            }
            //now replace relationships
            if (!epcmgr.Replace(input.RowId, PathwayComponent.PathwayComponentRelationship_TargetComponent, profiles, ref status))
            {
                //status.AddErrorRange( messages );
            }

            return(newId);
        }
Beispiel #2
0
        //
        /// <summary>
        /// Handle component import
        /// TODO - should a save be done for each component or wait until the end
        /// </summary>
        /// <param name="input"></param>
        /// <param name="pathway"></param>
        /// <param name="bnodes"></param>
        /// <param name="status">TODO - do we want to continue using the pathway SaveStatus?</param>
        /// <returns></returns>
        public OutputComponent ImportComponent(InputComponent input, ThisEntity pathway, List <BNode> bnodes, SaveStatus status)
        {
            MappingHelperV3 helper = new MappingHelperV3(CodesManager.ENTITY_TYPE_PATHWAY_COMPONENT);

            //do we need to reference blank nodes here? - if so pass to this method
            helper.entityBlankNodes  = bnodes;
            helper.CurrentEntityCTID = input.CTID;
            helper.CurrentEntityName = input.Name.ToString();
            OutputComponent output = new OutputComponent();

            //
            LoggingHelper.DoTrace(5, "======== Component ======== ");
            LoggingHelper.DoTrace(5, "		type: "+ input.PathwayComponentType.ToString());
            LoggingHelper.DoTrace(5, "		name: "+ (input.Name ?? new JInput.LanguageMap("componentNameMissing")).ToString());
            LoggingHelper.DoTrace(5, "		ctid: "+ input.CTID);
            LoggingHelper.DoTrace(6, "		@Id: "+ input.CtdlId);

            try
            {
                //add/updating Pathway
                if (!DoesComponentExist(input.CTID, ref output))
                {
                    //set the rowid now, so that can be referenced as needed
                    //no, the guid comes from the resolving of entity references
                    //actually OK, as earlier references would result in a pending record
                    output.RowId = Guid.NewGuid();
                }
                helper.currentBaseObject = output;
                if (input.CTID == "ce-fa6c139f-0615-401f-9920-6ec8c445baca")
                {
                }
                //initialize json properties
                output.JsonProperties = new PathwayComponentProperties();
                //
                output.PathwayComponentType = input.PathwayComponentType;
                output.Name           = helper.HandleLanguageMap(input.Name, output, "Name");
                output.Description    = helper.HandleLanguageMap(input.Description, output, "Description");
                output.SubjectWebpage = input.SubjectWebpage;
                output.SourceData     = input.SourceData;

                if (!string.IsNullOrWhiteSpace(output.SourceData) && output.SourceData.IndexOf("/resources/") > 0)
                {
                    var ctid = ResolutionServices.ExtractCtid(output.SourceData);
                    if (!string.IsNullOrWhiteSpace(ctid))
                    {
                        if (output.PathwayComponentType.ToLower().IndexOf("credential") > -1)
                        {
                            var target = CredentialManager.GetMinimumByCtid(ctid);
                            if (target != null && target.Id > 0)
                            {
                                //this approach 'buries' the cred from external references like credential in pathway
                                output.SourceCredential = new TopLevelEntityReference()
                                {
                                    Id             = target.Id,
                                    Name           = target.Name,
                                    Description    = target.Description,
                                    CTID           = target.CTID,
                                    SubjectWebpage = target.SubjectWebpage,
                                    //RowId = target.RowId
                                };
                                output.JsonProperties.SourceCredential = output.SourceCredential;
                            }
                        }
                        else if (output.PathwayComponentType.ToLower().IndexOf("assessmentcomp") > -1)
                        {
                            var target = AssessmentManager.GetSummaryByCtid(ctid);
                            if (target != null && target.Id > 0)
                            {
                                //may not really need this, just the json
                                output.SourceAssessment = new TopLevelEntityReference()
                                {
                                    Id             = target.Id,
                                    Name           = target.Name,
                                    Description    = target.Description,
                                    CTID           = target.CTID,
                                    SubjectWebpage = target.SubjectWebpage,
                                    //RowId = target.RowId
                                };
                                output.JsonProperties.SourceAssessment = output.SourceAssessment;
                            }
                        }
                        else if (output.PathwayComponentType.ToLower().IndexOf("coursecomp") > -1)
                        {
                            var target = LearningOpportunityManager.GetByCtid(ctid);
                            if (target != null && target.Id > 0)
                            {
                                //may not really need this, just the json
                                output.SourceLearningOpportunity = new TopLevelEntityReference()
                                {
                                    Id             = target.Id,
                                    Name           = target.Name,
                                    Description    = target.Description,
                                    CTID           = target.CTID,
                                    SubjectWebpage = target.SubjectWebpage,
                                    //RowId = target.RowId
                                };
                                output.JsonProperties.SourceLearningOpportunity = output.SourceLearningOpportunity;
                            }
                        }
                    }
                }

                output.CTID        = input.CTID;
                output.PathwayCTID = pathway.CTID;


                //output.CodedNotation = input.CodedNotation;
                output.Identifier = helper.MapIdentifierValueListInternal(input.Identifier);
                if (output.Identifier != null && output.Identifier.Count() > 0)
                {
                    output.IdentifierJson = JsonConvert.SerializeObject(output.Identifier, MappingHelperV3.GetJsonSettings());
                }
                //
                output.ComponentDesignationList = helper.MapCAOListToList(input.ComponentDesignation);

                //
                output.CredentialType = input.CredentialType;
                output.CreditValue    = helper.HandleValueProfileList(input.CreditValue, output.PathwayComponentType + ".CreditValue");

                //TBD - how to handle. Will need to have imported the concept scheme/concept
                if (input.HasProgressionLevel != null && input.HasProgressionLevel.Any())
                {
                    foreach (var item in input.HasProgressionLevel)
                    {
                        output.HasProgressionLevels.Add(ResolutionServices.ExtractCtid(item));
                    }
                }

                output.PointValue = helper.HandleQuantitiveValue(input.PointValue, output.PathwayComponentType + ".PointValue");

                //
                output.ProgramTerm = helper.HandleLanguageMap(input.ProgramTerm, output, "ProgramTerm");
                //need to get relationshiptype to store-> this can be done by manager
                //3
                output.HasChildList = helper.MapEntityReferenceGuids("PathwayComponent.HasChild", input.HasChild, CodesManager.ENTITY_TYPE_PATHWAY_COMPONENT, ref status);
                //2
                output.HasIsChildOfList = helper.MapEntityReferenceGuids("PathwayComponent.IsChildOf", input.IsChildOf, CodesManager.ENTITY_TYPE_PATHWAY_COMPONENT, ref status);

                output.HasPrerequisiteList = helper.MapEntityReferenceGuids("PathwayComponent.Prerequisite", input.Prerequisite, CodesManager.ENTITY_TYPE_PATHWAY_COMPONENT, ref status);
                output.HasPreceedsList     = helper.MapEntityReferenceGuids("PathwayComponent.Preceeds", input.Preceeds, CodesManager.ENTITY_TYPE_PATHWAY_COMPONENT, ref status);

                //populate JSON properties
                output.JsonProperties.ComponentDesignationList = output.ComponentDesignationList;
                output.JsonProperties.CreditValue = output.CreditValue;
                output.JsonProperties.Identifier  = output.Identifier;
                output.JsonProperties.PointValue  = output.PointValue;

                //
                if (input.HasCondition != null && input.HasCondition.Count() > 0)
                {
                    output.HasCondition = new List <PathwayComponentCondition>();
                    foreach (var item in input.HasCondition)
                    {
                        //var jcc = JsonConvert.DeserializeObject<JInput.ComponentCondition>( item.ToString() );
                        var cc = new PathwayComponentCondition();
                        cc.Name                   = helper.HandleLanguageMap(item.Name, cc, "ComponentCondition.Name");
                        cc.Description            = helper.HandleLanguageMap(item.Description, cc, "ComponentCondition.Description");
                        cc.RequiredNumber         = item.RequiredNumber;
                        cc.PathwayCTID            = pathway.CTID;
                        cc.HasTargetComponentList = helper.MapEntityReferenceGuids("ComponentCondition.TargetComponent", item.TargetComponent, CodesManager.ENTITY_TYPE_PATHWAY_COMPONENT, ref status);

                        output.HasCondition.Add(cc);
                    }
                }
            } catch (Exception ex)
            {
                LoggingHelper.LogError(ex, "ImportPathways.ImportComponent");
                //status.AddError( string.Format( "ImportPathways.ImportComponent. ComponentType: {0}, Name: {1}, Message: {2}", output.ComponentTypeId, output.Name, ex.Message ) );
            }
            //then save
            return(output);
        }
        public void HandleComponents(ThisEntity pathway, ref SaveStatus status)
        {
            try
            {
                //components
                //delete all not in current list
                new Entity_PathwayComponentManager().DeleteNotInList(pathway.CTID, pathway.HasPart, ref status);
                //

                //TBD - should we do a fresh get of the pathway with components - or clear all?
                //handle components
                foreach (var item in pathway.HasPart)
                {
                    var component = new PathwayComponent();
                    //handle each component
                    //add to pathway HasParts on conclusion (with existance checking
                    var recordExists = false;
                    if (HandlePathwayComponent(item, pathway, ref component, ref recordExists, ref status) < 1)
                    {
                        status.RecordsFailed++;
                        continue;
                    }
                    else
                    {
                        if (recordExists)
                        {
                            status.RecordsUpdated++;
                        }
                        else
                        {
                            status.RecordsAdded++;
                        }
                    }

                    //add pathway HasPart for component
                    //?do we need has part in the finder?
                    //will be useful to retrieve data for the detail page
                    epcmgr.Add(pathway.RowId, component.Id, PathwayComponent.PathwayComponentRelationship_HasPart, ref status);
                }

                //handle conditions
                var candidates = pathway.HasPart.Where(s => s.HasCondition != null && s.HasCondition.Count() > 0).ToList();
                foreach (var pc in candidates)
                {
                    foreach (var item in pc.HasCondition)
                    {
                        //get parent component
                        var component = PathwayComponentManager.GetByCtid(pc.CTID);
                        if (component == null || component.Id == 0)
                        {
                            //shouldn't happen here - although the add attempt could have failed?
                            status.AddError(string.Format("The parent pathway component: {0} for ConditionComponent: {1} was not found. This could have been due the an issue adding the component - which should have resulted in an earlier error message.", pc.Name, item.Name));
                            continue;
                        }
                        var condition = new PathwayComponentCondition();
                        //add to pathway component Entity.HasPathwayComponent on conclusion

                        if (HandleComponentCondition(item, pathway, component, ref status) < 1)
                        {
                            status.RecordsFailed++;
                            //could continue if have an id (i.e. failed after saved)?
                            continue;
                        }
                    }
                }

                //now handle relationships
                int cntr = 0;
                foreach (var item in pathway.HasPart)
                {
                    cntr++;
                    var component = PathwayComponentManager.GetByCtid(item.CTID, PathwayComponentManager.componentActionOfNone);
                    //handle each component
                    //add to pathway HasParts on conclusion (with existance checking
                    ReplacePathwayComponentRelationships(cntr, component.RowId, item.HasChildList, pathway, PathwayComponent.PathwayComponentRelationship_HasChild, "PathwayComponent.HasChild", ref status);

                    ReplacePathwayComponentRelationships(cntr, component.RowId, item.HasPrerequisiteList, pathway, PathwayComponent.PathwayComponentRelationship_Prerequiste, "PathwayComponent.Prerequisite", ref status);
                    //
                    ReplacePathwayComponentRelationships(cntr, component.RowId, item.HasPreceedsList, pathway, PathwayComponent.PathwayComponentRelationship_Preceeds, "PathwayComponent.Preceeds", ref status);
                }

                //these may have to been done after processing components
                //================ destination component
                ReplacePathwayToPathwayComponentRelationships(pathway.HasDestinationList, pathway, PathwayComponent.PathwayComponentRelationship_HasDestinationComponent, "Pathway.HasDestinationComponent", ref status);
                //
                ReplacePathwayToPathwayComponentRelationships(pathway.HasChildList, pathway, PathwayComponent.PathwayComponentRelationship_HasChild, "Pathway.HasChild", ref status);
            }
            catch (Exception ex)
            {
                LoggingHelper.DoTrace(1, string.Format(thisClassName + ".HandleComponents. Pathway: {0} ({1}) Exception encountered: {2}", pathway.Name, pathway.Id, ex.Message));
                //only fail current, and allow to continue
                status.AddError(string.Format("Exception encountered. Pathway: {0}, Message: {1}", pathway.Name, ex.Message));

                LoggingHelper.LogError(ex, string.Format(thisClassName + ".HandleComponents. Pathway: {0} ({1}) Exception encountered", pathway.Name, pathway.Id));
            }
        }