/// <summary> /// Inserisce un nuovo Archive /// </summary> /// <param name="Archive">DocumentArchive da inserire</param> public static Guid AddArchive(DocumentArchive Archive) { // IsLegal è sempre true o false e LastIdBiblos non è mai null if (String.IsNullOrEmpty(Archive.Name)) { throw new Exception("Impossibile proseguire. Name deve essere valorizzato"); } return(DbAdminProvider.AddArchive(Archive)); }
public static Guid CloneArchive(string templateName, string archiveName) { DocumentArchive archiveToClone = null, existingArchive; List <DocumentAttribute> attributesAdded = new List <DocumentAttribute>(); List <DocumentStorageArea> storageAreasAdded = new List <DocumentStorageArea>(); List <DocumentStorage> storagesAdded = new List <DocumentStorage>(); try { if (string.IsNullOrWhiteSpace(archiveName)) { throw new BiblosDS.Library.Common.Exceptions.Archive_Exception(string.Format("Destination archive name is null or empty for source archive {0}.", templateName)); } archiveToClone = ArchiveService.GetArchiveByName(templateName); if (archiveToClone == null) { throw new BiblosDS.Library.Common.Exceptions.Archive_Exception(string.Format("Archive with name {0} not found.", templateName)); } //Il nome può essere lungo al massimo 63 caratteri. if (archiveName.Length > 63) { archiveName = archiveName.Remove(63); } //Controlla se per caso l'archivio che si desidera clonare è già presente in banca dati. existingArchive = ArchiveService.GetArchiveByName(archiveName); if (existingArchive != null) { return(existingArchive.IdArchive); } Guid idArchiveToClone = archiveToClone.IdArchive; archiveToClone.IdArchive = Guid.NewGuid(); archiveToClone.Name = Regex.Replace(archiveName, @"[^a-zA-Z0-9]", "-"); if (!string.IsNullOrEmpty(archiveToClone.PathTransito) && archiveToClone.PathTransito.LastIndexOf('\\') > 0) { archiveToClone.PathTransito = Path.Combine(archiveToClone.PathTransito.Substring(0, archiveToClone.PathTransito.LastIndexOf('\\')), archiveToClone.Name); } if (!string.IsNullOrEmpty(archiveToClone.PathPreservation) && archiveToClone.PathPreservation.LastIndexOf('\\') > 0) { archiveToClone.PathPreservation = Path.Combine(archiveToClone.PathPreservation.Substring(0, archiveToClone.PathPreservation.LastIndexOf('\\')), archiveToClone.Name); } DbAdminProvider.AddArchive(archiveToClone); //Clone attributes var attributes = AttributeService.GetAttributesFromArchive(idArchiveToClone); foreach (var attributeItem in attributes) { var attribute = new DocumentAttribute { IdAttribute = Guid.NewGuid(), Archive = archiveToClone, AttributeType = attributeItem.AttributeType, ConservationPosition = attributeItem.ConservationPosition, DefaultValue = attributeItem.DefaultValue, Description = attributeItem.Description, Disabled = attributeItem.Disabled, Format = attributeItem.Format, IsAutoInc = attributeItem.IsAutoInc, IsChainAttribute = attributeItem.IsChainAttribute, IsEnumerator = attributeItem.IsEnumerator, IsMainDate = attributeItem.IsMainDate, IsRequired = attributeItem.IsRequired, IsRequiredForPreservation = attributeItem.IsRequiredForPreservation, IsSectional = attributeItem.IsSectional, IsUnique = attributeItem.IsUnique, IsVisible = attributeItem.IsVisible, IsVisibleForUser = attributeItem.IsVisibleForUser, KeyFilter = attributeItem.KeyFilter, KeyFormat = attributeItem.KeyFormat, KeyOrder = attributeItem.KeyOrder, MaxLenght = attributeItem.MaxLenght, Mode = attributeItem.Mode, Name = attributeItem.Name, Validation = attributeItem.Validation }; DbAdminProvider.AddAttribute(attribute); attributesAdded.Add(attribute); } //Retrive storage var storage = DbProvider.GetStoragesActiveFromArchive(idArchiveToClone); foreach (var storageItem in storage) { var storageArea = DbProvider.GetStorageAreaActiveFromStorage(storageItem.IdStorage); if (storageArea.Any(x => x.Archive != null && x.Archive.IdArchive == idArchiveToClone)) { storageArea = new BindingList <DocumentStorageArea>(storageArea.Where(x => x.Archive != null && x.Archive.IdArchive == idArchiveToClone).ToList()); } foreach (var storageAreaItem in storageArea) { var newStorageArea = new DocumentStorageArea { IdStorageArea = Guid.NewGuid(), Archive = archiveToClone, Storage = storageItem, Enable = true, MaxFileNumber = storageAreaItem.MaxFileNumber, MaxSize = storageAreaItem.MaxSize, Name = archiveToClone.Name, Path = archiveToClone.Name, Priority = storageAreaItem.Priority, Status = storageAreaItem.Status }; DbAdminProvider.AddStorageArea(newStorageArea); storageAreasAdded.Add(newStorageArea); } DbAdminProvider.AddArchiveStorage(new DocumentArchiveStorage { Storage = storageItem, Archive = archiveToClone, Active = true }); storagesAdded.Add(storageItem); } return(archiveToClone.IdArchive); } catch (Exception) { try { if (archiveToClone != null) { foreach (var item in storagesAdded) { DbAdminProvider.DeleteArchiveStorage(new DocumentArchiveStorage { Archive = archiveToClone, Storage = item });; } foreach (var item in storageAreasAdded) { DbAdminProvider.DeleteStorageArea(item.IdStorageArea); } foreach (var item in attributesAdded) { DbAdminProvider.DeleteAttribute(item.IdAttribute, false); } DbAdminProvider.DeleteArchive(archiveToClone); } } catch (Exception ex) { logger.Warn(ex); throw; } throw; } }