EvaldoMaciel

Dataset IBGE - Municípios

Nov 23rd, 2021 (edited)
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* https://servicodados.ibge.gov.br/api/v1/localidades/municipios */
  2.  
  3. function defineStructure() {
  4.     addColumn("cod", DatasetFieldType.STRING);
  5.     addColumn("municipio");
  6.     addColumn("uf");
  7.     addColumn("uf_sigla");
  8.     addColumn("regiao");
  9.     setKey(["cod", "municipio"]);
  10.     addIndex(["cod"]);
  11.     addIndex(["cod", "municipio", "uf_sigla"]);
  12. }
  13.  
  14. function createDataset(fields, constraints, sortFields) {
  15.     var dataset = DatasetBuilder.newDataset();
  16.     dataset.addColumn("cod");
  17.     dataset.addColumn("municipio");
  18.     dataset.addColumn("uf");
  19.     dataset.addColumn("uf_sigla");
  20.     dataset.addColumn("regiao");
  21.  
  22.     var cidadesRetorno = getDados();
  23.  
  24.     for (var index = 0; index < cidadesRetorno.length; index++) {
  25.         dataset.addRow(cidadesRetorno[index]);             
  26.     }
  27.  
  28.     return dataset;
  29. }
  30.  
  31. function onSync(lastSyncDate) {
  32.     var dataset = DatasetBuilder.newDataset();
  33.  
  34.     var cidadesRetorno = getDados();
  35.  
  36.     for (var index = 0; index < cidadesRetorno.length; index++) {
  37.         dataset.addOrUpdateRow(cidadesRetorno[index]);             
  38.     }
  39.  
  40.     return dataset;
  41. }
  42.  
  43. function getDados() {
  44.     var clientService = fluigAPI.getAuthorizeClientService();
  45.     var data = {
  46.         companyId: String(getValue("WKCompany")),
  47.         serviceCode: 'IBGE',
  48.         endpoint: '/api/v1/localidades/municipios',
  49.         method: 'get',
  50.     }
  51.     var vo = clientService.invoke(JSON.stringify(data));
  52.  
  53.     var objeto = JSON.parse(vo.getResult());
  54.  
  55. //  var cidades = new java.util.ArrayList();
  56.     var cidades = [];
  57.  
  58.     if (vo.getHttpStatusResult() == 200) {
  59.         if (vo.getResult() == null || vo.getResult().isEmpty()) {
  60.             cidades.push([
  61.                 "Retorno vazio ou com erro",
  62.                 "Retorno vazio ou com erro"
  63.             ]);
  64.             return cidades;
  65.         } else {
  66.             for (var index = 0; index < objeto.length; index++) {
  67.                 cidades.push([
  68.                     String(objeto[index].id),
  69.                     objeto[index].nome,
  70.                     objeto[index].microrregiao.mesorregiao.UF.nome,
  71.                     objeto[index].microrregiao.mesorregiao.UF.sigla,
  72.                     objeto[index].microrregiao.mesorregiao.UF.regiao.nome
  73.                 ]);
  74.             }
  75.             return cidades;
  76.         }
  77.     }
  78. }
Add Comment
Please, Sign In to add comment