//currently use education framework
        public bool DoesEntityExist(string ctid, ref Framework entity)
        {
            bool exists = false;

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

            return(exists);
        }
        public bool ProcessEnvelope(EntityServices mgr, ReadEnvelope item, SaveStatus status)
        {
            if (item == null || string.IsNullOrWhiteSpace(item.EnvelopeIdentifier))
            {
                status.AddError(thisClassName + " A valid ReadEnvelope must be provided.");
                return(false);
            }

            DateTime createDate         = new DateTime();
            DateTime envelopeUpdateDate = new DateTime();

            if (DateTime.TryParse(item.NodeHeaders.CreatedAt.Replace("UTC", "").Trim(), out createDate))
            {
                //entity.DocumentUpdatedAt = updateDate;
            }
            if (DateTime.TryParse(item.NodeHeaders.UpdatedAt.Replace("UTC", "").Trim(), out envelopeUpdateDate))
            {
                //entity.DocumentUpdatedAt = envelopeUpdateDate;
            }

            string payload            = item.DecodedResource.ToString();
            string envelopeIdentifier = item.EnvelopeIdentifier;
            string ctdlType           = RegistryServices.GetResourceType(payload);
            string envelopeUrl        = RegistryServices.GetEnvelopeUrl(envelopeIdentifier);

            LoggingHelper.DoTrace(5, "		envelopeUrl: "+ envelopeUrl);
            LoggingHelper.WriteLogFile(1, item.EnvelopeIdentifier + "_competencyFrameswork", payload, "", false);
            //input = JsonConvert.DeserializeObject<InputGraph>( item.DecodedResource.ToString() );

            //InputEntity framework = GetFramework( input.Graph );
            //LoggingHelper.DoTrace( 5, "		framework name: " + framework.name.ToString() );

            //just store input for now
            return(Import(mgr, payload, envelopeIdentifier, status));

            //return true;
        } //
        public string Import(string startingDate, string endingDate, int maxRecords, string community, bool downloadOnly = false)
        {
            bool importingThisType = UtilityManager.GetAppKeyValue("importing_competency_frameworks", true);

            if (!importingThisType)
            {
                LoggingHelper.DoTrace(1, "===  *****************  Skipping import of Competency Frameworks  ***************** ");
                return("Skipped import of competency_frameworks");
            }

            LoggingHelper.DoTrace(1, string.Format("===  *****************  {0}  ***************** ", thisClassName));
            JsonEntity          input    = new JsonEntity();
            ReadEnvelope        envelope = new ReadEnvelope();
            List <ReadEnvelope> list     = new List <ReadEnvelope>();
            EntityServices      mgr      = new EntityServices();
            string type = "competency_framework";

            int           pageNbr       = 1;
            int           pageSize      = 75;
            string        importError   = "";
            string        importResults = "";
            string        importNote    = "";
            ThisEntity    output        = new ThisEntity();
            List <string> messages      = new List <string>();

            //the nbr of records needs to be monitored, to determine the optimum
            //NOTE: consider the IOER approach that all candidate records are first downloaded, and then a separate process does the import

            int cntr       = 0;
            int pTotalRows = 0;

            int        exceptionCtr      = 0;
            string     statusMessage     = "";
            bool       isComplete        = false;
            bool       importSuccessfull = true;
            int        newImportId       = 0;
            SaveStatus status            = new SaveStatus();

            //will need to handle multiple calls - watch for time outs
            while (pageNbr > 0 && !isComplete)
            {
                list = RegistryServices.Search(type, startingDate, endingDate, pageNbr, pageSize, ref pTotalRows, ref statusMessage, community);

                //list = GetLatest( type, startingDate, endingDate, pageNbr, pageSize, ref pTotalRows, ref statusMessage, community );

                if (list == null || list.Count == 0)
                {
                    isComplete = true;
                    if (pageNbr == 1)
                    {
                        importNote = "Competency Frameworks: No records where found for date range ";
                        //Console.WriteLine( thisClassName + importNote );
                        LoggingHelper.DoTrace(4, thisClassName + importNote);
                    }
                    break;
                }

                foreach (ReadEnvelope item in list)
                {
                    cntr++;
                    string payload = item.DecodedResource.ToString();
                    LoggingHelper.DoTrace(2, string.Format("{0}. EnvelopeIdentifier: {1} ", cntr, item.EnvelopeIdentifier));
                    status = new SaveStatus();
                    status.DoingDownloadOnly = downloadOnly;
                    importError       = "";
                    importSuccessfull = false;

                    try
                    {
                        //Console.WriteLine( string.Format( "{0}. Competency Frameswork Envelope Identifier {1} ", cntr, item.EnvelopeIdentifier ) );


                        importSuccessfull = entityImportMgr.ProcessEnvelope(mgr, item, status);
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message.IndexOf("Path '@context', line 1") > 0)
                        {
                            importError = "The referenced registry document is using an old schema. Please republish it with the latest schema!";
                            status.AddError(importError);
                        }
                        else
                        {
                            LoggingHelper.LogError(ex, string.Format("Exception encountered in envelopeId: {0}", item.EnvelopeIdentifier), false, "CredentialFinder Import exception");
                            status.AddError(ex.Message);
                            importError = ex.Message;
                            LoggingHelper.DoTrace(1, " show exception to use getting related envelopeId " + ex.Message);
                        }

                        //make continue on exceptions an option
                        exceptionCtr++;
                        if (maxExceptions > 0 && exceptionCtr > maxExceptions)
                        {
                            //arbitrarily stop if large number of exceptions
                            importNote = string.Format(thisClassName + " - Many exceptions ({0}) were encountered during import - abandoning.", exceptionCtr);
                            Console.WriteLine(importNote);
                            LoggingHelper.DoTrace(1, importNote);
                            LoggingHelper.LogError(importNote, true, thisClassName + "- many exceptions");
                            isComplete = true;
                            break;
                        }
                    }
                    finally
                    {
                        if (!importSuccessfull)
                        {
                            if (string.IsNullOrWhiteSpace(importError))
                            {
                                importError = string.Join("\r\n", status.GetAllMessages().ToArray());
                            }
                        }
                        //store document
                        //add indicator of success
                        newImportId = importMgr.Add(item, entityTypeId, status.Ctid, importSuccessfull, importError, ref messages);
                        if (newImportId > 0 && status.Messages != null && status.Messages.Count > 0)
                        {
                            importMgr.AddMessages(newImportId, status, ref messages);
                        }
                    } //finally

                    if (maxRecords > 0 && cntr > maxRecords)
                    {
                        break;
                    }
                } //end foreach

                pageNbr++;
                if ((maxRecords > 0 && cntr > maxRecords) || cntr == pTotalRows)
                {
                    isComplete = true;
                    if (cntr < pTotalRows)
                    {
                        LoggingHelper.DoTrace(2, string.Format("CompetencyFramesworkImport EARLY EXIT. Completed {0} records out of a total of {1} ", cntr, pTotalRows));
                    }
                }
            } //
            importResults = string.Format("{0} - Processed {1} records, with {2} exceptions. \r\n", thisClassName, cntr, exceptionCtr);
            if (!string.IsNullOrWhiteSpace(importNote))
            {
                importResults += importNote;
            }

            //always call, as deletes are not tracked
            if (UtilityManager.GetAppKeyValue("updateCompetencyFrameworkReportTotals", false) == true)
            {
                mgr.UpdateCompetencyFrameworkReportTotals();
            }
            return(importResults);
        }
        } //

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