示例#1
0
        public static ThisEntity Get(int id, bool includingComponents = true)
        {
            ThisEntity entity = new ThisEntity();

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

                if (item != null && item.Id > 0)
                {
                    MapFromDB(item, entity, includingComponents);
                }
            }
            return(entity);
        }
示例#2
0
        public static ThisEntity GetByCtid(string ctid, bool includingComponents = true)
        {
            ThisEntity entity = new ThisEntity();

            if (string.IsNullOrWhiteSpace(ctid))
            {
                return(entity);
            }
            ctid = ctid.ToLower();
            using (var context = new EntityContext())
            {
                DBEntity item = context.ConceptScheme
                                .FirstOrDefault(s => s.CTID.ToLower() == ctid);
                if (item != null && item.Id > 0)
                {
                    MapFromDB(item, entity, includingComponents);
                }
            }

            return(entity);
        }
示例#3
0
        public static void MapToDB(ThisEntity from, DBEntity to)
        {
            //want to ensure fields from create are not wiped
            if (to.Id < 1)
            {
                if (IsValidDate(from.Created))
                {
                    to.Created = from.Created;
                }
            }
            //from may not have these values
            //to.Id = from.Id;
            //to.RowId = from.RowId;
            to.OrgId              = from.OrganizationId;
            to.Name               = from.Name;
            to.EntityStateId      = from.EntityStateId;
            to.Description        = from.Description;
            to.IsProgressionModel = from.IsProgressionModel;
            to.CTID               = from.CTID;
            //make sure not overwritten
            if (!string.IsNullOrWhiteSpace(from.CredentialRegistryId))
            {
                to.CredentialRegistryId = from.CredentialRegistryId;
            }

            //watch for overwriting these new properties.
            to.Source = from.Source;
            to.PublicationStatusType = from.PublicationStatusType;


            //using last updated date from interface, as we don't have all data here.
            if (IsValidDate(from.LastUpdated))
            {
                to.LastUpdated = from.LastUpdated;
            }
        }
示例#4
0
        public static void MapFromDB(DBEntity from, ThisEntity to, bool includingComponents = true)
        {
            to.Id             = from.Id;
            to.RowId          = from.RowId;
            to.EntityStateId  = from.EntityStateId;
            to.OrganizationId = from.OrgId;
            to.Name           = from.Name;
            to.Description    = from.Description;

            to.IsProgressionModel = from.IsProgressionModel == null ? false : (bool)from.IsProgressionModel;
            to.CTID   = from.CTID.ToLower();
            to.Source = from.Source;

            to.PublicationStatusType = from.PublicationStatusType;
            to.CredentialRegistryId  = from.CredentialRegistryId;
            if (to.OrganizationId > 0)
            {
                to.OwningOrganization = OrganizationManager.GetForSummary(to.OrganizationId);

                to.OwningAgentUid = to.OwningOrganization.RowId;
                //get roles- not sure. Can have own and offer
                //OrganizationRoleProfile orp = Entity_AgentRelationshipManager.AgentEntityRole_GetAsEnumerationFromCSV( to.RowId, to.OwningAgentUid );
                //to.OwnerRoles = orp.AgentRole;
                //if ( to.OwnerRoles.HasItems() == false )
                //{
                //	EnumeratedItem ei = Entity_AgentRelationshipManager.GetAgentRole( "Owned By" );
                //	if ( ei == null || ei.Id == 0 )
                //	{
                //		//messages.Add( string.Format( "The organization role: {0} is not valid", "OwnedBy" ) );
                //	}
                //	else
                //	{
                //		to.OwnerRoles.Items.Add( ei );
                //	}
                //}
            }
            //
            to.OrganizationRole = Entity_AgentRelationshipManager.AgentEntityRole_GetAll_ToEnumeration(to.RowId, true);
            //
            if (from.ConceptScheme_Concept != null && from.ConceptScheme_Concept.Any())
            {
                foreach (var item in from.ConceptScheme_Concept)
                {
                    to.HasConcepts.Add(new Models.Common.Concept()
                    {
                        Id           = item.Id,
                        CTID         = item.CTID,
                        PrefLabel    = item.PrefLabel,
                        Definition   = item.Definition,
                        IsTopConcept = item.IsTopConcept ?? false
                    });
                }
            }
            //
            if (includingComponents)
            {
                //how to know if this is a progression model? Or just do get anyway
                to.Pathways   = PathwayManager.GetAllForProgressionModel(to.CTID);
                to.HasPathway = to.Pathways.Select(m => m.CTID).ToList();
            }
            //
            if (IsValidDate(from.Created))
            {
                to.Created = ( DateTime )from.Created;
            }
            if (IsValidDate(from.LastUpdated))
            {
                to.LastUpdated = ( DateTime )from.LastUpdated;
            }
        }
示例#5
0
        /// <summary>
        /// Add/Update a ConceptScheme
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="messages"></param>
        /// <returns></returns>
        public bool Save(ThisEntity entity,
                         ref SaveStatus status, bool addingActivity = false)
        {
            bool isValid = true;
            int  count   = 0;

            DBEntity efEntity = new DBEntity();

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

                    if (entity.Id == 0)
                    {
                        //add
                        efEntity             = new DBEntity();
                        entity.EntityStateId = 3;
                        MapToDB(entity, efEntity);

                        if (IsValidDate(status.EnvelopeCreatedDate))
                        {
                            efEntity.Created = status.LocalCreatedDate;
                        }
                        else
                        {
                            efEntity.Created = DateTime.Now;
                        }
                        //
                        if (IsValidDate(status.EnvelopeUpdatedDate))
                        {
                            efEntity.LastUpdated = status.LocalUpdatedDate;
                        }
                        else
                        {
                            efEntity.LastUpdated = DateTime.Now;
                        }

                        if (IsValidGuid(entity.RowId))
                        {
                            efEntity.RowId = entity.RowId;
                        }
                        else
                        {
                            efEntity.RowId = Guid.NewGuid();
                        }

                        context.ConceptScheme.Add(efEntity);

                        count = context.SaveChanges();

                        entity.Id    = efEntity.Id;
                        entity.RowId = efEntity.RowId;
                        if (count == 0)
                        {
                            status.AddWarning(string.Format(" Unable to add Profile: {0} <br\\> ", string.IsNullOrWhiteSpace(entity.Name) ? "no description" : entity.Name));
                        }
                        else
                        {
                            if (addingActivity)
                            {
                                //add log entry
                                SiteActivity sa = new SiteActivity()
                                {
                                    ActivityType     = "ConceptScheme",
                                    Activity         = "Import",
                                    Event            = "Add",
                                    Comment          = string.Format("New ConceptScheme was found by the import. Name: {0}, URI: {1}", entity.Name, entity.Source),
                                    ActivityObjectId = entity.Id
                                };
                                new ActivityManager().SiteActivityAdd(sa);
                            }

                            UpdateParts(entity, ref status);
                            //
                            HandleConceptsSimple(entity, ref status);
                        }
                    }
                    else
                    {
                        efEntity = context.ConceptScheme.FirstOrDefault(s => s.Id == entity.Id);
                        if (efEntity != null && efEntity.Id > 0)
                        {
                            entity.RowId = efEntity.RowId;
                            //update
                            MapToDB(entity, efEntity);

                            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))
                            {
                                //NOTE UTC vs central
                                if (IsValidDate(status.EnvelopeUpdatedDate))
                                {
                                    efEntity.LastUpdated = status.LocalUpdatedDate;
                                }
                                else
                                {
                                    efEntity.LastUpdated = DateTime.Now;
                                }

                                count = context.SaveChanges();
                                if (addingActivity)
                                {
                                    //add log entry
                                    SiteActivity sa = new SiteActivity()
                                    {
                                        ActivityType     = "ConceptScheme",
                                        Activity         = "Import",
                                        Event            = "Update",
                                        Comment          = string.Format("Updated ConceptScheme found by the import. Name: {0}, URI: {1}", entity.Name, entity.Source),
                                        ActivityObjectId = entity.Id
                                    };
                                    new ActivityManager().SiteActivityAdd(sa);
                                }
                            }
                            UpdateParts(entity, ref status);
                            //
                            HandleConceptsSimple(entity, ref status);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, "ConceptSchemeManager.Save()");
            }

            return(isValid);
        }
示例#6
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.ConceptScheme
                                        .FirstOrDefault(s => s.CredentialRegistryId == envelopeId ||
                                                        (s.CTID == ctid)
                                                        );

                    if (efEntity != null && efEntity.Id > 0)
                    {
                        Guid rowId = efEntity.RowId;
                        if (efEntity.OrgId > 0)
                        {
                            Organization org = OrganizationManager.GetForSummary(efEntity.OrgId);
                            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(" ConceptScheme. 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.ConceptScheme.Remove( efEntity );
                        efEntity.EntityStateId = 0;
                        efEntity.LastUpdated   = System.DateTime.Now;
                        int count = context.SaveChanges();
                        if (count > 0)
                        {
                            new ActivityManager().SiteActivityAdd(new SiteActivity()
                            {
                                ActivityType     = "ConceptSchemeProfile",
                                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_CONCEPT_SCHEME, 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 ConceptScheme cannot be deleted as it is being referenced by other items. These associations must be removed before this ConceptScheme can be deleted.";
                    }
                }
            }
            return(isValid);
        }