private ICollection <UDSDocument> AddDocumentsByType(Database db, Guid udsId, Document model, UDSDocumentType docType) { IList <UDSDocument> documents = new List <UDSDocument>(); if (model != null && model.Instances != null) { DocumentInstance instance = model.Instances.First(); UDSDocument doc = new UDSDocument() { UDSDocumentId = Guid.NewGuid(), UDSId = udsId, DocumentName = !model.AllowMultiFile ? instance.DocumentName : string.Empty, IdDocument = Guid.Parse(instance.StoredChainId), DocumentType = (short)docType, DocumentLabel = model.Label }; doc.Insert(db, _builder.DbSchema, _builder.UDSDocumentsTableName); documents.Add(doc); } return(documents); }
private ICollection <UDSDocument> UpdateDocumentsByType(Database db, Guid udsId, Document model, UDSRelations existingRels, UDSDocumentType docType, Action <Guid> detachAction) { string deleteAllSQL = string.Format(@"delete from {0}.{1} where {2}=@0 and DocumentType=@1", _builder.DbSchema, _builder.UDSDocumentsTableName, UDSTableBuilder.UDSFK); string deleteItemSQL = string.Format(@"delete from {0}.{1} where {2}=@0 and DocumentType=@1 and {3}=@2", _builder.DbSchema, _builder.UDSDocumentsTableName, UDSTableBuilder.UDSFK, UDSTableBuilder.UDSDocumentsFK); List <UDSDocument> documents = new List <UDSDocument>(); if (model != null && model.Instances != null) { //situazione db IEnumerable <Guid> existings = existingRels.Documents.Where(p => p.DocumentType == (short)docType).Select(p => p.IdDocument); //elimino gli elementi per l'aggiornamento db.Execute(deleteAllSQL, udsId, docType); //elementi da aggiungere DocumentInstance instance = model.Instances.First(); Guid idDocument = Guid.Parse(instance.StoredChainId); string documentName = instance.DocumentName; if (string.IsNullOrEmpty(documentName) && docType == UDSDocumentType.Document) { documentName = existingRels.Documents.SingleOrDefault(f => f.IdDocument == idDocument)?.DocumentName; } UDSDocument doc = new UDSDocument() { UDSDocumentId = Guid.NewGuid(), UDSId = udsId, IdDocument = idDocument, DocumentName = !model.AllowMultiFile ? documentName : existingRels.Documents.FirstOrDefault(f => f.DocumentType == (short)docType)?.DocumentName, DocumentType = (short)docType }; doc.Insert(db, _builder.DbSchema, _builder.UDSDocumentsTableName); doc.DocumentLabel = model.Label; documents.Add(doc); } else { db.Execute(deleteAllSQL, udsId, docType); //elimina le relazioni tipo Document Guid?chainId = existingRels.Documents.Where(p => p.DocumentType == (short)docType).Select(s => s.IdDocument).FirstOrDefault(); if (chainId.HasValue && chainId.Value != Guid.Empty) { detachAction(chainId.Value); } } return(documents); }