Árvore de páginas

Data update in Protheus is also performed via WebServices as seen in the validation using the afterProcessFinish process event that is run at the end of the request.

 

function afterProcessFinish(processId){

var wsService = ServiceManager.getService("WSCLIENTE");

var serviceHelper = wsService.getBean();

var serviceLocator = serviceHelper.instantiate("localhost.WSCLIENTELocator");

var service = serviceLocator.getWSCLIENTESOAP();

var SA1 = new XML("<SA1/>");

 

SA1.A1_COD = hAPI.getCardValue("A1_COD");

SA1.A1_LOJA = hAPI.getCardValue("A1_LOJA");

SA1.A1_TIPO = hAPI.getCardValue("A1_TIPO");

SA1.A1_PESSOA = hAPI.getCardValue("A1_PESSOA");

SA1.A1_NOME = hAPI.getCardValue("A1_NOME");

SA1.A1_NREDUZ = hAPI.getCardValue("A1_NREDUZ");

SA1.A1_END = hAPI.getCardValue("A1_END");

SA1.A1_MUN = hAPI.getCardValue("A1_MUN");

SA1.A1_EST = hAPI.getCardValue("A1_EST");

 

try {

   ret = service.INCLUIR(new java.lang.String(SA1.toXMLString()).getBytes());

}

catch(erro){

   throw erro.message

}

}

MVC

The validation process of a MVC routine is also performed using the FWWSMODEL WebService (http://hostexemplo:8080/ws/fwwsmodel.apw?wsdl) with the invocation of the GETXMLDATADETAIL method that returns the XML of the model to populate the data and the PUTXMLDATA method to save the XML.

 

function afterProcessFinish(processId){

var wsService = ServiceManager.getService("TOTVS_FWMVC");

var serviceHelper = wsService.getBean();

var serviceLocator = serviceHelper.instantiate("br.com.totvs.webservices.fwwsmodel_apw.FWWSMODELLocator");

var service = serviceLocator.getFWWSMODELSOAP();

var err = {message:"", empty:true};

var ret, xml;

 

try {

   ret = service.GETXMLDATADETAIL([],"MATA030_MVC");

   xml = new XML(new String(new java.lang.String(ret)).replace(/<\?.*\?>/g,""));

}

catch(erro){

   throw erro.message;

   return;

}

 

updateXMLFields(xml,err);

 

eval("xml.@Operation = 3");

try{

   service.PUTXMLDATA([],"MATA030_MVC",new java.lang.String(xml.toXMLString()).getBytes());

} catch(e){

   throw e.message;

}

}

 

function updateXMLFields(node,err){

var list = node.children();

var name,value;

 

for (var i=0;i<list.length();i++){

   switch (Trim(eval("list[i][email protected]()"))){

      case "FIELDS":

         updateXMLFields(list[i],err);

         break;

      default:

         name = list[i].name().localName;

         value = hAPI.getCardValue(name);

         if (value != null)

            list[i].value = convertValue(name,list[i],value,err);

         break;

   }

   if (err.message.length > 0)

      break;

}

}

 

function convertValue(name,struct,value,err){

var y,m,d,n,len,str,reg

var setYear = new Date().getFullYear().toString().substring(0,2);

var setDate = "dd/mm/yyyy";

var yCount = setDate.match(/yyyy/) ? 4 : 2;

var hasErr = false;

 

value = value.trim();

 

switch (eval("[email protected]()")){

case "D":

   if (value.replace("/","").trim() != ""){

      len = value.length();

      d = setDate.indexOf("d");

      m = setDate.indexOf("m");

      y = setDate.indexOf("y");

 

      if (d + 2 > len || m + 2 > len || y + yCount > len){

         hasErr = true;

      }

      else{

         str = value.substr(m,2) + value.substr(d,2);

         if (yCount == 4)

            str = value.substr(y,4) + str;

         else

            str = setYear + value.substr(y,2) + str;

 

         if (str.match(/((((19|20)(([02468][048])|([13579][26]))0229))|((19|20)[0-9][0-9])((((0[1-9])|(1[0-2]))((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))31)|(((0[1,3-9])|(1[0-2]))(29|30)))))/g))

            value = new java.lang.String(str);

         else

            hasErr = true;

      }

 

      if (hasErr)

         err.message = "The field "+eval("struct.@info")+" ("+name+") has an invalid date";

      else

         err.empty = false;

   }

   break;

case "N":

   len = eval("[email protected]().split(',')");

   if (len[1] == "0"){

      reg = new RegExp("^[0-9]{1,"+len[0]+"}$");

      str = value.replace(",","");

      if(!reg.test(str)){

         str = value.replace(".","");

         if(!reg.test(str)){

            err.message = "The field "+eval("struct.@info")+" ("+name+") has an invalid number (value or size)"

            hasErr = true;

            break;

         }

      }

 

      if (!hasErr){

         n = parseInt(str);

         if (!isNaN(n)){

            value = new java.lang.String(str);

            if (n > 0)

               err.empty = false;

         }

      }

   }

   else{

      reg = new RegExp("^0$|^0\.[0-9]{1,"+len[1]+"}$|^[0-9]{0,"+len[0]+"}(\.[0-9]{1,"+len[1]+"})?$");

      str = value.replace(".","").replace(",",".");

      if(!reg.test(str)){

         str = value.replace(",","");

         if(!reg.test(str)){

            err.message = "The field "+eval("struct.@info")+" ("+name+") has an invalid number (value or size)"

            hasErr = true;

            break;

         }

      }

            

      if (!hasErr){

         n = parseFloat(str);

         if (!isNaN(n)){

            value = new java.lang.String(str);

            if (n > 0)

               err.empty = false;

         }

      }

   }

   break;

default:

   if (value != "")

      err.empty = false;

   break;

}

return value;

}

function Trim(str){return str.replace(/^\s+|\s+$/g,"");}

 

Automatic Routine (Model 1)

Since automatic routines perform the update after validation, instead of using the afterProcessFinish event, use the beforeTaskSave event when the process is in the end activity. In addition to the MVC WebService, also use the FWWSMODEL (http://hostexemplo:8080/ws/fwwsecm.apw?wsdl) by calling the PUTDATA method.

 

#include "totvs.ch"

User Function WSMVC()

Local cName := "TOTVS_FWWSECM"

Local cURL := "http://hostname:8080/ws/fwwsecm.apw?wsdl"

Local cDescription := "TOTVS Microsiga Protheus service for integrating automatic routines with ECM"

Local aService := FWWFGetService(cName)

 

If Empty(aService) .and. FWWFPutService(cName,cUrl,cDescription)

   MsgInfo("Service successfully registered")

EndIf

Return

 

beforeTaskSave event:

 

function beforeTaskSave(colleagueId,nextSequenceId,userList){

var wsService = ServiceManager.getService("TOTVS_FWMVC");

var serviceHelper = wsService.getBean();

var serviceLocator = serviceHelper.instantiate("br.com.totvs.webservices.fwwsmodel_apw.FWWSMODELLocator");

var service = serviceLocator.getFWWSMODELSOAP();

var err = {message:"", empty:true};

var ret, xml;

 

try {

   ret = service.GETXMLDATADETAIL([],"MATA030_MVC");

   xml = new XML(new String(new java.lang.String(ret)).replace(/<\?.*\?>/g,""));

}

catch(erro){

   throw erro.message;

   return;

}

 

updateXMLFields(xml,err);

 

if (err.message.length > 0){

   throw err.message;

}

else if (err.empty){

   throw "Fill out form";

}

else{

   wsService = ServiceManager.getService("TOTVS_FWWSECM");

   serviceHelper = wsService.getBean();

   serviceLocator = serviceHelper.instantiate("br.com.totvs.webservices.fwwsecm_apw.FWWSECMLocator");

   service = serviceLocator.getFWWSMODELSOAP();

 

   eval("xml.@Operation = 3");

   try{

      service.PUTDATA([],"SA1","MATA030",new java.lang.String(xml.toXMLString()).getBytes());;

   } catch(e){

      throw e.message;

   }

}

}

 

function updateXMLFields(node,err){

var list = node.children();

var name,value;

 

for (var i=0;i<list.length();i++){

   switch (Trim(eval("list[i][email protected]()"))){

      case "FIELDS":

         updateXMLFields(list[i],err);

         break;

      default:

         name = list[i].name().localName;

         value = hAPI.getCardValue(name);

         if (value != null)

            list[i].value = convertValue(name,list[i],value,err);

         break;

   }

   if (err.message.length > 0)

      break;

}

}

 

function convertValue(name,struct,value,err){

var y,m,d,n,len,str,reg

var setYear = new Date().getFullYear().toString().substring(0,2);

var setDate = "dd/mm/yyyy";

var yCount = setDate.match(/yyyy/) ? 4 : 2;

var hasErr = false;

 

value = value.trim();

 

switch (eval("[email protected]()")){

case "D":

   if (value.replace("/","").trim() != ""){

      len = value.length();

      d = setDate.indexOf("d");

      m = setDate.indexOf("m");

      y = setDate.indexOf("y");

 

      if (d + 2 > len || m + 2 > len || y + yCount > len){

         hasErr = true;

      }

      else{

         str = value.substr(m,2) + value.substr(d,2);

         if (yCount == 4)

            str = value.substr(y,4) + str;

         else

            str = setYear + value.substr(y,2) + str;

 

         if (str.match(/((((19|20)(([02468][048])|([13579][26]))0229))|((19|20)[0-9][0-9])((((0[1-9])|(1[0-2]))((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))31)|(((0[1,3-9])|(1[0-2]))(29|30)))))/g))

            value = new java.lang.String(str);

         else

            hasErr = true;

      }

 

      if (hasErr)

         err.message = "The field "+eval("struct.@info")+" ("+name+") has an invalid date";

      else

         err.empty = false;

   }

   break;

case "N":

   len = eval("[email protected]().split(',')");

   if (len[1] == "0"){

      reg = new RegExp("^[0-9]{1,"+len[0]+"}$");

      str = value.replace(",","");

      if(!reg.test(str)){

         str = value.replace(".","");

         if(!reg.test(str)){

            err.message = "The field "+eval("struct.@info")+" ("+name+") has an invalid number (value or size)"

            hasErr = true;

            break;

         }

      }

 

      if (!hasErr){

         n = parseInt(str);

         if (!isNaN(n)){

            value = new java.lang.String(str);

            if (n > 0)

               err.empty = false;

         }

      }

   }

   else{

      reg = new RegExp("^0$|^0\.[0-9]{1,"+len[1]+"}$|^[0-9]{0,"+len[0]+"}(\.[0-9]{1,"+len[1]+"})?$");

      str = value.replace(".","").replace(",",".");

      if(!reg.test(str)){

         str = value.replace(",","");

         if(!reg.test(str)){

            err.message = "The field "+eval("struct.@info")+" ("+name+") has an invalid number (value or size)"

            hasErr = true;

            break;

         }

      }

            

      if (!hasErr){

         n = parseFloat(str);

         if (!isNaN(n)){

            value = new java.lang.String(str);

            if (n > 0)

               err.empty = false;

         }

      }

   }

   break;

default:

   if (value != "")

      err.empty = false;

   break;

}

return value;

}

function Trim(str){return str.replace(/^\s+|\s+$/g,"");}

 

 

 

  • Sem rótulos