/// <summary> /// Get default reference from revision /// </summary> /// <param name="userContext"></param> /// <param name="newObject"></param> /// <param name="taxonRevision"></param> /// <param name="referencesToAdd"> </param> /// <returns></returns> public static ReferenceRelationList GetDefaultReferences(IUserContext userContext, object newObject, ITaxonRevision taxonRevision, ReferenceRelationList referencesToAdd) { // Check if list is null the create a new one var referencesToAddTemp = new ReferenceRelationList(); if (referencesToAdd.IsNotNull()) { referencesToAddTemp = referencesToAdd; } if (taxonRevision.GetReferences(userContext).IsNotEmpty()) { // Set all revision references of type Source as references foreach (var referenceRelation in taxonRevision.GetReferences(userContext)) { if (referenceRelation.Type.Id == (int)ReferenceRelationTypeId.Source) { IReference newReference = new ArtDatabanken.Data.Reference(); newReference.Id = referenceRelation.ReferenceId; IReferenceRelation newReferenceRelation = GetReferenceRelation(newObject, newReference); if (newReferenceRelation.IsNotNull()) { referencesToAddTemp.Add(newReferenceRelation); } } } // Get first reference of type Used in and set as Source reference. if (referencesToAddTemp.Count == 0) { IReference newReference = new ArtDatabanken.Data.Reference(); newReference.Id = taxonRevision.GetReferences(userContext).First().ReferenceId; IReferenceRelation newReferenceRelation = GetReferenceRelation(newObject, newReference); if (newReferenceRelation.IsNotNull()) { referencesToAddTemp.Add(newReferenceRelation); } } } // If no references found set dyntaxa as reference else { IReference newReference = new ArtDatabanken.Data.Reference(); newReference.Id = DyntaxaReference; IReferenceRelation newReferenceRelation = GetReferenceRelation(newObject, newReference); if (newReferenceRelation.IsNotNull()) { referencesToAddTemp.Add(newReferenceRelation); } } return(referencesToAddTemp); }
/// <summary> /// The create new reference. /// </summary> /// <param name="refModel"> /// The ref model. /// </param> public void CreateNewReference(ReferenceViewModel refModel) { IReference reference = new ArtDatabanken.Data.Reference(); reference.Id = refModel.Id; reference.Name = refModel.Name; reference.Year = refModel.Year; reference.Title = refModel.Text; using (ITransaction transaction = user.StartTransaction(30)) { CoreData.ReferenceManager.CreateReference(user, reference); transaction.Commit(); } }
/// <summary> /// Gets the reference relations that will be created. /// </summary> /// <param name="guid">The unique identifier.</param> /// <param name="newReferences">The new references.</param> /// <param name="applyMode">The apply mode.</param> /// <returns>A list with all reference relations that will be created.</returns> private ReferenceRelationList GetReferenceRelationsThatWillBeCreated(string guid, ReferenceViewModel[] newReferences, ReferenceApplyMode applyMode) { ReferenceRelationList referencesToAdd = new ReferenceRelationList(); IEnumerable <IReferenceRelation> existingReferenceRelations = CoreData.ReferenceManager.GetReferenceRelations(user, guid); if (applyMode == ReferenceApplyMode.ReplaceOnlySourceInUnderlyingTaxa) { existingReferenceRelations = existingReferenceRelations.Where(referenceRelation => referenceRelation.Type.Id == (int)ReferenceRelationTypeId.Source); } foreach (ReferenceViewModel newReference in newReferences) { // If the new reference isn't source and we are in Replace only source in underlying mode, don't add this reference. if (applyMode == ReferenceApplyMode.ReplaceOnlySourceInUnderlyingTaxa && newReference.UsageTypeId != (int)ReferenceRelationTypeId.Source) { continue; } bool referenceAlreadyExists = existingReferenceRelations.Any(existingReferenceRelation => newReference.Id == existingReferenceRelation.ReferenceId && newReference.UsageTypeId == existingReferenceRelation.Type.Id); if (!referenceAlreadyExists) { IReference reference = new ArtDatabanken.Data.Reference(); reference.Id = newReference.Id; ReferenceRelation newReferenceRelation = new ReferenceRelation() { RelatedObjectGuid = guid, Type = CoreData.ReferenceManager.GetReferenceRelationType(user, newReference.UsageTypeId), Reference = null, ReferenceId = newReference.Id }; referencesToAdd.Add(newReferenceRelation); } } return(referencesToAdd); }