Beispiel #1
0
        public static ThisEntity GetByName_SubjectWebpage(string name, string swp)
        {
            ThisEntity entity = new ThisEntity();

            using (var context = new EntityContext())
            {
                context.Configuration.LazyLoadingEnabled = false;
                DBEntity from = context.Pathway
                                .FirstOrDefault(s => s.Name.ToLower() == name.ToLower() && s.SubjectWebpage.ToLower() == swp.ToLower());

                if (from != null && from.Id > 0)
                {
                    entity.RowId          = from.RowId;
                    entity.Id             = from.Id;
                    entity.Name           = from.Name;
                    entity.EntityStateId  = ( int )(from.EntityStateId ?? 1);
                    entity.Description    = from.Description;
                    entity.SubjectWebpage = from.SubjectWebpage;

                    entity.CTID = from.CTID;
                    entity.CredentialRegistryId = from.CredentialRegistryId;
                }
            }
            return(entity);
        }
Beispiel #2
0
        public static void MapFromDB(DBEntity from, ThisEntity to, bool includingComponents, bool includingExtra = true)
        {
            MapFromDB_Basic(from, to, includingComponents);

            to.CredentialRegistryId = from.CredentialRegistryId;

            //=============================
            if (includingExtra)
            {
                to.Subject = Entity_ReferenceManager.GetAll(to.RowId, CodesManager.PROPERTY_CATEGORY_SUBJECT);

                to.Keyword = Entity_ReferenceManager.GetAll(to.RowId, CodesManager.PROPERTY_CATEGORY_KEYWORD);
                //properties

                try
                {
                    to.OccupationType         = Reference_FrameworksManager.FillEnumeration(to.RowId, CodesManager.PROPERTY_CATEGORY_SOC);
                    to.AlternativeOccupations = Entity_ReferenceManager.GetAll(to.RowId, CodesManager.PROPERTY_CATEGORY_SOC);

                    to.IndustryType          = Reference_FrameworksManager.FillEnumeration(to.RowId, CodesManager.PROPERTY_CATEGORY_NAICS);
                    to.AlternativeIndustries = Entity_ReferenceManager.GetAll(to.RowId, CodesManager.PROPERTY_CATEGORY_NAICS);
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".MapFromDB_B(), Name: {0} ({1})", to.Name, to.Id));
                    to.StatusMessage = FormatExceptions(ex);
                }
            }
        }
Beispiel #3
0
        public int AddPendingRecord(Guid entityUid, string ctid, string registryAtId, ref string status)
        {
            DBEntity efEntity = new DBEntity();

            try
            {
                using (var context = new EntityContext())
                {
                    if (!IsValidGuid(entityUid))
                    {
                        status = thisClassName + " - A valid GUID must be provided to create a pending entity";
                        return(0);
                    }
                    //quick check to ensure not existing
                    ThisEntity entity = GetByCtid(ctid);
                    if (entity != null && entity.Id > 0)
                    {
                        return(entity.Id);
                    }

                    //only add DB required properties
                    //NOTE - an entity will be created via trigger
                    efEntity.Name          = "Placeholder until full document is downloaded";
                    efEntity.Description   = "Placeholder until full document is downloaded";
                    efEntity.EntityStateId = 1;
                    efEntity.RowId         = entityUid;
                    //watch that Ctid can be  updated if not provided now!!
                    efEntity.CTID           = ctid;
                    efEntity.SubjectWebpage = registryAtId;

                    efEntity.Created     = System.DateTime.Now;
                    efEntity.LastUpdated = System.DateTime.Now;

                    context.Pathway.Add(efEntity);
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        return(efEntity.Id);
                    }

                    status = thisClassName + " Error - the save was not successful, but no message provided. ";
                }
            }

            catch (Exception ex)
            {
                string message = FormatExceptions(ex);
                LoggingHelper.LogError(ex, thisClassName + string.Format(".AddPendingRecord. entityUid:  {0}, ctid: {1}", entityUid, ctid));
                status = thisClassName + " Error - the save was not successful. " + message;
            }
            return(0);
        }
Beispiel #4
0
        }         //

        public static void MapToDB(ThisEntity input, DBEntity output)
        {
            //want output ensure fields input create are not wiped
            if (output.Id == 0)
            {
                output.CTID = input.CTID;
            }
            if (!string.IsNullOrWhiteSpace(input.CredentialRegistryId))
            {
                output.CredentialRegistryId = input.CredentialRegistryId;
            }

            output.Id                  = input.Id;
            output.Name                = GetData(input.Name);
            output.Description         = GetData(input.Description);
            output.SubjectWebpage      = GetUrlData(input.SubjectWebpage);
            output.HasProgressionModel = input.ProgressionModelURI;

            if (IsGuidValid(input.OwningAgentUid))
            {
                if (output.Id > 0 && output.OwningAgentUid != input.OwningAgentUid)
                {
                    if (IsGuidValid(output.OwningAgentUid))
                    {
                        //need output remove the owner role, or could have been others
                        string statusMessage = "";
                        new Entity_AgentRelationshipManager().Delete(output.RowId, output.OwningAgentUid, Entity_AgentRelationshipManager.ROLE_TYPE_OWNER, ref statusMessage);
                    }
                }
                output.OwningAgentUid = input.OwningAgentUid;
                //get for use to add to elastic pending
                input.OwningOrganization = OrganizationManager.GetForSummary(input.OwningAgentUid);
                //input.OwningOrganizationId = org.Id;
            }
            else
            {
                //always have output have an owner
                //output.OwningAgentUid = null;
            }

            //if ( input.InLanguageId > 0 )
            //	output.InLanguageId = input.InLanguageId;
            //else if ( !string.IsNullOrWhiteSpace( input.InLanguage ) )
            //{
            //	output.InLanguageId = CodesManager.GetLanguageId( input.InLanguage );
            //}
            //else


            //======================================================================
        }
Beispiel #5
0
        public static ThisEntity GetBasic(int id)
        {
            ThisEntity entity = new ThisEntity();

            using (var context = new EntityContext())
            {
                DBEntity item = context.Pathway
                                .SingleOrDefault(s => s.Id == id);

                if (item != null && item.Id > 0)
                {
                    MapFromDB_Basic(item, entity, false);
                }
            }

            return(entity);
        }
Beispiel #6
0
        //
        public static ThisEntity GetDetails(int id)
        {
            ThisEntity entity = new ThisEntity();

            using (var context = new EntityContext())
            {
                DBEntity item = context.Pathway
                                .SingleOrDefault(s => s.Id == id);

                if (item != null && item.Id > 0)
                {
                    //check for virtual deletes
                    if (item.EntityStateId == 0)
                    {
                        return(entity);
                    }

                    MapFromDB(item, entity, true);
                }
            }

            return(entity);
        }
Beispiel #7
0
        public static ThisEntity GetByCtid(string ctid)
        {
            ThisEntity entity = new ThisEntity();

            using (var context = new EntityContext())
            {
                DBEntity from = context.Pathway
                                .FirstOrDefault(s => s.CTID.ToLower() == ctid.ToLower());

                if (from != null && from.Id > 0)
                {
                    entity.RowId                = from.RowId;
                    entity.Id                   = from.Id;
                    entity.EntityStateId        = ( int )(from.EntityStateId ?? 1);
                    entity.Name                 = from.Name;
                    entity.Description          = from.Description;
                    entity.SubjectWebpage       = from.SubjectWebpage;
                    entity.CTID                 = from.CTID;
                    entity.CredentialRegistryId = from.CredentialRegistryId;
                }
            }

            return(entity);
        }
Beispiel #8
0
        public bool Delete(string envelopeId, string ctid, ref string statusMessage)
        {
            bool isValid = true;

            if ((string.IsNullOrWhiteSpace(envelopeId) || !IsValidGuid(envelopeId)) &&
                string.IsNullOrWhiteSpace(ctid))
            {
                statusMessage = thisClassName + ".Delete() Error - a valid envelope identifier must be provided - OR  valid CTID";
                return(false);
            }
            if (string.IsNullOrWhiteSpace(envelopeId))
            {
                envelopeId = "SKIP ME";
            }
            if (string.IsNullOrWhiteSpace(ctid))
            {
                ctid = "SKIP ME";
            }
            int  orgId  = 0;
            Guid orgUid = new Guid();

            using (var context = new EntityContext())
            {
                try
                {
                    context.Configuration.LazyLoadingEnabled = false;
                    DBEntity efEntity = context.Pathway
                                        .FirstOrDefault(s => s.CredentialRegistryId == envelopeId ||
                                                        (s.CTID == ctid)
                                                        );

                    if (efEntity != null && efEntity.Id > 0)
                    {
                        Guid rowId = efEntity.RowId;
                        if (IsValidGuid(efEntity.OwningAgentUid))
                        {
                            Organization org = OrganizationManager.GetBasics(( Guid )efEntity.OwningAgentUid);
                            orgId  = org.Id;
                            orgUid = org.RowId;
                        }
                        //need to remove from Entity.
                        //-using before delete trigger - verify won't have RI issues
                        string msg = string.Format(" Pathway. Id: {0}, Name: {1}, Ctid: {2}.", efEntity.Id, efEntity.Name, efEntity.CTID);
                        //18-04-05 mparsons - change to set inactive, and notify - seems to have been some incorrect deletes
                        //context.Pathway.Remove( efEntity );
                        efEntity.EntityStateId = 0;
                        efEntity.LastUpdated   = System.DateTime.Now;
                        int count = context.SaveChanges();
                        if (count > 0)
                        {
                            new ActivityManager().SiteActivityAdd(new SiteActivity()
                            {
                                ActivityType     = "Pathway",
                                Activity         = "Import",
                                Event            = "Delete",
                                Comment          = msg,
                                ActivityObjectId = efEntity.Id
                            });
                            isValid = true;
                            //add pending request
                            List <String> messages = new List <string>();
                            new SearchPendingReindexManager().AddDeleteRequest(CodesManager.ENTITY_TYPE_PATHWAY, efEntity.Id, ref messages);
                            //mark owning org for updates (actually should be covered by ReindexAgentForDeletedArtifact
                            new SearchPendingReindexManager().Add(CodesManager.ENTITY_TYPE_ORGANIZATION, orgId, 1, ref messages);

                            //delete all relationships
                            workIT.Models.SaveStatus        status = new SaveStatus();
                            Entity_AgentRelationshipManager earmgr = new Entity_AgentRelationshipManager();
                            earmgr.DeleteAll(rowId, ref status);
                            //also check for any relationships
                            //There could be other orgs from relationships to be reindexed as well!

                            //also check for any relationships
                            new Entity_AgentRelationshipManager().ReindexAgentForDeletedArtifact(orgUid);
                        }
                    }
                    else
                    {
                        statusMessage = thisClassName + ".Delete() Warning No action taken, as the record was not found.";
                    }
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + ".Delete(envelopeId)");
                    isValid       = false;
                    statusMessage = FormatExceptions(ex);
                    if (statusMessage.ToLower().IndexOf("the delete statement conflicted with the reference constraint") > -1)
                    {
                        statusMessage = "Error: this Pathway cannot be deleted as it is being referenced by other items, such as roles or credentials. These associations must be removed before this Pathway can be deleted.";
                    }
                }
            }
            return(isValid);
        }
Beispiel #9
0
        public bool Save(ThisEntity entity, ref SaveStatus status)
        {
            bool isValid = true;
            int  count   = 0;

            try
            {
                using (var context = new EntityContext())
                {
                    if (ValidateProfile(entity, ref status) == false)
                    {
                        return(false);
                    }

                    if (entity.Id > 0)
                    {
                        //TODO - consider if necessary, or interferes with anything
                        context.Configuration.LazyLoadingEnabled = false;
                        DBEntity efEntity = context.Pathway
                                            .SingleOrDefault(s => s.Id == entity.Id);

                        if (efEntity != null && efEntity.Id > 0)
                        {
                            //fill in fields that may not be in entity
                            entity.RowId = efEntity.RowId;

                            MapToDB(entity, efEntity);

                            //19-05-21 mp - should add a check for an update where currently is deleted
                            if ((efEntity.EntityStateId ?? 0) == 0)
                            {
                                var url = string.Format(UtilityManager.GetAppKeyValue("credentialFinderSite") + "pathway/{0}", efEntity.Id);
                                //notify, and???
                                //EmailManager.NotifyAdmin( "Previously Deleted Pathway has been reactivated", string.Format( "<a href='{2}'>Pathway: {0} ({1})</a> was deleted and has now been reactivated.", efEntity.Name, efEntity.Id, url ) );
                                SiteActivity sa = new SiteActivity()
                                {
                                    ActivityType     = "Pathway",
                                    Activity         = "Import",
                                    Event            = "Reactivate",
                                    Comment          = string.Format("Pathway had been marked as deleted, and was reactivted by the import. Name: {0}, SWP: {1}", entity.Name, entity.SubjectWebpage),
                                    ActivityObjectId = entity.Id
                                };
                                new ActivityManager().SiteActivityAdd(sa);
                            }
                            //assume and validate, that if we get here we have a full record
                            if ((efEntity.EntityStateId ?? 1) != 2)
                            {
                                efEntity.EntityStateId = 3;
                            }

                            if (IsValidDate(status.EnvelopeCreatedDate) && status.LocalCreatedDate < efEntity.Created)
                            {
                                efEntity.Created = status.LocalCreatedDate;
                            }
                            if (IsValidDate(status.EnvelopeUpdatedDate) && status.LocalUpdatedDate != efEntity.LastUpdated)
                            {
                                efEntity.LastUpdated = status.LocalUpdatedDate;
                            }
                            //has changed?
                            if (HasStateChanged(context))
                            {
                                if (IsValidDate(status.EnvelopeUpdatedDate))
                                {
                                    efEntity.LastUpdated = status.LocalUpdatedDate;
                                }
                                else
                                {
                                    efEntity.LastUpdated = DateTime.Now;
                                }
                                //NOTE efEntity.EntityStateId is set to 0 in delete method )
                                count = context.SaveChanges();
                                //can be zero if no data changed
                                if (count >= 0)
                                {
                                    isValid = true;
                                }
                                else
                                {
                                    //?no info on error

                                    isValid = false;
                                    string message = string.Format(thisClassName + ".Save Failed", "Attempted to update a Pathway. The process appeared to not work, but was not an exception, so we have no message, or no clue. Pathway: {0}, Id: {1}", entity.Name, entity.Id);
                                    status.AddError("Error - the update was not successful. " + message);
                                    EmailManager.NotifyAdmin(thisClassName + ".Save Failed Failed", message);
                                }
                            }
                            else
                            {
                                //update entity.LastUpdated - assuming there has to have been some change in related data
                                //new EntityManager().UpdateModifiedDate( entity.RowId, ref status );
                            }
                            if (isValid)
                            {
                                if (!UpdateParts(entity, ref status))
                                {
                                    isValid = false;
                                }

                                SiteActivity sa = new SiteActivity()
                                {
                                    ActivityType     = "Pathway",
                                    Activity         = "Import",
                                    Event            = "Update",
                                    Comment          = string.Format("Pathway was updated by the import. Name: {0}, SWP: {1}", entity.Name, entity.SubjectWebpage),
                                    ActivityObjectId = entity.Id
                                };
                                new ActivityManager().SiteActivityAdd(sa);

                                //if ( isValid || partsUpdateIsValid )
                                new EntityManager().UpdateModifiedDate(entity.RowId, ref status, efEntity.LastUpdated);
                            }
                        }
                        else
                        {
                            status.AddError("Error - update failed, as record was not found.");
                        }
                    }
                    else
                    {
                        //add
                        int newId = Add(entity, ref status);
                        if (newId == 0 || status.HasErrors)
                        {
                            isValid = false;
                        }
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
            {
                string message = HandleDBValidationError(dbex, thisClassName + string.Format(".Save. id: {0}, Name: {1}", entity.Id, entity.Name), "Pathway");
                status.AddError(thisClassName + ".Save(). Error - the save was not successful. " + message);
            }
            catch (Exception ex)
            {
                string message = FormatExceptions(ex);
                LoggingHelper.LogError(ex, thisClassName + string.Format(".Save. id: {0}, Name: {1}", entity.Id, entity.Name));
                status.AddError(thisClassName + ".Save(). Error - the save was not successful. " + message);
                isValid = false;
            }


            return(isValid);
        }
Beispiel #10
0
        /// <summary>
        /// add a Pathway
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        private int Add(ThisEntity entity, ref SaveStatus status)
        {
            DBEntity efEntity = new DBEntity();

            using (var context = new EntityContext())
            {
                try
                {
                    MapToDB(entity, efEntity);

                    if (IsValidGuid(entity.RowId))
                    {
                        efEntity.RowId = entity.RowId;
                    }
                    else
                    {
                        efEntity.RowId = Guid.NewGuid();
                    }
                    efEntity.EntityStateId = 3;
                    if (IsValidDate(status.EnvelopeCreatedDate))
                    {
                        efEntity.Created     = status.LocalCreatedDate;
                        efEntity.LastUpdated = status.LocalCreatedDate;
                    }
                    else
                    {
                        efEntity.Created     = System.DateTime.Now;
                        efEntity.LastUpdated = System.DateTime.Now;
                    }

                    context.Pathway.Add(efEntity);

                    // submit the change to database
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        entity.Id    = efEntity.Id;
                        entity.RowId = efEntity.RowId;
                        //add log entry
                        SiteActivity sa = new SiteActivity()
                        {
                            ActivityType     = "Pathway",
                            Activity         = "Import",
                            Event            = "Add",
                            Comment          = string.Format("Full Pathway was added by the import. Name: {0}, SWP: {1}", entity.Name, entity.SubjectWebpage),
                            ActivityObjectId = entity.Id
                        };
                        new ActivityManager().SiteActivityAdd(sa);

                        if (UpdateParts(entity, ref status) == false)
                        {
                        }

                        return(efEntity.Id);
                    }
                    else
                    {
                        //?no info on error

                        string message = thisClassName + string.Format(". Add Failed", "Attempted to add a Pathway. The process appeared to not work, but was not an exception, so we have no message, or no clue. Pathway: {0}, ctid: {1}", entity.Name, entity.CTID);
                        status.AddError(thisClassName + ". Error - the add was not successful. " + message);
                        EmailManager.NotifyAdmin("PathwayManager. Add Failed", message);
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
                {
                    string message = HandleDBValidationError(dbex, thisClassName + ".Add() ", "Pathway");
                    status.AddError(thisClassName + ".Add(). Error - the save was not successful. " + message);

                    LoggingHelper.LogError(message, true);
                }
                catch (Exception ex)
                {
                    string message = FormatExceptions(ex);
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".Add(), Name: {0}\r\n", efEntity.Name));
                    status.AddError(thisClassName + ".Add(). Error - the save was not successful. \r\n" + message);
                }
            }

            return(efEntity.Id);
        }
Beispiel #11
0
        public static void MapFromDB_Basic(DBEntity input, ThisEntity output, bool includingComponents)
        {
            output.Id            = input.Id;
            output.RowId         = input.RowId;
            output.EntityStateId = ( int )(input.EntityStateId ?? 1);
            output.Name          = input.Name;
            output.Description   = input.Description == null ? "" : input.Description;
            output.CTID          = input.CTID;
            if (IsGuidValid(input.OwningAgentUid))
            {
                output.OwningAgentUid     = ( Guid )input.OwningAgentUid;
                output.OwningOrganization = OrganizationManager.GetForSummary(output.OwningAgentUid);
                output.OrganizationId     = output.OwningOrganization.Id;
                //get roles
                OrganizationRoleProfile orp = Entity_AgentRelationshipManager.AgentEntityRole_GetAsEnumerationFromCSV(output.RowId, output.OwningAgentUid);
                output.OwnerRoles = orp.AgentRole;
            }
            //
            output.OrganizationRole = Entity_AgentRelationshipManager.AgentEntityRole_GetAll_ToEnumeration(output.RowId, true);
            //
            output.SubjectWebpage = input.SubjectWebpage;
            if (IsValidDate(input.Created))
            {
                output.Created = ( DateTime )input.Created;
            }
            if (IsValidDate(input.LastUpdated))
            {
                output.LastUpdated = ( DateTime )input.LastUpdated;
            }



            if (string.IsNullOrWhiteSpace(output.CTID) || output.EntityStateId < 3)
            {
                output.IsReferenceVersion = true;
                return;
            }
            //=====
            var relatedEntity = EntityManager.GetEntity(output.RowId, false);

            if (relatedEntity != null && relatedEntity.Id > 0)
            {
                output.EntityLastUpdated = relatedEntity.LastUpdated;
            }

            //components
            if (includingComponents)
            {
                //
                output.ProgressionModelURI = input.HasProgressionModel;
                if (!string.IsNullOrWhiteSpace(output.ProgressionModelURI) && includingComponents)
                {
                    //ensure this is not called always from ProgressionModel/CS or will get a stack overflow
                    output.HasProgressionModel = ConceptSchemeManager.GetByCtid(output.ProgressionModelURI);
                }

                //include conditions
                //to.HasPart = PathwayComponentManager.GetAllForPathway( to.CTID, PathwayComponentManager.componentActionOfDeep );
                //actual may be better to do through Entity_PathwayComponent
                //	but only destination component is under pathway
                //will there be an issue with recursion?
                //compare
                //one less that parts???
                //var parts1 = PathwayComponentManager.GetAllForPathway( to.CTID, PathwayComponentManager.componentActionOfDeep );
                //and
                var parts = Entity_PathwayComponentManager.GetAll(output.RowId, PathwayComponentManager.componentActionOfDeep);
                //may want to split out here, do in context

                foreach (var item in parts)
                {
                    if (item.ComponentRelationshipTypeId == PathwayComponent.PathwayComponentRelationship_HasDestinationComponent)
                    {
                        output.HasDestinationComponent.Add(item);
                    }
                    else if (item.ComponentRelationshipTypeId == PathwayComponent.PathwayComponentRelationship_HasChild)
                    {
                        output.HasChild.Add(item);
                    }
                }
                //now get a unique list
                //var parts = to.HasPart;
                output.HasPart = new List <PathwayComponent>();
                foreach (var item in parts)
                {
                    int index = output.HasPart.FindIndex(a => a.CTID == item.CTID);
                    if (index == -1)
                    {
                        output.HasPart.Add(item);
                    }
                }
            }
        }         //