Fix Checkstyle issues
[clamp.git] / src / main / java / org / onap / clamp / clds / dao / CldsDao.java
index d3e89fb..1cff6bb 100644 (file)
@@ -2,9 +2,11 @@
  * ============LICENSE_START=======================================================
  * ONAP CLAMP
  * ================================================================================
- * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
+ * 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,20 +28,14 @@ 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.Arrays;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
-import java.util.stream.Collectors;
 
 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;
@@ -47,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,15 +82,21 @@ public class CldsDao {
     private SimpleJdbcCall procInsertDictionaryElement;
 
     private static final String DATE_FORMAT = "MM-dd-yyyy HH:mm:ss";
+
     /**
-     * Log message when instantiating
+     * 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
      */
     public void setDataSource(DataSource dataSource) {
         this.jdbcTemplateObject = new JdbcTemplate(dataSource);
@@ -117,18 +119,15 @@ public class CldsDao {
 
     /**
      * Get a model from the database given the model name.
+     *
+     * @param modelName
+     *        the model name
+     * @return the model
      */
     public CldsModel getModel(String modelName) {
         return getModel(modelName, null);
     }
 
-    /**
-     * Get a model from the database given the controlNameUuid.
-     */
-    public CldsModel getModelByUuid(String controlNameUuid) {
-        return getModel(null, controlNameUuid);
-    }
-
     // Get a model from the database given the model name or a controlNameUuid.
     private CldsModel getModel(String modelName, String controlNameUuid) {
         CldsModel model = new CldsModel();
@@ -140,12 +139,25 @@ public class CldsDao {
         return model;
     }
 
+    /**
+     * Get a model from the database given the controlNameUuid.
+     *
+     * @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
-     * @return model
+     *        the model name
+     * @return model model template
      */
+
     public CldsModel getModelTemplate(String modelName) {
         CldsModel model = new CldsModel();
         model.setName(modelName);
@@ -175,8 +187,10 @@ public class CldsDao {
      * object.
      *
      * @param model
+     *        the model
      * @param userid
-     * @return
+     *        the userid
+     * @return model
      */
     public CldsModel setModel(CldsModel model, String userid) {
         SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", model.getName())
@@ -184,6 +198,7 @@ public class CldsDao {
             .addValue("v_model_prop_text", model.getPropText())
             .addValue("v_model_blueprint_text", model.getBlueprintText())
             .addValue("v_service_type_id", model.getTypeId()).addValue("v_deployment_id", model.getDeploymentId())
+            .addValue("v_deployment_status_url", model.getDeploymentStatusUrl())
             .addValue("v_control_name_prefix", model.getControlNamePrefix())
             .addValue(V_CONTROL_NAME_UUID, model.getControlNameUuid());
         Map<String, Object> out = logSqlExecution(procSetModel, in);
@@ -203,8 +218,9 @@ public class CldsDao {
      * updated model object.
      *
      * @param model
+     *        the model
      * @param modelInstancesList
-     * @return
+     *        the model instances list
      */
     public void insModelInstance(CldsModel model, List<CldsModelInstance> modelInstancesList) {
         // Delete all existing model instances for given controlNameUUID
@@ -237,10 +253,14 @@ public class CldsDao {
      * controlNamePrefix/controlNameUuid.
      *
      * @param modelName
+     *        the model name
      * @param controlNamePrefix
+     *        the control name prefix
      * @param controlNameUuid
+     *        the control name uuid
      * @param cldsEvent
-     * @return
+     *        the clds event
+     * @return clds event
      */
     public CldsEvent insEvent(String modelName, String controlNamePrefix, String controlNameUuid, CldsEvent cldsEvent) {
         CldsEvent event = new CldsEvent();
@@ -264,7 +284,9 @@ public class CldsDao {
      * Update event with 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)
@@ -273,7 +295,7 @@ public class CldsDao {
     }
 
     /**
-     * Return list of model names
+     * Return list of model names.
      *
      * @return model names
      */
@@ -287,7 +309,9 @@ public class CldsDao {
      * template object.
      *
      * @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())
@@ -305,7 +329,7 @@ public class CldsDao {
     }
 
     /**
-     * Return list of template names
+     * Return list of template names.
      *
      * @return template names
      */
@@ -318,7 +342,8 @@ public class CldsDao {
      * Get a template from the database given the model name.
      *
      * @param templateName
-     * @return model
+     *        the template name
+     * @return model template
      */
     public CldsTemplate getTemplate(String templateName) {
         CldsTemplate template = new CldsTemplate();
@@ -338,45 +363,6 @@ public class CldsDao {
         return template;
     }
 
-    public void clearServiceCache() {
-        String clearCldsServiceCacheSql = "TRUNCATE clds_service_cache";
-        jdbcTemplateObject.execute(clearCldsServiceCacheSql);
-    }
-
-    public CldsServiceData getCldsServiceCache(String invariantUUID) {
-        CldsServiceData cldsServiceData = null;
-        try {
-            String getCldsServiceSQL = "SELECT * , TIMESTAMPDIFF(SECOND, timestamp, CURRENT_TIMESTAMP()) FROM clds_service_cache where invariant_service_id  = ? ";
-            cldsServiceData = jdbcTemplateObject.queryForObject(getCldsServiceSQL, new Object[] { invariantUUID },
-                new CldsServiceDataMapper());
-            if (cldsServiceData != null) {
-                logger.info("CldsServiceData found in cache for Service Invariant ID:"
-                    + cldsServiceData.getServiceInvariantUUID());
-                return cldsServiceData;
-            } else {
-                logger.warn("CldsServiceData not found in cache for Service Invariant ID:" + invariantUUID);
-                return null;
-            }
-        } catch (EmptyResultDataAccessException e) {
-            logger.info("CldsServiceData not found in cache for Service Invariant ID: " + invariantUUID);
-            logger.debug("CldsServiceData not found in cache for Service Invariant ID: " + invariantUUID, e);
-            return null;
-        }
-    }
-
-    public void setCldsServiceCache(CldsDbServiceCache cldsDBServiceCache) {
-        if (cldsDBServiceCache != null && cldsDBServiceCache.getInvariantId() != null
-            && cldsDBServiceCache.getServiceId() != null) {
-            String invariantUuid = cldsDBServiceCache.getInvariantId();
-            String serviceUuid = cldsDBServiceCache.getServiceId();
-            InputStream is = cldsDBServiceCache.getCldsDataInstream();
-            String insertCldsServiceCacheSql = "INSERT INTO clds_service_cache"
-                + "(invariant_service_id,service_id,timestamp,object_data) VALUES"
-                + "(?,?,CURRENT_TIMESTAMP,?) ON DUPLICATE KEY UPDATE invariant_service_id = VALUES(invariant_service_id) , timestamp = CURRENT_TIMESTAMP , object_data = VALUES(object_data) ";
-            jdbcTemplateObject.update(insertCldsServiceCacheSql, invariantUuid, serviceUuid, is);
-        }
-    }
-
     private static Map<String, Object> logSqlExecution(SimpleJdbcCall call, SqlParameterSource source) {
         try {
             return call.execute(source);
@@ -386,6 +372,9 @@ public class CldsDao {
         }
     }
 
+    /**
+     * Do health check.
+     */
     public void doHealthCheck() {
         jdbcTemplateObject.execute(HEALTHCHECK);
     }
@@ -397,7 +386,8 @@ 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 "
+        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 "
             + "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;
@@ -424,10 +414,11 @@ public class CldsDao {
      *         TEMPLATE_NAME | Template used to generate the ClosedLoop model.
      *         ACTION_CD | Current state of the ClosedLoop in CLDS application.
      */
-    public List<CldsMonitoringDetails> getCLDSMonitoringDetails() {
+    public List<CldsMonitoringDetails> getCldsMonitoringDetails() {
         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
         List<CldsMonitoringDetails> cldsMonitoringDetailsList = new ArrayList<>();
-        String modelsSql = "SELECT CONCAT(M.CONTROL_NAME_PREFIX, M.CONTROL_NAME_UUID) AS CLOSELOOP_NAME , M.MODEL_NAME, M.SERVICE_TYPE_ID, M.DEPLOYMENT_ID, T.TEMPLATE_NAME, E.ACTION_CD, E.USER_ID, E.TIMESTAMP "
+        String modelsSql = "SELECT CONCAT(M.CONTROL_NAME_PREFIX, M.CONTROL_NAME_UUID) AS CLOSELOOP_NAME , "
+            + "M.MODEL_NAME, M.SERVICE_TYPE_ID, M.DEPLOYMENT_ID, T.TEMPLATE_NAME, E.ACTION_CD, E.USER_ID, E.TIMESTAMP "
             + "FROM MODEL M, TEMPLATE T, EVENT E " + "WHERE M.TEMPLATE_ID = T.TEMPLATE_ID AND M.EVENT_ID = E.EVENT_ID "
             + "ORDER BY ACTION_CD";
         List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(modelsSql);
@@ -451,6 +442,7 @@ public class CldsDao {
      * Method to delete model from database.
      *
      * @param modelName
+     *        the model name
      */
     public void deleteModel(String modelName) {
         SqlParameterSource in = new MapSqlParameterSource().addValue("v_model_name", modelName);
@@ -475,12 +467,12 @@ public class CldsDao {
         model.getEvent().setUserid((String) out.get("v_event_user_id"));
         model.setTypeId((String) out.get("v_service_type_id"));
         model.setDeploymentId((String) out.get("v_deployment_id"));
+        model.setDeploymentStatusUrl((String) out.get("v_deployment_status_url"));
     }
 
     /**
      * Method to retrieve a tosca models by Policy Type from database.
      *
-     * @param policyType
      * @return List of CldsToscaModel
      */
     public List<CldsToscaModel> getAllToscaModels() {
@@ -491,6 +483,7 @@ public class CldsDao {
      * Method to retrieve a tosca models by Policy Type from database.
      *
      * @param policyType
+     *        the policy type
      * @return List of CldsToscaModel
      */
     public List<CldsToscaModel> getToscaModelByPolicyType(String policyType) {
@@ -500,7 +493,8 @@ public class CldsDao {
     /**
      * Method to retrieve a tosca models by toscaModelName, version from database.
      *
-     * @param policyType
+     * @param toscaModelName
+     *        the tosca model name
      * @return List of CldsToscaModel
      */
     public List<CldsToscaModel> getToscaModelByName(String toscaModelName) {
@@ -512,41 +506,48 @@ public class CldsDao {
     private List<CldsToscaModel> getToscaModel(String toscaModelName, String policyType) {
         SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
         List<CldsToscaModel> cldsToscaModels = new ArrayList<>();
-        MapSqlParameterSource params = new MapSqlParameterSource();
 
-        String toscaModelSql = "SELECT tm.tosca_model_name, tm.tosca_model_id, tm.policy_type, tmr.tosca_model_revision_id, tmr.version, tmr.user_id, tmr.createdTimestamp, tmr.lastUpdatedTimestamp, tmr.tosca_model_yaml FROM tosca_model tm, tosca_model_revision tmr WHERE tm.tosca_model_id = tmr.tosca_model_id ";
-        if (toscaModelName != null) {
-            toscaModelSql += " AND tm.tosca_model_name = :toscaModelName";
-            params.addValue("toscaModelName", toscaModelName);
-        }
-        if (policyType != null) {
-            toscaModelSql += " AND tm.policy_type = :policyType";
-            params.addValue("policyType", policyType);
+        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);
+
+        if (rows != null) {
+            rows.forEach(row -> {
+                CldsToscaModel cldsToscaModel = new CldsToscaModel();
+                cldsToscaModel.setId((String) row.get("tosca_model_id"));
+                cldsToscaModel.setPolicyType((String) row.get("policy_type"));
+                cldsToscaModel.setToscaModelName((String) row.get("tosca_model_name"));
+                cldsToscaModel.setUserId((String) row.get("user_id"));
+                cldsToscaModel.setRevisionId((String) row.get("tosca_model_revision_id"));
+                cldsToscaModel.setToscaModelJson((String) row.get("tosca_model_json"));
+                cldsToscaModel.setVersion(((Double) row.get("version")));
+                cldsToscaModel.setCreatedDate(sdf.format(row.get("createdTimestamp")));
+                cldsToscaModel.setLastUpdatedDate(sdf.format(row.get("lastUpdatedTimestamp")));
+                if (toscaModelName != null) {
+                    cldsToscaModel.setToscaModelYaml((String) row.get("tosca_model_yaml"));
+                }
+                cldsToscaModels.add(cldsToscaModel);
+            });
         }
-        toscaModelSql += " AND tmr.version = (select max(version) from tosca_model_revision st where tmr.tosca_model_id=st.tosca_model_id)";
-
-        Optional.ofNullable(jdbcTemplateObject.queryForList(toscaModelSql, params)).orElse(Collections.emptyList()).forEach(row -> {
-            CldsToscaModel cldsToscaModel = new CldsToscaModel();
-            cldsToscaModel.setId((String) row.get("tosca_model_id"));
-            cldsToscaModel.setPolicyType((String) row.get("policy_type"));
-            cldsToscaModel.setToscaModelName((String) row.get("tosca_model_name"));
-            cldsToscaModel.setUserId((String) row.get("user_id"));
-            cldsToscaModel.setRevisionId((String) row.get("tosca_model_revision_id"));
-            cldsToscaModel.setVersion(((Double) row.get("version")));
-            cldsToscaModel.setCreatedDate(sdf.format(row.get("createdTimestamp")));
-            cldsToscaModel.setToscaModelYaml((String) row.get("tosca_model_yaml"));
-            cldsToscaModels.add(cldsToscaModel);
-        });
         return cldsToscaModels;
     }
 
     /**
-     * Method to upload a new version of Tosca Model Yaml in Database
+     * Method to upload a new version of Tosca Model Yaml in Database.
      *
      * @param cldsToscaModel
+     *        the clds tosca model
      * @param userId
-     * @return CldsToscaModel
-     *
+     *        the user id
+     * @return CldsToscaModel clds tosca model
      */
     public CldsToscaModel updateToscaModelWithNewVersion(CldsToscaModel cldsToscaModel, String userId) {
         SqlParameterSource in = new MapSqlParameterSource().addValue("v_tosca_model_id", cldsToscaModel.getId())
@@ -562,8 +563,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
-     * @return CldsToscaModel
+     *        the user id
+     * @return CldsToscaModel clds tosca model
      */
     public CldsToscaModel insToscaModel(CldsToscaModel cldsToscaModel, String userId) {
         SqlParameterSource in = new MapSqlParameterSource()
@@ -580,9 +583,10 @@ public class CldsDao {
     }
 
     /**
-     * Method to insert a new Dictionary in Database
+     * Method to insert a new Dictionary in Database.
      *
      * @param cldsDictionary
+     *        the clds dictionary
      */
     public void insDictionary(CldsDictionary cldsDictionary) {
         SqlParameterSource in = new MapSqlParameterSource()
@@ -593,56 +597,77 @@ public class CldsDao {
     }
 
     /**
-     * Method to update Dictionary with new info in Database
+     * Method to update Dictionary with new info in Database.
      *
      * @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 = :dictionary_name, modified_by = :modified_by WHERE dictionary_id = :dictionary_id";
-        SqlParameterSource namedParameters = new MapSqlParameterSource()
-            .addValue("dictionary_name", cldsDictionary.getDictionaryName()).addValue("modified_by", userId)
-            .addValue("dictionary_id", dictionaryId);
-        jdbcTemplateObject.update(dictionarySql, namedParameters);
+
+        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);
     }
 
     /**
-     * Method to get list of Dictionaries from the Database
+     * Method to get list of Dictionaries from the Database.
      *
      * @param dictionaryId
+     *        the dictionary id
      * @param dictionaryName
-     * @return
+     *        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 WHERE ";
-        MapSqlParameterSource namedParameters = new MapSqlParameterSource();
-        Optional.ofNullable(dictionaryName).ifPresent(dn -> namedParameters.addValue("dictionary_name", dn));
-        Optional.ofNullable(dictionaryId).ifPresent(dn -> namedParameters.addValue("dictionary_id", dn));
-        dictionarySql += Optional.ofNullable(namedParameters.getParameterNames()).filter(a -> a.length > 0)
-            .map(Arrays::stream).map(s -> s.map(param -> param + " = :" + param).collect(Collectors.joining(" AND ")))
-            .orElse("1");
-
-        Optional.ofNullable(jdbcTemplateObject.queryForList(dictionarySql, namedParameters)).orElse(Collections.emptyList()).forEach(row -> {
-            CldsDictionary cldsDictionary = new CldsDictionary();
-            cldsDictionary.setDictionaryId((String) row.get("dictionary_id"));
-            cldsDictionary.setDictionaryName((String) row.get("dictionary_name"));
-            cldsDictionary.setCreatedBy((String) row.get("created_by"));
-            cldsDictionary.setUpdatedBy((String) row.get("modified_by"));
-            cldsDictionary.setLastUpdatedDate(sdf.format(row.get("timestamp")));
-            dictionaries.add(cldsDictionary);
-        });
+
+        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);
+
+        if (rows != null) {
+            rows.forEach(row -> {
+                CldsDictionary cldsDictionary = new CldsDictionary();
+                cldsDictionary.setDictionaryId((String) row.get("dictionary_id"));
+                cldsDictionary.setDictionaryName((String) row.get("dictionary_name"));
+                cldsDictionary.setCreatedBy((String) row.get("created_by"));
+                cldsDictionary.setUpdatedBy((String) row.get("modified_by"));
+                cldsDictionary.setLastUpdatedDate(sdf.format(row.get("timestamp")));
+                dictionaries.add(cldsDictionary);
+            });
+        }
         return dictionaries;
     }
 
     /**
-     * Method to insert a new Dictionary Element for given dictionary in Database
+     * Method to insert a new Dictionary Element for given dictionary in Database.
      *
      * @param cldsDictionaryItem
+     *        the clds dictionary item
      * @param userId
+     *        the user id
      */
     public void insDictionarElements(CldsDictionaryItem cldsDictionaryItem, String userId) {
         SqlParameterSource in = new MapSqlParameterSource()
@@ -657,69 +682,97 @@ public class CldsDao {
 
     /**
      * Method to update Dictionary Elements with new info for a given dictionary in
-     * Database
+     * Database.
      *
      * @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 = :dict_element_name, dict_element_short_name = :dict_element_short_name, dict_element_description = :dict_element_description,dict_element_type=:dict_element_type, modified_by = :modified_by WHERE dict_element_id = :dict_element_id";
-        SqlParameterSource namedParameters = new MapSqlParameterSource()
-            .addValue("dict_element_name", cldsDictionaryItem.getDictElementName())
-            .addValue("dict_element_short_name", cldsDictionaryItem.getDictElementShortName())
-            .addValue("dict_element_description", cldsDictionaryItem.getDictElementDesc())
-            .addValue("dict_element_type", cldsDictionaryItem.getDictElementType())
-            .addValue("modified_by", userId)
-            .addValue("dict_element_id", dictionaryElementId);
-        jdbcTemplateObject.update(dictionarySql, namedParameters);
+        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);
     }
 
     /**
      * Method to get list of all dictionary elements for a given dictionary in the
-     * Database
+     * Database.
      *
      * @param dictionaryName
+     *        the dictionary name
      * @param dictionaryId
+     *        the dictionary id
      * @param dictElementShortName
-     * @return
+     *        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<>();
-        MapSqlParameterSource namedParameters = new MapSqlParameterSource();
-        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 ";
-        if (dictionaryId != null) {
-            dictionarySql+=" AND d.dictionary_id = :dictionaryId";
-            namedParameters.addValue("dictionaryId", dictionaryId);
-        }
-        if (dictElementShortName!=null) {
-            dictionarySql+=" AND de.dict_element_short_name = :dictElementShortName";
-            namedParameters.addValue("dictElementShortName", dictElementShortName);
-        }
-        if (dictionaryName!=null) {
-            dictionarySql+=" AND dictionary_name = :dictionaryName";
-            namedParameters.addValue("dictionaryName", 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);
+
+        if (rows != null) {
+            rows.forEach(row -> {
+                CldsDictionaryItem dictionaryItem = new CldsDictionaryItem();
+                dictionaryItem.setDictElementId((String) row.get("dict_element_id"));
+                dictionaryItem.setDictionaryId((String) row.get("dictionary_id"));
+                dictionaryItem.setDictElementName((String) row.get("dict_element_name"));
+                dictionaryItem.setDictElementShortName((String) row.get("dict_element_short_name"));
+                dictionaryItem.setDictElementDesc((String) row.get("dict_element_description"));
+                dictionaryItem.setDictElementType((String) row.get("dict_element_type"));
+                dictionaryItem.setCreatedBy((String) row.get("created_by"));
+                dictionaryItem.setUpdatedBy((String) row.get("modified_by"));
+                dictionaryItem.setLastUpdatedDate(sdf.format(row.get("timestamp")));
+                dictionaryItems.add(dictionaryItem);
+            });
         }
+        return dictionaryItems;
+    }
 
-        Optional.ofNullable(jdbcTemplateObject.queryForList(dictionarySql,namedParameters)).orElse(Collections.emptyList()).forEach(row -> {
-            CldsDictionaryItem dictionaryItem = new CldsDictionaryItem();
-            dictionaryItem.setDictElementId((String) row.get("dict_element_id"));
-            dictionaryItem.setDictionaryId((String) row.get("dictionary_id"));
-            dictionaryItem.setDictElementName((String) row.get("dict_element_name"));
-            dictionaryItem.setDictElementShortName((String) row.get("dict_element_short_name"));
-            dictionaryItem.setDictElementDesc((String) row.get("dict_element_description"));
-            dictionaryItem.setDictElementType((String) row.get("dict_element_type"));
-            dictionaryItem.setCreatedBy((String) row.get("created_by"));
-            dictionaryItem.setUpdatedBy((String) row.get("modified_by"));
-            dictionaryItem.setLastUpdatedDate(sdf.format(row.get("timestamp")));
-            dictionaryItems.add(dictionaryItem);
-        });
+    /**
+     * 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
+     * @return Map of dictionary elements as key value pair
+     */
+    public Map<String, String> getDictionaryElementsByType(String dictionaryElementType) {
+        Map<String, String> dictionaryItems = new HashMap<>();
+        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);
+
+        if (rows != null) {
+            rows.forEach(row -> {
+                dictionaryItems.put(((String) row.get("dict_element_short_name")),
+                    ((String) row.get("dict_element_name")));
+            });
+        }
         return dictionaryItems;
     }
 }