Exemplo n.º 1
0
        static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    TestCardParameter testCard = null;
                    using (var docConnector = new WindchillDocConnector())
                        using (var testCardConnector = new WindchillTestCardConnector())
                        {
                            Console.WriteLine("Dimmi il productCode");
                            string productCodeX = Console.ReadLine();
                            Console.WriteLine("Dimmi il productRevision");
                            string productRevisionX = Console.ReadLine();
                            testCard = testCardConnector.GetTestCard(productCodeX, productRevisionX);

                            Console.WriteLine(JsonConvert.SerializeObject(testCard));
                            //Console.WriteLine("ProductCode: ");
                            //string productCode = Console.ReadLine();
                            //Console.WriteLine("101110060");
                            //Console.WriteLine("Revision: ");
                            //string revision = Console.ReadLine();


                            var docs = docConnector.GetDocumentList(productCodeX, productRevisionX);
                            foreach (var doc in docs)
                            {
                                Console.WriteLine(doc.Number + " " + doc.Revision + " " + doc.SoftType);
                                doc.SoftType = "wt.doc.WTDocument";
                                docConnector.DownloadDoc(doc);
                            }
                        }
                    Console.ReadLine();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }
        private DABImportDocuments.Response DABImportDocumentsHandler(DABImportDocuments command)
        {
            var response = new DABImportDocuments.Response();

            var processes        = Platform.ProjectionQuery <Process>().Where(p => p.FinalMaterialId != null).ToDictionary(p => p.Id, p => p.FinalMaterialId.Value);
            var finalMaterialIds = processes.Select(p => p.Value).Distinct().ToList();
            var finalMaterials   = Platform.ProjectionQuery <MaterialDefinition>().Where(m => finalMaterialIds.Contains(m.Id)).ToList();

            WindchillDocConnector docConnector = new WindchillDocConnector();
            var matDocDictionary = new Dictionary <int, List <Guid> >();

            foreach (var finalMat in finalMaterials)
            {
                Platform.Tracer.Write("Siemens-SimaticIT-Trace-UADMRuntime", $"Retrieving documents for FinalMaterial {finalMat.NId} {finalMat.Revision}");
                string revision    = finalMat.Revision == "n/a" ? string.Empty : finalMat.Revision;
                var    docInfoList = docConnector.GetDocumentList(finalMat.NId, revision);
                if (!docInfoList.Any())
                {
                    continue;
                }
                matDocDictionary.Add(finalMat.Id, new List <Guid>());
                foreach (var docInfo in docInfoList)
                {
                    string docNId = $"{docInfo.Revision}_{docInfo.Number}";
                    if (Platform.ProjectionQuery <Document>().Any(doc => doc.FileName == docInfo.Number && doc.Type == docInfo.DocType && doc.NId == doc.NId))
                    {
                        continue;
                    }
                    Platform.Tracer.Write("Siemens-SimaticIT-Trace-UADMRuntime", $"Importing document {docNId} for FinalMaterial {finalMat.NId} {finalMat.Revision}");
                    var docFile = docConnector.DownloadDoc(docInfo);
                    if (docFile == null)
                    {
                        continue;
                    }

                    var createDocInput = new CreateDocument()
                    {
                        Document = new DocumentParameterType
                        {
                            File     = docFile.Content,
                            FileName = docFile.Name,
                            NId      = docNId,
                            Type     = docFile.DocType,
                            MIMEType = GetMimeType(docFile.Name)
                        }
                    };

                    var createDocResponse = Platform.CallCommand <CreateDocument, CreateDocument.Response>(createDocInput);
                    if (!createDocResponse.Succeeded)
                    {
                        response.SetError(createDocResponse.Error.ErrorCode, createDocResponse.Error.ErrorMessage);
                        return(response);
                    }
                    matDocDictionary[finalMat.Id].Add(createDocResponse.Id.Value);
                }
                foreach (var mat in matDocDictionary.Keys)
                {
                    var processIds = processes.Where(kvp => kvp.Value.Equals(mat)).Select(kvp => kvp.Key);
                    foreach (var procId in processIds)
                    {
                        var opIds = Platform.ProjectionQuery <Process>().Include(p => p.Operations).Where(p => p.Id.Equals(procId)).SelectMany(p => p.Operations).Select(o => o.Id);
                        foreach (var opId in opIds)
                        {
                            if (!Platform.ProjectionQuery <OperationDependency>().Any(od => od.ContextProcess_Id.Equals(procId) && od.ToOperation_Id.Equals(opId)))
                            {
                                var createDocAssoc = Platform.CallCommand <LinkDocumentListToOperation, LinkDocumentListToOperation.Response>(new LinkDocumentListToOperation
                                {
                                    ListOfDocumentId = matDocDictionary[mat],
                                    OperationId      = opId
                                });
                                if (!createDocAssoc.Succeeded)
                                {
                                    response.SetError(createDocAssoc.Error.ErrorCode, createDocAssoc.Error.ErrorMessage);
                                    return(response);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            return(response);
        }