Example #1
0
        internal ICollection <UDSDocument> UpdateDocuments(Database db, Guid udsId, UDSRelations existingRels, Action <Guid> detachAction)
        {
            List <UDSDocument> documents = new List <UDSDocument>();

            //non ci sono documenti elimino tutti quelli collegati
            if (_uds.Model.Documents == null)
            {
                db.Execute(SQLDeleteRelationsString(_builder.UDSDocumentsTableName), udsId);
                return(documents);
            }

            documents.AddRange(UpdateDocumentsByType(db, udsId, _uds.Model.Documents.Document, existingRels, UDSDocumentType.Document, detachAction));
            documents.AddRange(UpdateDocumentsByType(db, udsId, _uds.Model.Documents.DocumentAnnexed, existingRels, UDSDocumentType.DocumentAnnexed, detachAction));
            documents.AddRange(UpdateDocumentsByType(db, udsId, _uds.Model.Documents.DocumentAttachment, existingRels, UDSDocumentType.DocumentAttachment, detachAction));
            documents.AddRange(UpdateDocumentsByType(db, udsId, _uds.Model.Documents.DocumentDematerialisation, existingRels, UDSDocumentType.Dematerialisation, detachAction));

            return(documents);
        }
Example #2
0
        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);
        }