From f9f7fa56adf725801a789ed6c3f5503bb5d243a2 Mon Sep 17 00:00:00 2001 From: Arindam Mondal Date: Wed, 5 Jun 2019 18:25:23 +0900 Subject: Sonar reduce method parameter +)update ++)updateAtomic Issue-ID: MUSIC-408 Change-Id: I89d981a31a00f024b3a7be24f5845a0a40c4a6a2 Signed-off-by: Arindam Mondal --- .../conductor/conditionals/MusicConditional.java | 40 ++++--- .../conditionals/RestMusicConditionalAPI.java | 10 +- .../conductor/conditionals/UpdateDataObject.java | 119 +++++++++++++++++++++ 3 files changed, 150 insertions(+), 19 deletions(-) create mode 100644 src/main/java/org/onap/music/conductor/conditionals/UpdateDataObject.java (limited to 'src/main') diff --git a/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java b/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java index 29740d83..18fa8a18 100644 --- a/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java +++ b/src/main/java/org/onap/music/conductor/conditionals/MusicConditional.java @@ -165,12 +165,10 @@ public class MusicConditional { } - public static ReturnType update(Map queryBank, String keyspace, String tableName, - String primaryKey, String primaryKeyValue, String planId, String cascadeColumnName, - Map 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); @@ -178,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 { @@ -194,25 +201,24 @@ public class MusicConditional { } } - public static ReturnType updateAtomic(String lockId, String keyspace, String tableName, String primaryKey,String primaryKeyValue, - Map queryBank,String planId,Map 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 updatedValues = cascadeColumnUpdateSpecific(row, cascadeColumnValues, casscadeColumnName, planId); + Map 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) { @@ -220,9 +226,9 @@ public class MusicConditional { 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 { diff --git a/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java b/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java index 7372fc46..584a9e47 100644 --- a/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java +++ b/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditionalAPI.java @@ -183,8 +183,14 @@ public class RestMusicConditionalAPI { queryBank.put(MusicUtil.SELECT, select); queryBank.put(MusicUtil.UPSERT, upsert); String planId = casscadeColumnData.get("key").toString(); - ReturnType result = MusicConditional.update(queryBank, keyspace, tablename, - primaryKey,primaryKeyValue,planId,casscadeColumnName,casscadeColumnValueMap); + ReturnType result = MusicConditional.update(new UpdateDataObject().setQueryBank(queryBank) + .setKeyspace(keyspace) + .setTableName(tablename) + .setPrimaryKey(primaryKey) + .setPrimaryKeyValue(primaryKeyValue) + .setPlanId(planId) + .setCascadeColumnName(casscadeColumnName) + .setCascadeColumnValues(casscadeColumnValueMap)); if (result.getResult() == ResultType.SUCCESS) { return response.status(Status.OK) .entity(new JsonResponse(result.getResult()) diff --git a/src/main/java/org/onap/music/conductor/conditionals/UpdateDataObject.java b/src/main/java/org/onap/music/conductor/conditionals/UpdateDataObject.java new file mode 100644 index 00000000..1ea8994e --- /dev/null +++ b/src/main/java/org/onap/music/conductor/conditionals/UpdateDataObject.java @@ -0,0 +1,119 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Samsung Electronics Co., Ltd. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.music.conductor.conditionals; + +import java.util.Map; +import org.onap.music.datastore.PreparedQueryObject; + +public class UpdateDataObject { + + Map queryBank; + String keyspace; + String tableName; + String primaryKey; + String primaryKeyValue; + String planId; + String cascadeColumnName; + Map cascadeColumnValues; + String lockId; + + public Map getQueryBank() { + return queryBank; + } + + public UpdateDataObject setQueryBank(Map queryBank) { + this.queryBank = queryBank; + return this; + } + + public String getKeyspace() { + return keyspace; + } + + public UpdateDataObject setKeyspace(String keyspace) { + this.keyspace = keyspace; + return this; + } + + public String getTableName() { + return tableName; + } + + public UpdateDataObject setTableName(String tableName) { + this.tableName = tableName; + return this; + } + + public String getPrimaryKey() { + return primaryKey; + } + + public UpdateDataObject setPrimaryKey(String primaryKey) { + this.primaryKey = primaryKey; + return this; + } + + public String getPrimaryKeyValue() { + return primaryKeyValue; + } + + public UpdateDataObject setPrimaryKeyValue(String primaryKeyValue) { + this.primaryKeyValue = primaryKeyValue; + return this; + } + + public String getPlanId() { + return planId; + } + + public UpdateDataObject setPlanId(String planId) { + this.planId = planId; + return this; + } + + public String getCascadeColumnName() { + return cascadeColumnName; + } + + public UpdateDataObject setCascadeColumnName(String cascadeColumnName) { + this.cascadeColumnName = cascadeColumnName; + return this; + } + + public Map getCascadeColumnValues() { + return cascadeColumnValues; + } + + public UpdateDataObject setLockId(String lockId) { + this.lockId=lockId; + return this; + } + + public String getLockId() { + return lockId; + } + + public UpdateDataObject setCascadeColumnValues(Map cascadeColumnValues) { + this.cascadeColumnValues = cascadeColumnValues; + return this; + } + + +} -- cgit 1.2.3-korg