Advertisement
Guest User

ds_publicaArtigo

a guest
Feb 19th, 2020
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function getConfig() {
  2.     return {
  3.         community: "licoes-aprendidas"
  4.     }
  5. }
  6.  
  7. function createDataset(fields, constraints, sortFields) {
  8.     var datasetLicoesAprend = "ds_licoesAprend";
  9.     var datasetPaiFilhoSeguranca = "ds_paiFilhoSeguranca";
  10.  
  11.     var numero_solicitacao = 0;
  12.     var dataset = DatasetBuilder.newDataset();
  13.  
  14.     dataset.addColumn('id');
  15.     dataset.addColumn('version');
  16.  
  17.     // var gson = new com.google.gson.Gson();
  18.     var parser = new com.google.gson.JsonParser();
  19.  
  20.     if (constraints != null) {
  21.         for (i = 0; i < constraints.length; i++) {
  22.             if (constraints[i].fieldName == "numero_solicitacao" && constraints[i].initialValue != "") {
  23.                 numero_solicitacao = constraints[i].initialValue;
  24.             }
  25.         }
  26.     }
  27.  
  28.     var resultado = DatasetFactory.getDataset(datasetLicoesAprend, null, [
  29.         DatasetFactory.createConstraint("numero_solicitacao", numero_solicitacao, numero_solicitacao, ConstraintType.MUST)
  30.     ], null);
  31.     try {
  32.         var clientService = fluigAPI.getAuthorizeClientService();
  33.  
  34.         var link_src = '/api/public/ecm/document/documentfile/';
  35.  
  36.         var img_principal_html = '<div class="row"><div class="col-md-12" id="img-principal" style="width: calc(100% - 30px); display: flex; justify-content: center;"><img src="' + resultado.getValue('0', 'linkDownload_1') + '" style="height: 300px;"></div></div>';
  37.         var html = '<div class="row" style="width: 100%; margin: 30px 0; float: left; font-family: "Open Sans", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.42857143; color: #58595b;">';
  38.  
  39.         var type_img_principal = resultado.getValue('0', 'img_principal');
  40.         var img_esquerda = "";
  41.         var img_direita = "";
  42.         var img_centro = "";
  43.         var video = "";
  44.         var count = 0;
  45.  
  46.         html += '<div class="row" style="display: flex; justify-content: center;">';
  47.  
  48.         if (type_img_principal == '1') {
  49.             img_principal = resultado.getValue('0', 'idAnexo_1');
  50.             img_esquerda = resultado.getValue('0', 'linkDownload_2');
  51.             img_centro = resultado.getValue('0', 'linkDownload_3');
  52.             img_direita = resultado.getValue('0', 'linkDownload_4');
  53.  
  54.             if (img_esquerda) {
  55.                 count = count + 1;
  56.             }
  57.  
  58.             if (img_centro) {
  59.                 count = count + 1;
  60.             }
  61.  
  62.             if (img_direita) {
  63.                 count = count + 1;
  64.             }
  65.  
  66.             if (img_esquerda != "") {
  67.                 html = html + '<div class="col-md-4" id="img-lado-esquerdo" style="width: calc(100% / ' + count + ' - 30px); float: left; margin: 0 15px;"><img src="' + img_esquerda + '" style="width: auto; max-height: 200px;"></div>';
  68.             }
  69.  
  70.             if (img_centro != "") {
  71.                 html = html + '<div class="col-md-4" id="img-centro" style="width: calc(100% / ' + count + ' - 30px); float: left; margin: 0 15px;"><img src="' + img_centro + '" style="width: auto; max-height: 200px;"></div>';
  72.             }
  73.  
  74.             if (img_direita != "") {
  75.                 html = html + '<div class="col-md-4" id="img-lado-direito" style="width: calc(100% / ' + count + ' - 30px); float: right; margin: 0 15px;"><img src="' + img_direita + '" style="width: auto; max-height: 200px;"></div>';
  76.             }
  77.  
  78.         }
  79.  
  80.         if (type_img_principal == '2') {
  81.  
  82.             img_principal = parseInt(resultado.getValue('0', 'idAnexo_2'));
  83.             img_esquerda = resultado.getValue('0', 'linkDownload_3');
  84.             video = resultado.getValue('0', 'linkDownload_1');
  85.             img_direita = resultado.getValue('0', 'linkDownload_4');
  86.  
  87.             if (img_esquerda) {
  88.                 count = count + 1;
  89.             }
  90.  
  91.             if (video) {
  92.                 count = count + 1;
  93.             }
  94.  
  95.             if (img_direita) {
  96.                 count = count + 1;
  97.             }
  98.  
  99.             if (img_esquerda != "") {
  100.                 html = html + '<div class="col-md-4" id="img-lado-esquerdo" style="width: calc(100% / ' + count + ' - 30px); float: left; margin: 0 15px;"><div><img src="' + img_esquerda + '" style="width: auto; max-height: 200px;"></div></div>';
  101.             }
  102.  
  103.             if (video != "") {
  104.                 html = html + '<div class="col-md-4" id="video-preview" style="width: calc(100% / ' + count + ' - 30px); float: left; margin: 0 15px;"><div><video width="100%" controls="controls" src="' + video + '"><source type="video/mp4" src="' + link_src + video + '/1000' + '"><source type="video/avi" src=""><source type="video/3gp" src="' + link_src + video + '/1000' + '"></div></div>';
  105.             }
  106.  
  107.             if (img_direita != "") {
  108.                 html = html + '<div class="col-md-4" id="img-lado-direito" style="width: calc(100% / ' + count + ' - 30px); float: left; margin: 0 15px;"><div><img src="' + img_direita + '" style="width: auto; max-height: 200px;"></div></div>';
  109.             }
  110.         }
  111.  
  112.         html = html + "</div></div>";
  113.  
  114.         var tipoSolicitacao = resultado.getValue('0', 'tipo_solicitacao');
  115.         var TIPOS_SOLICITACAO = {
  116.             novo: 1,
  117.             atualizar: 2
  118.         }
  119.  
  120.         // Informações do novo artigo
  121.         var nArticleId = parseInt(resultado.getValue('0', 'id_artigo_publicado'));
  122.         var nFolderId = parseInt(resultado.getValue('0', 'pasta_armazenamento'));
  123.         var sTitle = new String(resultado.getValue('0', 'titulo_artigo'));
  124.         var sContent = new String('<div style="word-wrap: break-word">' + resultado.getValue('0', 'artigo_sem_formatacao') + img_principal_html + html + '</div>')
  125.         var sTags = new String(resultado.getValue('0', 'tags_busca'));
  126.  
  127.         var createVO;
  128.  
  129.         if (tipoSolicitacao == TIPOS_SOLICITACAO.novo) {
  130.             log.info("CRIANDO NOVO ARTIGO");
  131.  
  132.             // Cria um rascunho
  133.             createVO = createDraft(clientService, nFolderId, sTitle, sContent, sTags, img_principal, null, '');
  134.             dataset.addRow(['create', createVO]);
  135.  
  136.             // Atribui o novo ID do artigo
  137.             var result = parser.parse(createVO).getAsJsonObject();
  138.             nArticleId = parseInt(result.getAsJsonObject("content").get("id"));
  139.         } else {
  140.             log.info("ATUALIZANDO ARTIGO JÁ EXISTENTE");
  141.  
  142.             // Atualiza sem incluir a capa
  143.             update(clientService, nArticleId, nFolderId, sTitle, sContent, sTags, false);
  144.         }
  145.  
  146.         // Muda a capa
  147.         changeCover(clientService, nArticleId, nFolderId, sTitle, sContent, sTags, true, img_principal);
  148.  
  149.         // Configura e aplica a capa
  150.         var updateVO = update(clientService, nArticleId, nFolderId, sTitle, sContent, sTags, false);
  151.  
  152.         // Versão do artigo
  153.         var resultUpdate = parser.parse(updateVO).getAsJsonObject();
  154.         var version = parseInt(resultUpdate.getAsJsonObject("content").get("version"));
  155.         dataset.addRow([nArticleId, version]);
  156.  
  157.         // Segurança
  158.         var seguranca = DatasetFactory.getDataset(datasetPaiFilhoSeguranca, null, [
  159.             DatasetFactory.createConstraint('numero_solicitacao', numero_solicitacao, numero_solicitacao, ConstraintType.MUST)
  160.         ], null);
  161.  
  162.         if (seguranca.rowsCount > 0) {
  163.             var arrayPermissionVo = [];
  164.             var typePermission = { 'user': 1, 'group': 2 }
  165.  
  166.             // Criar o array de Objetos com as permissões e tipo de permissão
  167.             for (var s = 0; s < seguranca.rowsCount; s++) {
  168.                 var attributionType = parseInt(typePermission[seguranca.getValue(s, 'tipo_seguranca')]);
  169.                 var attributionValue = seguranca.getValue(s, 'seguranca_intens');
  170.  
  171.                 if (
  172.                     attributionValue == "Todos" ||
  173.                     attributionValue == "LMS - Todos" ||
  174.                     attributionValue == "Todos os Usuários do Fluig"
  175.                 ) {
  176.                     attributionValue = "all";
  177.                     attributionType = 3;
  178.                 }
  179.  
  180.                 var objSeg = {
  181.                     "attributionType": attributionType,
  182.                     "attributionValue": attributionValue,
  183.                     "securityLevel": parseInt(seguranca.getValue(s, 'nivel_seguranca')),
  184.                     "downloadEnabled": false
  185.                 }
  186.  
  187.                 arrayPermissionVo.push(objSeg);
  188.             }
  189.  
  190.             var permissao = {
  191.                 companyId : getValue("WKCompany") + '',
  192.                 serviceCode : 'SetDocumentPermissions',
  193.                 endpoint : '/public/2.0/documents/setDocumentPermissions',
  194.                 method : 'post',  
  195.                 timeoutService: '100',
  196.                 params: {
  197.                     "documentId": parseInt(nArticleId),
  198.                     "documentPermissionVO": arrayPermissionVo
  199.                 }
  200.             }
  201.  
  202.             clientService.invoke(JSONUtil.toJSON(permissao));
  203.         }
  204.  
  205.     } catch (e) {
  206.         log.info(e);
  207.         dataset.addRow([e.message]);
  208.     }
  209.  
  210.     return dataset;
  211. }
  212.  
  213. function create(clientService, sFolderId, sTitle, sContent, sTags, sCoverId, sCoverName, nId) {
  214.     var config = getConfig();
  215.  
  216.     var data = {
  217.         companyId : getValue("WKCompany") + '',
  218.         serviceCode : 'ArticlesCreate',
  219.         endpoint : '/api/public/2.0/communities/articles/create',
  220.         method : 'post',  
  221.         timeoutService: '100',
  222.         params: {
  223.             "id": '' + nId,
  224.             "alias": '' + config.community,
  225.             "articleCoverVO": {
  226.                 "pictureName": sCoverName,
  227.                 "pictureId": sCoverId
  228.             },
  229.             "categoryId": '' + sFolderId,
  230.             "content": '' + sContent,
  231.             "description": '' + sTitle,
  232.             "expirationDate": "",
  233.             "keyWord": '' + sTags,
  234.             "version": 1000
  235.         }
  236.     };
  237.  
  238.     var vo = clientService.invoke(JSONUtil.toJSON(data));
  239.     return vo.getResult();
  240. }
  241.  
  242. function createDraft(clientService, nFolderId, sTitle, sContent, sTags, sCoverId, sCoverName, nId) {
  243.     var config = getConfig();
  244.  
  245.     var data = {
  246.         companyId : getValue("WKCompany") + '',
  247.         serviceCode : 'CreateDraft',
  248.         endpoint : '/api/public/2.0/communities/articles/createDraft',
  249.         method : 'post',  
  250.         timeoutService: '100',
  251.         params: {
  252.             "id": '' + nId,
  253.             "alias": '' + config.community,
  254.             "articleCoverVO": {
  255.                 "pictureName": sCoverName,
  256.                 "pictureId": sCoverId
  257.             },
  258.             "categoryId": '' + nFolderId,
  259.             "content": '' + sContent,
  260.             "description": '' + sTitle,
  261.             "expirationDate": "",
  262.             "keyWord": '' + sTags,
  263.             "version": 1000
  264.         }
  265.     }
  266.  
  267.     var vo = clientService.invoke(JSONUtil.toJSON(data));
  268.     return vo.getResult();
  269. }
  270.  
  271. function changeCover(clientService, nId, nFolderId, sTitle, sContent, sTags, bDraft, sIdCover) {
  272.     var config = getConfig();
  273.  
  274.     var data = {
  275.         companyId : getValue("WKCompany") + '',
  276.         serviceCode : 'ChangeCover',
  277.         endpoint : '/api/public/2.0/communities/articles/changeCover',
  278.         method : 'post',  
  279.         timeoutService: '100',
  280.         params: {
  281.             "alias": '' + config.community,
  282.             "articleCoverVO": {
  283.                 "pictureId": sIdCover,
  284.             },
  285.             "categoryId": '' + nFolderId,
  286.             "content": '' + sContent,
  287.             "description": '' + sTitle,
  288.             "draft": bDraft,
  289.             "expires": false,
  290.             "id": '' + nId,
  291.             "keyWord": '' + sTags,
  292.             "topicId": "1",
  293.             "userNotify": false,
  294.             "version": 1000
  295.         }
  296.     }
  297.  
  298.     var vo = clientService.invoke(JSONUtil.toJSON(data));
  299.     return vo.getResult();
  300. }
  301.  
  302. function update(clientService, nId, sFolderId, sTitle, sContent, sTags, bDraft) {
  303.     var config = getConfig();
  304.  
  305.     // Medidas um tanto arbitrárias, mas funcionais
  306.     var articleCoverVO = {
  307.         "width": 824,
  308.         "height": 165,
  309.         "coordinateX": 0,
  310.         "coordinateY": 0,
  311.     };
  312.  
  313.     var data = {
  314.         companyId : getValue("WKCompany") + '',
  315.         serviceCode : 'Update',
  316.         endpoint : '/api/public/2.0/communities/articles/update',
  317.         method : 'post',  
  318.         timeoutService: '100',
  319.         params: {
  320.             "alias": '' + config.community,
  321.             "articleCoverVO": articleCoverVO,
  322.             "categoryId": '' + sFolderId,
  323.             "content": '' + sContent,
  324.             "description": '' + sTitle,
  325.             "draft": bDraft,
  326.             "id": nId,
  327.             "keyWord": '' + sTags,
  328.             "topicId": '1',
  329.             "version": 1000
  330.         }
  331.     }
  332.  
  333.     var vo = clientService.invoke(JSONUtil.toJSON(data));
  334.     return vo.getResult();
  335. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement