CA Service Management

 View Only

update_lrel 

Jan 03, 2019 05:22 AM

SPEL API methods

update_lrel

 

This method allows to create or delete lrel relation

 

Definition:

update_lrel( int, string, string, int, int, ... ) ;

int - operation type: 0 = add relation; 1 = delete relation

string - persistent_id of the object that contains lrel relationship

string - name of the lrel attribute

int - override lock

int - defines which parameter from this parameter is first that defines attribute name, usually 0 (zero means that next parameter contains first attribute name to update)

... - persistent_id of object to relate

 

working example:

//Adds asset to the change
send_wait(0, top_object(), "call_attr", "api", "update_lrel", 0, "chg:429903", "asset", 1, 0, "nr:8B0E0521F447724EAF8BAAE0DE5936C3";
                  

Statistics
0 Favorited
18 Views
0 Files
0 Shares
0 Downloads

Tags and Keywords

Comments

Jan 03, 2019 08:45 AM

This is my custom version:

 

string z_update_lrel(string zs_persid, string zs_factory, string zs_lrel_factory, string zs_lrel_add_persids, string zs_lrel_remove_persids, int zi_debug, string zs_method)
{
     ////////////////////////////////////////////////////////////////////////
     // Metodo:           z_update_lrel
     // Autor:               Daniel Becker Bighelini
     // Criado em:          22/10/2015
     // Modificado por:     Daniel Becker Bighelini
     // Modificado em:     26/10/2015
     //
     // Parametros de entrada:
     // ARG #0 : (string) zs_persid                    Define o persid do objeto
     // ARG #1 : (string) zs_factory                    Define a factory do objeto
     // ARG #2 : (string) zs_lrel_factory          Define a factory LREL que se relaciona com a factory principal
     // ARG #3 : (string) zs_lrel_add_persids     Define os persids que serao adicionados
     // ARG #4 : (string) zs_lrel_remove_persids     Define os persids que serao removidos
     // ARG #5 : (int)    zi_debug                    Nivel de depuracao da funcao 0-nenhum / 1-resumido / 2-detalhado
     // ARG #6 : (string) zs_method                    Define o nome do metodo que esta chamando este metodo
     //
     // Forma de funcionamento:
     //          1) Armazena os persids para adicionar e remover em dois arrays;
     //          2) Identifica o usuario logado;
     //          3) Localiza o atributo na factory que referencia um objeto LREL;
     //          4) Inspeciona cada atributo envolvido para adicionar/remover corretamente os registros;
     //          5) Adiciona/Remove registro(s) no objeto LREL;
     //
     // Resultado:
     //          Adiciona e/ou remove registros em um objeto LREL.
     //
     // Exemplo:
     //          z_update_lrel('nr:E43612BA0A13E34892C03D1ACF420864', 'nr', 'cnt', 'cnt:ED4D469F18BC4943B940AB934D020C6D;cnt:1B947EECD09DB148ABE8675FED5D006D', '', 1, 'teste de chamada')
     ////////////////////////////////////////////////////////////////////////
     string zs_msg;
     int zi_i, zi_j, zi_start;
     
     ///////////////////////////////////////////////////////////////////////////////////////
    // Inicio do codigo
     zi_start                    = (int) now();
     zs_method                    += " z_update_lrel";
     zs_msg                         = '';

     // Entrada para debug mode
     if (zi_debug > 0) {
          z_start_block(zi_debug, zs_method);
          logf(SIGNIFICANT, "%s Quant. de argumentos : %d", zs_method, argc);
          for (zi_i=0; zi_i<argc; zi_i++) {
               logf(SIGNIFICANT, "%s ARG #%s : %s", zs_method, zi_i, argv[zi_i]);
          }
     } else {
          logf(SIGNIFICANT, "%s '%s'", zs_method, zs_persid);
     }

     ////////////////////////////////////////////////////
     // Armazenando os PERSIDS nos respectivos ARRAYS
     ////////////////////////////////////////////////////
     string zs_add_persids[50], zs_remove_persids[50];
     int zi_add_persids_count, zi_remove_persids_count;
     zi_add_persids_count = split(zs_add_persids, zs_lrel_add_persids, ";");
     zi_remove_persids_count = split(zs_remove_persids, zs_lrel_remove_persids, ";");
     if (zi_debug > 0) {
          logf(SIGNIFICANT, "%s Registros ADICIONADOS: %d", zs_method, zi_add_persids_count);
          logf(SIGNIFICANT, "%s Registros REMOVIDOS  : %d", zs_method, zi_remove_persids_count);
     }
     
     ////////////////////////////////////////////////////
     // Identificando USUARIO LOGADO
     ////////////////////////////////////////////////////
     if (zi_debug > 1) logf(SIGNIFICANT, "%s Identificando usuario logado...", zs_method);
     send_wait(0, top_object(), "call_attr", "cnt", "current_user_id");
     if (msg_error()) {
          zs_msg = format("%s ERRO ao identificar o usuario logado: '%s'", zs_method, msg[0]);
          logf(ERROR, "%s", zs_msg);
          return zs_msg;
     }
     uuid zu_who;
     zu_who = msg[0];
     if (zi_debug > 0) logf(SIGNIFICANT, "%s Usuario LOGADO       : '%s'", zs_method, zu_who);
     
     ////////////////////////////////////////////////////
     // Consultando os atributos da factory para localizar o atributo que referencia o objeto LREL estrangeiro
     ////////////////////////////////////////////////////
     if (zi_debug > 0) logf(SIGNIFICANT, "%s Consultando os atributos da factory '%s'...", zs_method, zs_factory);
     send_wait(0, top_object(), "call_attr", zs_factory, "dob_attr_list", "type_info", "extended");
     if (msg_error()) {
          zs_msg = format("%s ERRO ao consultar a factory '%s': %s", zs_method, zs_factory, msg[0]);
          logf(ERROR, "%s", zs_msg);
          return zs_msg;
     }

    int zi_args_count, zi_attrs_count;
     zi_args_count = msg_length();
    zi_attrs_count = msg[0];
     
     if (zi_debug > 0) {
          logf(SIGNIFICANT, "%s Numero de ARGUMENTOS : %d", zs_method, zi_args_count);
          logf(SIGNIFICANT, "%s Numero de ATRIBUTOS  : %d", zs_method, zi_attrs_count);
     }

     for (zi_i=1; zi_i<zi_args_count; zi_i=zi_i) {
          string zs_name, zs_datatype;
          zs_name = msg[zi_i];
          zs_datatype = msg[zi_i+1];
          
          ////////////////////////////////////////////////////
          // IDENTIFICA se EH um atributo LREL
          ////////////////////////////////////////////////////
          if (zs_datatype == "QREL" && msg[zi_i+10] != "") {
               if (msg[zi_i+10] == zs_lrel_factory) { // Achei o atributo LREL da factory que referencia a LREL_FACTORY
                    int zi_lrel_list_count;
                    string zs_lrel_factory_producer, zs_lrel_domset, zs_lrel_wc, zs_lrel_wc_new, zs_lrel_attr_key, zs_lrel_parent_attr, zs_lrel_child_attr;
                    zs_lrel_factory_producer = msg[zi_i+2];
                    zs_lrel_wc = msg[zi_i+3];
                    zs_lrel_attr_key = msg[zi_i+4];
                    zs_lrel_domset = msg[zi_i+5];
                    zs_lrel_parent_attr = msg[zi_i+9];
                    zs_lrel_child_attr = msg[zi_i+10];
                    if (zi_debug > 0) {
                         logf(SIGNIFICANT, "%s PERSID               : '%s'", zs_method, zs_persid);
                         logf(SIGNIFICANT, "%s LREL_ADD_PERSIDS     : '%s'", zs_method, zs_lrel_add_persids);
                         logf(SIGNIFICANT, "%s LREL_REMOVE_PERSIDS  : '%s'", zs_method, zs_lrel_remove_persids);
                         logf(SIGNIFICANT, "%s Atributo             : '%s'", zs_method, zs_name);
                         logf(SIGNIFICANT, "%s Tipo de atributo     : '%s'", zs_method, zs_datatype);
                         logf(SIGNIFICANT, "%s LREL Factory Producer: '%s'", zs_method, zs_lrel_factory_producer);
                         logf(SIGNIFICANT, "%s LREL Domset          : '%s'", zs_method, zs_lrel_domset);
                         logf(SIGNIFICANT, "%s LREL WhereClause     : '%s'", zs_method, zs_lrel_wc);
                    }
                    
                    // Descobrindo qual eh a FACTORY e KEY do atributo PARENT na factory LREL
                    if (zi_debug > 0) logf(SIGNIFICANT, "%s Consultando o atributo '%s' da factory '%s'...", zs_method, zs_lrel_parent_attr, zs_lrel_factory_producer);
                    send_wait(0, top_object(), "call_attr", zs_lrel_factory_producer, "dob_attr_type_info", zs_lrel_parent_attr);
                    if (msg_error()) {
                         zs_msg = format("%s ERRO ao consultar o atributo '%s' da factory '%s': %s", zs_method, zs_lrel_parent_attr, zs_lrel_factory_producer, msg[0]);
                         logf(ERROR, "%s", zs_msg);
                         break;
                    }
                    string zs_lrel_parent_factory, zs_lrel_parent_attr_key;
                    zs_lrel_parent_factory = msg[2];
                    zs_lrel_parent_attr_key = msg[3];
                    if (zi_debug > 0) {
                         logf(SIGNIFICANT, "%s LREL Parent Attr     : '%s'", zs_method, zs_lrel_parent_attr);
                         logf(SIGNIFICANT, "%s LREL Parent Factory  : '%s'", zs_method, zs_lrel_parent_factory);
                         logf(SIGNIFICANT, "%s LREL Parent Attr Key : '%s'", zs_method, zs_lrel_parent_attr_key);
                    }

                    // Descobrindo o TIPO de atributo na KEY do PARENT
                    string zs_lrel_parent_attr_type;
                    zs_lrel_parent_attr_type = z_consulta_tipo_dado_atributo(zs_lrel_parent_factory, zs_lrel_parent_attr_key, zi_debug, zs_method);
                    if (zi_debug > 0) logf(SIGNIFICANT, "%s LREL Parent Attr Type: '%s'", zs_method, zs_lrel_parent_attr_type);
                    
                    // Descobrindo qual eh a FACTORY e KEY do atributo CHILD na factory LREL
                    if (zi_debug > 0) logf(SIGNIFICANT, "%s Consultando o atributo '%s' da factory '%s'...", zs_method, zs_lrel_child_attr, zs_lrel_factory_producer);
                    send_wait(0, top_object(), "call_attr", zs_lrel_factory_producer, "dob_attr_type_info", zs_lrel_child_attr);
                    if (msg_error()) {
                         zs_msg = format("%s ERRO ao consultar o atributo '%s' da factory '%s': %s", zs_method, zs_lrel_child_attr, zs_lrel_factory_producer, msg[0]);
                         logf(ERROR, "%s", zs_msg);
                         break;
                    }
                    string zs_lrel_child_factory, zs_lrel_child_attr_key;
                    zs_lrel_child_factory = msg[2];
                    zs_lrel_child_attr_key = msg[3];
                    if (zi_debug > 0) {
                         logf(SIGNIFICANT, "%s LREL Child Attr      : '%s'", zs_method, zs_lrel_child_attr);
                         logf(SIGNIFICANT, "%s LREL Child Factory   : '%s'", zs_method, zs_lrel_child_factory);
                         logf(SIGNIFICANT, "%s LREL Child Attr Key  : '%s'", zs_method, zs_lrel_child_attr_key);
                    }
                    
                    // Descobrindo o TIPO de atributo na KEY do CHILD
                    string zs_lrel_child_attr_type;
                    zs_lrel_child_attr_type = z_consulta_tipo_dado_atributo(zs_lrel_child_factory, zs_lrel_child_attr_key, zi_debug, zs_method);
                    if (zi_debug > 0) logf(SIGNIFICANT, "%s LREL Child Attr Type : '%s'", zs_method, zs_lrel_child_attr_type);
                    
                    ////////////////////////////////////////////////////
                    // Consultando VALORES
                    ////////////////////////////////////////////////////
                    // PARENT
                    if (zi_debug > 0) logf(SIGNIFICANT, "%s Consultando o atributo '%s' do objeto '%s' da factory '%s'...", zs_method, zs_lrel_parent_attr_key, zs_persid, zs_lrel_parent_factory);
                    send_wait(0, top_object(), "call_attr", zs_lrel_parent_factory, "val_by_key", "persistent_id", zs_persid, 1, zs_lrel_parent_attr_key);
                    if (msg_error()) {
                         zs_msg = format("%s ERRO ao consultar o atributo '%s' do objeto '%s' da factory '%s': %s", zs_method, zs_lrel_parent_attr_key, zs_persid, zs_lrel_parent_factory, msg[0]);
                         logf(ERROR, "%s", zs_msg);
                         break;
                    }
                    string zs_lrel_parent_attr_value;
                    zs_lrel_parent_attr_value = (string) msg[1];
                    if (zi_debug > 0) logf(SIGNIFICANT, "%s Valor do atributo '%s' do objeto '%s': '%s'", zs_method, zs_lrel_parent_attr_key, zs_persid, zs_lrel_parent_attr_value);

                    ////////////////////////////////////////////////////
                    // ADICIONANDO registros
                    ////////////////////////////////////////////////////
                    if (zi_add_persids_count > 0) { // Caso existam registros a serem ADICIONADOS
                         for (zi_j=0; zi_j<zi_add_persids_count; zi_j++) {
                              // CHILD
                              if (zi_debug > 0) logf(SIGNIFICANT, "%s Consultando o atributo '%s' do objeto '%s' da factory '%s'...", zs_method, zs_lrel_child_attr_key, zs_add_persids[zi_j], zs_lrel_child_factory);
                              send_wait(0, top_object(), "call_attr", zs_lrel_child_factory, "val_by_key", "persistent_id", zs_add_persids[zi_j], 1, zs_lrel_child_attr_key);
                              if (msg_error()) {
                                   zs_msg = format("%s ERRO ao consultar o atributo '%s' do objeto '%s' da factory '%s': %s", zs_method, zs_lrel_child_attr_key, zs_add_persids[zi_j], zs_lrel_child_factory, msg[0]);
                                   logf(ERROR, "%s", zs_msg);
                                   break;
                              }
                              zs_lrel_child_attr_value = (string) msg[1];
                              if (zi_debug > 0) logf(SIGNIFICANT, "%s Valor do atributo '%s' do objeto '%s': '%s'", zs_method, zs_lrel_attr_key, zs_add_persids[zi_j], zs_lrel_child_attr_value);
                              
                              ////////////////////////////////////////////////////
                              // Montando a WHERE CLAUSE
                              ////////////////////////////////////////////////////
                              zs_lrel_wc_new = z_update_lrel_build_wc(zs_lrel_wc, zs_lrel_parent_attr_type, zs_lrel_parent_attr_value, zs_lrel_child_attr, zs_lrel_child_attr_type, zs_lrel_child_attr_value, zi_debug, zs_method);

                              ////////////////////////////////////////////////////
                              // Verifica se o registro LREL ja EXISTE na factory LREL
                              ////////////////////////////////////////////////////
                              if (zi_debug > 0) logf(SIGNIFICANT, "%s Verificando se o registro LREL existe na factory '%s' utilizando a clausula '%s'...", zs_method, zs_lrel_factory_producer, zs_lrel_wc_new);
                              send_wait(0, top_object(), "call_attr", zs_lrel_factory_producer, "sync_fetch", zs_lrel_domset, zs_lrel_wc_new, -1, 0);
                              if (msg_error()) {
                                   zs_msg = format("%s ERRO ao listar registros da factory '%s' utilizando a clausula '%s': %s", zs_method, zs_lrel_factory_producer, zs_lrel_wc_new, msg[0]);
                                   logf(ERROR, "%s", zs_msg);
                                   break;
                              }
                              zi_lrel_list_count = msg[1];
                              if (zi_debug > 1) logf(SIGNIFICANT, "%s Quantidade de registros: %d", zs_method, zi_lrel_list_count);
                              
                              if (zi_lrel_list_count == 0) { // Caso o registro NAO exista
                                   ////////////////////////////////////////////////////
                                   // Cria NOVO REGISTRO
                                   ////////////////////////////////////////////////////
                                   if (zi_debug > 0) logf(SIGNIFICANT, "%s Criando novo registro na factory '%s'...", zs_method, zs_lrel_factory_producer);
                                   send_wait(0, top_object(), "call_attr", "api", "insert_object", zu_who, zs_lrel_factory_producer, NULL, 0, zs_lrel_parent_attr, zs_lrel_parent_attr_value, zs_lrel_child_attr, zs_lrel_child_attr_value);
                                   if (msg_error()) {
                                        zs_msg = format("%s ERRO ao criar registro na factory '%s': '%s'", zs_method, zs_lrel_factory_producer, msg[0]);
                                        logf(ERROR, "%s", zs_msg);
                                        break;
                                   }
                                   object zo_lrel;
                                   zo_lrel = msg[0];
                                   if (zi_debug > 0) logf(SIGNIFICANT, "%s Registro criado com SUCESSO na factory '%s': '%s'", zs_method, zs_lrel_factory_producer, zo_lrel.id);
                              
                              } else {
                                   if (zi_debug > 0) logf(SIGNIFICANT, "%s AVISO: O registro '%s' foi IGNORADO pois ja existe.", zs_method, zs_lrel_child_attr_value);
                              }
                         } // Fim do FOR
                    } // Fim do IF
                    
                    ////////////////////////////////////////////////////
                    // REMOVENDO registros
                    ////////////////////////////////////////////////////
                    if (zi_remove_persids_count > 0) { // Caso existam registros a serem REMOVIDOS
                         for (zi_j=0; zi_j<zi_remove_persids_count; zi_j++) {
                              // CHILD
                              if (zi_debug > 0) logf(SIGNIFICANT, "%s Consultando o atributo '%s' do objeto '%s' da factory '%s'...", zs_method, zs_lrel_child_attr_key, zs_remove_persids[zi_j], zs_lrel_child_factory);
                              send_wait(0, top_object(), "call_attr", zs_lrel_child_factory, "val_by_key", "persistent_id", zs_remove_persids[zi_j], 1, zs_lrel_child_attr_key);
                              if (msg_error()) {
                                   zs_msg = format("%s ERRO ao consultar o atributo '%s' do objeto '%s' da factory '%s': %s", zs_method, zs_lrel_child_attr_key, zs_remove_persids[zi_j], zs_lrel_child_factory, msg[0]);
                                   logf(ERROR, "%s", zs_msg);
                                   break;
                              }
                              zs_lrel_child_attr_value = (string) msg[1];
                              if (zi_debug > 0) logf(SIGNIFICANT, "%s Valor do atributo '%s' do objeto '%s': '%s'", zs_method, zs_lrel_attr_key, zs_remove_persids[zi_j], zs_lrel_child_attr_value);

                              ////////////////////////////////////////////////////
                              // Montando a WHERE CLAUSE
                              ////////////////////////////////////////////////////
                              zs_lrel_wc_new = z_update_lrel_build_wc(zs_lrel_wc, zs_lrel_parent_attr_type, zs_lrel_parent_attr_value, zs_lrel_child_attr, zs_lrel_child_attr_type, zs_lrel_child_attr_value, zi_debug, zs_method);

                              ////////////////////////////////////////////////////
                              // REMOVE REGISTRO
                              ////////////////////////////////////////////////////
                              if (zi_debug > 0) logf(SIGNIFICANT, "%s Removendo o registro '%s' na factory '%s'...", zs_method, zs_lrel_child_attr_value, zs_lrel_factory_producer);
                              send_wait(0, top_object(), "call_attr", "api", "delete_wc", zs_lrel_factory_producer, zs_lrel_wc_new, NULL);
                              if (msg_error()) {
                                   zs_msg = format("%s ERRO ao remover registro na factory '%s': '%s'", zs_method, zs_lrel_factory_producer, msg[0]);
                                   logf(ERROR, "%s", zs_msg);
                                   break;
                              }
                              if (zi_debug > 0) logf(SIGNIFICANT, "%s Registro removido com SUCESSO na factory '%s'", zs_method, zs_lrel_factory_producer);
          
                         } // Fim do FOR
                    } // Fim do IF
                    
                    break; // Encerra a procura
               } // Fim do IF
          } // Fim do IF
          
          // Posiciona o ponteiro do FOR para o proximo atributo
          if (zs_datatype == "VALUE") {
               zi_i += 19;
          } else if (zs_datatype == "SREL") {
               zi_i += 20;
          } else if (zs_datatype == "QREL") {
               zi_i += 13;
          } else if (zs_datatype == "LREL") {
               zi_i += 7;
          }
          
     } // Fim do FOR

     ////////////////////////////////////////////////////
     // RESUMO DA EXECUCAO
     ////////////////////////////////////////////////////
     if (zi_debug > 0) {
          logf(SIGNIFICANT, "%s Tempo decorrido (seg): %d", zs_method, ((int) now() - zi_start));
     }
     
     return zs_msg;
     // Fim do codigo
    ///////////////////////////////////
}

Jan 03, 2019 05:59 AM

Great giedrius!! More one...

Related Entries and Links

No Related Resource entered.