Esempio n. 1
0
        }         //

        public bool Import(string payload, string envelopeIdentifier, SaveStatus status)
        {
            LoggingHelper.DoTrace(6, "ImportPathways - entered.");
            List <string>  messages          = new List <string>();
            bool           importSuccessfull = false;
            EntityServices mgr = new EntityServices();
            //
            InputEntity input           = new InputEntity();
            var         inputComponents = new List <InputComponent>();
            var         mainEntity      = new Dictionary <string, object>();
            //
            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            var    bnodes    = new List <BNode>();
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    mainEntity = RegistryServices.JsonToDictionary(main);
                    input      = JsonConvert.DeserializeObject <InputEntity>(main);
                }
                else
                {
                    //may have blank nodes?
                    var bn = item.ToString();
                    bnodes.Add(JsonConvert.DeserializeObject <BNode>(bn));
                    var child = item.ToString();
                    if (child.IndexOf("_:") > -1)
                    {
                        bnodes.Add(JsonConvert.DeserializeObject <BNode>(child));
                    }
                    else if (child.IndexOf("Component") > -1)
                    {
                        inputComponents.Add(JsonConvert.DeserializeObject <InputComponent>(child));
                    }
                    else
                    {
                        //unexpected
                        Dictionary <string, object> unexpected = RegistryServices.JsonToDictionary(child);
                        object unexpectedType = unexpected["@type"];
                        status.AddError("Unexpected document type");
                    }
                }
            }

            MappingHelperV3 helper = new MappingHelperV3(CodesManager.ENTITY_TYPE_PATHWAY);

            helper.entityBlankNodes  = bnodes;
            helper.CurrentEntityCTID = input.CTID;
            helper.CurrentEntityName = input.Name.ToString();

            status.EnvelopeId = envelopeIdentifier;
            try
            {
                string ctid           = input.CTID;
                string referencedAtId = input.CtdlId;

                LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());
                LoggingHelper.DoTrace(6, "		url: "+ input.SubjectWebpage);
                LoggingHelper.DoTrace(5, "		ctid: "+ input.CTID);
                LoggingHelper.DoTrace(6, "		@Id: "+ input.CtdlId);
                status.Ctid = ctid;

                if (status.DoingDownloadOnly)
                {
                    return(true);
                }


                if (!DoesEntityExist(input.CTID, ref output))
                {
                    //TODO - perhaps create a pending pathway immediately

                    //set the rowid now, so that can be referenced as needed
                    output.RowId = Guid.NewGuid();
                    LoggingHelper.DoTrace(1, string.Format(thisClassName + ".ImportV3(). Record was NOT found using CTID: '{0}'", input.CTID));
                }
                else
                {
                    LoggingHelper.DoTrace(1, string.Format(thisClassName + ".ImportV3(). Found record: '{0}' using CTID: '{1}'", input.Name, input.CTID));
                }
                helper.currentBaseObject = output;

                output.Name           = helper.HandleLanguageMap(input.Name, output, "Name");
                output.Description    = helper.HandleLanguageMap(input.Description, output, "Description");
                output.SubjectWebpage = input.SubjectWebpage;
                output.CTID           = input.CTID;
                //TBD handling of referencing third party publisher
                if (!string.IsNullOrWhiteSpace(status.DocumentPublishedBy))
                {
                    //output.PublishedByOrganizationCTID = status.DocumentPublishedBy;
                    var porg = OrganizationManager.GetSummaryByCtid(status.DocumentPublishedBy);
                    if (porg != null && porg.Id > 0)
                    {
                        //TODO - store this in a json blob??????????
                        //this will result in being added to Entity.AgentRelationship
                        output.PublishedBy = new List <Guid>()
                        {
                            porg.RowId
                        };
                    }
                    else
                    {
                        //if publisher not imported yet, all publishee stuff will be orphaned
                        var entityUid = Guid.NewGuid();
                        var statusMsg = "";
                        var resPos    = referencedAtId.IndexOf("/resources/");
                        var swp       = referencedAtId.Substring(0, (resPos + "/resources/".Length)) + status.DocumentPublishedBy;
                        int orgId     = new OrganizationManager().AddPendingRecord(entityUid, status.DocumentPublishedBy, swp, ref statusMsg);
                    }
                }
                else
                {
                    //may need a check for existing published by to ensure not lost
                    if (output.Id > 0)
                    {
                        if (output.OrganizationRole != null && output.OrganizationRole.Any())
                        {
                            var publishedByList = output.OrganizationRole.Where(s => s.RoleTypeId == 30).ToList();
                            if (publishedByList != null && publishedByList.Any())
                            {
                                var pby = publishedByList[0].ActingAgentUid;
                                output.PublishedBy = new List <Guid>()
                                {
                                    publishedByList[0].ActingAgentUid
                                };
                            }
                        }
                    }
                }
                //warning this gets set to blank if doing a manual import by ctid
                output.CredentialRegistryId = envelopeIdentifier;

                //BYs - do owned and offered first
                output.OfferedBy = helper.MapOrganizationReferenceGuids("Pathway.OfferedBy", input.OfferedBy, ref status);
                //note need to set output.OwningAgentUid to the first entry
                output.OwnedBy = helper.MapOrganizationReferenceGuids("Pathway.OwnedBy", input.OwnedBy, ref status);
                if (output.OwnedBy != null && output.OwnedBy.Count > 0)
                {
                    output.OwningAgentUid        = output.OwnedBy[0];
                    helper.CurrentOwningAgentUid = output.OwnedBy[0];
                }
                else
                {
                    //add warning?
                    if (output.OfferedBy == null && output.OfferedBy.Count == 0)
                    {
                        status.AddWarning("document doesn't have an owning or offering organization.");
                    }
                }
                //hasPart could contain all components. The API should have done a validation
                //not clear if necessary to do anything here
                //this would be a first step to create pending records?
                if (input.HasPart != null && input.HasPart.Count() > 0)
                {
                    output.HasPartList = helper.MapEntityReferenceGuids("Pathway.HasPart", input.HasPart, CodesManager.ENTITY_TYPE_PATHWAY_COMPONENT, ref status);
                }
                //
                output.HasChildList       = helper.MapEntityReferenceGuids("Pathway.HasChild", input.HasChild, CodesManager.ENTITY_TYPE_PATHWAY_COMPONENT, ref status);
                output.HasDestinationList = helper.MapEntityReferenceGuids("Pathway.HasDestination", input.HasDestinationComponent, CodesManager.ENTITY_TYPE_PATHWAY_COMPONENT, ref status);

                //has progression model
                //TODO - IMPORT CONCEPT SCHEMES
                // will need to check if related scheme has been imported.
                output.ProgressionModelURI = ResolutionServices.ExtractCtid(input.HasProgressionModel);

                output.Keyword = helper.MapToTextValueProfile(input.Keyword, output, "Keyword");
                output.Subject = helper.MapCAOListToTextValueProfile(input.Subject, CodesManager.PROPERTY_CATEGORY_SUBJECT);
                //Industries/occupations
                output.Industries  = helper.MapCAOListToCAOProfileList(input.IndustryType);
                output.Occupations = helper.MapCAOListToCAOProfileList(input.OccupationType);

                //may need to save the pathway and then handle components
                //or do a create pending for hasDestination and any hasChild (actually already done by MapEntityReferenceGuids)

                //=== if any messages were encountered treat as warnings for now
                if (messages.Count > 0)
                {
                    status.SetMessages(messages, true);
                }
                //components now or after save ?
                foreach (var item in inputComponents)
                {
                    var c = ImportComponent(item, output, bnodes, status);
                    output.HasPart.Add(c);
                }                //

                //adding common import pattern
                importSuccessfull = mgr.Import(output, ref status);
                //
                status.DocumentId    = output.Id;
                status.DetailPageUrl = string.Format("~/pathway/{0}", output.Id);
                status.DocumentRowId = output.RowId;
                //if record was added to db, add to/or set EntityResolution as resolved
                int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                           ctid,
                                                                           CodesManager.ENTITY_TYPE_PATHWAY,
                                                                           output.RowId,
                                                                           output.Id,
                                                                           (output.Id > 0),
                                                                           ref messages,
                                                                           output.Id > 0);
                //just in case - not sure if applicable, as will want to do components if the pathway exists
                if (status.HasErrors)
                {
                    importSuccessfull = false;
                    //email an error report, and/or add to activity log?
                }
                //
                //if ( output.Id > 0 )
                //{
                //	foreach ( var item in inputComponents )
                //	{
                //		var c=ImportComponent( item, output, bnodes, status );
                //		output.HasPart.Add( c );
                //	}//
                //	//call method to handle components
                //}
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, string.Format("Exception encountered in envelopeId: {0}", envelopeIdentifier), false, "Pathway Import exception");
            }

            return(importSuccessfull);
        }
        }         //

        public bool Import(string payload, string envelopeIdentifier, SaveStatus status)
        {
            /* checklist
             *
             *      Y		ceasn:abilityEmbodied
             *      Y		ceasn:comment
             *      Y		ceasn:knowledgeEmbodied
             *      Y		ceasn:skillEmbodied
             *      Y		ceterms:classification
             *      Y		ceterms:ctid
             *      Y		ceterms:description
             *      ceterms:hasJob			- to Job
             *      ceterms:hasSpecialization	- to narrower Occupation
             *      ceterms:hasWorkRole		- to WorkRole
             *      Y		ceterms:identifier
             *      Y		ceterms:industryType
             *      ceterms:isSpecializationOf	- to broader occupation
             *      Y		ceterms:keyword
             *      Y		ceterms:name
             *      Y		ceterms:occupationType
             *      Y		ceterms:sameAs
             *      Y		ceterms:subjectWebpage
             *      Y		ceterms:versionIdentifier
             *
             */
            LoggingHelper.DoTrace(6, "ImportOccupations - entered.");
            List <string>  messages          = new List <string>();
            bool           importSuccessfull = false;
            EntityServices mgr = new EntityServices();
            //
            InputEntity input      = new InputEntity();
            var         mainEntity = new Dictionary <string, object>();
            //
            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            var    bnodes    = new List <BNode>();
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    mainEntity = RegistryServices.JsonToDictionary(main);
                    input      = JsonConvert.DeserializeObject <InputEntity>(main);
                }
                else
                {
                    //may have blank nodes?
                    var bn = item.ToString();
                    bnodes.Add(JsonConvert.DeserializeObject <BNode>(bn));
                    var child = item.ToString();
                    if (child.IndexOf("_:") > -1)
                    {
                        bnodes.Add(JsonConvert.DeserializeObject <BNode>(child));
                    }
                    else
                    {
                        //unexpected
                        Dictionary <string, object> unexpected = RegistryServices.JsonToDictionary(child);
                        object unexpectedType = unexpected["@type"];
                        status.AddError("Unexpected document type");
                    }
                }
            }

            MappingHelperV3 helper = new MappingHelperV3(entityTypeId);

            helper.entityBlankNodes  = bnodes;
            helper.CurrentEntityCTID = input.CTID;
            helper.CurrentEntityName = input.Name.ToString();

            status.EnvelopeId = envelopeIdentifier;
            try
            {
                string ctid           = input.CTID;
                string referencedAtId = input.CtdlId;

                LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());
                LoggingHelper.DoTrace(6, "		url: "+ input.SubjectWebpage);
                LoggingHelper.DoTrace(5, "		ctid: "+ input.CTID);
                LoggingHelper.DoTrace(6, "		@Id: "+ input.CtdlId);
                status.Ctid = ctid;

                if (status.DoingDownloadOnly)
                {
                    return(true);
                }


                if (!DoesEntityExist(input.CTID, ref output))
                {
                    //set the rowid now, so that can be referenced as needed
                    output.RowId = Guid.NewGuid();
                    LoggingHelper.DoTrace(1, string.Format(thisClassName + ".Import(). Record was NOT found using CTID: '{0}'", input.CTID));
                }
                else
                {
                    LoggingHelper.DoTrace(1, string.Format(thisClassName + ".Import(). Found record: '{0}' using CTID: '{1}'", input.Name, input.CTID));
                }
                helper.currentBaseObject = output;

                output.Name           = helper.HandleLanguageMap(input.Name, output, "Name");
                output.Description    = helper.HandleLanguageMap(input.Description, output, "Description");
                output.SubjectWebpage = input.SubjectWebpage;
                output.CTID           = input.CTID;
                //TBD handling of referencing third party publisher
                if (!string.IsNullOrWhiteSpace(status.DocumentPublishedBy))
                {
                    //output.PublishedByOrganizationCTID = status.DocumentPublishedBy;
                    var porg = OrganizationManager.GetSummaryByCtid(status.DocumentPublishedBy);
                    if (porg != null && porg.Id > 0)
                    {
                        //TODO - store this in a json blob??????????
                        //this will result in being added to Entity.AgentRelationship
                        output.PublishedBy = new List <Guid>()
                        {
                            porg.RowId
                        };
                    }
                    else
                    {
                        //if publisher not imported yet, all publishee stuff will be orphaned
                        var entityUid = Guid.NewGuid();
                        var statusMsg = "";
                        var resPos    = referencedAtId.IndexOf("/resources/");
                        var swp       = referencedAtId.Substring(0, (resPos + "/resources/".Length)) + status.DocumentPublishedBy;
                        int orgId     = new OrganizationManager().AddPendingRecord(entityUid, status.DocumentPublishedBy, swp, ref statusMsg);
                    }
                }
                else
                {
                    //may need a check for existing published by to ensure not lost
                    //?????????????
                    if (output.Id > 0)
                    {
                        //if ( output.OrganizationRole != null && output.OrganizationRole.Any() )
                        //{
                        //	var publishedByList = output.OrganizationRole.Where( s => s.RoleTypeId == 30 ).ToList();
                        //	if ( publishedByList != null && publishedByList.Any() )
                        //	{
                        //		var pby = publishedByList[ 0 ].ActingAgentUid;
                        //		output.PublishedBy = new List<Guid>() { publishedByList[ 0 ].ActingAgentUid };
                        //	}
                        //}
                    }
                }
                //warning this gets set to blank if doing a manual import by ctid
                output.CredentialRegistryId = envelopeIdentifier;

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



                //what to do with these uris?
                output.AbilityEmbodied = input.AbilityEmbodied;
                //a concept, but unknown concept scheme?
                output.Classification    = input.Classification;
                output.KnowledgeEmbodied = input.KnowledgeEmbodied;
                output.SkillEmbodied     = input.SkillEmbodied;

                //SameAs URI - need to chg this to json -
                output.SameAs = helper.MapToTextValueProfile(input.SameAs);
                //
                output.VersionIdentifier = helper.MapIdentifierValueListInternal(input.VersionIdentifier);
                if (output.VersionIdentifier != null && output.VersionIdentifier.Count() > 0)
                {
                    output.VersionIdentifierJson = JsonConvert.SerializeObject(output.VersionIdentifier, MappingHelperV3.GetJsonSettings());
                }

                output.Keyword = helper.MapToTextValueProfile(input.Keyword, output, "Keyword");
                //Industries/occupations
                output.Industries  = helper.MapCAOListToCAOProfileList(input.IndustryType);
                output.Occupations = helper.MapCAOListToCAOProfileList(input.OccupationType);

                //may need to save the occupation and then handle components
                //or do a create pending for hasDestination and any hasChild (actually already done by MapEntityReferenceGuids)

                //=== if any messages were encountered treat as warnings for now
                if (messages.Count > 0)
                {
                    status.SetMessages(messages, true);
                }
                //

                //adding common import pattern
                importSuccessfull = mgr.Import(output, ref status);
                //
                status.DocumentId    = output.Id;
                status.DetailPageUrl = string.Format("~/occupation/{0}", output.Id);
                status.DocumentRowId = output.RowId;
                //if record was added to db, add to/or set EntityResolution as resolved
                int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                           ctid,
                                                                           entityTypeId,
                                                                           output.RowId,
                                                                           output.Id,
                                                                           (output.Id > 0),
                                                                           ref messages,
                                                                           output.Id > 0);
                //just in case - not sure if applicable, as will want to do components if the occupation exists
                if (status.HasErrors)
                {
                    importSuccessfull = false;
                    //email an error report, and/or add to activity log?
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, string.Format("Exception encountered in envelopeId: {0}", envelopeIdentifier), false, "Occupation Import exception");
            }

            return(importSuccessfull);
        }
Esempio n. 3
0
        public bool ImportV3(string payload, string envelopeIdentifier, SaveStatus status)
        {
            InputEntityV3 input      = new InputEntityV3();
            var           bnodes     = new List <BNode>();
            var           mainEntity = new Dictionary <string, object>();

            //status.AddWarning( "The resource uses @graph and is not handled yet" );

            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    mainEntity = RegistryServices.JsonToDictionary(main);
                    input      = JsonConvert.DeserializeObject <InputEntityV3>(main);
                }
                else
                {
                    var bn = item.ToString();
                    bnodes.Add(JsonConvert.DeserializeObject <BNode>(bn));
                }
            }

            List <string>   messages          = new List <string>();
            bool            importSuccessfull = false;
            EntityServices  mgr    = new EntityServices();
            MappingHelperV3 helper = new MappingHelperV3();

            helper.entityBlankNodes = bnodes;

            string ctid           = input.Ctid;
            string referencedAtId = input.CtdlId;

            LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());
            LoggingHelper.DoTrace(6, "		url: "+ input.SubjectWebpage);
            LoggingHelper.DoTrace(5, "		ctid: "+ input.Ctid);
            LoggingHelper.DoTrace(5, "		@Id: "+ input.CtdlId);
            status.Ctid = ctid;

            if (status.DoingDownloadOnly)
            {
                return(true);
            }

            if (!DoesEntityExist(input.Ctid, ref output))
            {
                //set the rowid now, so that can be referenced as needed
                output.RowId = Guid.NewGuid();
            }
            helper.currentBaseObject = output;

            //start with language and may use with language maps
            foreach (var l in input.InLanguage)
            {
                if (!string.IsNullOrWhiteSpace(l))
                {
                    var language = CodesManager.GetLanguage(l);
                    output.InLanguageCodeList.Add(new TextValueProfile()
                    {
                        CodeId    = language.CodeId,
                        TextTitle = language.Name,
                        TextValue = language.Value
                    });
                }
            }

            if (input.InLanguage.Count > 0)
            {
                //could use to alter helper.DefaultLanguage
            }
            output.Name        = helper.HandleLanguageMap(input.Name, output, "Name");
            output.Description = helper.HandleLanguageMap(input.Description, output, "Description");
            output.Keyword     = helper.MapToTextValueProfile(input.Keyword, output, "Keyword");

            output.CTID = input.Ctid;
            output.CredentialRegistryId = envelopeIdentifier;

            output.DateEffective = input.DateEffective;

            output.SubjectWebpage = input.SubjectWebpage;

            output.AvailabilityListing     = helper.MapListToString(input.AvailabilityListing);
            output.AvailableOnlineAt       = helper.MapListToString(input.AvailableOnlineAt);
            output.DeliveryType            = helper.MapCAOListToEnumermation(input.DeliveryType);
            output.DeliveryTypeDescription = helper.HandleLanguageMap(input.DeliveryTypeDescription, output, "DeliveryTypeDescription");
            //AudienceType
            output.AudienceType = helper.MapCAOListToEnumermation(input.AudienceType);
            //CAO
            output.AudienceLevelType     = helper.MapCAOListToEnumermation(input.AudienceLevelType);
            output.VersionIdentifier     = helper.MapIdentifierValueListToString(input.VersionIdentifier);
            output.VersionIdentifierList = helper.MapIdentifierValueList(input.VersionIdentifier);
            output.CodedNotation         = input.CodedNotation;
            //handle QuantitativeValue
            output.CreditValue = helper.HandleQuantitiveValue(input.CreditValue, output, "CreditValue");
            //
            if (!output.CreditValue.HasData())
            {
                //if ( UtilityManager.GetAppKeyValue( "usingQuantitiveValue", false ) )
                //{
                //	//will not handle ranges
                //	output.CreditValue = new workIT.Models.Common.QuantitativeValue
                //	{
                //		Value = input.CreditHourValue,
                //		CreditUnitType = helper.MapCAOToEnumermation( input.CreditUnitType ),
                //		Description = helper.HandleLanguageMap( input.CreditUnitTypeDescription, output, "CreditUnitTypeDescription" )
                //	};
                //	//what about hours?
                //	//if there is hour data, can't be unit data, so assign
                //	if ( input.CreditHourValue > 0 )
                //	{
                //		output.CreditValue.Value = input.CreditHourValue;
                //		output.CreditValue.Description = helper.HandleLanguageMap( input.CreditHourType, output, "CreditHourType" );
                //	}
                //}
                //else
                //{
                //	output.CreditHourType = helper.HandleLanguageMap( input.CreditHourType, output, "CreditHourType" );
                //	output.CreditHourValue = input.CreditHourValue;

                //	output.CreditUnitType = helper.MapCAOToEnumermation( input.CreditUnitType );
                //	output.CreditUnitValue = input.CreditUnitValue;
                //	output.CreditUnitTypeDescription = helper.HandleLanguageMap( input.CreditUnitTypeDescription, output, "CreditUnitTypeDescription" );
                //}
            }


            //occupations
            //output.Occupation = helper.MapCAOListToEnumermation( input.OccupationType );
            //actually used by import
            output.Occupations = helper.MapCAOListToCAOProfileList(input.OccupationType);
            //just append alternative items. Ensure empty lists are ignored
            output.Occupations.AddRange(helper.AppendLanguageMapListToCAOProfileList(input.AlternativeOccupationType));

            //skip if no occupations
            if (output.Occupations.Count() == 0 &&
                UtilityManager.GetAppKeyValue("skipCredImportIfNoOccupations", false))
            {
                //LoggingHelper.DoTrace( 2, string.Format( "		***Skipping lopp# {0}, {1} as it has no occupations and this is a special run.", output.Id, output.Name ) );
                //return true;
            }
            //Industries
            output.Industries = helper.MapCAOListToCAOProfileList(input.IndustryType);
            output.Industries.AddRange(helper.AppendLanguageMapListToCAOProfileList(input.AlternativeIndustryType));
            //naics
            //output.Naics = input.Naics;

            output.InstructionalProgramTypes = helper.MapCAOListToCAOProfileList(input.InstructionalProgramType);
            output.InstructionalProgramTypes.AddRange(helper.AppendLanguageMapListToCAOProfileList(input.AlternativeInstructionalProgramType));
            if (output.InstructionalProgramTypes.Count() == 0 && UtilityManager.GetAppKeyValue("skipAsmtImportIfNoCIP", false))
            {
                //skip
                //LoggingHelper.DoTrace( 2, string.Format( "		***Skipping lopp# {0}, {1} as it has no InstructionalProgramTypes and this is a special run.", output.Id, output.Name ) );
                //return true;
            }

            output.LearningMethodType = helper.MapCAOListToEnumermation(input.LearningMethodType);
            output.Subject            = helper.MapCAOListToTextValueProfile(input.Subject, CodesManager.PROPERTY_CATEGORY_SUBJECT);

            //output.VerificationMethodDescription = helper.HandleLanguageMap( input.VerificationMethodDescription, output, "VerificationMethodDescription" );
            //financial assitance
            output.FinancialAssistance = helper.FormatFinancialAssistance(input.FinancialAssistance, ref status);

            output.Jurisdiction = helper.MapToJurisdiction(input.Jurisdiction, ref status);

            //***EstimatedCost
            //will need to format, all populate Entity.RelatedCosts (for bubble up) - actually this would be for asmts, and lopps
            output.EstimatedCost = helper.FormatCosts(input.EstimatedCost, ref status);
            //connections
            output.AdvancedStandingFrom = helper.FormatConditionProfile(input.AdvancedStandingFrom, ref status);
            output.AdvancedStandingFor  = helper.FormatConditionProfile(input.IsAdvancedStandingFor, ref status);

            output.PreparationFrom  = helper.FormatConditionProfile(input.PreparationFrom, ref status);
            output.IsPreparationFor = helper.FormatConditionProfile(input.IsPreparationFor, ref status);

            output.IsRequiredFor    = helper.FormatConditionProfile(input.IsRequiredFor, ref status);
            output.IsRecommendedFor = helper.FormatConditionProfile(input.IsRecommendedFor, ref status);

            //EstimatedDuration
            output.EstimatedDuration = helper.FormatDuration(input.EstimatedDuration, ref status);

            //conditions ======================================
            output.Requires       = helper.FormatConditionProfile(input.Requires, ref status);
            output.Recommends     = helper.FormatConditionProfile(input.Recommends, ref status);
            output.EntryCondition = helper.FormatConditionProfile(input.EntryCondition, ref status);
            output.Corequisite    = helper.FormatConditionProfile(input.Corequisite, ref status);

            //TODO - develope entity for IdentitifierValue
            output.VersionIdentifier     = helper.MapIdentifierValueListToString(input.VersionIdentifier);
            output.VersionIdentifierList = helper.MapIdentifierValueList(input.VersionIdentifier);


            //teaches compentencies
            output.TeachesCompetencies = helper.MapCAOListToCAOProfileList(input.Teaches);
            if (output.TeachesCompetencies.Count() == 0 && UtilityManager.GetAppKeyValue("skipLoppImportIfNoCompetencies", false))
            {
                //skip
                LoggingHelper.DoTrace(2, string.Format("		***Skipping lopp# {0}, {1} as it has no competencies and this is a special run.", output.Id, output.Name));
                return(true);
            }

            //common conditions
            output.ConditionManifestIds = helper.MapEntityReferences(input.CommonConditions, CodesManager.ENTITY_TYPE_CONDITION_MANIFEST, CodesManager.ENTITY_TYPE_LEARNING_OPP_PROFILE, ref status);
            //common costs
            output.CostManifestIds = helper.MapEntityReferences(input.CommonCosts, CodesManager.ENTITY_TYPE_COST_MANIFEST, CodesManager.ENTITY_TYPE_LEARNING_OPP_PROFILE, ref status);

            //ADDRESSES
            output.Addresses = helper.FormatAvailableAtAddresses(input.AvailableAt, ref status);

            //BYs
            output.AccreditedBy = helper.MapOrganizationReferenceGuids(input.AccreditedBy, ref status);
            output.ApprovedBy   = helper.MapOrganizationReferenceGuids(input.ApprovedBy, ref status);
            output.OwnedBy      = helper.MapOrganizationReferenceGuids(input.OwnedBy, ref status);
            output.OfferedBy    = helper.MapOrganizationReferenceGuids(input.OfferedBy, ref status);
            if (output.OwnedBy != null && output.OwnedBy.Count > 0)
            {
                output.OwningAgentUid = output.OwnedBy[0];
            }
            else
            {
                //add warning?
                if (output.OfferedBy == null && output.OfferedBy.Count == 0)
                {
                    status.AddWarning("document doesn't have an owning or offering organization.");
                }
            }
            output.RecognizedBy = helper.MapOrganizationReferenceGuids(input.RecognizedBy, ref status);
            output.RegulatedBy  = helper.MapOrganizationReferenceGuids(input.RegulatedBy, ref status);

            //INs
            output.AccreditedIn = helper.MapToJurisdiction(input.AccreditedIn, ref status);
            output.ApprovedIn   = helper.MapToJurisdiction(input.ApprovedIn, ref status);
            output.ApprovedIn   = helper.MapToJurisdiction(input.ApprovedIn, ref status);
            output.RecognizedIn = helper.MapToJurisdiction(input.RecognizedIn, ref status);
            output.RegulatedIn  = helper.MapToJurisdiction(input.RegulatedIn, ref status);


            //var hasPartIds = input.HasPart.Select( x => x.CtdlId ).ToList();
            output.HasPartIds = helper.MapEntityReferences(input.HasPart, CodesManager.ENTITY_TYPE_LEARNING_OPP_PROFILE, CodesManager.ENTITY_TYPE_LEARNING_OPP_PROFILE, ref status);

            //var isPartIds = input.IsPartOf.Select( x => x.CtdlId ).ToList();
            output.IsPartOfIds = helper.MapEntityReferences(input.IsPartOf, CodesManager.ENTITY_TYPE_LEARNING_OPP_PROFILE, CodesManager.ENTITY_TYPE_LEARNING_OPP_PROFILE, ref status);

            //=== if any messages were encountered treat as warnings for now
            if (messages.Count > 0)
            {
                status.SetMessages(messages, true);
            }
            //just in case check if entity added since start
            if (output.Id == 0)
            {
                ThisEntity entity = EntityServices.GetByCtid(ctid);
                if (entity != null && entity.Id > 0)
                {
                    output.Id    = entity.Id;
                    output.RowId = entity.RowId;
                }
            }
            importSuccessfull = mgr.Import(output, ref status);

            status.DocumentId    = output.Id;
            status.DetailPageUrl = string.Format("~/learningOpportunity/{0}", output.Id);
            status.DocumentRowId = output.RowId;

            //just in case
            if (status.HasErrors)
            {
                importSuccessfull = false;
            }

            //if record was added to db, add to/or set EntityResolution as resolved
            int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                       ctid, CodesManager.ENTITY_TYPE_LEARNING_OPP_PROFILE,
                                                                       output.RowId,
                                                                       output.Id,
                                                                       (output.Id > 0),
                                                                       ref messages,
                                                                       output.Id > 0);

            return(importSuccessfull);
        }
        public bool ImportV3(string payload, string envelopeIdentifier, SaveStatus status)
        {
            //instantiate the input document (credential)
            InputEntityV3 input      = new InputEntityV3();
            var           bnodes     = new List <BNode>();
            var           mainEntity = new Dictionary <string, object>();
            //map payload to a dictionary
            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            //get the @graph property
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    //mainEntity = RegistryServices.JsonToDictionary( main );
                    input = JsonConvert.DeserializeObject <InputEntityV3>(main);
                }
                else
                {
                    //save blank nodes - typically reference organizations
                    var bn = item.ToString();
                    bnodes.Add(JsonConvert.DeserializeObject <BNode>(bn));
                }
            }

            List <string>   messages          = new List <string>();
            bool            importSuccessfull = false;
            EntityServices  mgr    = new EntityServices();
            MappingHelperV3 helper = new MappingHelperV3();

            helper.entityBlankNodes = bnodes;

            status.EnvelopeId = envelopeIdentifier;
            try
            {
                //input = JsonConvert.DeserializeObject<InputEntity>( item.DecodedResource.ToString() );
                string ctid           = input.Ctid;
                string referencedAtId = input.CtdlId;

                LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());
                LoggingHelper.DoTrace(6, "		url: "+ input.SubjectWebpage);
                LoggingHelper.DoTrace(5, "		ctid: "+ input.Ctid);
                LoggingHelper.DoTrace(6, "		@Id: "+ input.CtdlId);
                status.Ctid = ctid;

                if (status.DoingDownloadOnly)
                {
                    return(true);
                }

                if (!DoesEntityExist(input.Ctid, ref output))
                {
                    //set the rowid now, so that can be referenced as needed
                    output.RowId = Guid.NewGuid();
                }
                helper.currentBaseObject = output;

                //start with language and may use with language maps
                foreach (var l in input.InLanguage)
                {
                    if (!string.IsNullOrWhiteSpace(l))
                    {
                        var language = CodesManager.GetLanguage(l);
                        output.InLanguageCodeList.Add(new TextValueProfile()
                        {
                            CodeId    = language.CodeId,
                            TextTitle = language.Name,
                            TextValue = language.Value
                        });
                    }
                }

                if (input.InLanguage.Count > 0)
                {
                    //could use to alter helper.DefaultLanguage
                }
                output.Name           = helper.HandleLanguageMap(input.Name, output, "Name");
                output.Description    = helper.HandleLanguageMap(input.Description, output, "Description");
                output.SubjectWebpage = input.SubjectWebpage;
                output.CTID           = input.Ctid;
                //warning this gets set to blank if doing a manual import by ctid
                output.CredentialRegistryId = envelopeIdentifier;
                output.CredentialStatusType = helper.MapCAOToEnumermation(input.CredentialStatusType);
                output.DateEffective        = input.DateEffective;

                output.AlternateNames       = helper.MapToTextValueProfile(input.AlternateName, output, "AlternateName");
                output.ImageUrl             = input.Image;
                output.CredentialTypeSchema = input.CredentialType;


                output.AvailabilityListing = helper.MapListToString(input.AvailabilityListing);
                output.AvailableOnlineAt   = helper.MapListToString(input.AvailableOnlineAt);

                output.CredentialId = input.CredentialId;
                //TODO - develope entity for IdentitifierValue
                output.VersionIdentifier     = helper.MapIdentifierValueListToString(input.VersionIdentifier);
                output.VersionIdentifierList = helper.MapIdentifierValueList(input.VersionIdentifier);

                output.CodedNotation = input.CodedNotation;
                output.ISICV4        = input.IsicV4;

                output.ProcessStandards            = input.ProcessStandards;
                output.ProcessStandardsDescription = helper.HandleLanguageMap(input.ProcessStandardsDescription, output, "ProcessStandardsDescription");
                output.LatestVersion   = input.LatestVersion;
                output.PreviousVersion = input.PreviousVersion;
                output.NextVersion     = input.NextVersion;
                output.SupersededBy    = input.SupersededBy;
                output.Supersedes      = input.Supersedes;

                output.Subject = helper.MapCAOListToTextValueProfile(input.Subject, CodesManager.PROPERTY_CATEGORY_SUBJECT);

                //occupations
                //output.Occupation = helper.MapCAOListToEnumermation( input.OccupationType );
                //actually used by import
                output.Occupations = helper.MapCAOListToCAOProfileList(input.OccupationType);
                //just append alternative items. Ensure empty lists are ignored
                output.Occupations.AddRange(helper.AppendLanguageMapListToCAOProfileList(input.AlternativeOccupationType));

                //skip if no occupations
                if (output.Occupations.Count() == 0 &&
                    UtilityManager.GetAppKeyValue("skipCredImportIfNoOccupations", false))
                {
                    //LoggingHelper.DoTrace( 2, string.Format( "		***Skipping Credential# {0}, {1} as it has no occupations and this is a special run.", output.Id, output.Name ) );
                    //return true;
                }
                //Industries
                output.Industries = helper.MapCAOListToCAOProfileList(input.IndustryType);
                output.Industries.AddRange(helper.AppendLanguageMapListToCAOProfileList(input.AlternativeIndustryType));
                //naics
                output.Naics = input.Naics;

                output.InstructionalProgramTypes = helper.MapCAOListToCAOProfileList(input.InstructionalProgramType);
                output.InstructionalProgramTypes.AddRange(helper.AppendLanguageMapListToCAOProfileList(input.AlternativeInstructionalProgramType));
                //
                //will want a custom method to lookup the rating
                NavyServices nsrvs = new NavyServices();
                output.NavyRating = NavyServices.MapRatingsListToEnumermation(input.HasRating);
                output.NavyRatingType.AddRange(nsrvs.MapCAOListToCAOProfileList(input.HasRating, ref messages));
                //

                output.Keyword = helper.MapToTextValueProfile(input.Keyword, output, "Keyword");

                output.Jurisdiction = helper.MapToJurisdiction(input.Jurisdiction, ref status);
                //CopyrightHolder - expecting single; will need to expand
                output.CopyrightHolder = helper.MapOrganizationReferencesGuid("Credential.CopyrightHolder", input.CopyrightHolder, ref status);
                //CAO
                output.AudienceLevelType = helper.MapCAOListToEnumermation(input.AudienceLevelType);
                //
                output.AudienceType        = helper.MapCAOListToEnumermation(input.AudienceType);
                output.DegreeConcentration = helper.MapCAOListToTextValueProfile(input.DegreeConcentration, CodesManager.PROPERTY_CATEGORY_DEGREE_CONCENTRATION);
                output.DegreeMajor         = helper.MapCAOListToTextValueProfile(input.DegreeMajor, CodesManager.PROPERTY_CATEGORY_DEGREE_MAJOR);
                output.DegreeMinor         = helper.MapCAOListToTextValueProfile(input.DegreeMinor, CodesManager.PROPERTY_CATEGORY_DEGREE_MINOR);

                output.AssessmentDeliveryType = helper.MapCAOListToEnumermation(input.AssessmentDeliveryType);
                output.LearningDeliveryType   = helper.MapCAOListToEnumermation(input.LearningDeliveryType);

                //EstimatedCost
                //will need to format, all populate Entity.RelatedCosts (for bubble up) - actually this would be for asmts, and lopps
                output.EstimatedCost = helper.FormatCosts(input.EstimatedCost, ref status);

                //EstimatedDuration
                output.EstimatedDuration = helper.FormatDuration(input.EstimatedDuration, ref status);
                output.RenewalFrequency  = helper.FormatDurationItem(input.RenewalFrequency);

                //conditions
                output.Requires    = helper.FormatConditionProfile(input.Requires, ref status);
                output.Recommends  = helper.FormatConditionProfile(input.Recommends, ref status);
                output.Renewal     = helper.FormatConditionProfile(input.Renewal, ref status);
                output.Corequisite = helper.FormatConditionProfile(input.Corequisite, ref status);
                output.Revocation  = helper.FormatRevocationProfile(input.Revocation, ref status);

                //connections
                output.AdvancedStandingFrom = helper.FormatConditionProfile(input.AdvancedStandingFrom, ref status);
                output.AdvancedStandingFor  = helper.FormatConditionProfile(input.IsAdvancedStandingFor, ref status);

                output.PreparationFrom  = helper.FormatConditionProfile(input.PreparationFrom, ref status);
                output.IsPreparationFor = helper.FormatConditionProfile(input.IsPreparationFor, ref status);

                output.IsRequiredFor    = helper.FormatConditionProfile(input.IsRequiredFor, ref status);
                output.IsRecommendedFor = helper.FormatConditionProfile(input.IsRecommendedFor, ref status);

                //common conditions
                output.ConditionManifestIds = helper.MapEntityReferences(input.CommonConditions, CodesManager.ENTITY_TYPE_CONDITION_MANIFEST, CodesManager.ENTITY_TYPE_CREDENTIAL, ref status);
                //common costs
                output.CostManifestIds = helper.MapEntityReferences(input.CommonCosts, CodesManager.ENTITY_TYPE_COST_MANIFEST, CodesManager.ENTITY_TYPE_CREDENTIAL, ref status);

                //HasPart/IsPart
                //WARNING - these methods assume all parts are the same type - the provided thisEntityTypeId. AT THIS TIME, THE PARTS SHOULD ALL BE CREDENTIALS
                output.HasPartIds  = helper.MapEntityReferences(input.HasPart, thisEntityTypeId, ref status);
                output.IsPartOfIds = helper.MapEntityReferences(input.IsPartOf, thisEntityTypeId, ref status);

                //Process profiles
                output.AdministrationProcess = helper.FormatProcessProfile(input.AdministrationProcess, ref status);
                output.DevelopmentProcess    = helper.FormatProcessProfile(input.DevelopmentProcess, ref status);
                output.MaintenanceProcess    = helper.FormatProcessProfile(input.MaintenanceProcess, ref status);

                output.AppealProcess     = helper.FormatProcessProfile(input.AppealProcess, ref status);
                output.ComplaintProcess  = helper.FormatProcessProfile(input.ComplaintProcess, ref status);
                output.ReviewProcess     = helper.FormatProcessProfile(input.ReviewProcess, ref status);
                output.RevocationProcess = helper.FormatProcessProfile(input.RevocationProcess, ref status);

                //FinancialAssistance
                output.FinancialAssistance = helper.FormatFinancialAssistance(input.FinancialAssistance, ref status);


                output.Addresses = helper.FormatAvailableAtAddresses(input.AvailableAt, ref status);

                //BYs
                output.AccreditedBy = helper.MapOrganizationReferenceGuids("Credential.AccreditedBy", input.AccreditedBy, ref status);
                output.ApprovedBy   = helper.MapOrganizationReferenceGuids("Credential.ApprovedBy", input.ApprovedBy, ref status);
                output.OfferedBy    = helper.MapOrganizationReferenceGuids("Credential.OfferedBy", input.OfferedBy, ref status);
                //note need to set output.OwningAgentUid to the first entry
                output.OwnedBy = helper.MapOrganizationReferenceGuids("Credential.OwnedBy", input.OwnedBy, ref status);
                if (output.OwnedBy != null && output.OwnedBy.Count > 0)
                {
                    output.OwningAgentUid = output.OwnedBy[0];
                }
                else
                {
                    //add warning?
                    if (output.OfferedBy == null && output.OfferedBy.Count == 0)
                    {
                        status.AddWarning("document doesn't have an owning or offering organization.");
                    }
                }

                output.RecognizedBy = helper.MapOrganizationReferenceGuids("Credential.RecognizedBy", input.RecognizedBy, ref status);
                output.RegulatedBy  = helper.MapOrganizationReferenceGuids("Credential.RegulatedBy", input.RegulatedBy, ref status);
                output.RevokedBy    = helper.MapOrganizationReferenceGuids("Credential.RevokedBy", input.RevokedBy, ref status);
                output.RenewedBy    = helper.MapOrganizationReferenceGuids("Credential.RenewedBy", input.RenewedBy, ref status);

                //INs
                output.AccreditedIn = helper.MapToJurisdiction(input.AccreditedIn, ref status);
                output.ApprovedIn   = helper.MapToJurisdiction(input.ApprovedIn, ref status);
                output.ApprovedIn   = helper.MapToJurisdiction(input.ApprovedIn, ref status);
                output.RecognizedIn = helper.MapToJurisdiction(input.RecognizedIn, ref status);
                output.RegulatedIn  = helper.MapToJurisdiction(input.RegulatedIn, ref status);
                output.RevokedIn    = helper.MapToJurisdiction(input.RevokedIn, ref status);
                output.RenewedIn    = helper.MapToJurisdiction(input.RenewedIn, ref status);

                //=== if any messages were encountered treat as warnings for now
                if (messages.Count > 0)
                {
                    status.SetMessages(messages, true);
                }
                //just in case check if entity added since start
                if (output.Id == 0)
                {
                    ThisEntity entity = EntityServices.GetByCtid(ctid);
                    if (entity != null && entity.Id > 0)
                    {
                        output.Id    = entity.Id;
                        output.RowId = entity.RowId;
                    }
                }
                //save the data
                importSuccessfull    = mgr.Import(output, ref status);
                status.DocumentId    = output.Id;
                status.DetailPageUrl = string.Format("~/credential/{0}", output.Id);
                status.DocumentRowId = output.RowId;

                //if record was added to db, add to/or set EntityResolution as resolved
                int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                           ctid,
                                                                           CodesManager.ENTITY_TYPE_CREDENTIAL,
                                                                           output.RowId,
                                                                           output.Id,
                                                                           (output.Id > 0),
                                                                           ref messages,
                                                                           output.Id > 0);
                //just in case
                if (status.HasErrors)
                {
                    importSuccessfull = false;
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, string.Format("Exception encountered in envelopeId: {0}", envelopeIdentifier), false, "workIT Import exception");
            }

            return(importSuccessfull);
        }
        public bool ImportV3(string payload, SaveStatus status)
        {
            DateTime      started      = DateTime.Now;
            var           saveDuration = new TimeSpan();
            InputEntityV3 input        = new InputEntityV3();
            var           bnodes       = new List <BNodeV3>();
            var           mainEntity   = new Dictionary <string, object>();

            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    mainEntity = RegistryServices.JsonToDictionary(main);
                    input      = JsonConvert.DeserializeObject <InputEntityV3>(main);
                }
                else //is this too much of an assumption?
                {
                    var bn = item.ToString();
                    bnodes.Add(JsonConvert.DeserializeObject <BNodeV3>(bn));
                }
            }

            List <string>   messages          = new List <string>();
            bool            importSuccessfull = false;
            EntityServices  mgr    = new EntityServices();
            MappingHelperV3 helper = new MappingHelperV3(2)
            {
                entityBlankNodes = bnodes
            };

            helper.entityBlankNodes  = bnodes;
            helper.CurrentEntityCTID = input.CTID;
            helper.CurrentEntityName = input.Name.ToString();

            try {
                string ctid           = input.CTID;
                string referencedAtId = input.CtdlId;

                LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());
                LoggingHelper.DoTrace(6, "		url: "+ input.SubjectWebpage);
                LoggingHelper.DoTrace(5, "		ctid: "+ input.CTID);
                LoggingHelper.DoTrace(5, "		@Id: "+ input.CtdlId);
                status.Ctid = ctid;

                if (status.DoingDownloadOnly)
                {
                    return(true);
                }

                if (!DoesEntityExist(input.CTID, ref output))
                {
                    //set the rowid now, so that can be referenced as needed
                    output.RowId = Guid.NewGuid();
                    LoggingHelper.DoTrace(1, string.Format(thisClassName + ".ImportV3(). Record was NOT found using CTID: '{0}'", input.CTID));
                }
                else
                {
                    LoggingHelper.DoTrace(1, string.Format(thisClassName + ".ImportV3(). Found record: '{0}' using CTID: '{1}'", input.Name, input.CTID));
                }

                helper.currentBaseObject     = output;
                helper.CurrentOwningAgentUid = output.RowId;

                output.AgentDomainType = input.Type;
                output.Name            = helper.HandleLanguageMap(input.Name, output, "Name");
                output.Description     = helper.HandleLanguageMap(input.Description, output, "Description");
                //map from idProperty to url
                output.SubjectWebpage = input.SubjectWebpage;
                output.CTID           = input.CTID;
                //TBD handling
                if (!string.IsNullOrWhiteSpace(status.DocumentPublishedBy))
                {
                    //output.PublishedByOrganizationCTID = status.DocumentPublishedBy;
                    var porg = OrganizationManager.GetSummaryByCtid(status.DocumentPublishedBy);
                    if (porg != null && porg.Id > 0)
                    {
                        //TODO - store this in a json blob??????????
                        //output.PublishedByOrganizationId = porg.Id;
                        //output.PublishedByOrganizationName = porg.Name;
                        //this will result in being added to Entity.AgentRelationship
                        output.PublishedBy = new List <Guid>()
                        {
                            porg.RowId
                        };
                    }
                    else
                    {
                        //if publisher not imported yet, all publishee stuff will be orphaned
                        var entityUid = Guid.NewGuid();
                        var statusMsg = "";
                        var resPos    = referencedAtId.IndexOf("/resources/");
                        var swp       = referencedAtId.Substring(0, (resPos + "/resources/".Length)) + status.DocumentPublishedBy;
                        int orgId     = new OrganizationManager().AddPendingRecord(entityUid, status.DocumentPublishedBy, swp, ref statusMsg);
                    }
                }
                else
                {
                    //may need a check for existing published by to ensure not lost
                    if (output.Id > 0)
                    {
                        if (output.OrganizationRole_Recipient != null && output.OrganizationRole_Recipient.Any())
                        {
                            var publishedByList = output.OrganizationRole_Recipient.Where(s => s.RoleTypeId == 30).ToList();
                            if (publishedByList != null && publishedByList.Any())
                            {
                                var pby = publishedByList[0].ActingAgentUid;
                                output.PublishedBy = new List <Guid>()
                                {
                                    publishedByList[0].ActingAgentUid
                                };
                            }
                        }
                    }
                }

                output.CredentialRegistryId = status.EnvelopeId;;
                output.AlternateNames       = helper.MapToTextValueProfile(input.AlternateName, output, "AlternateName");
                output.Image = input.Image;

                output.AgentPurpose            = input.AgentPurpose;
                output.AgentPurposeDescription = helper.HandleLanguageMap(input.AgentPurposeDescription, output, "AgentPurposeDescription");

                output.FoundingDate = input.FoundingDate;
                //output.AvailabilityListing = helper.MapListToString( input.AvailabilityListing );
                //future prep
                output.AvailabilityListings = input.AvailabilityListing;

                output.MissionAndGoalsStatement            = input.MissionAndGoalsStatement;
                output.MissionAndGoalsStatementDescription = helper.HandleLanguageMap(input.MissionAndGoalsStatementDescription, output, "MissionAndGoalsStatementDescription");

                output.Addresses = helper.FormatAvailableAtAddresses(input.Address, ref status);
                if (output.Addresses != null && output.Addresses.Any())
                {
                    output.AddressesJson = JsonConvert.SerializeObject(output.Addresses, MappingHelperV3.GetJsonSettings());
                }
                //
                if (UtilityManager.GetAppKeyValue("skipOppImportIfNoShortRegions", false))
                {
                    if (output.Addresses.Count == 0)
                    {
                        //skip
                        LoggingHelper.DoTrace(2, string.Format("		***Skipping org# {0}, {1} as it has no addresses and this is a special run.", output.Id, output.Name));
                        return(true);
                    }
                    else if (output.HasAnyShortRegions == false)
                    {
                        //skip
                        LoggingHelper.DoTrace(2, string.Format("		***Skipping org# {0}, {1} as it has no addresses with short regions and this is a special run.", output.Id, output.Name));
                        return(true);
                    }
                }

                //agent type, map to enumeration
                output.AgentType = helper.MapCAOListToEnumermation(input.AgentType);

                output.TransferValueStatement            = input.TransferValueStatement;
                output.TransferValueStatementDescription = helper.HandleLanguageMap(input.TransferValueStatementDescription, output, "TransferValueStatementDescription");

                //Manifests
                output.ConditionManifestIds = helper.MapEntityReferences(input.HasConditionManifest, CodesManager.ENTITY_TYPE_CONDITION_MANIFEST, CodesManager.ENTITY_TYPE_ORGANIZATION, ref status);
                output.CostManifestIds      = helper.MapEntityReferences(input.HasCostManifest, CodesManager.ENTITY_TYPE_COST_MANIFEST, CodesManager.ENTITY_TYPE_ORGANIZATION, ref status);

                //hasVerificationService
                output.VerificationServiceProfiles = helper.MapVerificationServiceProfiles(input.VerificationServiceProfiles, ref status);

                // output.targetc
                //other enumerations
                //	serviceType, AgentSectorType
                output.ServiceType     = helper.MapCAOListToEnumermation(input.ServiceType);
                output.AgentSectorType = helper.MapCAOListToEnumermation(input.AgentSectorType);

                //Industries
                //output.Industry = helper.MapCAOListToEnumermation( input.IndustryType );
                output.Industries = helper.MapCAOListToCAOProfileList(input.IndustryType);
                //naics
                output.Naics = input.Naics;

                //keywords
                output.Keyword = helper.MapToTextValueProfile(input.Keyword, output, "Keyword");

                //duns, Fein.  IpedsID, opeID
                if (!string.IsNullOrWhiteSpace(input.DUNS))
                {
                    output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                        CodeSchema = "ceterms:duns", TextValue = input.DUNS
                    });
                }
                if (!string.IsNullOrWhiteSpace(input.FEIN))
                {
                    output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                        CodeSchema = "ceterms:fein", TextValue = input.FEIN
                    });
                }

                if (!string.IsNullOrWhiteSpace(input.IpedsID))
                {
                    output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                        CodeSchema = "ceterms:ipedsID", TextValue = input.IpedsID
                    });
                }

                if (!string.IsNullOrWhiteSpace(input.OPEID))
                {
                    output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                        CodeSchema = "ceterms:opeID", TextValue = input.OPEID
                    });
                }
                if (!string.IsNullOrWhiteSpace(input.LEICode))
                {
                    output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                        CodeSchema = "ceterms:leiCode", TextValue = input.LEICode
                    });
                }
                //
                if (!string.IsNullOrWhiteSpace(input.NcesID))
                {
                    output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                        CodeSchema = "ceterms:ncesID", TextValue = input.NcesID
                    });
                }
                //
                if (!string.IsNullOrWhiteSpace(input.ISICV4))
                {
                    output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                        CodeSchema = "ceterms:isicV4", TextValue = input.ISICV4
                    });
                }

                //alternativeidentifier - should just be added to IdentificationCodes
                //output.AlternativeIdentifier = helper.MapIdentifierValueListToString( input.AlternativeIdentifier );
                //output.AlternativeIdentifierList = helper.MapIdentifierValueList( input.AlternativeIdentifier );
                //20-10-31 - replacing AlternativeIdentifier with Identifier
                output.Identifier = helper.MapIdentifierValueList(input.Identifier);
                if (output.Identifier != null && output.Identifier.Count() > 0)
                {
                    output.IdentifierJson = JsonConvert.SerializeObject(output.Identifier, MappingHelperV3.GetJsonSettings());
                }

                //email

                output.Emails = helper.MapToTextValueProfile(input.Email);
                //contact point
                //output.ContactPoint = helper.FormatContactPoints( input.ContactPoint, ref status );
                //Jurisdiction
                output.Jurisdiction = helper.MapToJurisdiction(input.Jurisdiction, ref status);

                //SameAs URI
                output.SameAs = helper.MapToTextValueProfile(input.SameAs);
                //Social media
                output.SocialMediaPages = helper.MapToTextValueProfile(input.SocialMedia);

                //departments
                //not sure - MP - want to change how depts, and subs are handled
                output.ParentOrganization = helper.MapOrganizationReferenceGuids("Organization.ParentOrganization", input.ParentOrganization, ref status);
                output.Departments        = helper.MapOrganizationReferenceGuids("Organization.Department", input.Department, ref status);
                output.SubOrganizations   = helper.MapOrganizationReferenceGuids("Organization.SubOrganization", input.SubOrganization, ref status);

                //output.OrganizationRole_Subsidiary = helper.FormatOrganizationReferences( input.SubOrganization );

                //Process profiles
                output.AdministrationProcess = helper.FormatProcessProfile(input.AdministrationProcess, ref status);
                output.MaintenanceProcess    = helper.FormatProcessProfile(input.MaintenanceProcess, ref status);
                output.ComplaintProcess      = helper.FormatProcessProfile(input.ComplaintProcess, ref status);
                output.DevelopmentProcess    = helper.FormatProcessProfile(input.DevelopmentProcess, ref status);
                output.RevocationProcess     = helper.FormatProcessProfile(input.RevocationProcess, ref status);
                output.ReviewProcess         = helper.FormatProcessProfile(input.ReviewProcess, ref status);
                output.AppealProcess         = helper.FormatProcessProfile(input.AppealProcess, ref status);

                //BYs
                output.AccreditedBy = helper.MapOrganizationReferenceGuids("Organization.AccreditedBy", input.AccreditedBy, ref status);
                output.ApprovedBy   = helper.MapOrganizationReferenceGuids("Organization.ApprovedBy", input.ApprovedBy, ref status);
                output.RecognizedBy = helper.MapOrganizationReferenceGuids("Organization.RecognizedBy", input.RecognizedBy, ref status);
                output.RegulatedBy  = helper.MapOrganizationReferenceGuids("Organization.RegulatedBy", input.RegulatedBy, ref status);
                //INs
                output.AccreditedIn = helper.MapToJurisdiction("Organization.AccreditedIn", input.AccreditedIn, ref status);
                output.ApprovedIn   = helper.MapToJurisdiction("Organization.ApprovedIn", input.ApprovedIn, ref status);
                output.RecognizedIn = helper.MapToJurisdiction("Organization.RecognizedIn", input.RecognizedIn, ref status);
                output.RegulatedIn  = helper.MapToJurisdiction("Organization.RegulatedIn", input.RegulatedIn, ref status);

                //Asserts
                //the entity type is not known
                output.Accredits = helper.MapEntityReferenceGuids("Organization.Accredits", input.Accredits, 0, ref status);
                output.Approves  = helper.MapEntityReferenceGuids("Organization.Approves", input.Approves, 0, ref status);
                if (output.Approves.Count > 0)
                {
                }
                output.Offers     = helper.MapEntityReferenceGuids("Organization.Offers", input.Offers, 0, ref status);
                output.Owns       = helper.MapEntityReferenceGuids("Organization.Owns", input.Owns, 0, ref status);
                output.Renews     = helper.MapEntityReferenceGuids("Organization.Renews", input.Renews, 0, ref status);
                output.Revokes    = helper.MapEntityReferenceGuids("Organization.Revokes", input.Revokes, 0, ref status);
                output.Recognizes = helper.MapEntityReferenceGuids("Organization.Recognizes", input.Recognizes, 0, ref status);
                output.Regulates  = helper.MapEntityReferenceGuids("Organization.Regulates", input.Regulates, 0, ref status);

                //

                TimeSpan duration = DateTime.Now.Subtract(started);
                if (duration.TotalSeconds > 5)
                {
                    LoggingHelper.DoTrace(6, string.Format("         WARNING Mapping Duration: {0:N2} seconds ", duration.TotalSeconds));
                }
                DateTime saveStarted = DateTime.Now;

                //=== if any messages were encountered treat as warnings for now
                if (messages.Count > 0)
                {
                    status.SetMessages(messages, true);
                }
                //just in case check if entity added since start
                if (output.Id == 0)
                {
                    ThisEntity entity = EntityServices.GetSummaryByCtid(ctid);
                    if (entity != null && entity.Id > 0)
                    {
                        output.Id    = entity.Id;
                        output.RowId = entity.RowId;
                    }
                }
                importSuccessfull = mgr.Import(output, ref status);
                saveDuration      = DateTime.Now.Subtract(saveStarted);
                if (saveDuration.TotalSeconds > 5)
                {
                    LoggingHelper.DoTrace(6, string.Format("         WARNING SAVE Duration: {0:N2} seconds ", saveDuration.TotalSeconds));
                }
                //
                status.DocumentId    = output.Id;
                status.DetailPageUrl = string.Format("~/organization/{0}", output.Id);
                status.DocumentRowId = output.RowId;

                //just in case
                if (status.HasErrors)
                {
                    importSuccessfull = false;
                }

                //if record was added to db, add to/or set EntityResolution as resolved
                int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                           ctid, CodesManager.ENTITY_TYPE_ORGANIZATION,
                                                                           output.RowId,
                                                                           output.Id,
                                                                           false,
                                                                           ref messages,
                                                                           output.Id > 0);
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, string.Format("OrganizationImportV3. Exception encountered in CTID: {0}", input.CTID), false, "Organization Import exception");
            }
            finally
            {
                var totalDuration = DateTime.Now.Subtract(started);
                if (totalDuration.TotalSeconds > 9 && (totalDuration.TotalSeconds - saveDuration.TotalSeconds > 3))
                {
                    LoggingHelper.DoTrace(5, string.Format("         WARNING Total Duration: {0:N2} seconds ", totalDuration.TotalSeconds));
                }
            }
            return(importSuccessfull);
        }
        }         //

        public bool Import(string payload, string envelopeIdentifier, SaveStatus status)
        {
            List <string>  messages          = new List <string>();
            bool           importSuccessfull = false;
            EntityServices mgr = new EntityServices();
            //
            InputEntity input      = new InputEntity();
            var         bnodes     = new List <BNode>();
            var         mainEntity = new Dictionary <string, object>();
            //
            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    mainEntity = RegistryServices.JsonToDictionary(main);
                    input      = JsonConvert.DeserializeObject <InputEntity>(main);
                }
                else
                {
                    var bn = item.ToString();
                    //20-07-02 need to handle the enhanced bnodes
                    bnodes.Add(JsonConvert.DeserializeObject <BNode>(bn));
                }
            }

            ///============= process =============================
            MappingHelperV3 helper = new MappingHelperV3(3);

            helper.entityBlankNodes  = bnodes;
            helper.CurrentEntityCTID = input.CTID;
            helper.CurrentEntityName = input.Name.ToString();

            string ctid = input.CTID;

            status.Ctid = ctid;
            string referencedAtId = input.CtdlId;

            LoggingHelper.DoTrace(5, "		ctid: "+ ctid);
            LoggingHelper.DoTrace(5, "		@Id: "+ input.CtdlId);
            LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());

            if (status.DoingDownloadOnly)
            {
                return(true);
            }

            //add/updating TransferValue
            if (!DoesEntityExist(input.CTID, ref output, ref status))
            {
                //set the rowid now, so that can be referenced as needed
                output.RowId = Guid.NewGuid();
            }
            helper.currentBaseObject = output;

            output.Name        = helper.HandleLanguageMap(input.Name, output, "Name");
            output.Description = helper.HandleLanguageMap(input.Description, output, "Description");
            output.CTID        = input.CTID;
            //TBD handling of referencing third party publisher
            if (!string.IsNullOrWhiteSpace(status.DocumentPublishedBy))
            {
                //output.PublishedByOrganizationCTID = status.DocumentPublishedBy;
                var porg = OrganizationManager.GetSummaryByCtid(status.DocumentPublishedBy);
                if (porg != null && porg.Id > 0)
                {
                    //TODO - store this in a json blob??????????
                    //this will result in being added to Entity.AgentRelationship
                    output.PublishedBy = new List <Guid>()
                    {
                        porg.RowId
                    };
                }
                else
                {
                    //if publisher not imported yet, all publishee stuff will be orphaned
                    var entityUid = Guid.NewGuid();
                    var statusMsg = "";
                    var resPos    = referencedAtId.IndexOf("/resources/");
                    var swp       = referencedAtId.Substring(0, (resPos + "/resources/".Length)) + status.DocumentPublishedBy;
                    int orgId     = new OrganizationManager().AddPendingRecord(entityUid, status.DocumentPublishedBy, swp, ref statusMsg);
                }
            }
            else
            {
                //may need a check for existing published by to ensure not lost
                if (output.Id > 0)
                {
                    if (output.OrganizationRole != null && output.OrganizationRole.Any())
                    {
                        var publishedByList = output.OrganizationRole.Where(s => s.RoleTypeId == 30).ToList();
                        if (publishedByList != null && publishedByList.Any())
                        {
                            var pby = publishedByList[0].ActingAgentUid;
                            output.PublishedBy = new List <Guid>()
                            {
                                publishedByList[0].ActingAgentUid
                            };
                        }
                    }
                }
            }
            output.CredentialRegistryId = envelopeIdentifier;
            output.SubjectWebpage       = input.SubjectWebpage;
            //****owner missing
            output.OwnedBy = helper.MapOrganizationReferenceGuids("TransferValue.OwnedBy", input.OwnedBy, ref status);
            if (output.OwnedBy != null && output.OwnedBy.Count > 0)
            {
                output.OwningAgentUid        = output.OwnedBy[0];
                helper.CurrentOwningAgentUid = output.OwnedBy[0];
            }

            //
            //output.DerivedFromForImport = helper.MapEntityReferenceGuids( "TransferValue.DerivedFrom", input.DerivedFrom, CodesManager.ENTITY_TYPE_TRANSFER_VALUE_PROFILE, ref status );
            output.DerivedFromForImport = helper.MapEntityReferences("TransferValue.DerivedFrom", input.DerivedFrom, CodesManager.ENTITY_TYPE_TRANSFER_VALUE_PROFILE, ref status);
            //
            output.DevelopmentProcess = helper.FormatProcessProfile(input.DevelopmentProcess, ref status);


            //TBD - will replace codedNotation
            //output.CodedNotation = input.CodedNotation;
            //TBD - store more stuff as Json
            output.Identifier = helper.MapIdentifierValueList(input.Identifier);
            if (output.Identifier != null && output.Identifier.Count() > 0)
            {
                output.IdentifierJson = JsonConvert.SerializeObject(output.Identifier, MappingHelperV3.GetJsonSettings());
            }
            //

            //need to handle partial dates
            output.StartDate = input.StartDate;
            output.EndDate   = input.EndDate;
            //
            output.LifecycleStatusType = helper.MapCAOToString(input.LifecycleStatusType);
            //output.LifecycleStatusType = helper.MapCAOToEnumermation( input.LifecycleStatusType );
            //adding common import pattern
            //new PathwayManager().Save( ef, ref status, true );

            //

            output.TransferValue = helper.HandleValueProfileList(input.TransferValue, "TransferValueProfile.TransferValue");
            if (output.TransferValue != null && output.TransferValue.Count() > 0)
            {
                output.TransferValueJson = JsonConvert.SerializeObject(output.TransferValue, MappingHelperV3.GetJsonSettings());
            }
            //the class type must be provided for a blank node
            //TODO - make sure code handles extended properties
            //20-07-29 - getting duplicates - need to properly check for existing
            //			- actually a common approach is to delete all existing. this suggests NOT creating the pending entities!!
            //			- could be part of exists check
            output.TransferValueForImport = helper.MapEntityReferenceGuids("TransferValue.TransferValueFor", input.TransferValueFor, 0, ref status);
            if (output.TransferValueForImport != null && output.TransferValueForImport.Count() > 0)
            {
                //TransferValueForImport is a list of guids which could reference a blank node
                foreach (var item in output.TransferValueForImport)
                {
                    var tlo = ProfileServices.GetEntityAsTopLevelObject(item);
                    if (tlo != null && tlo.Id > 0)
                    {
                        output.TransferValueFor.Add(tlo);
                    }
                    else
                    {
                        //log error
                    }
                }
                //get all object as
                output.TransferValueForJson = JsonConvert.SerializeObject(output.TransferValueFor, MappingHelperV3.GetJsonSettings());
            }
            output.TransferValueFromImport = helper.MapEntityReferenceGuids("TransferValue.TransferValueFrom", input.TransferValueFrom, 0, ref status);
            if (output.TransferValueFromImport != null && output.TransferValueFromImport.Count() > 0)
            {
                //TransferValueFromImport is a list of guids which could reference a blank node
                foreach (var item in output.TransferValueFromImport)
                {
                    var tlo = ProfileServices.GetEntityAsTopLevelObject(item);
                    if (tlo != null && tlo.Id > 0)
                    {
                        output.TransferValueFrom.Add(tlo);
                    }
                    else
                    {
                        //log error
                    }
                }
                //get all object as
                output.TransferValueFromJson = JsonConvert.SerializeObject(output.TransferValueFrom, MappingHelperV3.GetJsonSettings());
            }

            importSuccessfull = new TransferValueServices().Import(output, ref status);
            //
            status.DocumentId    = output.Id;
            status.DetailPageUrl = string.Format("~/transfervalue/{0}", output.Id);
            status.DocumentRowId = output.RowId;

            //just in case
            if (status.HasErrors)
            {
                importSuccessfull = false;
            }

            //if record was added to db, add to/or set EntityResolution as resolved
            int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                       ctid, CodesManager.ENTITY_TYPE_TRANSFER_VALUE_PROFILE,
                                                                       output.RowId,
                                                                       output.Id,
                                                                       false,
                                                                       ref messages,
                                                                       output.Id > 0);

            //

            return(importSuccessfull);
        }
Esempio n. 7
0
        }         //

        public bool Import(string payload, string envelopeIdentifier, SaveStatus status)
        {
            List <string>  messages          = new List <string>();
            bool           importSuccessfull = false;
            EntityServices mgr = new EntityServices();
            //
            InputEntity input      = new InputEntity();
            var         bnodes     = new List <BNode>();
            var         mainEntity = new Dictionary <string, object>();
            //
            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    mainEntity = RegistryServices.JsonToDictionary(main);
                    input      = JsonConvert.DeserializeObject <InputEntity>(main);
                }
                else
                {
                    var bn = item.ToString();
                    //20-07-02 need to handle the enhanced bnodes
                    bnodes.Add(JsonConvert.DeserializeObject <BNode>(bn));
                }
            }

            ///============= process =============================
            MappingHelperV3 helper = new MappingHelperV3(3);

            helper.entityBlankNodes  = bnodes;
            helper.CurrentEntityCTID = input.CTID;
            helper.CurrentEntityName = input.Name.ToString();

            string ctid = input.CTID;

            status.Ctid = ctid;
            string referencedAtId = input.CtdlId;

            LoggingHelper.DoTrace(5, "		ctid: "+ ctid);
            LoggingHelper.DoTrace(5, "		@Id: "+ input.CtdlId);
            LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());

            if (status.DoingDownloadOnly)
            {
                return(true);
            }

            //add/updating DataSetProfile
            if (!DoesEntityExist(input.CTID, ref output, ref status))
            {
                //set the rowid now, so that can be referenced as needed
                output.RowId = Guid.NewGuid();
            }
            helper.currentBaseObject = output;

            output.Name        = helper.HandleLanguageMap(input.Name, output, "Name");
            output.Description = helper.HandleLanguageMap(input.Description, output, "Description");
            output.CTID        = input.CTID;
            //TBD handling of referencing third party publisher
            if (!string.IsNullOrWhiteSpace(status.DocumentPublishedBy))
            {
                //output.PublishedByOrganizationCTID = status.DocumentPublishedBy;
                var porg = OrganizationManager.GetSummaryByCtid(status.DocumentPublishedBy);
                if (porg != null && porg.Id > 0)
                {
                    //TODO - store this in a json blob??????????
                    //this will result in being added to Entity.AgentRelationship
                    output.PublishedBy = new List <Guid>()
                    {
                        porg.RowId
                    };
                }
                else
                {
                    //if publisher not imported yet, all publishee stuff will be orphaned
                    var entityUid = Guid.NewGuid();
                    var statusMsg = "";
                    var resPos    = referencedAtId.IndexOf("/resources/");
                    var swp       = referencedAtId.Substring(0, (resPos + "/resources/".Length)) + status.DocumentPublishedBy;
                    int orgId     = new OrganizationManager().AddPendingRecord(entityUid, status.DocumentPublishedBy, swp, ref statusMsg);
                }
            }
            else
            {
                //may need a check for existing published by to ensure not lost
                if (output.Id > 0)
                {
                    //TBD
                    //if ( output.DataProvider != null && output.DataProvider.Any() )
                    //{
                    //	var publishedByList = output.OrganizationRole.Where( s => s.RoleTypeId == 30 ).ToList();
                    //	if ( publishedByList != null && publishedByList.Any() )
                    //	{
                    //		var pby = publishedByList[ 0 ].ActingAgentUid;
                    //		output.PublishedBy = new List<Guid>() { publishedByList[ 0 ].ActingAgentUid };
                    //	}
                    //}
                }
            }
            output.Source = input.Source;
            //**** TBD DataProvider, or is the owner based on envelope?
            //output.OwnedBy = helper.MapOrganizationReferenceGuids( "DataSetProfile.OwnedBy", input.DataProvider, ref status );
            //if ( output.OwnedBy != null && output.OwnedBy.Count > 0 )
            //{
            //	output.OwningAgentUid = output.OwnedBy[ 0 ];
            //	helper.CurrentOwningAgentUid = output.OwnedBy[ 0 ];
            //}

            //



            importSuccessfull = new DataSetProfileServices().Import(output, ref status);
            //
            status.DocumentId    = output.Id;
            status.DetailPageUrl = string.Format("~/DataSetProfile/{0}", output.Id);
            status.DocumentRowId = output.RowId;

            //just in case
            if (status.HasErrors)
            {
                importSuccessfull = false;
            }

            //if record was added to db, add to/or set EntityResolution as resolved
            int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                       ctid, entityTypeId,
                                                                       output.RowId,
                                                                       output.Id,
                                                                       false,
                                                                       ref messages,
                                                                       output.Id > 0);

            //

            return(importSuccessfull);
        }
        } //

        public bool Import(string payload, string envelopeIdentifier, SaveStatus status)
        {
            LoggingHelper.DoTrace(7, "ImportCompetencyFramesworks - entered.");
            List <string>          messages          = new List <string>();
            MappingHelperV3        helper            = new MappingHelperV3(10);
            bool                   importSuccessfull = true;
            InputEntity            input             = new InputEntity();
            InputCompetency        comp         = new InputCompetency();
            var                    mainEntity   = new Dictionary <string, object>();
            List <InputCompetency> competencies = new List <InputCompetency>();

            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);
            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            var    bnodes    = new List <BNode>();
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                //note older frameworks will not be in the priority order
                var main = item.ToString();
                if (cntr == 1 || main.IndexOf("ceasn:CompetencyFramework") > -1)
                {
                    //HACK

                    if (main.IndexOf("ceasn:CompetencyFramework") > -1)
                    {
                        input = JsonConvert.DeserializeObject <InputEntity>(main);
                    }
                }
                else
                {
                    //Error converting value "https://credentialengineregistry.org/resources/ce-949fcaba-45ed-44d9-88bf-43677277eb84" to type 'System.Collections.Generic.List`1[System.String]'. Path 'ceasn:isPartOf', line 11, position 108.
                    //not set up to handle issues
                    //comp = JsonConvert.DeserializeObject<InputCompetency>( item.ToString() );
                    //competencies.Add( comp );

                    //should just have competencies, but should check for bnodes
                    var child = item.ToString();
                    if (child.IndexOf("_:") > -1)
                    {
                        bnodes.Add(JsonConvert.DeserializeObject <BNode>(child));
                        //ceasn:Competency
                    }
                    else if (child.IndexOf("ceasn:Competency") > -1)
                    {
                        competencies.Add(JsonConvert.DeserializeObject <InputCompetency>(child));
                    }
                    else
                    {
                        //unexpected
                    }
                }
            }

            //try
            //{
            //input = JsonConvert.DeserializeObject<InputGraph>( item.DecodedResource.ToString() );
            string ctid = input.CTID;

            status.Ctid = ctid;
            string referencedAtId = input.CtdlId;

            LoggingHelper.DoTrace(5, "		ctid: "+ ctid);
            LoggingHelper.DoTrace(5, "		@Id: "+ input.CtdlId);
            LoggingHelper.DoTrace(5, "		name: "+ input.name.ToString());

            string        framework = input.name.ToString();
            var           org       = new MC.Organization();
            string        orgCTID   = "";
            string        orgName   = "";
            List <string> publisher = input.publisher;

            //20-06-11 - need to get creator, publisher, owner where possible
            //	include an org reference with name, swp, and??
            //should check creator first? Or will publisher be more likely to have an account Ctid?
            if (publisher != null && publisher.Count() > 0)
            {
                orgCTID = ResolutionServices.ExtractCtid(publisher[0]);
                //look up org name
                org = OrganizationManager.GetSummaryByCtid(orgCTID);
            }
            else
            {
                //try creator
                List <string> creator = input.creator;
                if (creator != null && creator.Count() > 0)
                {
                    orgCTID = ResolutionServices.ExtractCtid(creator[0]);
                    //look up org name
                    org = OrganizationManager.GetSummaryByCtid(orgCTID);
                }
            }


            if (status.DoingDownloadOnly)
            {
                return(true);
            }

            //add/updating CompetencyFramework
            //21-02-22 HUH - WHY ARE WE USING ef here instead of output

            Framework ef = new Framework();

            if (!DoesEntityExist(input.CTID, ref ef))
            {
                //set the rowid now, so that can be referenced as needed
                //output.RowId = Guid.NewGuid();
                ef.RowId = Guid.NewGuid();
                LoggingHelper.DoTrace(1, string.Format(thisClassName + ".Import(). Record was NOT found using CTID: '{0}'", input.CTID));
            }
            else
            {
                LoggingHelper.DoTrace(1, string.Format(thisClassName + ".Import(). Found record: '{0}' using CTID: '{1}'", input.name, input.CTID));
            }

            helper.currentBaseObject = ef;
            ef.ExistsInRegistry      = true;

            //store graph
            ef.CompetencyFrameworkGraph = glist;
            ef.TotalCompetencies        = competencies.Count();

            ef.Name             = helper.HandleLanguageMap(input.name, ef, "Name");
            ef.Description      = helper.HandleLanguageMap(input.description, ef, "description");
            ef.CTID             = input.CTID;
            ef.OrganizationCTID = orgCTID;
            if (org != null && org.Id > 0)
            {
                orgName                      = org.Name;
                ef.OrganizationId            = org.Id;
                helper.CurrentOwningAgentUid = org.RowId;
            }

            helper.MapInLanguageToTextValueProfile(input.inLanguage, "CompetencyFramework.InLanguage.CTID: " + ctid);
            //foreach ( var l in input.InLanguage )
            //{
            //	if ( !string.IsNullOrWhiteSpace( l ) )
            //	{
            //		var language = CodesManager.GetLanguage( l );
            //		output.InLanguageCodeList.Add( new TextValueProfile()
            //		{
            //			CodeId = language.CodeId,
            //			TextTitle = language.Name,
            //			TextValue = language.Value
            //		} );
            //	}
            //}

            //TBD handling of referencing third party publisher
            if (!string.IsNullOrWhiteSpace(status.DocumentPublishedBy))
            {
                //output.PublishedByOrganizationCTID = status.DocumentPublishedBy;
                var porg = OrganizationManager.GetSummaryByCtid(status.DocumentPublishedBy);
                if (porg != null && porg.Id > 0)
                {
                    //TODO - store this in a json blob??????????
                    //this will result in being added to Entity.AgentRelationship
                    ef.PublishedBy = new List <Guid>()
                    {
                        porg.RowId
                    };
                }
                else
                {
                    //if publisher not imported yet, all publishee stuff will be orphaned
                    var entityUid = Guid.NewGuid();
                    var statusMsg = "";
                    var resPos    = referencedAtId.IndexOf("/resources/");
                    var swp       = referencedAtId.Substring(0, (resPos + "/resources/".Length)) + status.DocumentPublishedBy;
                    int orgId     = new OrganizationManager().AddPendingRecord(entityUid, status.DocumentPublishedBy, swp, ref statusMsg);
                }
            }
            else
            {
                //may need a check for existing published by to ensure not lost
                if (ef.Id > 0)
                {
                    //if ( ef.OrganizationRole != null && ef.OrganizationRole.Any() )
                    //{
                    //	var publishedByList = ef.OrganizationRole.Where( s => s.RoleTypeId == 30 ).ToList();
                    //	if ( publishedByList != null && publishedByList.Any() )
                    //	{
                    //		var pby = publishedByList[ 0 ].ActingAgentUid;
                    //		ef.PublishedBy = new List<Guid>() { publishedByList[ 0 ].ActingAgentUid };
                    //	}
                    //}
                }
            }
            ef.CredentialRegistryId = envelopeIdentifier;
            //additions
            //ef.ind
            //can only handle one source
            int pcnt = 0;

            if (input.source != null)
            {
                foreach (var url in input.source)
                {
                    pcnt++;
                    ef.SourceUrl = url;
                    break;
                }
            }
            ef.FrameworkUri = input.CtdlId;
            ef.hasTopChild  = input.hasTopChild;
            //
            ApiEntity apiFramework = new ApiEntity()
            {
                Name             = ef.Name,
                CTID             = ef.CTID,
                Source           = ef.SourceUrl,
                HasTopChild      = ef.hasTopChild,
                Meta_LastUpdated = status.LocalUpdatedDate
            };

            //?store competencies in string?
            if (competencies != null && competencies.Count > 0)
            {
                ef.TotalCompetencies = competencies.Count();
                //TODO - should we limit this if 1000+
                //do we use competencies in elastic? if not pause this usage
                cntr = 0;
                foreach (var c in competencies)
                {
                    cntr++;
                    var comments = helper.HandleLanguageMapList(c.comment, ef);
                    ef.Competencies.Add(
                        new workIT.Models.Elastic.IndexCompetency()
                    {
                        Name = c.competencyText.ToString(),
                        //CTID = c.CTID,
                        //Description = comments != null && comments.Count() > 0 ? comments[0].ToString()	 : ""
                    }
                        );
                    if (cntr >= 1000)
                    {
                        break;
                    }
                }
            }
            //20-07-02 just storing the index ready competencies
            //ef.CompentenciesJson = JsonConvert.SerializeObject( competencies, MappingHelperV3.GetJsonSettings() );
            ef.CompentenciesStore = JsonConvert.SerializeObject(ef.Competencies, MappingHelperV3.GetJsonSettings());

            //test
            //ElasticManager.LoadCompetencies( ef.Name, ef.CompentenciesStore );
            FormatCompetenciesHierarchy(apiFramework, competencies, helper);
            //TODO store whole framework or just the competencies?
            apiFramework.HasTopChild = null;
            ef.APIFramework          = JsonConvert.SerializeObject(apiFramework, MappingHelperV3.GetJsonSettings());

            //
            if (ef.TotalCompetencies == input.hasTopChild.Count())
            {
                //flat list - use for simple display
            }
            else
            {
                foreach (var item in input.hasTopChild)
                {
                }
            }

            //adding using common import pattern
            new CompetencyFrameworkServices().Import(ef, ref status);

            status.DocumentId    = ef.Id;
            status.DetailPageUrl = string.Format("~/competencyframework/{0}", ef.Id);
            status.DocumentRowId = ef.RowId;

            //
            //just in case
            if (status.HasErrors)
            {
                importSuccessfull = false;
            }

            //if record was added to db, add to/or set EntityResolution as resolved
            int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                       ctid,
                                                                       CodesManager.ENTITY_TYPE_COMPETENCY_FRAMEWORK,
                                                                       output.RowId,
                                                                       output.Id,
                                                                       (output.Id > 0),
                                                                       ref messages,
                                                                       output.Id > 0);

            //
            //framework checks
            if (input.inLanguage == null || input.inLanguage.Count() == 0)
            {
                //document for followup
                //LoggingHelper.DoTrace( 5, "		Framework missing inLanguage: " + input.name.ToString() );
            }
            //output.Name = helper.HandleLanguageMap( input.name, output, "Name" );
            //output.description = helper.HandleLanguageMap( input.description, output, "Description" );
            //output.CTID = input.CTID;

            return(importSuccessfull);
        }
        //
        public void FormatCompetenciesHierarchy(ApiEntity entity, List <InputCompetency> input, MappingHelperV3 helper)
        {
            /*
             * start with hasTopChild
             * loop through and fill out the hierarchy with embedded comps
             *
             *
             */

            var output = new List <ApiCompetency>();
            var ac     = new ApiCompetency();

            foreach (var item in entity.HasTopChild)
            {
                //get the competency
                var c = input.Where(s => s.CtdlId == item).FirstOrDefault();
                if (c != null && !string.IsNullOrWhiteSpace(c.CTID))
                {
                    ac = new ApiCompetency()
                    {
                        CompetencyText  = helper.HandleLanguageMap(c.competencyText, "CompetencyText"),
                        CTID            = c.CTID,
                        CompetencyLabel = helper.HandleLanguageMap(c.competencyLabel, "competencyLabel"),
                        HasChild        = null,
                        HasChildId      = null
                    };
                    if (c.hasChild != null && c.hasChild.Any())
                    {
                        ac.HasChild = new List <ApiCompetency>();
                        FormatCompetencyChildren(ac, c, input, helper);
                    }

                    output.Add(ac);
                }
                else
                {
                    //log error
                    LoggingHelper.DoTrace(1, string.Format("ImportCompetencyFramework. Framewwork: {0}, TopChild: {1} was not found in the list of competencies", entity.Name, item));
                }
            }

            entity.Meta_HasPart = output;
        }
        } //

        public bool Import(string payload, string envelopeIdentifier, SaveStatus status)
        {
            LoggingHelper.DoTrace(7, "ImportConceptSchemes - entered.");
            List <string>       messages          = new List <string>();
            MappingHelperV3     helper            = new MappingHelperV3(10);
            bool                importSuccessfull = true;
            var                 input             = new InputEntity();
            var                 concept           = new InputConcept();
            var                 mainEntity        = new Dictionary <string, object>();
            List <InputConcept> concepts          = new List <InputConcept>();

            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);
            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            var    bnodes    = new List <BNode>();
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                //note older frameworks will not be in the priority order
                var main = item.ToString();
                if (cntr == 1 || main.IndexOf("skos:ConceptScheme") > -1)
                {
                    //HACK

                    if (main.IndexOf("skos:ConceptScheme") > -1)
                    {
                        input = JsonConvert.DeserializeObject <InputEntity>(main);
                    }
                }
                else
                {
                    //should just have concepts, but should check for bnodes
                    var child = item.ToString();
                    if (child.IndexOf("_:") > -1)
                    {
                        bnodes.Add(JsonConvert.DeserializeObject <BNode>(child));
                    }
                    else if (child.IndexOf("skos:Concept") > -1)
                    {
                        concepts.Add(JsonConvert.DeserializeObject <InputConcept>(child));
                    }
                    else
                    {
                        //unexpected
                    }
                }
            }

            //try
            //{
            //input = JsonConvert.DeserializeObject<InputGraph>( item.DecodedResource.ToString() );
            string ctid = input.CTID;

            status.Ctid = ctid;
            string referencedAtId = input.CtdlId;

            LoggingHelper.DoTrace(5, "		ctid: "+ ctid);
            LoggingHelper.DoTrace(5, "		@Id: "+ input.CtdlId);
            LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());

            string framework = input.Name.ToString();
            var    org       = new MC.Organization();
            string orgCTID   = "";
            string orgName   = "";


            if (status.DoingDownloadOnly)
            {
                return(true);
            }

            //add/updating ConceptScheme
            //var output = new ConceptScheme();
            if (!DoesEntityExist(input.CTID, ref output))
            {
                //set the rowid now, so that can be referenced as needed
                output.RowId = Guid.NewGuid();
                //output.RowId = Guid.NewGuid();
            }
            helper.currentBaseObject = output;
            //

            output.Name                    = helper.HandleLanguageMap(input.Name, output, "Name");
            output.Description             = helper.HandleLanguageMap(input.Description, output, "description");
            output.CTID                    = input.CTID;
            output.PrimaryOrganizationCTID = orgCTID;
            //
            var publisher = input.Publisher;

            output.PublisherUid = helper.MapOrganizationReferenceGuid("ConceptScheme.Publisher", input.Publisher, ref status);
            output.Creator      = helper.MapOrganizationReferenceGuids("ConceptScheme.Creator", input.Creator, ref status);
            //20-06-11 - need to get creator, publisher, owner where possible
            //	include an org reference with name, swp, and??
            //should check creator first? Or will publisher be more likely to have an account Ctid?
            if (output.Creator != null && output.Creator.Count() > 0)
            {
                //get org or pending stub
                //look up org name
                org = OrganizationManager.Exists(output.Creator[0]);
                output.OwnedBy.Add(output.Creator[0]);
            }
            else
            {
                if (output.PublisherUid != Guid.Empty)
                {
                    //get org or pending stub
                    //look up org name
                    org = OrganizationManager.Exists(output.PublisherUid);
                    output.OwnedBy.Add(output.PublisherUid);
                }
            }
            //
            if (org != null && org.Id > 0)
            {
                orgName = org.Name;
                output.OrganizationId        = org.Id;
                helper.CurrentOwningAgentUid = org.RowId;
            }

            output.CredentialRegistryId = envelopeIdentifier;
            output.HasConcepts          = new List <MC.Concept>();
            //?store concepts in string?
            if (concepts != null && concepts.Count > 0)
            {
                output.TotalConcepts = concepts.Count();
                foreach (var item in concepts)
                {
                    var c = new MC.Concept()
                    {
                        PrefLabel  = helper.HandleLanguageMap(item.PrefLabel, output, "PrefLabel"),
                        Definition = helper.HandleLanguageMap(item.Definition, output, "Definition"),
                        Notes      = helper.HandleLanguageMapList(item.Note, output),
                        CTID       = item.CTID
                    };
                    if (c.Notes != null && c.Notes.Any())
                    {
                        c.Note = c.Notes[0];
                    }
                    output.HasConcepts.Add(c);
                }
            }
            //20-07-02 just storing the index ready concepts
            output.ConceptsStore = JsonConvert.SerializeObject(output.HasConcepts, MappingHelperV3.GetJsonSettings());

            //adding common import pattern

            new ConceptSchemeServices().Import(output, ref status);

            //

            return(importSuccessfull);
        }
        //public bool Import( InputEntity input, string envelopeIdentifier, SaveStatus status )
        //{
        //	List<string> messages = new List<string>();
        //	bool importSuccessfull = false;
        //          EntityServices mgr = new EntityServices();
        //          //try
        //          //{
        //          //input = JsonConvert.DeserializeObject<InputEntity>( item.DecodedResource.ToString() );
        //          string ctid = input.Ctid;
        //	string referencedAtId = input.CtdlId;
        //	LoggingHelper.DoTrace( 5, "		name: " + input.Name );
        //	LoggingHelper.DoTrace( 6, "		url: " + input.SubjectWebpage );
        //	LoggingHelper.DoTrace( 5, "		ctid: " + input.Ctid );
        //	LoggingHelper.DoTrace( 5, "		@Id: " + input.CtdlId );
        //          status.Ctid = ctid;

        //          if ( status.DoingDownloadOnly )
        //              return true;

        //          if ( !DoesEntityExist( input.Ctid, ref output ) )
        //	{
        //		output.RowId = Guid.NewGuid();
        //	}

        //	//re:messages - currently passed to mapping but no errors are trapped??
        //	//				- should use SaveStatus and skip import if errors encountered (vs warnings)

        //	output.Name = input.Name;
        //	output.Description = input.Description;
        //	output.CTID = input.Ctid;
        //	output.CredentialRegistryId = envelopeIdentifier;
        //	output.SubjectWebpage = input.SubjectWebpage;

        //	output.OwningAgentUid = MappingHelper.MapOrganizationReferencesGuid( input.ConditionManifestOf, ref status );

        //	output.Requires = MappingHelper.FormatConditionProfile( input.Requires, ref status );
        //	output.Recommends = MappingHelper.FormatConditionProfile( input.Recommends, ref status );
        //	output.EntryCondition = MappingHelper.FormatConditionProfile( input.EntryConditions, ref status );
        //	output.Corequisite = MappingHelper.FormatConditionProfile( input.Corequisite, ref status );
        //	output.Renewal = MappingHelper.FormatConditionProfile( input.Renewal, ref status );

        //	status.DocumentId = output.Id;
        //	status.DocumentRowId = output.RowId;

        //	//=== if any messages were encountered treat as warnings for now
        //	if ( messages.Count > 0 )
        //		status.SetMessages( messages, true );

        //	importSuccessfull = mgr.Import( output, ref status );
        //	//just in case
        //	if ( status.HasErrors )
        //		importSuccessfull = false;

        //	//if record was added to db, add to/or set EntityResolution as resolved
        //	int ierId = new ImportManager().Import_EntityResolutionAdd( referencedAtId,
        //				ctid,
        //				CodesManager.ENTITY_TYPE_CONDITION_MANIFEST,
        //				output.RowId,
        //				output.Id,
        //				false,
        //				ref messages,
        //				output.Id > 0 );

        //	return importSuccessfull;
        //}

        public bool ImportV3(string payload, string envelopeIdentifier, SaveStatus status)
        {
            InputEntityV3 input      = new InputEntityV3();
            var           bnodes     = new List <BNodeV3>();
            var           mainEntity = new Dictionary <string, object>();

            //status.AddWarning( "The resource uses @graph and is not handled yet" );

            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    mainEntity = RegistryServices.JsonToDictionary(main);
                    input      = JsonConvert.DeserializeObject <InputEntityV3>(main);
                }
                else
                {
                    var bn = item.ToString();
                    bnodes.Add(JsonConvert.DeserializeObject <BNodeV3>(bn));
                }
            }

            List <string>   messages          = new List <string>();
            bool            importSuccessfull = false;
            EntityServices  mgr    = new EntityServices();
            MappingHelperV3 helper = new MappingHelperV3(19);

            helper.entityBlankNodes  = bnodes;
            helper.CurrentEntityCTID = input.CTID;
            helper.CurrentEntityName = input.Name.ToString();

            //try
            //{
            //input = JsonConvert.DeserializeObject<InputEntity>( item.DecodedResource.ToString() );
            string ctid           = input.CTID;
            string referencedAtId = input.CtdlId;

            LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());
            LoggingHelper.DoTrace(6, "		url: "+ input.SubjectWebpage);
            LoggingHelper.DoTrace(5, "		ctid: "+ input.CTID);
            LoggingHelper.DoTrace(5, "		@Id: "+ input.CtdlId);
            status.Ctid = ctid;

            if (status.DoingDownloadOnly)
            {
                return(true);
            }

            try
            {
                if (!DoesEntityExist(input.CTID, ref output))
                {
                    output.RowId = Guid.NewGuid();
                    LoggingHelper.DoTrace(1, string.Format(thisClassName + ".ImportV3(). Record was NOT found using CTID: '{0}'", input.CTID));
                }
                else
                {
                    LoggingHelper.DoTrace(1, string.Format(thisClassName + ".ImportV3(). Found record: '{0}' using CTID: '{1}'", input.Name, input.CTID));
                }
                helper.currentBaseObject = output;
                //re:messages - currently passed to mapping but no errors are trapped??
                //				- should use SaveStatus and skip import if errors encountered (vs warnings)

                output.Name                 = helper.HandleLanguageMap(input.Name, output, "Name");
                output.Description          = helper.HandleLanguageMap(input.Description, output, "Description");
                output.CTID                 = input.CTID;
                output.CredentialRegistryId = envelopeIdentifier;
                output.SubjectWebpage       = input.SubjectWebpage;

                output.OwningAgentUid        = helper.MapOrganizationReferencesGuid("ConditionManifest.OwningAgentUid", input.ConditionManifestOf, ref status);
                helper.CurrentOwningAgentUid = output.OwningAgentUid;

                output.Requires       = helper.FormatConditionProfile(input.Requires, ref status);
                output.Recommends     = helper.FormatConditionProfile(input.Recommends, ref status);
                output.EntryCondition = helper.FormatConditionProfile(input.EntryConditions, ref status);
                output.Corequisite    = helper.FormatConditionProfile(input.Corequisite, ref status);
                output.Renewal        = helper.FormatConditionProfile(input.Renewal, ref status);

                status.DocumentId    = output.Id;
                status.DocumentRowId = output.RowId;

                //=== if any messages were encountered treat as warnings for now
                if (messages.Count > 0)
                {
                    status.SetMessages(messages, true);
                }

                importSuccessfull = mgr.Import(output, ref status);
                //just in case
                if (status.HasErrors)
                {
                    importSuccessfull = false;
                }

                //if record was added to db, add to/or set EntityResolution as resolved
                int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                           ctid,
                                                                           CodesManager.ENTITY_TYPE_CONDITION_MANIFEST,
                                                                           output.RowId,
                                                                           output.Id,
                                                                           false,
                                                                           ref messages,
                                                                           output.Id > 0);
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, string.Format("CostManifest ImportV3. Exception encountered for CTID: {0}", ctid), false, "CostManifest Import exception");
            }
            finally
            {
            }
            return(importSuccessfull);
        }
        public bool ImportV3(string payload, string envelopeIdentifier, SaveStatus status)
        {
            InputEntityV3 input      = new InputEntityV3();
            var           bnodes     = new List <BNodeV3>();
            var           mainEntity = new Dictionary <string, object>();

            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    mainEntity = RegistryServices.JsonToDictionary(main);
                    input      = JsonConvert.DeserializeObject <InputEntityV3>(main);
                }
                else //is this too much of an assumption?
                {
                    var bn = item.ToString();
                    bnodes.Add(JsonConvert.DeserializeObject <BNodeV3>(bn));
                }
            }

            List <string>   messages          = new List <string>();
            bool            importSuccessfull = false;
            EntityServices  mgr    = new EntityServices();
            MappingHelperV3 helper = new MappingHelperV3
            {
                entityBlankNodes = bnodes
            };

            string ctid           = input.Ctid;
            string referencedAtId = input.CtdlId;

            LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());
            LoggingHelper.DoTrace(6, "		url: "+ input.SubjectWebpage);
            LoggingHelper.DoTrace(5, "		ctid: "+ input.Ctid);
            LoggingHelper.DoTrace(5, "		@Id: "+ input.CtdlId);
            status.Ctid = ctid;

            if (status.DoingDownloadOnly)
            {
                return(true);
            }

            if (!DoesEntityExist(input.Ctid, ref output))
            {
                //set the rowid now, so that can be referenced as needed
                output.RowId = Guid.NewGuid();
            }

            helper.currentBaseObject = output;

            output.AgentDomainType = input.Type;
            output.Name            = helper.HandleLanguageMap(input.Name, output, "Name");
            output.Description     = helper.HandleLanguageMap(input.Description, output, "Description");
            //map from idProperty to url
            output.SubjectWebpage       = input.SubjectWebpage;
            output.CTID                 = input.Ctid;
            output.CredentialRegistryId = envelopeIdentifier;
            output.AlternateNames       = helper.MapToTextValueProfile(input.AlternateName, output, "AlternateName");
            output.ImageUrl             = input.Image;

            output.AgentPurpose            = input.AgentPurpose;
            output.AgentPurposeDescription = helper.HandleLanguageMap(input.AgentPurposeDescription, output, "AgentPurposeDescription");

            output.FoundingDate        = input.FoundingDate;
            output.AvailabilityListing = helper.MapListToString(input.AvailabilityListing);
            //future prep
            output.AvailabilityListings = input.AvailabilityListing;

            output.MissionAndGoalsStatement = input.MissionAndGoalsStatement;
            //output.MissionAndGoalsStatementDescription = input.MissionAndGoalsStatementDescription;
            output.MissionAndGoalsStatementDescription = helper.HandleLanguageMap(input.MissionAndGoalsStatementDescription, output, "MissionAndGoalsStatementDescription");

            output.Addresses = helper.FormatAvailableAtAddresses(input.Address, ref status);
            if (UtilityManager.GetAppKeyValue("skipOppImportIfNoShortRegions", false))
            {
                if (output.Addresses.Count == 0)
                {
                    //skip
                    LoggingHelper.DoTrace(2, string.Format("		***Skipping org# {0}, {1} as it has no addresses and this is a special run.", output.Id, output.Name));
                    return(true);
                }
                else if (output.HasAnyShortRegions == false)
                {
                    //skip
                    LoggingHelper.DoTrace(2, string.Format("		***Skipping org# {0}, {1} as it has no addresses with short regions and this is a special run.", output.Id, output.Name));
                    return(true);
                }
            }

            //agent type, map to enumeration
            output.AgentType = helper.MapCAOListToEnumermation(input.AgentType);


            //Manifests
            output.ConditionManifestIds = helper.MapEntityReferences(input.HasConditionManifest, CodesManager.ENTITY_TYPE_CONDITION_MANIFEST, CodesManager.ENTITY_TYPE_ORGANIZATION, ref status);
            output.CostManifestIds      = helper.MapEntityReferences(input.HasCostManifest, CodesManager.ENTITY_TYPE_COST_MANIFEST, CodesManager.ENTITY_TYPE_ORGANIZATION, ref status);

            //hasVerificationService
            output.VerificationServiceProfiles = helper.MapVerificationServiceProfiles(input.VerificationServiceProfiles, ref status);

            // output.targetc
            //other enumerations
            //	serviceType, AgentSectorType
            output.ServiceType     = helper.MapCAOListToEnumermation(input.ServiceType);
            output.AgentSectorType = helper.MapCAOListToEnumermation(input.AgentSectorType);

            //Industries
            //output.Industry = helper.MapCAOListToEnumermation( input.IndustryType );
            output.Industries = helper.MapCAOListToCAOProfileList(input.IndustryType);
            //naics
            output.Naics = input.Naics;

            //keywords
            output.Keyword = helper.MapToTextValueProfile(input.Keyword, output, "Keyword");

            //duns, Fein.  IpedsID, opeID
            if (!string.IsNullOrWhiteSpace(input.DUNS))
            {
                output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                    CodeSchema = "ceterms:duns", TextValue = input.DUNS
                });
            }
            if (!string.IsNullOrWhiteSpace(input.FEIN))
            {
                output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                    CodeSchema = "ceterms:fein", TextValue = input.FEIN
                });
            }

            if (!string.IsNullOrWhiteSpace(input.IpedsID))
            {
                output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                    CodeSchema = "ceterms:ipedsID", TextValue = input.IpedsID
                });
            }

            if (!string.IsNullOrWhiteSpace(input.OPEID))
            {
                output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                    CodeSchema = "ceterms:opeID", TextValue = input.OPEID
                });
            }
            if (!string.IsNullOrWhiteSpace(input.LEICode))
            {
                output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                    CodeSchema = "ceterms:leiCode", TextValue = input.LEICode
                });
            }
            //
            if (!string.IsNullOrWhiteSpace(input.NcesID))
            {
                output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                    CodeSchema = "ceterms:ncesID", TextValue = input.NcesID
                });
            }
            //
            if (!string.IsNullOrWhiteSpace(input.ISICV4))
            {
                output.IdentificationCodes.Add(new workIT.Models.ProfileModels.TextValueProfile {
                    CodeSchema = "ceterms:isicV4", TextValue = input.ISICV4
                });
            }
            //alternativeidentifier - should just be added to IdentificationCodes
            output.AlternativeIdentifier     = helper.MapIdentifierValueListToString(input.AlternativeIdentifier);
            output.AlternativeIdentifierList = helper.MapIdentifierValueList(input.AlternativeIdentifier);

            //email

            output.Emails = helper.MapToTextValueProfile(input.Email);
            //contact point
            //output.ContactPoint = helper.FormatContactPoints( input.ContactPoint, ref status );
            //Jurisdiction
            output.Jurisdiction = helper.MapToJurisdiction(input.Jurisdiction, ref status);

            //SameAs
            output.SameAs = helper.MapToTextValueProfile(input.SameAs);
            //Social media
            output.SocialMediaPages = helper.MapToTextValueProfile(input.SocialMedia);

            //departments
            //not sure - MP - want to change how depts, and subs are handled
            output.ParentOrganization = helper.MapOrganizationReferenceGuids(input.ParentOrganization, ref status);
            output.Departments        = helper.MapOrganizationReferenceGuids(input.Department, ref status);
            output.SubOrganizations   = helper.MapOrganizationReferenceGuids(input.SubOrganization, ref status);

            //output.OrganizationRole_Subsidiary = helper.FormatOrganizationReferences( input.SubOrganization );

            //Process profiles
            output.AdministrationProcess = helper.FormatProcessProfile(input.AdministrationProcess, ref status);
            output.MaintenanceProcess    = helper.FormatProcessProfile(input.MaintenanceProcess, ref status);
            output.ComplaintProcess      = helper.FormatProcessProfile(input.ComplaintProcess, ref status);
            output.DevelopmentProcess    = helper.FormatProcessProfile(input.DevelopmentProcess, ref status);
            output.RevocationProcess     = helper.FormatProcessProfile(input.RevocationProcess, ref status);
            output.ReviewProcess         = helper.FormatProcessProfile(input.ReviewProcess, ref status);
            output.AppealProcess         = helper.FormatProcessProfile(input.AppealProcess, ref status);

            //BYs
            output.AccreditedBy = helper.MapOrganizationReferenceGuids("Organization.AccreditedBy", input.AccreditedBy, ref status);
            output.ApprovedBy   = helper.MapOrganizationReferenceGuids("Organization.ApprovedBy", input.ApprovedBy, ref status);
            output.RecognizedBy = helper.MapOrganizationReferenceGuids("Organization.RecognizedBy", input.RecognizedBy, ref status);
            output.RegulatedBy  = helper.MapOrganizationReferenceGuids("Organization.RegulatedBy", input.RegulatedBy, ref status);
            //INs
            output.AccreditedIn = helper.MapToJurisdiction(input.AccreditedIn, ref status);
            output.ApprovedIn   = helper.MapToJurisdiction(input.ApprovedIn, ref status);
            output.RecognizedIn = helper.MapToJurisdiction(input.RecognizedIn, ref status);
            output.RegulatedIn  = helper.MapToJurisdiction(input.RegulatedIn, ref status);

            //Asserts
            //the entity type is not known
            output.Accredits = helper.MapEntityReferenceGuids(input.Accredits, 0, ref status);
            output.Approves  = helper.MapEntityReferenceGuids(input.Approves, 0, ref status);
            if (output.Approves.Count > 0)
            {
            }
            output.Offers     = helper.MapEntityReferenceGuids(input.Offers, 0, ref status);
            output.Owns       = helper.MapEntityReferenceGuids(input.Owns, 0, ref status);
            output.Renews     = helper.MapEntityReferenceGuids(input.Renews, 0, ref status);
            output.Revokes    = helper.MapEntityReferenceGuids(input.Revokes, 0, ref status);
            output.Recognizes = helper.MapEntityReferenceGuids(input.Recognizes, 0, ref status);
            output.Regulates  = helper.MapEntityReferenceGuids(input.Regulates, 0, ref status);

            //Ins - defer to later



            //=== if any messages were encountered treat as warnings for now
            if (messages.Count > 0)
            {
                status.SetMessages(messages, true);
            }
            //just in case check if entity added since start
            if (output.Id == 0)
            {
                ThisEntity entity = EntityServices.GetByCtid(ctid);
                if (entity != null && entity.Id > 0)
                {
                    output.Id    = entity.Id;
                    output.RowId = entity.RowId;
                }
            }
            importSuccessfull = mgr.Import(output, ref status);

            status.DocumentId    = output.Id;
            status.DetailPageUrl = string.Format("~/organization/{0}", output.Id);
            status.DocumentRowId = output.RowId;

            //just in case
            if (status.HasErrors)
            {
                importSuccessfull = false;
            }

            //if record was added to db, add to/or set EntityResolution as resolved
            int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                       ctid, CodesManager.ENTITY_TYPE_ORGANIZATION,
                                                                       output.RowId,
                                                                       output.Id,
                                                                       false,
                                                                       ref messages,
                                                                       output.Id > 0);

            return(importSuccessfull);
        }
Esempio n. 13
0
        }         //

        public bool Import(string payload, string envelopeIdentifier, SaveStatus status)
        {
            LoggingHelper.DoTrace(6, "ImportPathwaySets - entered.");
            List <string>  messages          = new List <string>();
            bool           importSuccessfull = false;
            EntityServices mgr = new EntityServices();
            //
            InputEntity input      = new InputEntity();
            var         mainEntity = new Dictionary <string, object>();
            //
            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            var    bnodes    = new List <BNode>();
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    mainEntity = RegistryServices.JsonToDictionary(main);
                    input      = JsonConvert.DeserializeObject <InputEntity>(main);
                }
                else
                {
                    //may have blank nodes?
                    var bn = item.ToString();
                    bnodes.Add(JsonConvert.DeserializeObject <BNode>(bn));
                    var child = item.ToString();
                    if (child.IndexOf("_:") > -1)
                    {
                        bnodes.Add(JsonConvert.DeserializeObject <BNode>(child));
                    }
                    else
                    {
                        //unexpected
                        Dictionary <string, object> unexpected = RegistryServices.JsonToDictionary(child);
                        object unexpectedType = unexpected["@type"];
                        status.AddError("Unexpected document type");
                    }
                }
            }

            MappingHelperV3 helper = new MappingHelperV3(CodesManager.ENTITY_TYPE_PATHWAY_SET);

            helper.entityBlankNodes  = bnodes;
            helper.CurrentEntityCTID = input.CTID;
            helper.CurrentEntityName = input.Name.ToString();

            status.EnvelopeId = envelopeIdentifier;
            try
            {
                string ctid           = input.CTID;
                string referencedAtId = input.CtdlId;

                LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());
                LoggingHelper.DoTrace(6, "		url: "+ input.SubjectWebpage);
                LoggingHelper.DoTrace(5, "		ctid: "+ input.CTID);
                LoggingHelper.DoTrace(6, "		@Id: "+ input.CtdlId);
                status.Ctid = ctid;

                if (status.DoingDownloadOnly)
                {
                    return(true);
                }


                //add/updating PathwaySet
                if (!DoesEntityExist(input.CTID, ref output))
                {
                    //set the rowid now, so that can be referenced as needed
                    output.RowId = Guid.NewGuid();
                }
                helper.currentBaseObject = output;

                output.Name           = helper.HandleLanguageMap(input.Name, output, "Name");
                output.Description    = helper.HandleLanguageMap(input.Description, output, "Description");
                output.SubjectWebpage = input.SubjectWebpage;
                output.CTID           = input.CTID;
                //TBD handling of referencing third party publisher
                if (!string.IsNullOrWhiteSpace(status.DocumentPublishedBy))
                {
                    //output.PublishedByOrganizationCTID = status.DocumentPublishedBy;
                    var porg = OrganizationManager.GetSummaryByCtid(status.DocumentPublishedBy);
                    if (porg != null && porg.Id > 0)
                    {
                        //TODO - store this in a json blob??????????
                        //this will result in being added to Entity.AgentRelationship
                        output.PublishedBy = new List <Guid>()
                        {
                            porg.RowId
                        };
                    }
                    else
                    {
                        //if publisher not imported yet, all publishee stuff will be orphaned
                        var entityUid = Guid.NewGuid();
                        var statusMsg = "";
                        var resPos    = referencedAtId.IndexOf("/resources/");
                        var swp       = referencedAtId.Substring(0, (resPos + "/resources/".Length)) + status.DocumentPublishedBy;
                        int orgId     = new OrganizationManager().AddPendingRecord(entityUid, status.DocumentPublishedBy, swp, ref statusMsg);
                    }
                }
                else
                {
                    //may need a check for existing published by to ensure not lost
                    if (output.Id > 0)
                    {
                        if (output.OrganizationRole != null && output.OrganizationRole.Any())
                        {
                            var publishedByList = output.OrganizationRole.Where(s => s.RoleTypeId == 30).ToList();
                            if (publishedByList != null && publishedByList.Any())
                            {
                                var pby = publishedByList[0].ActingAgentUid;
                                output.PublishedBy = new List <Guid>()
                                {
                                    publishedByList[0].ActingAgentUid
                                };
                            }
                        }
                    }
                }
                //warning this gets set to blank if doing a manual import by ctid
                output.CredentialRegistryId = envelopeIdentifier;

                //BYs - do owned and offered first
                output.OfferedBy = helper.MapOrganizationReferenceGuids("PathwaySet.OfferedBy", input.OfferedBy, ref status);
                //note need to set output.OwningAgentUid to the first entry
                output.OwnedBy = helper.MapOrganizationReferenceGuids("PathwaySet.OwnedBy", input.OwnedBy, ref status);
                if (output.OwnedBy != null && output.OwnedBy.Count > 0)
                {
                    output.OwningAgentUid        = output.OwnedBy[0];
                    helper.CurrentOwningAgentUid = output.OwnedBy[0];
                }
                else
                {
                    //add warning?
                    if (output.OfferedBy == null && output.OfferedBy.Count == 0)
                    {
                        status.AddWarning("document doesn't have an owning or offering organization.");
                    }
                }
                //HasPathway is required, so must have data
                output.HasPathwayList = helper.MapEntityReferenceGuids("PathwaySet.HasPathway", input.HasPathway, thisEntityTypeId, ref status);
                //
                //need to check if pathways have been imported. Normally would have as pathway is published before the set



                //=== if any messages were encountered treat as warnings for now
                if (messages.Count > 0)
                {
                    status.SetMessages(messages, true);
                }

                //adding common import pattern
                importSuccessfull    = mgr.PathwaySetImport(output, ref status);
                status.DocumentId    = output.Id;
                status.DetailPageUrl = string.Format("~/pathwayset/{0}", output.Id);
                status.DocumentRowId = output.RowId;
                //if record was added to db, add to/or set EntityResolution as resolved
                int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                           ctid,
                                                                           CodesManager.ENTITY_TYPE_PATHWAY_SET,
                                                                           output.RowId,
                                                                           output.Id,
                                                                           (output.Id > 0),
                                                                           ref messages,
                                                                           output.Id > 0);
                //just in case - not sure if applicable, as will want to do components if the pathway exists
                if (status.HasErrors)
                {
                    importSuccessfull = false;
                }
                //
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, string.Format("Exception encountered in envelopeId: {0}", envelopeIdentifier), false, "PathwaySet Import exception");
            }

            return(importSuccessfull);
        }
        } //

        public bool Import(EntityServices mgr, string payload, string envelopeIdentifier, SaveStatus status)
        {
            List <string>          messages          = new List <string>();
            MappingHelperV3        helper            = new MappingHelperV3();
            bool                   importSuccessfull = true;
            InputEntity            input             = new InputEntity();
            InputCompetency        comp         = new InputCompetency();
            var                    mainEntity   = new Dictionary <string, object>();
            List <InputCompetency> competencies = new List <InputCompetency>();

            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);
            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            var    bnodes    = new List <BNode>();
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                //note older frameworks will not be in the priority order
                var main = item.ToString();
                if (cntr == 1 || main.IndexOf("ceasn:CompetencyFramework") > -1)
                {
                    //HACK

                    if (main.IndexOf("ceasn:CompetencyFramework") > -1)
                    {
                        input = JsonConvert.DeserializeObject <InputEntity>(main);
                    }
                }
                else
                {
                    //Error converting value "https://credentialengineregistry.org/resources/ce-949fcaba-45ed-44d9-88bf-43677277eb84" to type 'System.Collections.Generic.List`1[System.String]'. Path 'ceasn:isPartOf', line 11, position 108.
                    //not set up to handle issues
                    //comp = JsonConvert.DeserializeObject<InputCompetency>( item.ToString() );
                    //competencies.Add( comp );

                    //should just have competencies, but should check for bnodes
                    var child = item.ToString();
                    if (child.IndexOf("_:") > -1)
                    {
                        bnodes.Add(JsonConvert.DeserializeObject <BNode>(child));
                        //ceasn:Competency
                    }
                    else if (child.IndexOf("ceasn:Competency") > -1)
                    {
                        competencies.Add(JsonConvert.DeserializeObject <InputCompetency>(child));
                    }
                    else
                    {
                        //unexpected
                    }
                }
            }

            //try
            //{
            //input = JsonConvert.DeserializeObject<InputGraph>( item.DecodedResource.ToString() );
            string ctid = input.CTID;

            status.Ctid = ctid;
            string referencedAtId = input.CtdlId;

            LoggingHelper.DoTrace(5, "		ctid: "+ ctid);
            LoggingHelper.DoTrace(5, "		@Id: "+ input.CtdlId);
            LoggingHelper.DoTrace(5, "		name: "+ input.name.ToString());

            string        framework = input.name.ToString();
            string        orgCTID   = "";
            string        orgName   = "";
            List <string> publisher = input.publisher;

            //should check creator first? Or will publisher be more likely to have an account Ctid?
            if (publisher != null && publisher.Count() > 0)
            {
                orgCTID = ResolutionServices.ExtractCtid(publisher[0]);
                //look up org name
                orgName = OrganizationManager.GetByCtid(orgCTID).Name ?? "missing";
            }
            else
            {
                //try creator
                List <string> creator = input.creator;
                if (creator != null && creator.Count() > 0)
                {
                    orgCTID = ResolutionServices.ExtractCtid(creator[0]);
                    //look up org name
                    orgName = OrganizationManager.GetByCtid(orgCTID).Name ?? "missing";
                }
            }

            if (status.DoingDownloadOnly)
            {
                return(true);
            }

            //add updating educationFramework
            Framework ef = new Framework();

            if (!DoesEntityExist(input.CTID, ref ef))
            {
                //set the rowid now, so that can be referenced as needed
                output.RowId = Guid.NewGuid();
                ef.RowId     = Guid.NewGuid();
            }
            helper.currentBaseObject = ef;
            ef.ExistsInRegistry      = true;

            ef.FrameworkName        = helper.HandleLanguageMap(input.name, ef, "Name");
            ef.CTID                 = input.CTID;
            ef.OrganizationCTID     = orgCTID;
            ef.CredentialRegistryId = envelopeIdentifier;
            //can only handle one source
            int pcnt = 0;

            foreach (var url in input.source)
            {
                pcnt++;
                ef.SourceUrl = url;
                break;
            }
            ef.FrameworkUri = input.CtdlId;
            new EducationFrameworkManager().Save(ef, ref status, true);
            //

            //
            //framework checks
            if (input.inLanguage == null || input.inLanguage.Count() == 0)
            {
                //document for followup
                //LoggingHelper.DoTrace( 5, "		Framework missing inLanguage: " + input.name.ToString() );
            }
            //output.Name = helper.HandleLanguageMap( input.name, output, "Name" );
            //output.description = helper.HandleLanguageMap( input.description, output, "Description" );
            output.Ctid = input.CTID;

            return(importSuccessfull);
        }
Esempio n. 15
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 FormatCompetencyChildren(ApiCompetency competency, InputCompetency input, List <InputCompetency> allCompetencies, MappingHelperV3 helper)
        {
            foreach (var item in input.hasChild)
            {
                //get the competency
                var c = allCompetencies.Where(s => s.CtdlId == item).FirstOrDefault();
                if (c != null && !string.IsNullOrWhiteSpace(c.CTID))
                {
                    var ac = new ApiCompetency()
                    {
                        CompetencyText  = helper.HandleLanguageMap(c.competencyText, "CompetencyText"),
                        CTID            = c.CTID,
                        CompetencyLabel = helper.HandleLanguageMap(c.competencyLabel, "competencyLabel"),
                        HasChild        = null,
                        HasChildId      = null
                    };
                    if (c.hasChild != null && c.hasChild.Any())
                    {
                        ac.HasChild = new List <ApiCompetency>();
                        FormatCompetencyChildren(ac, c, allCompetencies, helper);
                    }

                    competency.HasChild.Add(ac);
                }
                else
                {
                    //log error
                    LoggingHelper.DoTrace(1, string.Format("ImportCompetencyFramework.FormatCompetencyChildren() CompetencyCTID: {0}, child: {1} was not found in the list of competencies", competency.CTID, item));
                }
            }
        }
        public bool ImportV3(string payload, string envelopeIdentifier, SaveStatus status)
        {
            LoggingHelper.DoTrace(6, "ImportV3 Assessment- entered.");
            List <string> messages = new List <string>();

            bool           importSuccessfull = false;
            EntityServices mgr        = new EntityServices();
            InputEntityV3  input      = new InputEntityV3();
            var            bnodes     = new List <BNode>();
            var            mainEntity = new Dictionary <string, object>();

            //status.AddWarning( "The resource uses @graph and is not handled yet" );

            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    mainEntity = RegistryServices.JsonToDictionary(main);
                    input      = JsonConvert.DeserializeObject <InputEntityV3>(main);
                }
                else
                {
                    var bn = item.ToString();
                    bnodes.Add(JsonConvert.DeserializeObject <BNode>(bn));
                }
            }
            ///============= process =============================


            MappingHelperV3 helper = new MappingHelperV3(3);

            helper.entityBlankNodes  = bnodes;
            helper.CurrentEntityCTID = input.CTID;
            helper.CurrentEntityName = input.Name.ToString();

            string ctid = input.CTID;

            status.Ctid = ctid;
            string referencedAtId = input.CtdlId;

            LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());
            LoggingHelper.DoTrace(6, "		url: "+ input.SubjectWebpage);
            LoggingHelper.DoTrace(5, "		ctid: "+ input.CTID);
            LoggingHelper.DoTrace(5, "		@Id: "+ input.CtdlId);


            if (status.DoingDownloadOnly)
            {
                return(true);
            }

            if (!DoesEntityExist(input.CTID, ref output))
            {
                //set the rowid now, so that can be referenced as needed
                output.RowId = Guid.NewGuid();
                LoggingHelper.DoTrace(1, string.Format(thisClassName + ".ImportV3(). Record was NOT found using CTID: '{0}'", input.CTID));
            }
            else
            {
                LoggingHelper.DoTrace(1, string.Format(thisClassName + ".ImportV3(). Found record: '{0}' using CTID: '{1}'", input.Name, input.CTID));
            }
            helper.currentBaseObject = output;

            //start with language and may use with language maps
            helper.MapInLanguageToTextValueProfile(input.InLanguage, "Assessment.InLanguage.CTID: " + ctid);
            //foreach ( var l in input.InLanguage )
            //{
            //	if ( !string.IsNullOrWhiteSpace( l ) )
            //	{
            //		var language = CodesManager.GetLanguage( l );
            //		output.InLanguageCodeList.Add( new TextValueProfile()
            //		{
            //			CodeId = language.CodeId,
            //			TextTitle = language.Name,
            //			TextValue = language.Value
            //		} );
            //	}
            //}

            if (input.InLanguage.Count > 0)
            {
                //could use to alter helper.DefaultLanguage
            }
            output.Name        = helper.HandleLanguageMap(input.Name, output, "Name");
            output.Description = helper.HandleLanguageMap(input.Description, output, "Description");
            output.CTID        = input.CTID;
            //TBD handling of referencing third party publisher
            if (!string.IsNullOrWhiteSpace(status.DocumentPublishedBy))
            {
                //output.PublishedByOrganizationCTID = status.DocumentPublishedBy;
                var porg = OrganizationManager.GetSummaryByCtid(status.DocumentPublishedBy);
                if (porg != null && porg.Id > 0)
                {
                    //TODO - store this in a json blob??????????
                    //this will result in being added to Entity.AgentRelationship
                    output.PublishedBy = new List <Guid>()
                    {
                        porg.RowId
                    };
                }
                else
                {
                    //if publisher not imported yet, all publishee stuff will be orphaned
                    var entityUid = Guid.NewGuid();
                    var statusMsg = "";
                    var resPos    = referencedAtId.IndexOf("/resources/");
                    var swp       = referencedAtId.Substring(0, (resPos + "/resources/".Length)) + status.DocumentPublishedBy;
                    int orgId     = new OrganizationManager().AddPendingRecord(entityUid, status.DocumentPublishedBy, swp, ref statusMsg);
                }
            }
            else
            {
                //may need a check for existing published by to ensure not lost
                if (output.Id > 0)
                {
                    if (output.OrganizationRole != null && output.OrganizationRole.Any())
                    {
                        var publishedByList = output.OrganizationRole.Where(s => s.RoleTypeId == 30).ToList();
                        if (publishedByList != null && publishedByList.Any())
                        {
                            var pby = publishedByList[0].ActingAgentUid;
                            output.PublishedBy = new List <Guid>()
                            {
                                publishedByList[0].ActingAgentUid
                            };
                        }
                    }
                }
            }
            output.CredentialRegistryId = envelopeIdentifier;
            output.DateEffective        = input.DateEffective;
            output.ExpirationDate       = input.ExpirationDate;

            output.SubjectWebpage = input.SubjectWebpage;
            //
            //BYs - do owned and offered first

            output.OfferedBy = helper.MapOrganizationReferenceGuids("Assessment.OfferedBy", input.OfferedBy, ref status);
            output.OwnedBy   = helper.MapOrganizationReferenceGuids("Assessment.OwnedBy", input.OwnedBy, ref status);
            if (output.OwnedBy != null && output.OwnedBy.Count > 0)
            {
                output.OwningAgentUid        = output.OwnedBy[0];
                helper.CurrentOwningAgentUid = output.OwnedBy[0];
            }
            else
            {
                //add warning?
                if (output.OfferedBy == null && output.OfferedBy.Count == 0)
                {
                    status.AddWarning("document doesn't have an owning or offering organization.");
                }
            }
            output.AccreditedBy = helper.MapOrganizationReferenceGuids("Assessment.AccreditedBy", input.AccreditedBy, ref status);
            output.ApprovedBy   = helper.MapOrganizationReferenceGuids("Assessment.ApprovedBy", input.ApprovedBy, ref status);
            output.RecognizedBy = helper.MapOrganizationReferenceGuids("Assessment.RecognizedBy", input.RecognizedBy, ref status);
            output.RegulatedBy  = helper.MapOrganizationReferenceGuids("Assessment.RegulatedBy", input.RegulatedBy, ref status);


            //
            output.Subject             = helper.MapCAOListToTextValueProfile(input.Subject, CodesManager.PROPERTY_CATEGORY_SUBJECT);
            output.Keyword             = helper.MapToTextValueProfile(input.Keyword, output, "Keyword");
            output.AvailabilityListing = helper.MapListToString(input.AvailabilityListing);
            output.AvailableOnlineAt   = helper.MapListToString(input.AvailableOnlineAt);
            output.AssessmentExample   = input.AssessmentExample;
            output.ExternalResearch    = helper.MapListToString(input.ExternalResearch);

            output.AssessmentExampleDescription = helper.HandleLanguageMap(input.AssessmentExampleDescription, output, "AssessmentExampleDescription");
            output.AssessmentMethodType         = helper.MapCAOListToEnumermation(input.AssessmentMethodType);
            output.AssessmentMethodDescription  = helper.HandleLanguageMap(input.AssessmentMethodDescription, output, "AssessmentMethodDescription");
            //
            output.LearningMethodDescription = helper.HandleLanguageMap(input.LearningMethodDescription, output, "LearningMethodDescription");


            output.AudienceType = helper.MapCAOListToEnumermation(input.AudienceType);
            //CAO
            output.AudienceLevelType = helper.MapCAOListToEnumermation(input.AudienceLevelType);

            output.VersionIdentifier     = helper.MapIdentifierValueListToString(input.VersionIdentifier);
            output.VersionIdentifierList = helper.MapIdentifierValueList(input.VersionIdentifier);

            //To be looked
            output.CodedNotation = input.CodedNotation;
            output.Identifier    = helper.MapIdentifierValueList(input.Identifier);
            if (output.Identifier != null && output.Identifier.Count() > 0)
            {
                output.IdentifierJson = JsonConvert.SerializeObject(output.Identifier, MappingHelperV3.GetJsonSettings());
            }

            output.AssessmentOutput        = helper.HandleLanguageMap(input.AssessmentOutput, output, "AssessmentOutput");
            output.AssessmentUseType       = helper.MapCAOListToEnumermation(input.AssessmentUseType);
            output.DeliveryType            = helper.MapCAOListToEnumermation(input.DeliveryType);
            output.DeliveryTypeDescription = helper.HandleLanguageMap(input.DeliveryTypeDescription, output, "DeliveryTypeDescription");

            output.IsProctored           = input.IsProctored;
            output.HasGroupEvaluation    = input.HasGroupEvaluation;
            output.HasGroupParticipation = input.HasGroupParticipation;

            output.ProcessStandards            = input.ProcessStandards;
            output.ProcessStandardsDescription = helper.HandleLanguageMap(input.ProcessStandardsDescription, output, "ProcessStandardsDescription");
            output.ScoringMethodDescription    = helper.HandleLanguageMap(input.ScoringMethodDescription, output, "ScoringMethodDescription");

            output.ScoringMethodExample            = input.ScoringMethodExample;
            output.ScoringMethodExampleDescription = helper.HandleLanguageMap(input.ScoringMethodExampleDescription, output, "ScoringMethodExampleDescription");
            output.ScoringMethodType = helper.MapCAOListToEnumermation(input.ScoringMethodType);

            //TBD - a custom version
            //output.InstructionalProgramType = helper.MapCAOListToEnumermation( input.InstructionalProgramType );
            //occupations
            output.Occupations = helper.MapCAOListToCAOProfileList(input.OccupationType);
            //just append alternative items. Ensure empty lists are ignored
            //output.Occupations.AddRange( helper.AppendLanguageMapListToCAOProfileList( input.AlternativeOccupationType ) );

            //skip if no occupations
            if (output.Occupations.Count() == 0 &&
                UtilityManager.GetAppKeyValue("skipCredImportIfNoOccupations", false))
            {
                //LoggingHelper.DoTrace( 2, string.Format( "		***Skipping Credential# {0}, {1} as it has no occupations and this is a special run.", output.Id, output.Name ) );
                //return true;
            }
            //Industries
            output.Industries = helper.MapCAOListToCAOProfileList(input.IndustryType);
            //output.Industries.AddRange( helper.AppendLanguageMapListToCAOProfileList( input.AlternativeIndustryType ) );
            //naics
            //output.Naics = input.Naics;

            output.InstructionalProgramTypes = helper.MapCAOListToCAOProfileList(input.InstructionalProgramType);
            //output.InstructionalProgramTypes.AddRange( helper.AppendLanguageMapListToCAOProfileList( input.AlternativeInstructionalProgramType ) );
            if (output.InstructionalProgramTypes.Count() == 0 && UtilityManager.GetAppKeyValue("skipAsmtImportIfNoCIP", false))
            {
                //skip
                LoggingHelper.DoTrace(2, string.Format("		***Skipping asmt# {0}, {1} as it has no InstructionalProgramTypes and this is a special run.", output.Id, output.Name));
                return(true);
            }
            //============================================================
            //handle QuantitativeValue
            //21-03-23 making the move to ValueProfile
            //output.CreditValue = helper.HandleQuantitiveValue( input.CreditValue, "Assessment.CreditValue" );
            //output.QVCreditValueList = helper.HandleValueProfileListToQVList( input.CreditValue, "Assessment.CreditValue" );
            //don't initially need CreditValueList if using CreditValueJson here
            output.CreditValue = helper.HandleValueProfileList(input.CreditValue, "Assessment.CreditValue");
            //however, CreditValueJson must include resolved concepts
            //TODO - take the opportunity to move away from Enumerations
            output.CreditValueJson = JsonConvert.SerializeObject(output.CreditValue, MappingHelperV3.GetJsonSettings());
            //note can still have CreditUnitTypeDescription by itself. What to do if both?
            output.CreditUnitTypeDescription = helper.HandleLanguageMap(input.CreditUnitTypeDescription, output, "Assessment.CreditUnitTypeDescription");
            //if ( output.CreditValueList != null && output.CreditValueList.Any() )
            //	output.CreditValue = output.CreditValueList[ 0 ];
            //


            output.Jurisdiction = helper.MapToJurisdiction(input.Jurisdiction, ref status);

            //EstimatedCost
            //will need to format, all populate Entity.RelatedCosts (for bubble up) - actually this would be for asmts, and lopps
            output.EstimatedCost = helper.FormatCosts(input.EstimatedCost, ref status);

            //assesses compentencies
            output.AssessesCompetencies = helper.MapCAOListToCAOProfileList(input.Assesses);
            if (output.AssessesCompetencies.Count() == 0 && UtilityManager.GetAppKeyValue("skipAsmtImportIfNoCompetencies", false))
            {
                //skip
                LoggingHelper.DoTrace(2, string.Format("		***Skipping asmt# {0}, {1} as it has no competencies and this is a special run.", output.Id, output.Name));
                return(true);
            }

            //common conditions
            output.ConditionManifestIds = helper.MapEntityReferences(input.CommonConditions, CodesManager.ENTITY_TYPE_CONDITION_MANIFEST, CodesManager.ENTITY_TYPE_ASSESSMENT_PROFILE, ref status);
            //common costs
            output.CostManifestIds = helper.MapEntityReferences(input.CommonCosts, CodesManager.ENTITY_TYPE_COST_MANIFEST, CodesManager.ENTITY_TYPE_ASSESSMENT_PROFILE, ref status);

            //connections
            output.AdvancedStandingFrom  = helper.FormatConditionProfile(input.AdvancedStandingFrom, ref status);
            output.IsAdvancedStandingFor = helper.FormatConditionProfile(input.IsAdvancedStandingFor, ref status);

            output.PreparationFrom  = helper.FormatConditionProfile(input.PreparationFrom, ref status);
            output.IsPreparationFor = helper.FormatConditionProfile(input.IsPreparationFor, ref status);

            output.IsRequiredFor    = helper.FormatConditionProfile(input.IsRequiredFor, ref status);
            output.IsRecommendedFor = helper.FormatConditionProfile(input.IsRecommendedFor, ref status);

            //EstimatedDuration ==============================
            output.EstimatedDuration = helper.FormatDuration(input.EstimatedDuration, ref status);

            //conditions ======================================
            output.Requires       = helper.FormatConditionProfile(input.Requires, ref status);
            output.Recommends     = helper.FormatConditionProfile(input.Recommends, ref status);
            output.EntryCondition = helper.FormatConditionProfile(input.EntryCondition, ref status);
            output.Corequisite    = helper.FormatConditionProfile(input.Corequisite, ref status);

            //Process profiles ==============================
            output.AdministrationProcess = helper.FormatProcessProfile(input.AdministrationProcess, ref status);
            output.DevelopmentProcess    = helper.FormatProcessProfile(input.DevelopmentProcess, ref status);
            output.MaintenanceProcess    = helper.FormatProcessProfile(input.MaintenanceProcess, ref status);

            //

            output.Addresses = helper.FormatAvailableAtAddresses(input.AvailableAt, ref status);
            //targets
            if (input.TargetAssessment != null && input.TargetAssessment.Count > 0)
            {
                output.TargetAssessmentIds = helper.MapEntityReferences("Assessment.TargetAssessment", input.TargetAssessment, CodesManager.ENTITY_TYPE_ASSESSMENT_PROFILE, ref status);
            }
            //21-04-13 mp - TargetLearningResource will be URLs not registry resources
            if (input.TargetLearningResource != null && input.TargetLearningResource.Count > 0)
            {
                output.TargetLearningResource = input.TargetLearningResource;
                //output.TargetLearningOpportunityIds = helper.MapEntityReferences( "Assessment.TargetLearningOpportunity", input.TargetLearningResource, CodesManager.ENTITY_TYPE_LEARNING_OPP_PROFILE, ref status );
            }
            //
            if (input.TargetPathway != null && input.TargetPathway.Count > 0)
            {
                output.TargetPathwayIds = helper.MapEntityReferences("Assessment.TargetPathway", input.TargetPathway, CodesManager.ENTITY_TYPE_PATHWAY, ref status);
            }

            //INs
            output.AccreditedIn = helper.MapToJurisdiction(input.AccreditedIn, ref status);
            output.ApprovedIn   = helper.MapToJurisdiction(input.ApprovedIn, ref status);
            output.OfferedIn    = helper.MapToJurisdiction(input.OfferedIn, ref status);
            output.RecognizedIn = helper.MapToJurisdiction(input.RecognizedIn, ref status);
            output.RegulatedIn  = helper.MapToJurisdiction(input.RegulatedIn, ref status);

            //SameAs URI
            output.SameAs = helper.MapToTextValueProfile(input.SameAs);
            //FinancialAssistance ============================
            //output.FinancialAssistanceOLD = helper.FormatFinancialAssistance( input.FinancialAssistance, ref status );
            output.FinancialAssistance = helper.FormatFinancialAssistance(input.FinancialAssistance, ref status);
            if (output.FinancialAssistance != null && output.FinancialAssistance.Any())
            {
                output.FinancialAssistanceJson = JsonConvert.SerializeObject(output.FinancialAssistance, MappingHelperV3.GetJsonSettings());
            }

            //=== if any messages were encountered treat as warnings for now
            if (messages.Count > 0)
            {
                status.SetMessages(messages, true);
            }
            //just in case check if entity added since start
            if (output.Id == 0)
            {
                ThisEntity entity = EntityServices.GetByCtid(ctid);
                if (entity != null && entity.Id > 0)
                {
                    output.Id    = entity.Id;
                    output.RowId = entity.RowId;
                }
            }
            importSuccessfull = mgr.Import(output, ref status);

            status.DocumentId    = output.Id;
            status.DetailPageUrl = string.Format("~/assessment/{0}", output.Id);
            status.DocumentRowId = output.RowId;

            //just in case
            if (status.HasErrors)
            {
                importSuccessfull = false;
            }

            //if record was added to db, add to/or set EntityResolution as resolved
            int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                       ctid,
                                                                       CodesManager.ENTITY_TYPE_ASSESSMENT_PROFILE,
                                                                       output.RowId,
                                                                       output.Id,
                                                                       (output.Id > 0),
                                                                       ref messages,
                                                                       output.Id > 0);

            return(importSuccessfull);
        }
Esempio n. 18
0
        public bool ImportV3(string payload, string envelopeIdentifier, SaveStatus status)
        {
            InputEntityV3 input      = new InputEntityV3();
            var           bnodes     = new List <BNodeV3>();
            var           mainEntity = new Dictionary <string, object>();

            //status.AddWarning( "The resource uses @graph and is not handled yet" );

            Dictionary <string, object> dictionary = RegistryServices.JsonToDictionary(payload);
            object graph = dictionary["@graph"];
            //serialize the graph object
            var glist = JsonConvert.SerializeObject(graph);

            //parse graph in to list of objects
            JArray graphList = JArray.Parse(glist);
            int    cntr      = 0;

            foreach (var item in graphList)
            {
                cntr++;
                if (cntr == 1)
                {
                    var main = item.ToString();
                    //may not use this. Could add a trace method
                    mainEntity = RegistryServices.JsonToDictionary(main);
                    input      = JsonConvert.DeserializeObject <InputEntityV3>(main);
                }
                else
                {
                    var bn = item.ToString();
                    bnodes.Add(JsonConvert.DeserializeObject <BNodeV3>(bn));
                }
            }

            List <string>   messages          = new List <string>();
            bool            importSuccessfull = false;
            EntityServices  mgr    = new EntityServices();
            MappingHelperV3 helper = new MappingHelperV3();

            helper.entityBlankNodes = bnodes;

            string ctid           = input.Ctid;
            string referencedAtId = input.CtdlId;

            LoggingHelper.DoTrace(5, "		name: "+ input.Name.ToString());
            LoggingHelper.DoTrace(6, "		url: "+ input.CostDetails);
            LoggingHelper.DoTrace(5, "		ctid: "+ input.Ctid);
            LoggingHelper.DoTrace(5, "		@Id: "+ input.CtdlId);
            status.Ctid = ctid;

            if (status.DoingDownloadOnly)
            {
                return(true);
            }

            if (!DoesEntityExist(input.Ctid, ref output))
            {
                output.RowId = Guid.NewGuid();
            }

            helper.currentBaseObject = output;

            output.Name        = helper.HandleLanguageMap(input.Name, output, "Name");
            output.Description = helper.HandleLanguageMap(input.Description, output, "Description");

            output.CTID = input.Ctid;
            output.CredentialRegistryId = envelopeIdentifier;
            output.CostDetails          = input.CostDetails;

            output.OwningAgentUid = helper.MapOrganizationReferencesGuid("CostManifest.OwningAgentUid", input.CostManifestOf, ref status);

            output.StartDate = helper.MapDate(input.StartDate, "StartDate", ref status);
            output.EndDate   = helper.MapDate(input.EndDate, "StartDate", ref status);

            output.EstimatedCosts = helper.FormatCosts(input.EstimatedCost, ref status);

            status.DocumentId    = output.Id;
            status.DocumentRowId = output.RowId;

            //=== if any messages were encountered treat as warnings for now
            if (messages.Count > 0)
            {
                status.SetMessages(messages, true);
            }

            importSuccessfull = mgr.Import(output, ref status);
            //just in case
            if (status.HasErrors)
            {
                importSuccessfull = false;
            }

            //if record was added to db, add to/or set EntityResolution as resolved
            int ierId = new ImportManager().Import_EntityResolutionAdd(referencedAtId,
                                                                       ctid,
                                                                       CodesManager.ENTITY_TYPE_COST_MANIFEST,
                                                                       output.RowId,
                                                                       output.Id,
                                                                       false,
                                                                       ref messages,
                                                                       output.Id > 0);

            return(importSuccessfull);
        }