EvaldoMaciel

Excluir Solicitações

May 6th, 2021
695
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function defineStructure() {
  2.     addColumn("processInstanceId");
  3.     addColumn("processId");
  4.     addColumn("cancelado");
  5.     addColumn("excluido");
  6.     addColumn("doc_excluido");
  7.     setKey(["processInstanceId"]);
  8.     addIndex(["processInstanceId"]);
  9. }
  10.  
  11. function onSync(lastSyncDate) {
  12.     /*function createDataset(fields, constraints, sortFields) {*/
  13.     var dataset = DatasetBuilder.newDataset();
  14.     /*  dataset.addColumn("processInstanceId");
  15.         dataset.addColumn("processId");
  16.         dataset.addColumn("retorno");*/
  17.     try {
  18.         var datasetWorkflowProcess = DatasetFactory.getDataset('workflowProcess', [
  19.             "workflowProcessPK.processInstanceId",
  20.             "processId",
  21.             "cardDocumentId",
  22.             "status"
  23.         ], [
  24.             DatasetFactory.createConstraint('sqlLimit', '2000', '2000', ConstraintType.MUST),
  25.             /*          DatasetFactory.createConstraint('status', '1', '2', ConstraintType.MUST),
  26.                         DatasetFactory.createConstraint('cardDocumentId', '', '', ConstraintType.MUST_NOT),*/
  27.             DatasetFactory.createConstraint('requesterId', 'academy.aluno', 'academy.aluno', ConstraintType.MUST)
  28.         ], ["workflowProcessPK.processInstanceId;asc"]);
  29.  
  30.         for (var index = 0; index < datasetWorkflowProcess.rowsCount; index++) {
  31.             var processInstanceId = datasetWorkflowProcess.getValue(index, "workflowProcessPK.processInstanceId");
  32.             var processId = datasetWorkflowProcess.getValue(index, "processId");
  33.             var status = String(datasetWorkflowProcess.getValue(index, "status"));
  34.             var cardDocumentId = datasetWorkflowProcess.getValue(index, "cardDocumentId");
  35.             var total = datasetWorkflowProcess.rowsCount;
  36.             var cancelado = null;
  37.             var excluiuDocumento = null;
  38.             if (status != "1" && status != "2") {
  39.                 cancelado = cancelar(1, "academy.aluno", "academy.aluno", parseInt(processInstanceId));
  40.             }
  41.             var excluiuSolicitacao = excluirSolicitacao(parseInt(processInstanceId));
  42.             if (cardDocumentId != undefined && cardDocumentId != null && cardDocumentId != "") {
  43.                 excluiuDocumento = excluirDocumento(1, "academy.aluno", "academy.aluno", "delete", cardDocumentId);
  44.                 excluiuDocumento = String(excluiuDocumento) + " - " + String(cardDocumentId);
  45.             }
  46.  
  47.             log.info(String(index) + ' de ' + total
  48.                 + "\nCancelando: " + processId + ' - ' + String(processInstanceId)
  49.                 + "\ncancelado: " + cancelado
  50.                 + "\nexcluiuSolicitacao: " + excluiuSolicitacao
  51.                 + "\ncardDocumentId: " + cardDocumentId
  52.                 + "\nexcluiuDocumento: " + excluiuDocumento
  53.                 + "\nstatus: " + status);
  54.  
  55.             dataset.addOrUpdateRow([String(processInstanceId), processId, cancelado, excluiuSolicitacao, excluiuDocumento]);
  56.         }
  57.     } catch (e) {
  58.         dataset = DatasetBuilder.newDataset();
  59.         dataset.addOrUpdateRow([e.message, e.lineNumber]);
  60.         //dataset.addRow([e.message, e.lineNumber]);
  61.     }
  62.  
  63.     return dataset;
  64. }
  65.  
  66. function cancelar(company, user, password, processInstanceId) {
  67.  
  68.     // Inicia o serviço
  69.     var wep = ServiceManager.getServiceInstance("ECMWorkflowEngineService");
  70.     var wel = wep.instantiate("com.totvs.ECMWorkflowEngineServiceService");
  71.     var wes = wel.getWorkflowEngineServicePort();
  72.  
  73.     try {
  74.         resultado = wes.cancelInstance(user, password, company, processInstanceId, user, "Cancelado via dataset");
  75.         return resultado;
  76.     }
  77.     catch (e) {
  78.         log.error("ERRO: " + e.message);
  79.         return "ERRO: " + e.message;
  80.     }
  81. }
  82.  
  83. function excluirSolicitacao(nroSolicitacao) {
  84.     //  https://lab.fluig.com/ecm/api/rest/ecm/processdelete/getInstancesToDelete
  85.     try {
  86.         var params = { "selectedRows": [String(nroSolicitacao)] }
  87.  
  88.         var clientService = fluigAPI.getAuthorizeClientService();
  89.         var data = {
  90.             companyId: String(getValue("WKCompany")),
  91.             serviceCode: 'fluig',
  92.             endpoint: '/ecm/api/rest/ecm/processdelete/deleteInstances',
  93.             method: 'POST',
  94.             // Conteúdo do JSON que será enviado no POST
  95.             params: params,
  96.             // Aqui você pode incluir algum Header se necessário
  97.             headers: {}
  98.         }
  99.  
  100.         var vo = clientService.invoke(JSON.stringify(data));
  101.         var objeto2 = JSON.parse(vo.getResult())
  102.  
  103.         return objeto2.content;
  104.     }
  105.     catch (e) {
  106.         log.error("ERRO: " + e.message);
  107.         return "ERRO: " + e.message;
  108.     }
  109. }
  110.  
  111. function excluirDocumento(company, user, password, action, cardId) {
  112.     try {
  113.         var properties = {};
  114.         var result = null;
  115.         properties["disable.chunking"] = "true";
  116.         properties["log.soap.messages"] = "true";
  117.  
  118.         /* Create */
  119.         var serviceManager = ServiceManager.getService("ECMCardService");
  120.         var serviceInstance = serviceManager.instantiate("com.totvs.technology.ecm.dm.ws.ECMCardServiceService");
  121.         var service = serviceInstance.getCardServicePort();
  122.         var customClient = serviceManager.getCustomClient(service, "com.totvs.technology.ecm.dm.ws.CardService", properties);
  123.  
  124.         if (action == "delete") {
  125.             result = customClient.deleteCard(parseInt(company), String(user), String(password), parseInt(cardId));
  126.         }
  127.         if (result.getItem().get(0).getWebServiceMessage().equals("ok")) {
  128.             return String(result.getItem().get(0).getWebServiceMessage());
  129.         }
  130.         else {
  131.             log.error("Não foi: " + result.getItem().get(0).getWebServiceMessage());
  132.             return "Não foi: " + result.getItem().get(0).getWebServiceMessage();
  133.         }
  134.     }
  135.     catch (e) {
  136.         log.error("ERRO: " + e.message + " - Linha: " + e.lineNumber);
  137.         return "ERRO: " + e.message + " - Linha: " + e.lineNumber;
  138.     }
  139. }
RAW Paste Data