2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.xacml.rest.controller;
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import org.apache.commons.lang.StringUtils;
36 import org.json.JSONObject;
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
40 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
41 import org.onap.policy.rest.dao.CommonClassDao;
42 import org.onap.policy.rest.jpa.DCAEuuid;
43 import org.onap.policy.rest.jpa.MicroServiceAttribute;
44 import org.onap.policy.rest.jpa.MicroServiceConfigName;
45 import org.onap.policy.rest.jpa.MicroServiceLocation;
46 import org.onap.policy.rest.jpa.MicroServiceModels;
47 import org.onap.policy.rest.jpa.UserInfo;
48 import org.onap.policy.rest.util.MSAttributeObject;
49 import org.onap.policy.rest.util.MSModelUtils;
50 import org.onap.policy.utils.PolicyUtils;
51 import org.springframework.beans.factory.annotation.Autowired;
52 import org.springframework.http.MediaType;
53 import org.springframework.stereotype.Controller;
54 import org.springframework.web.bind.annotation.RequestMapping;
55 import org.springframework.web.servlet.ModelAndView;
57 import com.fasterxml.jackson.databind.DeserializationFeature;
58 import com.fasterxml.jackson.databind.JsonNode;
59 import com.fasterxml.jackson.databind.ObjectMapper;
60 import com.google.gson.Gson;
63 public class MicroServiceDictionaryController {
64 private static final Logger LOGGER = FlexLogger.getLogger(MicroServiceDictionaryController.class);
66 private static CommonClassDao commonClassDao;
68 private static String successMapKey= "successMapKey";
69 private static String successMsg = "success";
70 private static String operation = "operation";
71 private static String getDictionary = "getDictionary";
72 private static String errorMsg = "error";
73 private static String dictionaryDBQuery = "dictionaryDBQuery";
74 private HashMap<String,MSAttributeObject > classMap;
75 private List<String> modelList = new ArrayList<>();
76 private static String apiflag = "apiflag";
77 private static String dictionaryFields ="dictionaryFields";
78 private static String update = "update";
79 private static String duplicateResponseString = "Duplicate";
80 private static String successMessage = "Success";
81 private static String utf8 = "UTF-8";
82 private static String applicationJsonContentType = "application / json";
83 private static String existsResponseString = "Exists";
84 private static String microServiceModelsDictionaryDatas = "microServiceModelsDictionaryDatas";
85 private static String modelName = "modelName";
86 private static String microServiceModelsDictionaryData = "microServiceModelsDictionaryData";
87 private static String description = "description";
88 private static String version = "version";
89 private static String classMapData = "classMap";
91 * This is an empty constructor
93 public MicroServiceDictionaryController(){}
96 public MicroServiceDictionaryController(CommonClassDao commonClassDao){
97 MicroServiceDictionaryController.commonClassDao = commonClassDao;
99 public static void setCommonClassDao(CommonClassDao commonClassDao) {
100 MicroServiceDictionaryController.commonClassDao = commonClassDao;
103 public UserInfo getUserInfo(String loginId){
104 return (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
107 MSModelUtils utils = new MSModelUtils(XACMLPapServlet.getMsOnapName(), XACMLPapServlet.getMsPolicyName());
108 private MicroServiceModels newModel;
111 @RequestMapping(value={"/get_DCAEUUIDDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
112 public void getDCAEUUIDDictionaryByNameEntityData(HttpServletResponse response){
114 Map<String, Object> model = new HashMap<>();
115 ObjectMapper mapper = new ObjectMapper();
116 model.put("dcaeUUIDDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(DCAEuuid.class, "name")));
117 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
118 JSONObject j = new JSONObject(msg);
119 response.getWriter().write(j.toString());
126 @RequestMapping(value={"/get_DCAEUUIDData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
127 public void getDCAEUUIDDictionaryEntityData(HttpServletResponse response){
129 Map<String, Object> model = new HashMap<>();
130 ObjectMapper mapper = new ObjectMapper();
131 model.put("dcaeUUIDDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(DCAEuuid.class)));
132 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
133 JSONObject j = new JSONObject(msg);
134 response.addHeader(successMapKey, successMsg);
135 response.addHeader(operation, getDictionary);
136 response.getWriter().write(j.toString());
139 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
140 response.addHeader(errorMsg, dictionaryDBQuery);
145 @RequestMapping(value={"/ms_dictionary/save_dcaeUUID"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
146 public ModelAndView saveDCAEUUIDDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
148 boolean duplicateflag = false;
149 boolean isFakeUpdate = false;
150 boolean fromAPI = false;
151 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
154 ObjectMapper mapper = new ObjectMapper();
155 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
156 JsonNode root = mapper.readTree(request.getReader());
159 dCAEuuid = (DCAEuuid)mapper.readValue(root.get(dictionaryFields).toString(), DCAEuuid.class);
161 //check if update operation or create, get id for data to be updated and update attributeData
162 if ((update).equals(request.getParameter(operation))) {
163 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(dCAEuuid.getName(), "name", DCAEuuid.class);
164 DCAEuuid data = (DCAEuuid) duplicateData.get(0);
165 int id = data.getId();
174 dCAEuuid = (DCAEuuid)mapper.readValue(root.get("dcaeUUIDDictionaryData").toString(), DCAEuuid.class);
176 if(dCAEuuid.getId() == 0){
177 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(dCAEuuid.getName(), "name", DCAEuuid.class);
178 if(!duplicateData.isEmpty()){
179 duplicateflag = true;
181 commonClassDao.save(dCAEuuid);
185 commonClassDao.update(dCAEuuid);
188 String responseString = "";
190 responseString = duplicateResponseString;
192 responseString = mapper.writeValueAsString(commonClassDao.getData(DCAEuuid.class));
196 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
198 responseString = existsResponseString;
200 responseString = successMessage;
203 ModelAndView result = new ModelAndView();
204 result.setViewName(responseString);
207 response.setCharacterEncoding(utf8);
208 response.setContentType(applicationJsonContentType);
209 request.setCharacterEncoding(utf8);
211 PrintWriter out = response.getWriter();
212 JSONObject j = new JSONObject("{dcaeUUIDDictionaryDatas: " + responseString + "}");
213 out.write(j.toString());
216 }catch (Exception e){
217 response.setCharacterEncoding(utf8);
218 request.setCharacterEncoding(utf8);
219 PrintWriter out = response.getWriter();
220 out.write(PolicyUtils.CATCH_EXCEPTION);
226 @RequestMapping(value={"/ms_dictionary/remove_dcaeuuid"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
227 public ModelAndView removeDCAEUUIDDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
229 ObjectMapper mapper = new ObjectMapper();
230 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
231 JsonNode root = mapper.readTree(request.getReader());
232 DCAEuuid dCAEuuid = (DCAEuuid)mapper.readValue(root.get("data").toString(), DCAEuuid.class);
233 commonClassDao.delete(dCAEuuid);
234 response.setCharacterEncoding(utf8);
235 response.setContentType(applicationJsonContentType);
236 request.setCharacterEncoding(utf8);
238 PrintWriter out = response.getWriter();
240 String responseString = mapper.writeValueAsString(commonClassDao.getData(DCAEuuid.class));
241 JSONObject j = new JSONObject("{dcaeUUIDDictionaryDatas: " + responseString + "}");
242 out.write(j.toString());
248 response.setCharacterEncoding(utf8);
249 request.setCharacterEncoding(utf8);
250 PrintWriter out = response.getWriter();
251 out.write(PolicyUtils.CATCH_EXCEPTION);
257 @RequestMapping(value={"/get_MicroServiceConfigNameDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
258 public void getMicroServiceConfigNameByNameDictionaryEntityData(HttpServletResponse response){
260 Map<String, Object> model = new HashMap<>();
261 ObjectMapper mapper = new ObjectMapper();
262 model.put("microServiceCongigNameDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceConfigName.class, "name")));
263 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
264 JSONObject j = new JSONObject(msg);
265 response.getWriter().write(j.toString());
274 @RequestMapping(value={"/get_MicroServiceConfigNameData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
275 public void getMicroServiceConfigNameDictionaryEntityData(HttpServletResponse response){
277 Map<String, Object> model = new HashMap<>();
278 ObjectMapper mapper = new ObjectMapper();
279 model.put("microServiceCongigNameDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class)));
280 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
281 JSONObject j = new JSONObject(msg);
282 response.addHeader(successMapKey, successMsg);
283 response.addHeader(operation, getDictionary);
284 response.getWriter().write(j.toString());
287 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
288 response.addHeader(errorMsg, dictionaryDBQuery);
293 @RequestMapping(value={"/ms_dictionary/save_configName"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
294 public ModelAndView saveMicroServiceConfigNameDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
296 boolean duplicateflag = false;
297 boolean isFakeUpdate = false;
298 boolean fromAPI = false;
299 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
302 ObjectMapper mapper = new ObjectMapper();
303 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
304 JsonNode root = mapper.readTree(request.getReader());
305 MicroServiceConfigName microServiceConfigName;
307 microServiceConfigName = (MicroServiceConfigName)mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceConfigName.class);
309 //check if update operation or create, get id for data to be updated and update attributeData
310 if ((update).equals(request.getParameter(operation))) {
311 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceConfigName.getName(), "name", MicroServiceConfigName.class);
312 MicroServiceConfigName data = (MicroServiceConfigName) duplicateData.get(0);
313 int id = data.getId();
317 microServiceConfigName.setId(1);
319 microServiceConfigName.setId(id);
323 microServiceConfigName = (MicroServiceConfigName)mapper.readValue(root.get("microServiceCongigNameDictionaryData").toString(), MicroServiceConfigName.class);
325 if(microServiceConfigName.getId() == 0){
326 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceConfigName.getName(), "name", MicroServiceConfigName.class);
327 if(!duplicateData.isEmpty()){
328 duplicateflag = true;
330 commonClassDao.save(microServiceConfigName);
334 commonClassDao.update(microServiceConfigName);
337 String responseString = "";
339 responseString = duplicateResponseString;
341 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class));
345 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
347 responseString = existsResponseString;
349 responseString = successMessage;
352 ModelAndView result = new ModelAndView();
353 result.setViewName(responseString);
356 response.setCharacterEncoding(utf8);
357 response.setContentType(applicationJsonContentType);
358 request.setCharacterEncoding(utf8);
360 PrintWriter out = response.getWriter();
361 JSONObject j = new JSONObject("{microServiceCongigNameDictionaryDatas: " + responseString + "}");
362 out.write(j.toString());
365 }catch (Exception e){
366 response.setCharacterEncoding(utf8);
367 request.setCharacterEncoding(utf8);
368 PrintWriter out = response.getWriter();
369 out.write(PolicyUtils.CATCH_EXCEPTION);
375 @RequestMapping(value={"/ms_dictionary/remove_msConfigName"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
376 public ModelAndView removeMicroServiceConfigNameDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
378 ObjectMapper mapper = new ObjectMapper();
379 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
380 JsonNode root = mapper.readTree(request.getReader());
381 MicroServiceConfigName microServiceConfigName = (MicroServiceConfigName)mapper.readValue(root.get("data").toString(), MicroServiceConfigName.class);
382 commonClassDao.delete(microServiceConfigName);
383 response.setCharacterEncoding(utf8);
384 response.setContentType(applicationJsonContentType);
385 request.setCharacterEncoding(utf8);
387 PrintWriter out = response.getWriter();
389 String responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class));
390 JSONObject j = new JSONObject("{microServiceCongigNameDictionaryDatas: " + responseString + "}");
391 out.write(j.toString());
397 response.setCharacterEncoding(utf8);
398 request.setCharacterEncoding(utf8);
399 PrintWriter out = response.getWriter();
400 out.write(PolicyUtils.CATCH_EXCEPTION);
405 @RequestMapping(value={"/get_MicroServiceLocationDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
406 public void getMicroServiceLocationByNameDictionaryEntityData(HttpServletResponse response){
408 Map<String, Object> model = new HashMap<>();
409 ObjectMapper mapper = new ObjectMapper();
410 model.put("microServiceLocationDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceLocation.class, "name")));
411 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
412 JSONObject j = new JSONObject(msg);
413 response.getWriter().write(j.toString());
420 @RequestMapping(value={"/get_MicroServiceLocationData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
421 public void getMicroServiceLocationDictionaryEntityData(HttpServletResponse response){
423 Map<String, Object> model = new HashMap<>();
424 ObjectMapper mapper = new ObjectMapper();
425 model.put("microServiceLocationDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class)));
426 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
427 JSONObject j = new JSONObject(msg);
428 response.addHeader(successMapKey, successMsg);
429 response.addHeader(operation, getDictionary);
430 response.getWriter().write(j.toString());
433 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
434 response.addHeader(errorMsg, dictionaryDBQuery);
439 @RequestMapping(value={"/ms_dictionary/save_location"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
440 public ModelAndView saveMicroServiceLocationDictionary(HttpServletRequest request, HttpServletResponse response)throws IOException{
442 boolean duplicateflag = false;
443 boolean isFakeUpdate = false;
444 boolean fromAPI = false;
445 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
448 ObjectMapper mapper = new ObjectMapper();
449 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
450 JsonNode root = mapper.readTree(request.getReader());
451 MicroServiceLocation microServiceLocation;
453 microServiceLocation = (MicroServiceLocation)mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceLocation.class);
455 //check if update operation or create, get id for data to be updated and update attributeData
456 if ((update).equals(request.getParameter(operation))) {
457 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceLocation.getName(), "name", MicroServiceLocation.class);
458 MicroServiceLocation data = (MicroServiceLocation) duplicateData.get(0);
459 int id = data.getId();
463 microServiceLocation.setId(1);
465 microServiceLocation.setId(id);
469 microServiceLocation = (MicroServiceLocation)mapper.readValue(root.get("microServiceLocationDictionaryData").toString(), MicroServiceLocation.class);
471 if(microServiceLocation.getId() == 0){
472 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceLocation.getName(), "name", MicroServiceLocation.class);
473 if(!duplicateData.isEmpty()){
474 duplicateflag = true;
476 commonClassDao.save(microServiceLocation);
480 commonClassDao.update(microServiceLocation);
483 String responseString = "";
485 responseString = duplicateResponseString;
487 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class));
491 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
493 responseString = existsResponseString;
495 responseString = successMessage;
498 ModelAndView result = new ModelAndView();
499 result.setViewName(responseString);
502 response.setCharacterEncoding(utf8);
503 response.setContentType(applicationJsonContentType);
504 request.setCharacterEncoding(utf8);
506 PrintWriter out = response.getWriter();
507 JSONObject j = new JSONObject("{microServiceLocationDictionaryDatas: " + responseString + "}");
508 out.write(j.toString());
511 }catch (Exception e){
512 response.setCharacterEncoding(utf8);
513 request.setCharacterEncoding(utf8);
514 PrintWriter out = response.getWriter();
515 out.write(PolicyUtils.CATCH_EXCEPTION);
521 @RequestMapping(value={"/ms_dictionary/remove_msLocation"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
522 public ModelAndView removeMicroServiceLocationDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
524 ObjectMapper mapper = new ObjectMapper();
525 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
526 JsonNode root = mapper.readTree(request.getReader());
527 MicroServiceLocation microServiceLocation = (MicroServiceLocation)mapper.readValue(root.get("data").toString(), MicroServiceLocation.class);
528 commonClassDao.delete(microServiceLocation);
529 response.setCharacterEncoding(utf8);
530 response.setContentType(applicationJsonContentType);
531 request.setCharacterEncoding(utf8);
533 PrintWriter out = response.getWriter();
535 String responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class));
536 JSONObject j = new JSONObject("{microServiceLocationDictionaryDatas: " + responseString + "}");
537 out.write(j.toString());
543 response.setCharacterEncoding(utf8);
544 request.setCharacterEncoding(utf8);
545 PrintWriter out = response.getWriter();
546 out.write(PolicyUtils.CATCH_EXCEPTION);
551 @RequestMapping(value={"/get_MicroServiceAttributeDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
552 public void getMicroServiceAttributeByNameDictionaryEntityData(HttpServletResponse response){
554 Map<String, Object> model = new HashMap<>();
555 ObjectMapper mapper = new ObjectMapper();
556 model.put("microServiceAttributeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceAttribute.class, "name")));
557 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
558 JSONObject j = new JSONObject(msg);
559 response.getWriter().write(j.toString());
566 @RequestMapping(value={"/get_MicroServiceAttributeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
567 public void getMicroServiceAttributeDictionaryEntityData(HttpServletResponse response){
569 Map<String, Object> model = new HashMap<>();
570 ObjectMapper mapper = new ObjectMapper();
571 model.put("microServiceAttributeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(MicroServiceAttribute.class)));
572 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
573 JSONObject j = new JSONObject(msg);
574 response.addHeader(successMapKey, successMsg);
575 response.addHeader(operation, getDictionary);
576 response.getWriter().write(j.toString());
580 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
581 response.addHeader(errorMsg, dictionaryDBQuery);
586 @RequestMapping(value={"/ms_dictionary/save_modelAttribute"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
587 public ModelAndView saveMicroServiceAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
589 boolean duplicateflag = false;
590 boolean fromAPI = false;
591 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
595 ObjectMapper mapper = new ObjectMapper();
596 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
597 JsonNode root = mapper.readTree(request.getReader());
599 MicroServiceAttribute microServiceAttribute;
601 microServiceAttribute = (MicroServiceAttribute)mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceAttribute.class);
603 //check if update operation or create, get id for data to be updated and update attributeData
604 if ((update).equals(request.getParameter(operation))) {
605 MicroServiceAttribute initialAttribute = (MicroServiceAttribute)mapper.readValue(root.get("initialFields").toString(), MicroServiceAttribute.class);
607 String checkValue = initialAttribute.getName() + ":" + initialAttribute.getValue() + ":" + initialAttribute.getModelName();
608 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkValue, "name:value:modelName", MicroServiceAttribute.class);
610 for (int i= 0; i<duplicateData.size(); i++){
611 MicroServiceAttribute data = (MicroServiceAttribute) duplicateData.get(0);
614 microServiceAttribute.setId(id);
617 microServiceAttribute = (MicroServiceAttribute)mapper.readValue(root.get("modelAttributeDictionaryData").toString(), MicroServiceAttribute.class);
620 if(microServiceAttribute.getId() == 0){
621 String checkValue = microServiceAttribute.getName() + ":" + microServiceAttribute.getValue() + ":" + microServiceAttribute.getModelName();
622 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkValue, "name:value:modelName", MicroServiceAttribute.class);
623 if(!duplicateData.isEmpty()){
624 duplicateflag = true;
626 commonClassDao.save(microServiceAttribute);
629 commonClassDao.update(microServiceAttribute);
632 String responseString = "";
634 responseString = duplicateResponseString;
636 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceAttribute.class));
640 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
641 responseString = successMessage;
643 ModelAndView result = new ModelAndView();
644 result.setViewName(responseString);
647 response.setCharacterEncoding(utf8);
648 response.setContentType(applicationJsonContentType);
649 request.setCharacterEncoding(utf8);
651 PrintWriter out = response.getWriter();
652 JSONObject j = new JSONObject("{microServiceAttributeDictionaryDatas: " + responseString + "}");
653 out.write(j.toString());
658 response.setCharacterEncoding(utf8);
659 request.setCharacterEncoding(utf8);
660 PrintWriter out = response.getWriter();
661 out.write(PolicyUtils.CATCH_EXCEPTION);
667 @RequestMapping(value={"/ms_dictionary/remove_modelAttribute"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
668 public ModelAndView removeMicroServiceAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
670 ObjectMapper mapper = new ObjectMapper();
671 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
672 JsonNode root = mapper.readTree(request.getReader());
673 MicroServiceAttribute microServiceAttribute = (MicroServiceAttribute)mapper.readValue(root.get("data").toString(), MicroServiceAttribute.class);
674 commonClassDao.delete(microServiceAttribute);
675 response.setCharacterEncoding(utf8);
676 response.setContentType(applicationJsonContentType);
677 request.setCharacterEncoding(utf8);
679 PrintWriter out = response.getWriter();
681 String responseString = mapper.writeValueAsString(MicroServiceDictionaryController.commonClassDao.getData(MicroServiceAttribute.class));
682 JSONObject j = new JSONObject("{microServiceAttributeDictionaryDatas: " + responseString + "}");
683 out.write(j.toString());
689 response.setCharacterEncoding(utf8);
690 request.setCharacterEncoding(utf8);
691 PrintWriter out = response.getWriter();
692 out.write(PolicyUtils.CATCH_EXCEPTION);
698 @RequestMapping(value={"/get_MicroServiceModelsDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
699 public void getMicroServiceModelsDictionaryByNameEntityData(HttpServletResponse response){
701 Map<String, Object> model = new HashMap<>();
702 ObjectMapper mapper = new ObjectMapper();
703 model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceModels.class, modelName)));
704 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
705 JSONObject j = new JSONObject(msg);
706 response.getWriter().write(j.toString());
713 @RequestMapping(value={"/get_MicroServiceModelsDataByVersion"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
714 public void getMicroServiceModelsDictionaryByVersionEntityData(HttpServletRequest request, HttpServletResponse response){
716 Map<String, Object> model = new HashMap<>();
717 ObjectMapper mapper = new ObjectMapper();
718 JsonNode root = mapper.readTree(request.getReader());
719 String modelName = null;
720 if (root.get(microServiceModelsDictionaryData).has(modelName)){
721 modelName = root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", "");
723 if (modelName!=null){
724 model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(commonClassDao.getDataById(MicroServiceModels.class, modelName, modelName)));
726 model.put(errorMsg, "No model name given");
728 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
729 JSONObject j = new JSONObject(msg);
730 response.getWriter().write(j.toString());
737 @RequestMapping(value={"/get_MicroServiceModelsData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
738 public void getMicroServiceModelsDictionaryEntityData(HttpServletResponse response){
740 Map<String, Object> model = new HashMap<>();
741 ObjectMapper mapper = new ObjectMapper();
742 model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(commonClassDao.getData(MicroServiceModels.class)));
743 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
744 JSONObject j = new JSONObject(msg);
745 response.addHeader(successMapKey, successMsg);
746 response.addHeader(operation, getDictionary);
747 response.getWriter().write(j.toString());
750 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
751 response.addHeader(errorMsg, dictionaryDBQuery);
756 @RequestMapping(value={"/get_MicroServiceModelsDataServiceVersion"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
757 public void getMicroServiceModelsDictionaryEntityDataServiceVersion(HttpServletResponse response){
759 Map<String, Object> model = new HashMap<>();
760 ObjectMapper mapper = new ObjectMapper();
761 List<String> data = new ArrayList<>();
762 List<Object> datas = commonClassDao.getData(MicroServiceModels.class);
763 for(int i = 0; i < datas.size(); i++){
764 MicroServiceModels msmodel = (MicroServiceModels) datas.get(i);
765 if (!data.contains(msmodel.getModelName())){
766 data.add(msmodel.getModelName() + "-v" + msmodel.getVersion());
769 model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(data));
770 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
771 JSONObject j = new JSONObject(msg);
772 response.addHeader("successMapKey", "success");
773 response.addHeader("operation", "getDictionary");
774 response.getWriter().write(j.toString());
778 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
779 response.addHeader("error", "dictionaryDBQuery");
784 @RequestMapping(value={"/get_MicroServiceModelsDataByClass"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
785 public void getMicroServiceModelsDictionaryClassEntityData(HttpServletResponse response){
787 Map<String, Object> model = new HashMap<>();
788 ObjectMapper mapper = new ObjectMapper();
789 model.put("microServiceModelsDictionaryClassDatas", mapper.writeValueAsString(modelList));
790 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
791 JSONObject j = new JSONObject(msg);
792 response.addHeader(successMapKey, successMsg);
793 response.addHeader(operation, getDictionary);
794 response.getWriter().write(j.toString());
798 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
799 response.addHeader(errorMsg, dictionaryDBQuery);
804 @RequestMapping(value={"/ms_dictionary/save_model"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
805 public ModelAndView saveMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
807 boolean duplicateflag = false;
808 boolean fromAPI = false;
809 this.newModel = new MicroServiceModels();
810 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
813 ObjectMapper mapper = new ObjectMapper();
814 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
815 JsonNode root = mapper.readTree(request.getReader());
816 MicroServiceModels microServiceModels = new MicroServiceModels();
817 String userId = null;
819 if(root.has("modelType")){
820 JsonNode dataType = root.get("modelType");
821 String modelType= dataType.toString();
822 if(modelType.contains("yml")){
823 if (root.has(microServiceModelsDictionaryData)){
824 if (root.get(microServiceModelsDictionaryData).has(description)){
825 microServiceModels.setDescription(root.get(microServiceModelsDictionaryData).get(description).asText().replace("\"", ""));
827 if (root.get(microServiceModelsDictionaryData).has(modelName)){
828 microServiceModels.setModelName(root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", ""));
829 this.newModel.setModelName(microServiceModels.getModelName());
831 if (root.get(microServiceModelsDictionaryData).has(version)){
832 microServiceModels.setVersion(root.get(microServiceModelsDictionaryData).get(version).asText().replace("\"", ""));
833 this.newModel.setVersion(microServiceModels.getVersion());
837 classMap = new HashMap<>();
838 JsonNode data = root.get(classMapData);
839 ObjectMapper mapper1 = new ObjectMapper();
840 String data1 = data.toString().substring(1, data.toString().length()-1);
841 data1 = data1.replace("\\", "");
842 data1=data1.replace("\"{","{");
843 data1=data1.replace("}\"","}");
844 JSONObject jsonObject = new JSONObject(data1);
845 Set<String> keys = jsonObject.keySet();
846 for(String key : keys){
847 String value = jsonObject.get(key).toString();
848 MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class);
849 classMap.put(key, msAttributeObject);
852 userId = root.get("userid").textValue();
853 MSAttributeObject mainClass = classMap.get(this.newModel.getModelName());
854 this.newModel.setDependency("[]");
855 String value = new Gson().toJson(mainClass.getSubClass());
856 this.newModel.setSub_attributes(value);
857 String attributes= mainClass.getAttribute().toString().replace("{", "").replace("}", "");
858 int equalsIndexForAttributes= attributes.indexOf("=");
859 String atttributesAfterFirstEquals= attributes.substring(equalsIndexForAttributes+1, attributes.length()-1);
860 this.newModel.setAttributes(atttributesAfterFirstEquals);
861 String refAttributes= mainClass.getRefAttribute().toString().replace("{", "").replace("}", "");
862 int equalsIndex= refAttributes.indexOf("=");
863 String refAttributesAfterFirstEquals= refAttributes.substring(equalsIndex+1, refAttributes.length()-1);
864 this.newModel.setRef_attributes(refAttributesAfterFirstEquals);
865 this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
866 this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
870 microServiceModels = (MicroServiceModels)mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceModels.class);
873 //check if update operation or create, get id for data to be updated and update attributeData
874 if ((update).equals(request.getParameter(operation))) {
875 String checkName = microServiceModels.getModelName() + ":" + microServiceModels.getVersion();
876 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkName, "modelName:version", MicroServiceModels.class);
878 for (int i=0; i< duplicateData.size(); i++){
879 MicroServiceModels data = (MicroServiceModels) duplicateData.get(0);
882 microServiceModels.setId(id);
883 microServiceModels.setUserCreatedBy(this.getUserInfo(userId));
887 if (root.has(microServiceModelsDictionaryData)){
888 if (root.get(microServiceModelsDictionaryData).has(description)){
889 microServiceModels.setDescription(root.get(microServiceModelsDictionaryData).get(description).asText().replace("\"", ""));
891 if (root.get(microServiceModelsDictionaryData).has(modelName)){
892 microServiceModels.setModelName(root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", ""));
893 this.newModel.setModelName(microServiceModels.getModelName());
895 if (root.get(microServiceModelsDictionaryData).has(version)){
896 microServiceModels.setVersion(root.get(microServiceModelsDictionaryData).get(version).asText().replace("\"", ""));
897 this.newModel.setVersion(microServiceModels.getVersion());
900 if(root.has(classMapData)){
901 classMap = new HashMap<>();
902 JsonNode data = root.get(classMapData);
903 ObjectMapper mapper1 = new ObjectMapper();
904 String data1 = data.toString().substring(1, data.toString().length()-1);
905 data1 = data1.replace("\\", "");
906 JSONObject jsonObject = new JSONObject(data1);
907 Set<String> keys = jsonObject.keySet();
908 for(String key : keys){
909 String value = jsonObject.get(key).toString();
910 MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class);
911 classMap.put(key, msAttributeObject);
914 userId = root.get("userid").textValue();
915 addValuesToNewModel(classMap);
920 microServiceModels.setAttributes(this.newModel.getAttributes());
921 microServiceModels.setRef_attributes(this.newModel.getRef_attributes());
922 microServiceModels.setDependency(this.newModel.getDependency());
923 microServiceModels.setModelName(this.newModel.getModelName());
924 microServiceModels.setSub_attributes(this.newModel.getSub_attributes());
925 microServiceModels.setVersion(this.newModel.getVersion());
926 microServiceModels.setEnumValues(this.newModel.getEnumValues());
927 microServiceModels.setAnnotation(this.newModel.getAnnotation());
929 if(microServiceModels.getId() == 0){
930 String checkName = microServiceModels.getModelName() + ":" + microServiceModels.getVersion();
931 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkName, "modelName:version", MicroServiceModels.class);
932 if(!duplicateData.isEmpty()){
933 duplicateflag = true;
935 microServiceModels.setUserCreatedBy(this.getUserInfo(userId));
936 commonClassDao.save(microServiceModels);
939 commonClassDao.update(microServiceModels);
941 String responseString = "";
943 responseString = duplicateResponseString;
945 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceModels.class));
949 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
950 responseString = successMessage;
952 ModelAndView result = new ModelAndView();
953 result.setViewName(responseString);
956 response.setCharacterEncoding(utf8);
957 response.setContentType(applicationJsonContentType);
958 request.setCharacterEncoding(utf8);
960 PrintWriter out = response.getWriter();
961 JSONObject j = new JSONObject("{microServiceModelsDictionaryDatas: " + responseString + "}");
962 out.write(j.toString());
965 }catch (Exception e){
966 response.setCharacterEncoding(utf8);
967 request.setCharacterEncoding(utf8);
968 PrintWriter out = response.getWriter();
969 out.write(PolicyUtils.CATCH_EXCEPTION);
975 @RequestMapping(value={"/ms_dictionary/remove_msModel"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
976 public ModelAndView removeMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
978 ObjectMapper mapper = new ObjectMapper();
979 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
980 JsonNode root = mapper.readTree(request.getReader());
981 MicroServiceModels microServiceModels = (MicroServiceModels)mapper.readValue(root.get("data").toString(), MicroServiceModels.class);
982 commonClassDao.delete(microServiceModels);
983 response.setCharacterEncoding(utf8);
984 response.setContentType(applicationJsonContentType);
985 request.setCharacterEncoding(utf8);
987 PrintWriter out = response.getWriter();
989 String responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceModels.class));
990 JSONObject j = new JSONObject("{microServiceModelsDictionaryDatas: " + responseString + "}");
991 out.write(j.toString());
997 response.setCharacterEncoding(utf8);
998 request.setCharacterEncoding(utf8);
999 PrintWriter out = response.getWriter();
1000 out.write(PolicyUtils.CATCH_EXCEPTION);
1005 private void addValuesToNewModel(HashMap<String,MSAttributeObject > classMap) {
1006 //Loop through the classmap and pull out the required info for the new file.
1007 String subAttribute = null;
1009 MSAttributeObject mainClass = classMap.get(this.newModel.getModelName());
1011 if (mainClass !=null){
1012 String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[]{"[", "]", " "}, new String[]{"", "", ""});
1013 ArrayList<String> dependency = new ArrayList<>(Arrays.asList(dependTemp.split(",")));
1014 dependency = getFullDependencyList(dependency);
1015 for (String element : dependency){
1016 MSAttributeObject temp = new MSAttributeObject();
1017 temp = classMap.get(element);
1019 mainClass.addAllRefAttribute(temp.getRefAttribute());
1020 mainClass.addAllAttribute(temp.getAttribute());
1023 subAttribute = utils.createSubAttributes(dependency, classMap, this.newModel.getModelName());
1025 subAttribute = "{}";
1026 this.newModel.setDependency("");
1029 if (mainClass != null && mainClass.getDependency()==null){
1030 mainClass.setDependency("");
1032 if(mainClass != null){
1033 this.newModel.setDependency(mainClass.getDependency());
1034 this.newModel.setSub_attributes(subAttribute);
1035 this.newModel.setAttributes(mainClass.getAttribute().toString().replace("{", "").replace("}", ""));
1036 this.newModel.setRef_attributes(mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""));
1037 this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
1038 this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
1042 private ArrayList<String> getFullDependencyList(ArrayList<String> dependency) {
1043 ArrayList<String> returnList = new ArrayList<>();
1044 ArrayList<String> workingList = new ArrayList<>();
1045 returnList.addAll(dependency);
1046 for (String element : dependency ){
1047 if (classMap.containsKey(element)){
1048 MSAttributeObject value = classMap.get(element);
1049 String rawValue = StringUtils.replaceEach(value.getDependency(), new String[]{"[", "]"}, new String[]{"", ""});
1050 workingList = new ArrayList<>(Arrays.asList(rawValue.split(",")));
1051 for(String depend : workingList){
1052 if (!returnList.contains(depend) && !depend.isEmpty()){
1053 returnList.add(depend.trim());
1054 //getFullDepedency(workingList)