* @return a boolean value with the response of the operation.
*
*/
public boolean executeUpdateQuery(Map<String , Object> params, JSONObject metaParams) throws Throwable, Exception{
boolean toReturn = true;
DataConnection dataConnection = null;
SQLCommand sqlCommand = null;
DataResult dataResult = null;
logger.debug("IN");
try {
Connection jdbcConnection = datasource.getConnection();
jdbcConnection.setAutoCommit(false);
dataConnection = getDataConnection(jdbcConnection);
String statement = SQLStatements.getStatement((String)params.get( STMT ));
logger.debug("Parameter [" + STMT + "] is equals to [" + statement + "]");
Assert.assertTrue(!StringUtilities.isEmpty( statement ), "Parameter [" + STMT + "] cannot be null or empty");
String numParsStr = (String) params.get( NUM_PARS );
int numPars = (numParsStr != null)?Integer.parseInt(numParsStr):0;
logger.debug("Parameter [ numPars ] is equals to [" + numPars + "]");
sqlCommand = dataConnection.createUpdateCommand(statement);
dataConnection.initTransaction();
if (numPars > 0){
List inputParameter = new ArrayList(numPars);
if (metaParams == null){
Assert.assertTrue(metaParams == null, "Parameter [" + metaParams + "] cannot be null or empty.");
}else{
JSONArray queryPars = (JSONArray)metaParams.get("queryParams");
//for (int j=0; j<queryPars.length(); j++){
for (int j=0; j<numPars; j++){
JSONObject obj = (JSONObject)queryPars.get(j);
String paramType = (String)obj.get("type");
String paramName = (String)obj.get("name");
String paramValue = (String)params.get(paramName);
//if value isn't valorized, checks the defualt (if it's defined into meta section)
if (paramValue == null){
try{
paramValue = (String)obj.get("default");
}catch(JSONException je ){
logger.error("param " + paramName + "in JSON template not found. Parameter value is null!");
paramValue = null;
}
}
inputParameter.add(dataConnection.createDataField(paramName,getParamType(paramType), paramValue));
}
}
dataResult = sqlCommand.execute(inputParameter);
}else{
dataResult = sqlCommand.execute();
}
dataConnection.commitTransaction();
} // try
catch (Exception ex) {
toReturn = false;
logger.error("QueryExecutor::executeQuery:", ex);
try{
dataConnection.rollBackTransaction();
} catch (Throwable t) {
toReturn = false;
throw new Throwable(t);
}
throw new Throwable(ex);