2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.http.MediaType;
52 import org.springframework.stereotype.Controller;
53 import org.springframework.web.bind.annotation.RequestMapping;
54 import org.springframework.web.servlet.ModelAndView;
56 import com.fasterxml.jackson.databind.DeserializationFeature;
57 import com.fasterxml.jackson.databind.JsonNode;
58 import com.fasterxml.jackson.databind.ObjectMapper;
59 import com.google.gson.Gson;
62 public class MicroServiceDictionaryController {
63 private static final Logger LOGGER = FlexLogger.getLogger(MicroServiceDictionaryController.class);
65 private static CommonClassDao commonClassDao;
67 private static String successMapKey= "successMapKey";
68 private static String successMsg = "success";
69 private static String operation = "operation";
70 private static String getDictionary = "getDictionary";
71 private static String errorMsg = "error";
72 private static String dictionaryDBQuery = "dictionaryDBQuery";
73 private HashMap<String,MSAttributeObject > classMap;
74 private List<String> modelList = new ArrayList<>();
75 private static String apiflag = "apiflag";
76 private static String dictionaryFields ="dictionaryFields";
77 private static String update = "update";
78 private static String duplicateResponseString = "Duplicate";
79 private static String successMessage = "Success";
80 private static String utf8 = "UTF-8";
81 private static String applicationJsonContentType = "application / json";
82 private static String existsResponseString = "Exists";
83 private static String microServiceModelsDictionaryDatas = "microServiceModelsDictionaryDatas";
84 private static String modelName = "modelName";
85 private static String microServiceModelsDictionaryData = "microServiceModelsDictionaryData";
86 private static String description = "description";
87 private static String version = "version";
88 private static String classMapData = "classMap";
90 * This is an empty constructor
92 public MicroServiceDictionaryController(){}
95 public MicroServiceDictionaryController(CommonClassDao commonClassDao){
96 MicroServiceDictionaryController.commonClassDao = commonClassDao;
98 public static void setCommonClassDao(CommonClassDao commonClassDao) {
99 MicroServiceDictionaryController.commonClassDao = commonClassDao;
102 public UserInfo getUserInfo(String loginId){
103 return (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
106 MSModelUtils utils = new MSModelUtils(XACMLPapServlet.getMsOnapName(), XACMLPapServlet.getMsPolicyName());
107 private MicroServiceModels newModel;
110 @RequestMapping(value={"/get_DCAEUUIDDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
111 public void getDCAEUUIDDictionaryByNameEntityData(HttpServletResponse response){
113 Map<String, Object> model = new HashMap<>();
114 ObjectMapper mapper = new ObjectMapper();
115 model.put("dcaeUUIDDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(DCAEuuid.class, "name")));
116 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
117 JSONObject j = new JSONObject(msg);
118 response.getWriter().write(j.toString());
125 @RequestMapping(value={"/get_DCAEUUIDData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
126 public void getDCAEUUIDDictionaryEntityData(HttpServletResponse response){
128 Map<String, Object> model = new HashMap<>();
129 ObjectMapper mapper = new ObjectMapper();
130 model.put("dcaeUUIDDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(DCAEuuid.class)));
131 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
132 JSONObject j = new JSONObject(msg);
133 response.addHeader(successMapKey, successMsg);
134 response.addHeader(operation, getDictionary);
135 response.getWriter().write(j.toString());
138 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
139 response.addHeader(errorMsg, dictionaryDBQuery);
144 @RequestMapping(value={"/ms_dictionary/save_dcaeUUID"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
145 public ModelAndView saveDCAEUUIDDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
147 boolean duplicateflag = false;
148 boolean isFakeUpdate = false;
149 boolean fromAPI = false;
150 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
153 ObjectMapper mapper = new ObjectMapper();
154 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
155 JsonNode root = mapper.readTree(request.getReader());
158 dCAEuuid = (DCAEuuid)mapper.readValue(root.get(dictionaryFields).toString(), DCAEuuid.class);
160 //check if update operation or create, get id for data to be updated and update attributeData
161 if ((update).equals(request.getParameter(operation))) {
162 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(dCAEuuid.getName(), "name", DCAEuuid.class);
163 DCAEuuid data = (DCAEuuid) duplicateData.get(0);
164 int id = data.getId();
173 dCAEuuid = (DCAEuuid)mapper.readValue(root.get("dcaeUUIDDictionaryData").toString(), DCAEuuid.class);
175 if(dCAEuuid.getId() == 0){
176 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(dCAEuuid.getName(), "name", DCAEuuid.class);
177 if(!duplicateData.isEmpty()){
178 duplicateflag = true;
180 commonClassDao.save(dCAEuuid);
184 commonClassDao.update(dCAEuuid);
187 String responseString = "";
189 responseString = duplicateResponseString;
191 responseString = mapper.writeValueAsString(commonClassDao.getData(DCAEuuid.class));
195 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
197 responseString = existsResponseString;
199 responseString = successMessage;
202 ModelAndView result = new ModelAndView();
203 result.setViewName(responseString);
206 response.setCharacterEncoding(utf8);
207 response.setContentType(applicationJsonContentType);
208 request.setCharacterEncoding(utf8);
210 PrintWriter out = response.getWriter();
211 JSONObject j = new JSONObject("{dcaeUUIDDictionaryDatas: " + responseString + "}");
212 out.write(j.toString());
215 }catch (Exception e){
216 response.setCharacterEncoding(utf8);
217 request.setCharacterEncoding(utf8);
218 PrintWriter out = response.getWriter();
219 out.write(e.getMessage());
225 @RequestMapping(value={"/ms_dictionary/remove_dcaeuuid"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
226 public ModelAndView removeDCAEUUIDDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
228 ObjectMapper mapper = new ObjectMapper();
229 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
230 JsonNode root = mapper.readTree(request.getReader());
231 DCAEuuid dCAEuuid = (DCAEuuid)mapper.readValue(root.get("data").toString(), DCAEuuid.class);
232 commonClassDao.delete(dCAEuuid);
233 response.setCharacterEncoding(utf8);
234 response.setContentType(applicationJsonContentType);
235 request.setCharacterEncoding(utf8);
237 PrintWriter out = response.getWriter();
239 String responseString = mapper.writeValueAsString(commonClassDao.getData(DCAEuuid.class));
240 JSONObject j = new JSONObject("{dcaeUUIDDictionaryDatas: " + responseString + "}");
241 out.write(j.toString());
247 response.setCharacterEncoding(utf8);
248 request.setCharacterEncoding(utf8);
249 PrintWriter out = response.getWriter();
250 out.write(e.getMessage());
256 @RequestMapping(value={"/get_MicroServiceConfigNameDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
257 public void getMicroServiceConfigNameByNameDictionaryEntityData(HttpServletResponse response){
259 Map<String, Object> model = new HashMap<>();
260 ObjectMapper mapper = new ObjectMapper();
261 model.put("microServiceCongigNameDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceConfigName.class, "name")));
262 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
263 JSONObject j = new JSONObject(msg);
264 response.getWriter().write(j.toString());
273 @RequestMapping(value={"/get_MicroServiceConfigNameData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
274 public void getMicroServiceConfigNameDictionaryEntityData(HttpServletResponse response){
276 Map<String, Object> model = new HashMap<>();
277 ObjectMapper mapper = new ObjectMapper();
278 model.put("microServiceCongigNameDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class)));
279 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
280 JSONObject j = new JSONObject(msg);
281 response.addHeader(successMapKey, successMsg);
282 response.addHeader(operation, getDictionary);
283 response.getWriter().write(j.toString());
286 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
287 response.addHeader(errorMsg, dictionaryDBQuery);
292 @RequestMapping(value={"/ms_dictionary/save_configName"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
293 public ModelAndView saveMicroServiceConfigNameDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
295 boolean duplicateflag = false;
296 boolean isFakeUpdate = false;
297 boolean fromAPI = false;
298 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
301 ObjectMapper mapper = new ObjectMapper();
302 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
303 JsonNode root = mapper.readTree(request.getReader());
304 MicroServiceConfigName microServiceConfigName;
306 microServiceConfigName = (MicroServiceConfigName)mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceConfigName.class);
308 //check if update operation or create, get id for data to be updated and update attributeData
309 if ((update).equals(request.getParameter(operation))) {
310 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceConfigName.getName(), "name", MicroServiceConfigName.class);
311 MicroServiceConfigName data = (MicroServiceConfigName) duplicateData.get(0);
312 int id = data.getId();
316 microServiceConfigName.setId(1);
318 microServiceConfigName.setId(id);
322 microServiceConfigName = (MicroServiceConfigName)mapper.readValue(root.get("microServiceCongigNameDictionaryData").toString(), MicroServiceConfigName.class);
324 if(microServiceConfigName.getId() == 0){
325 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceConfigName.getName(), "name", MicroServiceConfigName.class);
326 if(!duplicateData.isEmpty()){
327 duplicateflag = true;
329 commonClassDao.save(microServiceConfigName);
333 commonClassDao.update(microServiceConfigName);
336 String responseString = "";
338 responseString = duplicateResponseString;
340 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class));
344 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
346 responseString = existsResponseString;
348 responseString = successMessage;
351 ModelAndView result = new ModelAndView();
352 result.setViewName(responseString);
355 response.setCharacterEncoding(utf8);
356 response.setContentType(applicationJsonContentType);
357 request.setCharacterEncoding(utf8);
359 PrintWriter out = response.getWriter();
360 JSONObject j = new JSONObject("{microServiceCongigNameDictionaryDatas: " + responseString + "}");
361 out.write(j.toString());
364 }catch (Exception e){
365 response.setCharacterEncoding(utf8);
366 request.setCharacterEncoding(utf8);
367 PrintWriter out = response.getWriter();
368 out.write(e.getMessage());
374 @RequestMapping(value={"/ms_dictionary/remove_msConfigName"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
375 public ModelAndView removeMicroServiceConfigNameDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
377 ObjectMapper mapper = new ObjectMapper();
378 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
379 JsonNode root = mapper.readTree(request.getReader());
380 MicroServiceConfigName microServiceConfigName = (MicroServiceConfigName)mapper.readValue(root.get("data").toString(), MicroServiceConfigName.class);
381 commonClassDao.delete(microServiceConfigName);
382 response.setCharacterEncoding(utf8);
383 response.setContentType(applicationJsonContentType);
384 request.setCharacterEncoding(utf8);
386 PrintWriter out = response.getWriter();
388 String responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class));
389 JSONObject j = new JSONObject("{microServiceCongigNameDictionaryDatas: " + responseString + "}");
390 out.write(j.toString());
396 response.setCharacterEncoding(utf8);
397 request.setCharacterEncoding(utf8);
398 PrintWriter out = response.getWriter();
399 out.write(e.getMessage());
404 @RequestMapping(value={"/get_MicroServiceLocationDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
405 public void getMicroServiceLocationByNameDictionaryEntityData(HttpServletResponse response){
407 Map<String, Object> model = new HashMap<>();
408 ObjectMapper mapper = new ObjectMapper();
409 model.put("microServiceLocationDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceLocation.class, "name")));
410 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
411 JSONObject j = new JSONObject(msg);
412 response.getWriter().write(j.toString());
419 @RequestMapping(value={"/get_MicroServiceLocationData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
420 public void getMicroServiceLocationDictionaryEntityData(HttpServletResponse response){
422 Map<String, Object> model = new HashMap<>();
423 ObjectMapper mapper = new ObjectMapper();
424 model.put("microServiceLocationDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class)));
425 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
426 JSONObject j = new JSONObject(msg);
427 response.addHeader(successMapKey, successMsg);
428 response.addHeader(operation, getDictionary);
429 response.getWriter().write(j.toString());
432 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
433 response.addHeader(errorMsg, dictionaryDBQuery);
438 @RequestMapping(value={"/ms_dictionary/save_location"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
439 public ModelAndView saveMicroServiceLocationDictionary(HttpServletRequest request, HttpServletResponse response)throws IOException{
441 boolean duplicateflag = false;
442 boolean isFakeUpdate = false;
443 boolean fromAPI = false;
444 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
447 ObjectMapper mapper = new ObjectMapper();
448 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
449 JsonNode root = mapper.readTree(request.getReader());
450 MicroServiceLocation microServiceLocation;
452 microServiceLocation = (MicroServiceLocation)mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceLocation.class);
454 //check if update operation or create, get id for data to be updated and update attributeData
455 if ((update).equals(request.getParameter(operation))) {
456 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceLocation.getName(), "name", MicroServiceLocation.class);
457 MicroServiceLocation data = (MicroServiceLocation) duplicateData.get(0);
458 int id = data.getId();
462 microServiceLocation.setId(1);
464 microServiceLocation.setId(id);
468 microServiceLocation = (MicroServiceLocation)mapper.readValue(root.get("microServiceLocationDictionaryData").toString(), MicroServiceLocation.class);
470 if(microServiceLocation.getId() == 0){
471 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceLocation.getName(), "name", MicroServiceLocation.class);
472 if(!duplicateData.isEmpty()){
473 duplicateflag = true;
475 commonClassDao.save(microServiceLocation);
479 commonClassDao.update(microServiceLocation);
482 String responseString = "";
484 responseString = duplicateResponseString;
486 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class));
490 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
492 responseString = existsResponseString;
494 responseString = successMessage;
497 ModelAndView result = new ModelAndView();
498 result.setViewName(responseString);
501 response.setCharacterEncoding(utf8);
502 response.setContentType(applicationJsonContentType);
503 request.setCharacterEncoding(utf8);
505 PrintWriter out = response.getWriter();
506 JSONObject j = new JSONObject("{microServiceLocationDictionaryDatas: " + responseString + "}");
507 out.write(j.toString());
510 }catch (Exception e){
511 response.setCharacterEncoding(utf8);
512 request.setCharacterEncoding(utf8);
513 PrintWriter out = response.getWriter();
514 out.write(e.getMessage());
520 @RequestMapping(value={"/ms_dictionary/remove_msLocation"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
521 public ModelAndView removeMicroServiceLocationDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
523 ObjectMapper mapper = new ObjectMapper();
524 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
525 JsonNode root = mapper.readTree(request.getReader());
526 MicroServiceLocation microServiceLocation = (MicroServiceLocation)mapper.readValue(root.get("data").toString(), MicroServiceLocation.class);
527 commonClassDao.delete(microServiceLocation);
528 response.setCharacterEncoding(utf8);
529 response.setContentType(applicationJsonContentType);
530 request.setCharacterEncoding(utf8);
532 PrintWriter out = response.getWriter();
534 String responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class));
535 JSONObject j = new JSONObject("{microServiceLocationDictionaryDatas: " + responseString + "}");
536 out.write(j.toString());
542 response.setCharacterEncoding(utf8);
543 request.setCharacterEncoding(utf8);
544 PrintWriter out = response.getWriter();
545 out.write(e.getMessage());
550 @RequestMapping(value={"/get_MicroServiceAttributeDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
551 public void getMicroServiceAttributeByNameDictionaryEntityData(HttpServletResponse response){
553 Map<String, Object> model = new HashMap<>();
554 ObjectMapper mapper = new ObjectMapper();
555 model.put("microServiceAttributeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceAttribute.class, "name")));
556 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
557 JSONObject j = new JSONObject(msg);
558 response.getWriter().write(j.toString());
565 @RequestMapping(value={"/get_MicroServiceAttributeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
566 public void getMicroServiceAttributeDictionaryEntityData(HttpServletResponse response){
568 Map<String, Object> model = new HashMap<>();
569 ObjectMapper mapper = new ObjectMapper();
570 model.put("microServiceAttributeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(MicroServiceAttribute.class)));
571 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
572 JSONObject j = new JSONObject(msg);
573 response.addHeader(successMapKey, successMsg);
574 response.addHeader(operation, getDictionary);
575 response.getWriter().write(j.toString());
579 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
580 response.addHeader(errorMsg, dictionaryDBQuery);
585 @RequestMapping(value={"/ms_dictionary/save_modelAttribute"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
586 public ModelAndView saveMicroServiceAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
588 boolean duplicateflag = false;
589 boolean fromAPI = false;
590 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
594 ObjectMapper mapper = new ObjectMapper();
595 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
596 JsonNode root = mapper.readTree(request.getReader());
598 MicroServiceAttribute microServiceAttribute;
600 microServiceAttribute = (MicroServiceAttribute)mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceAttribute.class);
602 //check if update operation or create, get id for data to be updated and update attributeData
603 if ((update).equals(request.getParameter(operation))) {
604 MicroServiceAttribute initialAttribute = (MicroServiceAttribute)mapper.readValue(root.get("initialFields").toString(), MicroServiceAttribute.class);
606 String checkValue = initialAttribute.getName() + ":" + initialAttribute.getValue() + ":" + initialAttribute.getModelName();
607 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkValue, "name:value:modelName", MicroServiceAttribute.class);
609 for (int i= 0; i<duplicateData.size(); i++){
610 MicroServiceAttribute data = (MicroServiceAttribute) duplicateData.get(0);
613 microServiceAttribute.setId(id);
616 microServiceAttribute = (MicroServiceAttribute)mapper.readValue(root.get("modelAttributeDictionaryData").toString(), MicroServiceAttribute.class);
619 if(microServiceAttribute.getId() == 0){
620 String checkValue = microServiceAttribute.getName() + ":" + microServiceAttribute.getValue() + ":" + microServiceAttribute.getModelName();
621 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkValue, "name:value:modelName", MicroServiceAttribute.class);
622 if(!duplicateData.isEmpty()){
623 duplicateflag = true;
625 commonClassDao.save(microServiceAttribute);
628 commonClassDao.update(microServiceAttribute);
631 String responseString = "";
633 responseString = duplicateResponseString;
635 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceAttribute.class));
639 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
640 responseString = successMessage;
642 ModelAndView result = new ModelAndView();
643 result.setViewName(responseString);
646 response.setCharacterEncoding(utf8);
647 response.setContentType(applicationJsonContentType);
648 request.setCharacterEncoding(utf8);
650 PrintWriter out = response.getWriter();
651 JSONObject j = new JSONObject("{microServiceAttributeDictionaryDatas: " + responseString + "}");
652 out.write(j.toString());
657 response.setCharacterEncoding(utf8);
658 request.setCharacterEncoding(utf8);
659 PrintWriter out = response.getWriter();
660 out.write(e.getMessage());
666 @RequestMapping(value={"/ms_dictionary/remove_modelAttribute"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
667 public ModelAndView removeMicroServiceAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
669 ObjectMapper mapper = new ObjectMapper();
670 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
671 JsonNode root = mapper.readTree(request.getReader());
672 MicroServiceAttribute microServiceAttribute = (MicroServiceAttribute)mapper.readValue(root.get("data").toString(), MicroServiceAttribute.class);
673 commonClassDao.delete(microServiceAttribute);
674 response.setCharacterEncoding(utf8);
675 response.setContentType(applicationJsonContentType);
676 request.setCharacterEncoding(utf8);
678 PrintWriter out = response.getWriter();
680 String responseString = mapper.writeValueAsString(MicroServiceDictionaryController.commonClassDao.getData(MicroServiceAttribute.class));
681 JSONObject j = new JSONObject("{microServiceAttributeDictionaryDatas: " + responseString + "}");
682 out.write(j.toString());
688 response.setCharacterEncoding(utf8);
689 request.setCharacterEncoding(utf8);
690 PrintWriter out = response.getWriter();
691 out.write(e.getMessage());
697 @RequestMapping(value={"/get_MicroServiceModelsDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
698 public void getMicroServiceModelsDictionaryByNameEntityData(HttpServletResponse response){
700 Map<String, Object> model = new HashMap<>();
701 ObjectMapper mapper = new ObjectMapper();
702 model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceModels.class, modelName)));
703 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
704 JSONObject j = new JSONObject(msg);
705 response.getWriter().write(j.toString());
712 @RequestMapping(value={"/get_MicroServiceModelsDataByVersion"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
713 public void getMicroServiceModelsDictionaryByVersionEntityData(HttpServletRequest request, HttpServletResponse response){
715 Map<String, Object> model = new HashMap<>();
716 ObjectMapper mapper = new ObjectMapper();
717 JsonNode root = mapper.readTree(request.getReader());
718 String modelName = null;
719 if (root.get(microServiceModelsDictionaryData).has(modelName)){
720 modelName = root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", "");
722 if (modelName!=null){
723 model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(commonClassDao.getDataById(MicroServiceModels.class, modelName, modelName)));
725 model.put(errorMsg, "No model name given");
727 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
728 JSONObject j = new JSONObject(msg);
729 response.getWriter().write(j.toString());
736 @RequestMapping(value={"/get_MicroServiceModelsData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
737 public void getMicroServiceModelsDictionaryEntityData(HttpServletResponse response){
739 Map<String, Object> model = new HashMap<>();
740 ObjectMapper mapper = new ObjectMapper();
741 model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(commonClassDao.getData(MicroServiceModels.class)));
742 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
743 JSONObject j = new JSONObject(msg);
744 response.addHeader(successMapKey, successMsg);
745 response.addHeader(operation, getDictionary);
746 response.getWriter().write(j.toString());
749 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
750 response.addHeader(errorMsg, dictionaryDBQuery);
755 @RequestMapping(value={"/get_MicroServiceModelsDataServiceVersion"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
756 public void getMicroServiceModelsDictionaryEntityDataServiceVersion(HttpServletResponse response){
758 Map<String, Object> model = new HashMap<>();
759 ObjectMapper mapper = new ObjectMapper();
760 List<String> data = new ArrayList<>();
761 List<Object> datas = commonClassDao.getData(MicroServiceModels.class);
762 for(int i = 0; i < datas.size(); i++){
763 MicroServiceModels msmodel = (MicroServiceModels) datas.get(i);
764 if (!data.contains(msmodel.getModelName())){
765 data.add(msmodel.getModelName() + "-v" + msmodel.getVersion());
768 model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(data));
769 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
770 JSONObject j = new JSONObject(msg);
771 response.addHeader("successMapKey", "success");
772 response.addHeader("operation", "getDictionary");
773 response.getWriter().write(j.toString());
777 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
778 response.addHeader("error", "dictionaryDBQuery");
783 @RequestMapping(value={"/get_MicroServiceModelsDataByClass"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
784 public void getMicroServiceModelsDictionaryClassEntityData(HttpServletResponse response){
786 Map<String, Object> model = new HashMap<>();
787 ObjectMapper mapper = new ObjectMapper();
788 model.put("microServiceModelsDictionaryClassDatas", mapper.writeValueAsString(modelList));
789 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
790 JSONObject j = new JSONObject(msg);
791 response.addHeader(successMapKey, successMsg);
792 response.addHeader(operation, getDictionary);
793 response.getWriter().write(j.toString());
797 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
798 response.addHeader(errorMsg, dictionaryDBQuery);
803 @RequestMapping(value={"/ms_dictionary/save_model"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
804 public ModelAndView saveMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
806 boolean duplicateflag = false;
807 boolean fromAPI = false;
808 this.newModel = new MicroServiceModels();
809 if (request.getParameter(apiflag)!=null && ("api").equalsIgnoreCase(request.getParameter(apiflag))) {
812 ObjectMapper mapper = new ObjectMapper();
813 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
814 JsonNode root = mapper.readTree(request.getReader());
815 MicroServiceModels microServiceModels = new MicroServiceModels();
816 String userId = null;
818 if(root.has("modelType")){
819 JsonNode dataType = root.get("modelType");
820 String modelType= dataType.toString();
821 if(modelType.contains("yml")){
822 if (root.has(microServiceModelsDictionaryData)){
823 if (root.get(microServiceModelsDictionaryData).has(description)){
824 microServiceModels.setDescription(root.get(microServiceModelsDictionaryData).get(description).asText().replace("\"", ""));
826 if (root.get(microServiceModelsDictionaryData).has(modelName)){
827 microServiceModels.setModelName(root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", ""));
828 this.newModel.setModelName(microServiceModels.getModelName());
830 if (root.get(microServiceModelsDictionaryData).has(version)){
831 microServiceModels.setVersion(root.get(microServiceModelsDictionaryData).get(version).asText().replace("\"", ""));
832 this.newModel.setVersion(microServiceModels.getVersion());
836 classMap = new HashMap<>();
837 JsonNode data = root.get(classMapData);
838 ObjectMapper mapper1 = new ObjectMapper();
839 String data1 = data.toString().substring(1, data.toString().length()-1);
840 data1 = data1.replace("\\", "");
841 data1=data1.replace("\"{","{");
842 data1=data1.replace("}\"","}");
843 JSONObject jsonObject = new JSONObject(data1);
844 Set<String> keys = jsonObject.keySet();
845 for(String key : keys){
846 String value = jsonObject.get(key).toString();
847 MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class);
848 classMap.put(key, msAttributeObject);
851 userId = root.get("userid").textValue();
852 MSAttributeObject mainClass = classMap.get(this.newModel.getModelName());
853 this.newModel.setDependency("[]");
854 String value = new Gson().toJson(mainClass.getSubClass());
855 this.newModel.setSub_attributes(value);
856 String attributes= mainClass.getAttribute().toString().replace("{", "").replace("}", "");
857 int equalsIndexForAttributes= attributes.indexOf("=");
858 String atttributesAfterFirstEquals= attributes.substring(equalsIndexForAttributes+1);
859 this.newModel.setAttributes(atttributesAfterFirstEquals);
860 String refAttributes= mainClass.getRefAttribute().toString().replace("{", "").replace("}", "");
861 int equalsIndex= refAttributes.indexOf("=");
862 String refAttributesAfterFirstEquals= refAttributes.substring(equalsIndex+1);
863 this.newModel.setRef_attributes(refAttributesAfterFirstEquals);
864 this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
865 this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
869 microServiceModels = (MicroServiceModels)mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceModels.class);
872 //check if update operation or create, get id for data to be updated and update attributeData
873 if ((update).equals(request.getParameter(operation))) {
874 String checkName = microServiceModels.getModelName() + ":" + microServiceModels.getVersion();
875 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkName, "modelName:version", MicroServiceModels.class);
877 for (int i=0; i< duplicateData.size(); i++){
878 MicroServiceModels data = (MicroServiceModels) duplicateData.get(0);
881 microServiceModels.setId(id);
882 microServiceModels.setUserCreatedBy(this.getUserInfo(userId));
886 if (root.has(microServiceModelsDictionaryData)){
887 if (root.get(microServiceModelsDictionaryData).has(description)){
888 microServiceModels.setDescription(root.get(microServiceModelsDictionaryData).get(description).asText().replace("\"", ""));
890 if (root.get(microServiceModelsDictionaryData).has(modelName)){
891 microServiceModels.setModelName(root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", ""));
892 this.newModel.setModelName(microServiceModels.getModelName());
894 if (root.get(microServiceModelsDictionaryData).has(version)){
895 microServiceModels.setVersion(root.get(microServiceModelsDictionaryData).get(version).asText().replace("\"", ""));
896 this.newModel.setVersion(microServiceModels.getVersion());
899 if(root.has(classMapData)){
900 classMap = new HashMap<>();
901 JsonNode data = root.get(classMapData);
902 ObjectMapper mapper1 = new ObjectMapper();
903 String data1 = data.toString().substring(1, data.toString().length()-1);
904 data1 = data1.replace("\\", "");
905 JSONObject jsonObject = new JSONObject(data1);
906 Set<String> keys = jsonObject.keySet();
907 for(String key : keys){
908 String value = jsonObject.get(key).toString();
909 MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class);
910 classMap.put(key, msAttributeObject);
913 userId = root.get("userid").textValue();
914 addValuesToNewModel(classMap);
919 microServiceModels.setAttributes(this.newModel.getAttributes());
920 microServiceModels.setRef_attributes(this.newModel.getRef_attributes());
921 microServiceModels.setDependency(this.newModel.getDependency());
922 microServiceModels.setModelName(this.newModel.getModelName());
923 microServiceModels.setSub_attributes(this.newModel.getSub_attributes());
924 microServiceModels.setVersion(this.newModel.getVersion());
925 microServiceModels.setEnumValues(this.newModel.getEnumValues());
926 microServiceModels.setAnnotation(this.newModel.getAnnotation());
928 if(microServiceModels.getId() == 0){
929 String checkName = microServiceModels.getModelName() + ":" + microServiceModels.getVersion();
930 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkName, "modelName:version", MicroServiceModels.class);
931 if(!duplicateData.isEmpty()){
932 duplicateflag = true;
934 microServiceModels.setUserCreatedBy(this.getUserInfo(userId));
935 commonClassDao.save(microServiceModels);
938 commonClassDao.update(microServiceModels);
940 String responseString = "";
942 responseString = duplicateResponseString;
944 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceModels.class));
948 if (responseString!=null && !(duplicateResponseString).equals(responseString)) {
949 responseString = successMessage;
951 ModelAndView result = new ModelAndView();
952 result.setViewName(responseString);
955 response.setCharacterEncoding(utf8);
956 response.setContentType(applicationJsonContentType);
957 request.setCharacterEncoding(utf8);
959 PrintWriter out = response.getWriter();
960 JSONObject j = new JSONObject("{microServiceModelsDictionaryDatas: " + responseString + "}");
961 out.write(j.toString());
964 }catch (Exception e){
965 response.setCharacterEncoding(utf8);
966 request.setCharacterEncoding(utf8);
967 PrintWriter out = response.getWriter();
968 out.write(e.getMessage());
974 @RequestMapping(value={"/ms_dictionary/remove_msModel"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
975 public ModelAndView removeMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
977 ObjectMapper mapper = new ObjectMapper();
978 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
979 JsonNode root = mapper.readTree(request.getReader());
980 MicroServiceModels microServiceModels = (MicroServiceModels)mapper.readValue(root.get("data").toString(), MicroServiceModels.class);
981 commonClassDao.delete(microServiceModels);
982 response.setCharacterEncoding(utf8);
983 response.setContentType(applicationJsonContentType);
984 request.setCharacterEncoding(utf8);
986 PrintWriter out = response.getWriter();
988 String responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceModels.class));
989 JSONObject j = new JSONObject("{microServiceModelsDictionaryDatas: " + responseString + "}");
990 out.write(j.toString());
996 response.setCharacterEncoding(utf8);
997 request.setCharacterEncoding(utf8);
998 PrintWriter out = response.getWriter();
999 out.write(e.getMessage());
1004 private void addValuesToNewModel(HashMap<String,MSAttributeObject > classMap) {
1005 //Loop through the classmap and pull out the required info for the new file.
1006 String subAttribute = null;
1008 MSAttributeObject mainClass = classMap.get(this.newModel.getModelName());
1010 if (mainClass !=null){
1011 String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[]{"[", "]", " "}, new String[]{"", "", ""});
1012 ArrayList<String> dependency = new ArrayList<>(Arrays.asList(dependTemp.split(",")));
1013 dependency = getFullDependencyList(dependency);
1014 for (String element : dependency){
1015 MSAttributeObject temp = new MSAttributeObject();
1016 temp = classMap.get(element);
1018 mainClass.addAllRefAttribute(temp.getRefAttribute());
1019 mainClass.addAllAttribute(temp.getAttribute());
1022 subAttribute = utils.createSubAttributes(dependency, classMap, this.newModel.getModelName());
1024 subAttribute = "{}";
1025 this.newModel.setDependency("");
1028 if (mainClass != null && mainClass.getDependency()==null){
1029 mainClass.setDependency("");
1031 if(mainClass != null){
1032 this.newModel.setDependency(mainClass.getDependency());
1033 this.newModel.setSub_attributes(subAttribute);
1034 this.newModel.setAttributes(mainClass.getAttribute().toString().replace("{", "").replace("}", ""));
1035 this.newModel.setRef_attributes(mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""));
1036 this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
1037 this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
1041 private ArrayList<String> getFullDependencyList(ArrayList<String> dependency) {
1042 ArrayList<String> returnList = new ArrayList<>();
1043 ArrayList<String> workingList = new ArrayList<>();
1044 returnList.addAll(dependency);
1045 for (String element : dependency ){
1046 if (classMap.containsKey(element)){
1047 MSAttributeObject value = classMap.get(element);
1048 String rawValue = StringUtils.replaceEach(value.getDependency(), new String[]{"[", "]"}, new String[]{"", ""});
1049 workingList = new ArrayList<>(Arrays.asList(rawValue.split(",")));
1050 for(String depend : workingList){
1051 if (!returnList.contains(depend) && !depend.isEmpty()){
1052 returnList.add(depend.trim());
1053 //getFullDepedency(workingList)