+ model.setDeploymentId((String) out.get("v_deployment_id"));
+ }
+
+
+ /**
+ * Method to retrieve a tosca models by Policy Type from database.
+
+ * @param policyType
+ * @return List of CldsToscaModel
+ */
+ public List<CldsToscaModel> getAllToscaModels() {
+ return getToscaModel(null, null);
+ }
+
+ /**
+ * Method to retrieve a tosca models by Policy Type from database.
+
+ * @param policyType
+ * @return List of CldsToscaModel
+ */
+ public List<CldsToscaModel> getToscaModelByPolicyType(String policyType) {
+ return getToscaModel(null, policyType);
+ }
+
+ /**
+ * Method to retrieve a tosca models by toscaModelName, version from database.
+ *
+ * @param policyType
+ * @return List of CldsToscaModel
+ */
+ public List<CldsToscaModel> getToscaModelByName(String toscaModelName) {
+ return getToscaModel(toscaModelName, null);
+ }
+
+ // Retrieve the latest tosca model for a policy type or by tosca model name
+
+ private List<CldsToscaModel> getToscaModel(String toscaModelName, String policyType) {
+ SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
+ 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.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)";
+
+ List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(toscaModelSql);
+
+ if (rows != null && rows.size() > 0) {
+
+ rows.stream().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.setLastUpdatedDate(sdf.format(row.get("lastUpdatedTimestamp")));
+ if (toscaModelName != null) {
+ cldsToscaModel.setToscaModelYaml((String) row.get("tosca_model_yaml"));
+ }
+ cldsToscaModels.add(cldsToscaModel);
+ });
+
+ }
+ return cldsToscaModels;
+ }
+
+ // Retrieve Tosca Models & its revisions by policy Type.
+ private List<CldsToscaModelDetails> getAllToscaModelVersion(String toscaModelName, String policyType,
+ String version) {
+ SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
+ List<CldsToscaModelDetails> cldsToscaModelDetailsList = new ArrayList<>();
+ 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 "
+ + "FROM tosca_model tm, tosca_model_revision tmr " + "WHERE tmr.tosca_model_id = tm.tosca_model_id "
+ + ((policyType != null) ? (" AND tm.policy_type = '" + policyType + "'") : " ")
+ + ((toscaModelName != null) ? (" AND tm.tosca_model_name = '" + toscaModelName + "'") : " ")
+ + ((version != null) ? (" AND tmr.version = '" + version + "'") : "");
+
+ List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(toscaModelSql);
+
+ if (rows != null && rows.size() > 0) {
+ // Get list of all available modelIds
+ List<String> listofModelIds = new ArrayList<>();
+ for (Map<String, Object> r : rows) {
+ if (r != null) {
+ listofModelIds.add((String) r.get("tosca_model_id"));
+ }
+ }
+ // Filter Distinct elements using streams
+ listofModelIds = listofModelIds.stream().distinct().collect(Collectors.toList());
+
+ // TODO change logic using java8
+ for (String modelId : listofModelIds) {
+ CldsToscaModelDetails cldsToscaModelDetails = new CldsToscaModelDetails();
+ List<CldsToscaModelRevision> revisions = new ArrayList<>();
+ for (Map<String, Object> row : rows) {
+ String id = (String) row.get("tosca_model_id");
+ if (modelId.equalsIgnoreCase(id)) {
+ cldsToscaModelDetails.setId(id);
+ cldsToscaModelDetails.setPolicyType((String) row.get("policy_type"));
+ cldsToscaModelDetails.setToscaModelName((String) row.get("tosca_model_name"));
+ cldsToscaModelDetails.setUserId((String) row.get("user_id"));
+
+ CldsToscaModelRevision modelRevision = new CldsToscaModelRevision();
+ modelRevision.setRevisionId((String) row.get("tosca_model_revision_id"));
+ modelRevision.setVersion(((Double) row.get("version")));
+ modelRevision.setUserId((String) row.get("user_id"));
+ modelRevision.setCreatedDate(sdf.format(row.get("createdTimestamp")));
+ modelRevision.setLastUpdatedDate(sdf.format(row.get("lastUpdatedTimestamp")));
+ revisions.add(modelRevision);
+ }
+ }
+ cldsToscaModelDetails.setToscaModelRevisions(revisions);
+ cldsToscaModelDetailsList.add(cldsToscaModelDetails);
+ }
+ }
+ return cldsToscaModelDetailsList;
+ }
+
+
+ /**
+ * Method to upload a new version of Tosca Model Yaml in Database
+ *
+ * @param cldsToscaModel
+ * @param userId
+ * @return CldsToscaModel
+ *
+ */
+ public CldsToscaModel updateToscaModelWithNewVersion(CldsToscaModel cldsToscaModel, String userId) {
+ SqlParameterSource in = new MapSqlParameterSource().addValue("v_tosca_model_id", cldsToscaModel.getId())
+ .addValue("v_version", cldsToscaModel.getVersion())
+ .addValue("v_tosca_model_yaml", cldsToscaModel.getToscaModelYaml())
+ .addValue("v_tosca_model_json", cldsToscaModel.getToscaModelJson()).addValue("v_user_id", userId);
+ Map<String, Object> out = logSqlExecution(procInsertNewToscaModelVersion, in);
+ cldsToscaModel.setRevisionId((String) (out.get("v_revision_id")));
+ return cldsToscaModel;
+ }
+
+
+ /**
+ * Method to upload a new Tosca model Yaml in DB. Default version is 1.0
+ *
+ * @param cldsToscaModel
+ * @param userId
+ * @return CldsToscaModel
+ */
+ public CldsToscaModel insToscaModel(CldsToscaModel cldsToscaModel, String userId) {
+ SqlParameterSource in = new MapSqlParameterSource()
+ .addValue("v_tosca_model_name", cldsToscaModel.getToscaModelName())
+ .addValue("v_policy_type", cldsToscaModel.getPolicyType())
+ .addValue("v_tosca_model_yaml", cldsToscaModel.getToscaModelYaml())
+ .addValue("v_tosca_model_json", cldsToscaModel.getToscaModelJson())
+ .addValue("v_version", cldsToscaModel.getVersion()).addValue("v_user_id", userId);
+ Map<String, Object> out = logSqlExecution(procInsertToscaModel, in);
+ cldsToscaModel.setId((String) (out.get("v_tosca_model_id")));
+ cldsToscaModel.setRevisionId((String) (out.get("v_revision_id")));
+ cldsToscaModel.setUserId((String) out.get("v_user_id"));
+ return cldsToscaModel;
+ }
+
+ /**
+ * Method to insert a new Dictionary in Database
+ *
+ * @param cldsDictionary
+ */
+ public void insDictionary(CldsDictionary cldsDictionary) {
+ SqlParameterSource in = new MapSqlParameterSource()
+ .addValue("v_dictionary_name", cldsDictionary.getDictionaryName())
+ .addValue("v_user_id", cldsDictionary.getCreatedBy());
+ Map<String, Object> out = logSqlExecution(procInsertDictionary, in);
+ cldsDictionary.setDictionaryId((String) (out.get("v_dictionary_id")));
+ }
+
+
+ /**
+ * Method to update Dictionary with new info in Database
+ *
+ * @param dictionaryId
+ * @param cldsDictionary
+ * @param userId
+ */
+ 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 + "'";
+ jdbcTemplateObject.update(dictionarySql);
+ cldsDictionary.setUpdatedBy(userId);
+ }
+
+
+ /**
+ * Method to get list of Dictionaries from the Database
+ *
+ * @param dictionaryId
+ * @param dictionaryName
+ * @return
+ */
+ public List<CldsDictionary> getDictionary(String dictionaryId, String dictionaryName) {
+ SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
+ 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 + "'") : "")): "");
+
+ List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(dictionarySql);
+
+ if (rows != null && rows.size() > 0) {
+ rows.stream().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
+ *
+ * @param cldsDictionaryItem
+ * @param userId
+ */
+ public void insDictionarElements(CldsDictionaryItem cldsDictionaryItem, String userId) {
+ SqlParameterSource in = new MapSqlParameterSource()
+ .addValue("v_dictionary_id", cldsDictionaryItem.getDictionaryId())
+ .addValue("v_dict_element_name", cldsDictionaryItem.getDictElementName())
+ .addValue("v_dict_element_short_name", cldsDictionaryItem.getDictElementShortName())
+ .addValue("v_dict_element_description", cldsDictionaryItem.getDictElementDesc())
+ .addValue("v_dict_element_type", cldsDictionaryItem.getDictElementType())
+ .addValue("v_user_id", userId);
+ Map<String, Object> out = logSqlExecution(procInsertDictionaryElement, in);
+ cldsDictionaryItem.setDictElementId((String) (out.get("v_dict_element_id")));
+ }
+
+
+ /**
+ * Method to update Dictionary Elements with new info for a given dictionary in Database
+ *
+ * @param dictionaryElementId
+ * @param cldsDictionaryItem
+ * @param userId
+ */
+ 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 + "'";
+ jdbcTemplateObject.update(dictionarySql);
+ cldsDictionaryItem.setUpdatedBy(userId);
+ }
+
+ /**
+ * Method to get list of all dictionary elements for a given dictionary in the Database
+ *
+ * @param dictionaryName
+ * @param dictionaryId
+ * @param dictElementShortName
+ * @return
+ */
+ public List<CldsDictionaryItem> getDictionaryElements(String dictionaryName, String dictionaryId,
+ String dictElementShortName) {
+ SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
+ 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 + "'") : "");
+
+ List<Map<String, Object>> rows = jdbcTemplateObject.queryForList(dictionarySql);
+
+ if (rows != null && rows.size() > 0) {
+ rows.stream().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;
+ }
+
+ /**
+ * Method to delete a tosca model from database.
+ *
+ * @param toscaModelId
+ * @param revisionId
+ */
+ /*public void deleteModel(String toscaModelId, String revisionId) {
+ SqlParameterSource in = new MapSqlParameterSource().addValue("v_tosca_model_id", toscaModelId)
+ .addValue("v_revision_id", revisionId);
+ logSqlExecution(procDelToscaModel, in);
+ }*/