//currently
        public bool DoesEntityExist(string ctid, ref ThisEntity entity)
        {
            bool exists = false;

            entity = EntityServices.GetByCtid(ctid);
            if (entity != null && entity.Id > 0)
            {
                return(true);
            }

            return(exists);
        }
        }         //

        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);
        }