Fix Checkstyle issues
[clamp.git] / src / main / java / org / onap / clamp / clds / dao / CldsDao.java
index 5da26b1..1cff6bb 100644 (file)
@@ -5,6 +5,8 @@
  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights
  *                             reserved.
  * ================================================================================
+ * 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.
  * You may obtain a copy of the License at
@@ -26,7 +28,6 @@ package org.onap.clamp.clds.dao;
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 
-import java.io.InputStream;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -35,7 +36,6 @@ import java.util.Map;
 
 import javax.sql.DataSource;
 
-import org.onap.clamp.clds.model.CldsDbServiceCache;
 import org.onap.clamp.clds.model.CldsDictionary;
 import org.onap.clamp.clds.model.CldsDictionaryItem;
 import org.onap.clamp.clds.model.CldsEvent;
@@ -43,11 +43,11 @@ import org.onap.clamp.clds.model.CldsModel;
 import org.onap.clamp.clds.model.CldsModelInstance;
 import org.onap.clamp.clds.model.CldsModelProp;
 import org.onap.clamp.clds.model.CldsMonitoringDetails;
-import org.onap.clamp.clds.model.CldsServiceData;
 import org.onap.clamp.clds.model.CldsTemplate;
 import org.onap.clamp.clds.model.CldsToscaModel;
 import org.onap.clamp.clds.model.ValueItem;
-import org.springframework.dao.EmptyResultDataAccessException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
 import org.springframework.jdbc.core.namedparam.SqlParameterSource;
@@ -86,14 +86,17 @@ public class CldsDao {
     /**
      * Log message when instantiating.
      */
-    public CldsDao() {
+    @Autowired
+    public CldsDao(@Qualifier("cldsDataSource") DataSource dataSource) {
         logger.info("CldsDao instantiating...");
+        setDataSource(dataSource);
     }
 
     /**
      * When dataSource is provided, instantiate spring jdbc objects.
      *
-     * @param dataSource the data source
+     * @param dataSource
+     *        the data source
      */
     public void setDataSource(DataSource dataSource) {
         this.jdbcTemplateObject = new JdbcTemplate(dataSource);
@@ -117,7 +120,8 @@ public class CldsDao {
     /**
      * Get a model from the database given the model name.
      *
-     * @param modelName the model name
+     * @param modelName
+     *        the model name
      * @return the model
      */
     public CldsModel getModel(String modelName) {
@@ -138,16 +142,19 @@ public class CldsDao {
     /**
      * Get a model from the database given the controlNameUuid.
      *
-     * @param controlNameUuid the control name uuid
+     * @param controlNameUuid
+     *        the control name uuid
      * @return the model by uuid
      */
     public CldsModel getModelByUuid(String controlNameUuid) {
         return getModel(null, controlNameUuid);
     }
+
     /**
      * Get a model and template information from the database given the model name.
      *
-     * @param modelName the model name
+     * @param modelName
+     *        the model name
      * @return model model template
      */
 
@@ -179,8 +186,10 @@ public class CldsDao {
      * Update model in the database using parameter values and return updated model
      * object.
      *
-     * @param model  the model
-     * @param userid the userid
+     * @param model
+     *        the model
+     * @param userid
+     *        the userid
      * @return model
      */
     public CldsModel setModel(CldsModel model, String userid) {
@@ -208,8 +217,10 @@ public class CldsDao {
      * Inserts new modelInstance in the database using parameter values and return
      * updated model object.
      *
-     * @param model              the model
-     * @param modelInstancesList the model instances list
+     * @param model
+     *        the model
+     * @param modelInstancesList
+     *        the model instances list
      */
     public void insModelInstance(CldsModel model, List<CldsModelInstance> modelInstancesList) {
         // Delete all existing model instances for given controlNameUUID
@@ -241,10 +252,14 @@ public class CldsDao {
      * Insert an event in the database - require either modelName or
      * controlNamePrefix/controlNameUuid.
      *
-     * @param modelName         the model name
-     * @param controlNamePrefix the control name prefix
-     * @param controlNameUuid   the control name uuid
-     * @param cldsEvent         the clds event
+     * @param modelName
+     *        the model name
+     * @param controlNamePrefix
+     *        the control name prefix
+     * @param controlNameUuid
+     *        the control name uuid
+     * @param cldsEvent
+     *        the clds event
      * @return clds event
      */
     public CldsEvent insEvent(String modelName, String controlNamePrefix, String controlNameUuid, CldsEvent cldsEvent) {
@@ -268,8 +283,10 @@ public class CldsDao {
     /**
      * Update event with process instance id.
      *
-     * @param eventId           the event id
-     * @param processInstanceId the process instance id
+     * @param eventId
+     *        the event id
+     * @param processInstanceId
+     *        the process instance id
      */
     public void updEvent(String eventId, String processInstanceId) {
         SqlParameterSource in = new MapSqlParameterSource().addValue("v_event_id", eventId)
@@ -291,8 +308,10 @@ public class CldsDao {
      * Update template in the database using parameter values and return updated
      * template object.
      *
-     * @param template the template
-     * @param userid   the userid
+     * @param template
+     *        the template
+     * @param userid
+     *        the userid
      */
     public void setTemplate(CldsTemplate template, String userid) {
         SqlParameterSource in = new MapSqlParameterSource().addValue("v_template_name", template.getName())
@@ -322,7 +341,8 @@ public class CldsDao {
     /**
      * Get a template from the database given the model name.
      *
-     * @param templateName the template name
+     * @param templateName
+     *        the template name
      * @return model template
      */
     public CldsTemplate getTemplate(String templateName) {
@@ -367,7 +387,7 @@ public class CldsDao {
     public List<CldsModelProp> getDeployedModelProperties() {
         List<CldsModelProp> cldsModelPropList = new ArrayList<>();
         String modelsSql = "select m.model_id, m.model_name, mp.model_prop_id, mp.model_prop_text FROM model m, "
-                + "model_properties mp, event e "
+            + "model_properties mp, event e "
             + "WHERE m.model_prop_id = mp.model_prop_id and m.event_id = e.event_id and e.action_cd = 'DEPLOY'";
         List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(modelsSql);
         CldsModelProp cldsModelProp = null;
@@ -421,7 +441,8 @@ public class CldsDao {
     /**
      * Method to delete model from database.
      *
-     * @param modelName the model name
+     * @param modelName
+     *        the model name
      */
     public void deleteModel(String modelName) {
         SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", modelName);
@@ -461,7 +482,8 @@ public class CldsDao {
     /**
      * Method to retrieve a tosca models by Policy Type from database.
      *
-     * @param policyType the policy type
+     * @param policyType
+     *        the policy type
      * @return List of CldsToscaModel
      */
     public List<CldsToscaModel> getToscaModelByPolicyType(String policyType) {
@@ -471,7 +493,8 @@ public class CldsDao {
     /**
      * Method to retrieve a tosca models by toscaModelName, version from database.
      *
-     * @param toscaModelName the tosca model name
+     * @param toscaModelName
+     *        the tosca model name
      * @return List of CldsToscaModel
      */
     public List<CldsToscaModel> getToscaModelByName(String toscaModelName) {
@@ -484,15 +507,15 @@ public class CldsDao {
         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
         List<CldsToscaModel> cldsToscaModels = new ArrayList<>();
 
-        String toscaModelSql = "SELECT tm.tosca_model_name, tm.tosca_model_id, tm.policy_type, "
-            + "tmr.tosca_model_revision_id, tmr.tosca_model_json, tmr.version, tmr.user_id, tmr.createdTimestamp, "
-            + "tmr.lastUpdatedTimestamp "
-            + ((toscaModelName != null) ? (", tmr.tosca_model_yaml ") : " ")
-            + "FROM tosca_model tm, tosca_model_revision tmr WHERE tm.tosca_model_id = tmr.tosca_model_id "
-            + ((toscaModelName != null) ? (" AND tm.tosca_model_name = '" + toscaModelName + "'") : " ")
-            + ((policyType != null) ? (" AND tm.policy_type = '" + policyType + "'") : " ")
-            + "AND tmr.version = (select max(version) from tosca_model_revision st "
-            + "where tmr.tosca_model_id=st.tosca_model_id)";
+        String toscaModelSql = new StringBuilder("SELECT tm.tosca_model_name, tm.tosca_model_id, tm.policy_type, "
+                + "tmr.tosca_model_revision_id, tmr.tosca_model_json, tmr.version, tmr.user_id, tmr.createdTimestamp,"
+                + "tmr.lastUpdatedTimestamp").append(toscaModelName != null ? (", tmr.tosca_model_yaml") : "")
+                .append(" FROM tosca_model tm, tosca_model_revision tmr WHERE tm.tosca_model_id = tmr.tosca_model_id")
+                .append(toscaModelName != null ? (" AND tm.tosca_model_name = '" + toscaModelName + "'") : "")
+                .append(policyType != null ? (" AND tm.policy_type = '" + policyType + "'") : "")
+                .append(" AND tmr.version = (select max(version) from tosca_model_revision st where tmr.tosca_model_id"
+                + "=st.tosca_model_id)")
+                .toString();
 
         List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(toscaModelSql);
 
@@ -520,8 +543,10 @@ public class CldsDao {
     /**
      * Method to upload a new version of Tosca Model Yaml in Database.
      *
-     * @param cldsToscaModel the clds tosca model
-     * @param userId         the user id
+     * @param cldsToscaModel
+     *        the clds tosca model
+     * @param userId
+     *        the user id
      * @return CldsToscaModel clds tosca model
      */
     public CldsToscaModel updateToscaModelWithNewVersion(CldsToscaModel cldsToscaModel, String userId) {
@@ -537,8 +562,10 @@ public class CldsDao {
     /**
      * Method to upload a new Tosca model Yaml in DB. Default version is 1.0
      *
-     * @param cldsToscaModel the clds tosca model
-     * @param userId         the user id
+     * @param cldsToscaModel
+     *        the clds tosca model
+     * @param userId
+     *        the user id
      * @return CldsToscaModel clds tosca model
      */
     public CldsToscaModel insToscaModel(CldsToscaModel cldsToscaModel, String userId) {
@@ -558,7 +585,8 @@ public class CldsDao {
     /**
      * Method to insert a new Dictionary in Database.
      *
-     * @param cldsDictionary the clds dictionary
+     * @param cldsDictionary
+     *        the clds dictionary
      */
     public void insDictionary(CldsDictionary cldsDictionary) {
         SqlParameterSource in = new MapSqlParameterSource()
@@ -571,14 +599,20 @@ public class CldsDao {
     /**
      * Method to update Dictionary with new info in Database.
      *
-     * @param dictionaryId   the dictionary id
-     * @param cldsDictionary the clds dictionary
-     * @param userId         the user id
+     * @param dictionaryId
+     *        the dictionary id
+     * @param cldsDictionary
+     *        the clds dictionary
+     * @param userId
+     *        the user id
      */
     public void updateDictionary(String dictionaryId, CldsDictionary cldsDictionary, String userId) {
 
-        String dictionarySql = "UPDATE dictionary " + "SET dictionary_name = '" + cldsDictionary.getDictionaryName()
-            + "', modified_by = '" + userId + "'" + "WHERE dictionary_id = '" + dictionaryId + "'";
+        String dictionarySql = new StringBuilder("UPDATE dictionary SET dictionary_name = '")
+                .append(cldsDictionary.getDictionaryName())
+                .append("', modified_by = '").append(userId)
+                .append("'WHERE dictionary_id = '").append(dictionaryId).append("'")
+                .toString();
         jdbcTemplateObject.update(dictionarySql);
         cldsDictionary.setUpdatedBy(userId);
     }
@@ -586,20 +620,30 @@ public class CldsDao {
     /**
      * Method to get list of Dictionaries from the Database.
      *
-     * @param dictionaryId   the dictionary id
-     * @param dictionaryName the dictionary name
+     * @param dictionaryId
+     *        the dictionary id
+     * @param dictionaryName
+     *        the dictionary name
      * @return dictionary
      */
     public List<CldsDictionary> getDictionary(String dictionaryId, String dictionaryName) {
         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
         List<CldsDictionary> dictionaries = new ArrayList<>();
-        String dictionarySql = "SELECT dictionary_id, dictionary_name, created_by, modified_by, "
-            + "timestamp FROM dictionary"
-            + ((dictionaryId != null || dictionaryName != null)
-                ? (" WHERE " + ((dictionaryName != null) ? ("dictionary_name = '" + dictionaryName + "'") : "")
-                    + ((dictionaryId != null && dictionaryName != null) ? (" AND ") : "")
-                    + ((dictionaryId != null) ? ("dictionary_id = '" + dictionaryId + "'") : ""))
-                : "");
+
+        String whereFilter = " WHERE ";
+        if (dictionaryName != null) {
+            whereFilter += "dictionary_name = '" + dictionaryName + "'";
+            if (dictionaryId != null) {
+                whereFilter += " AND dictionary_id = '" + dictionaryId + "'";
+            }
+        } else if (dictionaryId != null) {
+            whereFilter += "dictionary_id = '" + dictionaryId + "'";
+        } else {
+            whereFilter = "";
+        }
+        String dictionarySql = new StringBuilder("SELECT dictionary_id, dictionary_name, created_by, "
+                + "modified_by, timestamp FROM dictionary")
+                .append(whereFilter).toString();
 
         List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(dictionarySql);
 
@@ -620,8 +664,10 @@ public class CldsDao {
     /**
      * Method to insert a new Dictionary Element for given dictionary in Database.
      *
-     * @param cldsDictionaryItem the clds dictionary item
-     * @param userId             the user id
+     * @param cldsDictionaryItem
+     *        the clds dictionary item
+     * @param userId
+     *        the user id
      */
     public void insDictionarElements(CldsDictionaryItem cldsDictionaryItem, String userId) {
         SqlParameterSource in = new MapSqlParameterSource()
@@ -638,19 +684,25 @@ public class CldsDao {
      * Method to update Dictionary Elements with new info for a given dictionary in
      * Database.
      *
-     * @param dictionaryElementId the dictionary element id
-     * @param cldsDictionaryItem  the clds dictionary item
-     * @param userId              the user id
+     * @param dictionaryElementId
+     *        the dictionary element id
+     * @param cldsDictionaryItem
+     *        the clds dictionary item
+     * @param userId
+     *        the user id
      */
     public void updateDictionaryElements(String dictionaryElementId, CldsDictionaryItem cldsDictionaryItem,
         String userId) {
 
-        String dictionarySql = "UPDATE dictionary_elements SET dict_element_name = '"
-            + cldsDictionaryItem.getDictElementName() + "', dict_element_short_name = '"
-            + cldsDictionaryItem.getDictElementShortName() + "', dict_element_description= '"
-            + cldsDictionaryItem.getDictElementDesc() + "', dict_element_type = '"
-            + cldsDictionaryItem.getDictElementType() + "', modified_by = '" + userId + "' "
-            + "WHERE dict_element_id = '" + dictionaryElementId + "'";
+        String dictionarySql = new StringBuilder().append("UPDATE dictionary_elements SET dict_element_name = '")
+                .append(cldsDictionaryItem.getDictElementName())
+                .append("', dict_element_short_name = '").append(cldsDictionaryItem.getDictElementShortName())
+                .append("', dict_element_description= '").append(cldsDictionaryItem.getDictElementDesc())
+                .append("', dict_element_type = '").append(cldsDictionaryItem.getDictElementType())
+                .append("', modified_by = '").append(userId).append("'")
+                .append(" WHERE dict_element_id = '")
+                .append(dictionaryElementId).append("'")
+                .toString();
         jdbcTemplateObject.update(dictionarySql);
         cldsDictionaryItem.setUpdatedBy(userId);
     }
@@ -659,23 +711,25 @@ public class CldsDao {
      * Method to get list of all dictionary elements for a given dictionary in the
      * Database.
      *
-     * @param dictionaryName       the dictionary name
-     * @param dictionaryId         the dictionary id
-     * @param dictElementShortName the dict element short name
+     * @param dictionaryName
+     *        the dictionary name
+     * @param dictionaryId
+     *        the dictionary id
+     * @param dictElementShortName
+     *        the dict element short name
      * @return dictionary elements
      */
     public List<CldsDictionaryItem> getDictionaryElements(String dictionaryName, String dictionaryId,
         String dictElementShortName) {
         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
         List<CldsDictionaryItem> dictionaryItems = new ArrayList<>();
-        String dictionarySql = "SELECT de.dict_element_id, de.dictionary_id, de.dict_element_name, "
-            + "de.dict_element_short_name, de.dict_element_description, de.dict_element_type, de.created_by, "
-            + "de.modified_by, de.timestamp  "
-            + "FROM dictionary_elements de, dictionary d WHERE de.dictionary_id = d.dictionary_id "
-            + ((dictionaryId != null) ? (" AND d.dictionary_id = '" + dictionaryId + "'") : "")
-            + ((dictElementShortName != null) ? (" AND de.dict_element_short_name = '" + dictElementShortName + "'")
-                : "")
-            + ((dictionaryName != null) ? (" AND dictionary_name = '" + dictionaryName + "'") : "");
+        String dictionarySql = new StringBuilder("SELECT de.dict_element_id, de.dictionary_id, de.dict_element_name, " +
+                "de.dict_element_short_name, de.dict_element_description, de.dict_element_type, de.created_by, " +
+                "de.modified_by, de.timestamp FROM dictionary_elements de, " +
+                "dictionary d WHERE de.dictionary_id = d.dictionary_id")
+                .append((dictionaryId != null) ? (" AND d.dictionary_id = '" + dictionaryId + "'") : "")
+                .append((dictElementShortName != null) ? (" AND de.dict_element_short_name = '" + dictElementShortName + "'") : "")
+                .append((dictionaryName != null) ? (" AND dictionary_name = '" + dictionaryName + "'") : "").toString();
 
         List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(dictionarySql);
 
@@ -701,13 +755,15 @@ public class CldsDao {
      * Method to get Map of all dictionary elements with key as dictionary short
      * name and value as the full name.
      *
-     * @param dictionaryElementType the dictionary element type
+     * @param dictionaryElementType
+     *        the dictionary element type
      * @return Map of dictionary elements as key value pair
      */
     public Map<String, String> getDictionaryElementsByType(String dictionaryElementType) {
         Map<String, String> dictionaryItems = new HashMap<>();
-        String dictionarySql = "SELECT dict_element_name, dict_element_short_name " + "FROM dictionary_elements "
-            + "WHERE dict_element_type = '" + dictionaryElementType + "'";
+        String dictionarySql = new StringBuilder("SELECT dict_element_name, dict_element_short_name " +
+                "FROM dictionary_elements WHERE dict_element_type = '")
+                .append(dictionaryElementType).append("'").toString();
 
         List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(dictionarySql);