Sonar reduce method parameter
[music.git] / src / main / java / org / onap / music / conductor / conditionals / MusicConditional.java
index ff77bfd..18fa8a1 100644 (file)
@@ -3,7 +3,9 @@
  * org.onap.music
  * ===================================================================
  *  Copyright (c) 2017 AT&T Intellectual Property
+ * ===================================================================
  *  Modifications Copyright (C) 2019 IBM.
+ *  Modifications Copyright (c) 2019 Samsung
  * ===================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -90,7 +92,12 @@ public class MusicConditional {
         
         
         String key = keyspace + "." + tablename + "." + primaryKey;
-        String lockId = MusicCore.createLockReference(key);
+        String lockId;
+        try {
+               lockId = MusicCore.createLockReference(key);
+        } catch (MusicLockingException e) {
+               return new ReturnType(ResultType.FAILURE, e.getMessage());
+        }
         long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
         ReturnType lockAcqResult = MusicCore.acquireLockWithLease(key, lockId, leasePeriod);
 
@@ -112,6 +119,7 @@ public class MusicConditional {
                 return lockAcqResult;
             }
         } catch (Exception e) {
+            logger.error(EELFLoggerDelegate.applicationLogger, e);
             MusicCore.destroyLockRef(lockId);
             return new ReturnType(ResultType.FAILURE, e.getMessage());
         }
@@ -130,6 +138,7 @@ public class MusicConditional {
                 try {
                     results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(queryBank.get(MusicUtil.SELECT));
                 } catch (Exception e) {
+                    logger.error(EELFLoggerDelegate.applicationLogger, e);
                     return new ReturnType(ResultType.FAILURE, e.getMessage());
                 }
                 if (results.all().isEmpty()) {
@@ -148,6 +157,7 @@ public class MusicConditional {
             StringWriter sw = new StringWriter();
             e.printStackTrace(new PrintWriter(sw));
             String exceptionAsString = sw.toString();
+            logger.error(EELFLoggerDelegate.applicationLogger, e);
             return new ReturnType(ResultType.FAILURE,
                     "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n"
                             + exceptionAsString);
@@ -155,9 +165,10 @@ public class MusicConditional {
 
     }
 
-    public static ReturnType update(Map<String,PreparedQueryObject> queryBank, String keyspace, String tableName, String primaryKey,String primaryKeyValue,String planId,String cascadeColumnName,Map<String,String> cascadeColumnValues) throws MusicLockingException, MusicQueryException, MusicServiceException {
+    public static ReturnType update(UpdateDataObject dataObj)
+            throws MusicLockingException, MusicQueryException, MusicServiceException {
 
-        String key = keyspace + "." + tableName + "." + primaryKeyValue;
+        String key = dataObj.getKeyspace() + "." + dataObj.getTableName() + "." + dataObj.getPrimaryKeyValue();
         String lockId = MusicCore.createLockReference(key);
         long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
         ReturnType lockAcqResult = MusicCore.acquireLockWithLease(key, lockId, leasePeriod);
@@ -165,7 +176,16 @@ public class MusicConditional {
         try {
 
             if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
-                ReturnType criticalPutResult = updateAtomic(lockId, keyspace, tableName, primaryKey,primaryKeyValue, queryBank,planId,cascadeColumnValues,cascadeColumnName);
+                ReturnType criticalPutResult = updateAtomic(new UpdateDataObject().setLockId(lockId)
+                        .setKeyspace(dataObj.getKeyspace())
+                        .setTableName( dataObj.getTableName())
+                        .setPrimaryKey(dataObj.getPrimaryKey())
+                        .setPrimaryKeyValue(dataObj.getPrimaryKeyValue())
+                        .setQueryBank(dataObj.getQueryBank())
+                        .setPlanId(dataObj.getPlanId())
+                        .setCascadeColumnValues(dataObj.getCascadeColumnValues())
+                        .setCascadeColumnName(dataObj.getCascadeColumnName()));                     
+
                 MusicCore.destroyLockRef(lockId);
                 return criticalPutResult;
             } else {
@@ -175,39 +195,40 @@ public class MusicConditional {
 
         } catch (Exception e) {
             MusicCore.destroyLockRef(lockId);
+            logger.error(EELFLoggerDelegate.applicationLogger, e);
             return new ReturnType(ResultType.FAILURE, e.getMessage());
 
         }
     }
 
-    public static ReturnType updateAtomic(String lockId, String keyspace, String tableName, String primaryKey,String primaryKeyValue,
-            Map<String,PreparedQueryObject> queryBank,String planId,Map<String,String> cascadeColumnValues,String casscadeColumnName) {
+    public static ReturnType updateAtomic(UpdateDataObject dataObj) {
         try {
-            String fullyQualifiedKey = keyspace + "." + tableName + "." + primaryKeyValue;
-            ReturnType lockAcqResult = MusicCore.acquireLock(fullyQualifiedKey, lockId);
+            String fullyQualifiedKey = dataObj.getKeyspace() + "." + dataObj.getTableName() + "." + dataObj.getPrimaryKeyValue();
+            ReturnType lockAcqResult = MusicCore.acquireLock(fullyQualifiedKey, dataObj.getLockId());
 
             if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
-                Row row  = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(queryBank.get(MusicUtil.SELECT)).one();
+                Row row  = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(dataObj.getQueryBank().get(MusicUtil.SELECT)).one();
                 
                 if(row != null) {
-                    Map<String, String> updatedValues = cascadeColumnUpdateSpecific(row, cascadeColumnValues, casscadeColumnName, planId);
+                    Map<String, String> updatedValues = cascadeColumnUpdateSpecific(row, dataObj.getCascadeColumnValues(), dataObj.getCascadeColumnName(), dataObj.getPlanId());
                     JSONObject json = new JSONObject(updatedValues);
                     PreparedQueryObject update = new PreparedQueryObject();
                     String vector_ts = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
-                    update.appendQueryString("UPDATE " + keyspace + "." + tableName + " SET " + casscadeColumnName + "['" + planId
-                            + "'] = ?, vector_ts = ? WHERE " + primaryKey + " = ?");
+                    update.appendQueryString("UPDATE " + dataObj.getKeyspace() + "." + dataObj.getTableName() + " SET " + dataObj.getCascadeColumnName() + "['" + dataObj.getPlanId()
+                            + "'] = ?, vector_ts = ? WHERE " + dataObj.getPrimaryKey() + " = ?");
                     update.addValue(MusicUtil.convertToActualDataType(DataType.text(), json.toString()));
                     update.addValue(MusicUtil.convertToActualDataType(DataType.text(), vector_ts));
-                    update.addValue(MusicUtil.convertToActualDataType(DataType.text(), primaryKeyValue));
+                    update.addValue(MusicUtil.convertToActualDataType(DataType.text(), dataObj.getPrimaryKeyValue()));
                     try {
                         MusicDataStoreHandle.getDSHandle().executePut(update, "critical");
                     } catch (Exception ex) {
+                        logger.error(EELFLoggerDelegate.applicationLogger, ex);
                         return new ReturnType(ResultType.FAILURE, ex.getMessage());
                     }
                 }else {
-                    return new ReturnType(ResultType.FAILURE,"Cannot find data related to key: "+primaryKey);
+                    return new ReturnType(ResultType.FAILURE,"Cannot find data related to key: "+dataObj.getPrimaryKey());
                 }
-                MusicDataStoreHandle.getDSHandle().executePut(queryBank.get(MusicUtil.UPSERT), "critical");
+                MusicDataStoreHandle.getDSHandle().executePut(dataObj.getQueryBank().get(MusicUtil.UPSERT), "critical");
                 return new ReturnType(ResultType.SUCCESS, "update success");
 
             } else {
@@ -219,6 +240,7 @@ public class MusicConditional {
             StringWriter sw = new StringWriter();
             e.printStackTrace(new PrintWriter(sw));
             String exceptionAsString = sw.toString();
+            logger.error(EELFLoggerDelegate.applicationLogger, e);
             return new ReturnType(ResultType.FAILURE,
                     "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n"
                             + exceptionAsString);
@@ -254,11 +276,12 @@ public class MusicConditional {
         StringBuilder fieldsString = new StringBuilder("(vector_ts"+",");
         StringBuilder valueString = new StringBuilder("(" + "?" + ",");
         String vector = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
+        String localPrimaryKey;
         queryObject.addValue(vector);
         if(casscadeColumn!=null && casscadeColumnValues!=null) {
             fieldsString.append(casscadeColumn).append(" ,");
-          valueString.append("?,");
-          queryObject.addValue(casscadeColumnValues);
+            valueString.append("?,");
+            queryObject.addValue(casscadeColumnValues);
         }
         
         int counter = 0;
@@ -267,23 +290,23 @@ public class MusicConditional {
             fieldsString.append(entry.getKey());
             Object valueObj = entry.getValue();
             if (primaryKeyName.equals(entry.getKey())) {
-                primaryKey = entry.getValue() + "";
-                primaryKey = primaryKey.replace("'", "''");
+                localPrimaryKey = entry.getValue() + "";
+                localPrimaryKey = localPrimaryKey.replace("'", "''");
             }
             DataType colType = null;
             try {
                 colType = tableInfo.getColumn(entry.getKey()).getType();
             } catch(NullPointerException ex) {
-                logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() +" Invalid column name : "+entry.getKey(), AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
-               
+                logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() +" Invalid column name : "+entry.getKey(), 
+                    AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR, ex);
             }
 
             Object formattedValue = null;
             try {
-              formattedValue = MusicUtil.convertToActualDataType(colType, valueObj);
+                formattedValue = MusicUtil.convertToActualDataType(colType, valueObj);
             } catch (Exception e) {
-              logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
-          }
+                logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), e);
+            }
             
             valueString.append("?");
             queryObject.addValue(formattedValue);