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.PrintWriter;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.List;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.apache.commons.lang.StringUtils;
35 import org.json.JSONObject;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
39 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
40 import org.onap.policy.rest.dao.CommonClassDao;
41 import org.onap.policy.rest.jpa.DCAEuuid;
42 import org.onap.policy.rest.jpa.MicroServiceAttribute;
43 import org.onap.policy.rest.jpa.MicroServiceConfigName;
44 import org.onap.policy.rest.jpa.MicroServiceLocation;
45 import org.onap.policy.rest.jpa.MicroServiceModels;
46 import org.onap.policy.rest.jpa.UserInfo;
47 import org.onap.policy.rest.util.MSAttributeObject;
48 import org.onap.policy.rest.util.MSModelUtils;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.http.MediaType;
51 import org.springframework.stereotype.Controller;
52 import org.springframework.web.bind.annotation.RequestMapping;
53 import org.springframework.web.servlet.ModelAndView;
55 import com.fasterxml.jackson.databind.DeserializationFeature;
56 import com.fasterxml.jackson.databind.JsonNode;
57 import com.fasterxml.jackson.databind.ObjectMapper;
58 import com.google.gson.Gson;
61 public class MicroServiceDictionaryController {
62 private static final Logger LOGGER = FlexLogger.getLogger(MicroServiceDictionaryController.class);
64 private static CommonClassDao commonClassDao;
66 public static void setCommonClassDao(CommonClassDao commonClassDao) {
67 MicroServiceDictionaryController.commonClassDao = commonClassDao;
71 public MicroServiceDictionaryController(CommonClassDao commonClassDao){
72 MicroServiceDictionaryController.commonClassDao = commonClassDao;
75 public MicroServiceDictionaryController(){}
77 public UserInfo getUserInfo(String loginId){
78 UserInfo name = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
82 private static String SUCCESSKEY= "successMapKey";
83 private static String SUCCESS = "success";
84 private static String OPERATION = "operation";
85 private static String GETDICTIONARY = "getDictionary";
86 private static String ERROR = "error";
87 private static String DICTIONARYDBQUERY = "dictionaryDBQuery";
88 private HashMap<String,MSAttributeObject > classMap;
89 private List<String> modelList = new ArrayList<>();
93 MSModelUtils utils = new MSModelUtils(XACMLPapServlet.getMsOnapName(), XACMLPapServlet.getMsPolicyName());
94 private MicroServiceModels newModel;
97 @RequestMapping(value={"/get_DCAEUUIDDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
98 public void getDCAEUUIDDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
100 Map<String, Object> model = new HashMap<>();
101 ObjectMapper mapper = new ObjectMapper();
102 model.put("dcaeUUIDDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(DCAEuuid.class, "name")));
103 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
104 JSONObject j = new JSONObject(msg);
105 response.getWriter().write(j.toString());
112 @RequestMapping(value={"/get_DCAEUUIDData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
113 public void getDCAEUUIDDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
115 Map<String, Object> model = new HashMap<>();
116 ObjectMapper mapper = new ObjectMapper();
117 model.put("dcaeUUIDDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(DCAEuuid.class)));
118 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
119 JSONObject j = new JSONObject(msg);
120 response.addHeader(SUCCESSKEY, SUCCESS);
121 response.addHeader(OPERATION, GETDICTIONARY);
122 response.getWriter().write(j.toString());
125 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
126 response.addHeader(ERROR, DICTIONARYDBQUERY);
127 LOGGER.error(e.getMessage());
131 @RequestMapping(value={"/ms_dictionary/save_dcaeUUID"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
132 public ModelAndView saveDCAEUUIDDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
134 boolean duplicateflag = false;
135 boolean isFakeUpdate = false;
136 boolean fromAPI = false;
137 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
140 ObjectMapper mapper = new ObjectMapper();
141 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
142 JsonNode root = mapper.readTree(request.getReader());
145 dCAEuuid = (DCAEuuid)mapper.readValue(root.get("dictionaryFields").toString(), DCAEuuid.class);
147 //check if update operation or create, get id for data to be updated and update attributeData
148 if (request.getParameter(OPERATION).equals("update")) {
149 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(dCAEuuid.getName(), "name", DCAEuuid.class);
151 DCAEuuid data = (DCAEuuid) duplicateData.get(0);
161 dCAEuuid = (DCAEuuid)mapper.readValue(root.get("dcaeUUIDDictionaryData").toString(), DCAEuuid.class);
163 if(dCAEuuid.getId() == 0){
164 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(dCAEuuid.getName(), "name", DCAEuuid.class);
165 if(!duplicateData.isEmpty()){
166 duplicateflag = true;
168 commonClassDao.save(dCAEuuid);
172 commonClassDao.update(dCAEuuid);
175 String responseString = "";
177 responseString = "Duplicate";
179 responseString = mapper.writeValueAsString(commonClassDao.getData(DCAEuuid.class));
183 if (responseString!=null && !responseString.equals("Duplicate")) {
185 responseString = "Exists";
187 responseString = "Success";
190 ModelAndView result = new ModelAndView();
191 result.setViewName(responseString);
194 response.setCharacterEncoding("UTF-8");
195 response.setContentType("application / json");
196 request.setCharacterEncoding("UTF-8");
198 PrintWriter out = response.getWriter();
199 JSONObject j = new JSONObject("{dcaeUUIDDictionaryDatas: " + responseString + "}");
200 out.write(j.toString());
203 }catch (Exception e){
204 response.setCharacterEncoding("UTF-8");
205 request.setCharacterEncoding("UTF-8");
206 PrintWriter out = response.getWriter();
207 out.write(e.getMessage());
212 @RequestMapping(value={"/ms_dictionary/remove_dcaeuuid"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
213 public ModelAndView removeDCAEUUIDDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
215 ObjectMapper mapper = new ObjectMapper();
216 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
217 JsonNode root = mapper.readTree(request.getReader());
218 DCAEuuid dCAEuuid = (DCAEuuid)mapper.readValue(root.get("data").toString(), DCAEuuid.class);
219 commonClassDao.delete(dCAEuuid);
220 response.setCharacterEncoding("UTF-8");
221 response.setContentType("application / json");
222 request.setCharacterEncoding("UTF-8");
224 PrintWriter out = response.getWriter();
226 String responseString = mapper.writeValueAsString(commonClassDao.getData(DCAEuuid.class));
227 JSONObject j = new JSONObject("{dcaeUUIDDictionaryDatas: " + responseString + "}");
228 out.write(j.toString());
234 response.setCharacterEncoding("UTF-8");
235 request.setCharacterEncoding("UTF-8");
236 PrintWriter out = response.getWriter();
237 out.write(e.getMessage());
243 @RequestMapping(value={"/get_MicroServiceConfigNameDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
244 public void getMicroServiceConfigNameByNameDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
246 Map<String, Object> model = new HashMap<>();
247 ObjectMapper mapper = new ObjectMapper();
248 model.put("microServiceCongigNameDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceConfigName.class, "name")));
249 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
250 JSONObject j = new JSONObject(msg);
251 response.getWriter().write(j.toString());
260 @RequestMapping(value={"/get_MicroServiceConfigNameData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
261 public void getMicroServiceConfigNameDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
263 Map<String, Object> model = new HashMap<>();
264 ObjectMapper mapper = new ObjectMapper();
265 model.put("microServiceCongigNameDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class)));
266 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
267 JSONObject j = new JSONObject(msg);
268 response.addHeader(SUCCESSKEY, SUCCESS);
269 response.addHeader(OPERATION, GETDICTIONARY);
270 response.getWriter().write(j.toString());
273 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
274 response.addHeader(ERROR, DICTIONARYDBQUERY);
275 LOGGER.error(e.getMessage());
279 @RequestMapping(value={"/ms_dictionary/save_configName"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
280 public ModelAndView saveMicroServiceConfigNameDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
282 boolean duplicateflag = false;
283 boolean isFakeUpdate = false;
284 boolean fromAPI = false;
285 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
288 ObjectMapper mapper = new ObjectMapper();
289 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
290 JsonNode root = mapper.readTree(request.getReader());
291 MicroServiceConfigName microServiceConfigName;
293 microServiceConfigName = (MicroServiceConfigName)mapper.readValue(root.get("dictionaryFields").toString(), MicroServiceConfigName.class);
295 //check if update operation or create, get id for data to be updated and update attributeData
296 if (request.getParameter(OPERATION).equals("update")) {
297 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceConfigName.getName(), "name", MicroServiceConfigName.class);
299 MicroServiceConfigName data = (MicroServiceConfigName) duplicateData.get(0);
304 microServiceConfigName.setId(1);
306 microServiceConfigName.setId(id);
310 microServiceConfigName = (MicroServiceConfigName)mapper.readValue(root.get("microServiceCongigNameDictionaryData").toString(), MicroServiceConfigName.class);
312 if(microServiceConfigName.getId() == 0){
313 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceConfigName.getName(), "name", MicroServiceConfigName.class);
314 if(!duplicateData.isEmpty()){
315 duplicateflag = true;
317 commonClassDao.save(microServiceConfigName);
321 commonClassDao.update(microServiceConfigName);
324 String responseString = "";
326 responseString = "Duplicate";
328 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class));
332 if (responseString!=null && !responseString.equals("Duplicate")) {
334 responseString = "Exists";
336 responseString = "Success";
339 ModelAndView result = new ModelAndView();
340 result.setViewName(responseString);
343 response.setCharacterEncoding("UTF-8");
344 response.setContentType("application / json");
345 request.setCharacterEncoding("UTF-8");
347 PrintWriter out = response.getWriter();
348 JSONObject j = new JSONObject("{microServiceCongigNameDictionaryDatas: " + responseString + "}");
349 out.write(j.toString());
352 }catch (Exception e){
353 response.setCharacterEncoding("UTF-8");
354 request.setCharacterEncoding("UTF-8");
355 PrintWriter out = response.getWriter();
356 out.write(e.getMessage());
361 @RequestMapping(value={"/ms_dictionary/remove_msConfigName"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
362 public ModelAndView removeMicroServiceConfigNameDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
364 ObjectMapper mapper = new ObjectMapper();
365 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
366 JsonNode root = mapper.readTree(request.getReader());
367 MicroServiceConfigName microServiceConfigName = (MicroServiceConfigName)mapper.readValue(root.get("data").toString(), MicroServiceConfigName.class);
368 commonClassDao.delete(microServiceConfigName);
369 response.setCharacterEncoding("UTF-8");
370 response.setContentType("application / json");
371 request.setCharacterEncoding("UTF-8");
373 PrintWriter out = response.getWriter();
375 String responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class));
376 JSONObject j = new JSONObject("{microServiceCongigNameDictionaryDatas: " + responseString + "}");
377 out.write(j.toString());
383 response.setCharacterEncoding("UTF-8");
384 request.setCharacterEncoding("UTF-8");
385 PrintWriter out = response.getWriter();
386 out.write(e.getMessage());
391 @RequestMapping(value={"/get_MicroServiceLocationDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
392 public void getMicroServiceLocationByNameDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
394 Map<String, Object> model = new HashMap<>();
395 ObjectMapper mapper = new ObjectMapper();
396 model.put("microServiceLocationDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceLocation.class, "name")));
397 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
398 JSONObject j = new JSONObject(msg);
399 response.getWriter().write(j.toString());
406 @RequestMapping(value={"/get_MicroServiceLocationData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
407 public void getMicroServiceLocationDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
409 Map<String, Object> model = new HashMap<>();
410 ObjectMapper mapper = new ObjectMapper();
411 model.put("microServiceLocationDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class)));
412 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
413 JSONObject j = new JSONObject(msg);
414 response.addHeader(SUCCESSKEY, SUCCESS);
415 response.addHeader(OPERATION, GETDICTIONARY);
416 response.getWriter().write(j.toString());
419 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
420 response.addHeader(ERROR, DICTIONARYDBQUERY);
421 LOGGER.error(e.getMessage());
425 @RequestMapping(value={"/ms_dictionary/save_location"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
426 public ModelAndView saveMicroServiceLocationDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
428 boolean duplicateflag = false;
429 boolean isFakeUpdate = false;
430 boolean fromAPI = false;
431 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
434 ObjectMapper mapper = new ObjectMapper();
435 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
436 JsonNode root = mapper.readTree(request.getReader());
437 MicroServiceLocation microServiceLocation;
439 microServiceLocation = (MicroServiceLocation)mapper.readValue(root.get("dictionaryFields").toString(), MicroServiceLocation.class);
441 //check if update operation or create, get id for data to be updated and update attributeData
442 if (request.getParameter(OPERATION).equals("update")) {
443 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceLocation.getName(), "name", MicroServiceLocation.class);
445 MicroServiceLocation data = (MicroServiceLocation) duplicateData.get(0);
450 microServiceLocation.setId(1);
452 microServiceLocation.setId(id);
456 microServiceLocation = (MicroServiceLocation)mapper.readValue(root.get("microServiceLocationDictionaryData").toString(), MicroServiceLocation.class);
458 if(microServiceLocation.getId() == 0){
459 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceLocation.getName(), "name", MicroServiceLocation.class);
460 if(!duplicateData.isEmpty()){
461 duplicateflag = true;
463 commonClassDao.save(microServiceLocation);
467 commonClassDao.update(microServiceLocation);
470 String responseString = "";
472 responseString = "Duplicate";
474 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class));
478 if (responseString!=null && !responseString.equals("Duplicate")) {
480 responseString = "Exists";
482 responseString = "Success";
485 ModelAndView result = new ModelAndView();
486 result.setViewName(responseString);
489 response.setCharacterEncoding("UTF-8");
490 response.setContentType("application / json");
491 request.setCharacterEncoding("UTF-8");
493 PrintWriter out = response.getWriter();
494 JSONObject j = new JSONObject("{microServiceLocationDictionaryDatas: " + responseString + "}");
495 out.write(j.toString());
498 }catch (Exception e){
499 response.setCharacterEncoding("UTF-8");
500 request.setCharacterEncoding("UTF-8");
501 PrintWriter out = response.getWriter();
502 out.write(e.getMessage());
507 @RequestMapping(value={"/ms_dictionary/remove_msLocation"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
508 public ModelAndView removeMicroServiceLocationDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
510 ObjectMapper mapper = new ObjectMapper();
511 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
512 JsonNode root = mapper.readTree(request.getReader());
513 MicroServiceLocation microServiceLocation = (MicroServiceLocation)mapper.readValue(root.get("data").toString(), MicroServiceLocation.class);
514 commonClassDao.delete(microServiceLocation);
515 response.setCharacterEncoding("UTF-8");
516 response.setContentType("application / json");
517 request.setCharacterEncoding("UTF-8");
519 PrintWriter out = response.getWriter();
521 String responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class));
522 JSONObject j = new JSONObject("{microServiceLocationDictionaryDatas: " + responseString + "}");
523 out.write(j.toString());
529 response.setCharacterEncoding("UTF-8");
530 request.setCharacterEncoding("UTF-8");
531 PrintWriter out = response.getWriter();
532 out.write(e.getMessage());
537 @RequestMapping(value={"/get_MicroServiceAttributeDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
538 public void getMicroServiceAttributeByNameDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
540 Map<String, Object> model = new HashMap<>();
541 ObjectMapper mapper = new ObjectMapper();
542 model.put("microServiceAttributeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceAttribute.class, "name")));
543 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
544 JSONObject j = new JSONObject(msg);
545 response.getWriter().write(j.toString());
548 LOGGER.error(e.getMessage());
552 @RequestMapping(value={"/get_MicroServiceAttributeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
553 public void getMicroServiceAttributeDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
555 Map<String, Object> model = new HashMap<>();
556 ObjectMapper mapper = new ObjectMapper();
557 model.put("microServiceAttributeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(MicroServiceAttribute.class)));
558 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
559 JSONObject j = new JSONObject(msg);
560 response.addHeader(SUCCESSKEY, SUCCESS);
561 response.addHeader(OPERATION, GETDICTIONARY);
562 response.getWriter().write(j.toString());
566 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
567 response.addHeader(ERROR, DICTIONARYDBQUERY);
568 LOGGER.error(e.getMessage());
572 @RequestMapping(value={"/ms_dictionary/save_modelAttribute"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
573 public ModelAndView saveMicroServiceAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
575 boolean duplicateflag = false;
576 boolean fromAPI = false;
577 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
581 ObjectMapper mapper = new ObjectMapper();
582 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
583 JsonNode root = mapper.readTree(request.getReader());
585 MicroServiceAttribute microServiceAttribute;
587 microServiceAttribute = (MicroServiceAttribute)mapper.readValue(root.get("dictionaryFields").toString(), MicroServiceAttribute.class);
589 //check if update operation or create, get id for data to be updated and update attributeData
590 if (request.getParameter(OPERATION).equals("update")) {
591 MicroServiceAttribute initialAttribute = (MicroServiceAttribute)mapper.readValue(root.get("initialFields").toString(), MicroServiceAttribute.class);
593 String checkValue = initialAttribute.getName() + ":" + initialAttribute.getValue() + ":" + initialAttribute.getModelName();
594 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkValue, "name:value:modelName", MicroServiceAttribute.class);
596 for (int i= 0; i<duplicateData.size(); i++){
597 MicroServiceAttribute data = (MicroServiceAttribute) duplicateData.get(0);
600 microServiceAttribute.setId(id);
603 microServiceAttribute = (MicroServiceAttribute)mapper.readValue(root.get("modelAttributeDictionaryData").toString(), MicroServiceAttribute.class);
606 if(microServiceAttribute.getId() == 0){
607 String checkValue = microServiceAttribute.getName() + ":" + microServiceAttribute.getValue() + ":" + microServiceAttribute.getModelName();
608 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkValue, "name:value:modelName", MicroServiceAttribute.class);
609 if(!duplicateData.isEmpty()){
610 duplicateflag = true;
612 commonClassDao.save(microServiceAttribute);
615 commonClassDao.update(microServiceAttribute);
618 String responseString = "";
620 responseString = "Duplicate";
622 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceAttribute.class));
626 if (responseString!=null && !responseString.equals("Duplicate")) {
627 responseString = "Success";
629 ModelAndView result = new ModelAndView();
630 result.setViewName(responseString);
633 response.setCharacterEncoding("UTF-8");
634 response.setContentType("application / json");
635 request.setCharacterEncoding("UTF-8");
637 PrintWriter out = response.getWriter();
638 JSONObject j = new JSONObject("{microServiceAttributeDictionaryDatas: " + responseString + "}");
639 out.write(j.toString());
644 response.setCharacterEncoding("UTF-8");
645 request.setCharacterEncoding("UTF-8");
646 PrintWriter out = response.getWriter();
647 out.write(e.getMessage());
652 @RequestMapping(value={"/ms_dictionary/remove_modelAttribute"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
653 public ModelAndView removeMicroServiceAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
655 ObjectMapper mapper = new ObjectMapper();
656 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
657 JsonNode root = mapper.readTree(request.getReader());
658 MicroServiceAttribute microServiceAttribute = (MicroServiceAttribute)mapper.readValue(root.get("data").toString(), MicroServiceAttribute.class);
659 commonClassDao.delete(microServiceAttribute);
660 response.setCharacterEncoding("UTF-8");
661 response.setContentType("application / json");
662 request.setCharacterEncoding("UTF-8");
664 PrintWriter out = response.getWriter();
666 String responseString = mapper.writeValueAsString(MicroServiceDictionaryController.commonClassDao.getData(MicroServiceAttribute.class));
667 JSONObject j = new JSONObject("{microServiceAttributeDictionaryDatas: " + responseString + "}");
668 out.write(j.toString());
673 LOGGER.error(e.getMessage());
674 response.setCharacterEncoding("UTF-8");
675 request.setCharacterEncoding("UTF-8");
676 PrintWriter out = response.getWriter();
677 out.write(e.getMessage());
683 @RequestMapping(value={"/get_MicroServiceModelsDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
684 public void getMicroServiceModelsDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
686 Map<String, Object> model = new HashMap<>();
687 ObjectMapper mapper = new ObjectMapper();
688 model.put("microServiceModelsDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(MicroServiceModels.class, "modelName")));
689 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
690 JSONObject j = new JSONObject(msg);
691 response.getWriter().write(j.toString());
694 LOGGER.error(e.getMessage());
698 @RequestMapping(value={"/get_MicroServiceModelsDataByVersion"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
699 public void getMicroServiceModelsDictionaryByVersionEntityData(HttpServletRequest request, HttpServletResponse response){
701 Map<String, Object> model = new HashMap<>();
702 ObjectMapper mapper = new ObjectMapper();
703 JsonNode root = mapper.readTree(request.getReader());
704 String modelName = null;
705 if (root.get("microServiceModelsDictionaryData").has("modelName")){
706 modelName = root.get("microServiceModelsDictionaryData").get("modelName").asText().replace("\"", "");
708 if (modelName!=null){
709 model.put("microServiceModelsDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataById(MicroServiceModels.class, "modelName", modelName)));
711 model.put(ERROR, "No model name given");
713 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
714 JSONObject j = new JSONObject(msg);
715 response.getWriter().write(j.toString());
718 LOGGER.error(e.getMessage());
722 @RequestMapping(value={"/get_MicroServiceModelsData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
723 public void getMicroServiceModelsDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
725 Map<String, Object> model = new HashMap<>();
726 ObjectMapper mapper = new ObjectMapper();
727 model.put("microServiceModelsDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(MicroServiceModels.class)));
728 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
729 JSONObject j = new JSONObject(msg);
730 response.addHeader(SUCCESSKEY, SUCCESS);
731 response.addHeader(OPERATION, GETDICTIONARY);
732 response.getWriter().write(j.toString());
735 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
736 response.addHeader(ERROR, DICTIONARYDBQUERY);
737 LOGGER.error(e.getMessage());
741 @RequestMapping(value={"/get_MicroServiceModelsDataServiceVersion"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
742 public void getMicroServiceModelsDictionaryEntityDataServiceVersion(HttpServletRequest request, HttpServletResponse response){
744 Map<String, Object> model = new HashMap<>();
745 ObjectMapper mapper = new ObjectMapper();
746 List<String> data = new ArrayList<>();
747 List<Object> datas = commonClassDao.getData(MicroServiceModels.class);
748 for(int i = 0; i < datas.size(); i++){
749 MicroServiceModels msmodel = (MicroServiceModels) datas.get(i);
750 if (!data.contains(msmodel.getModelName())){
751 data.add(msmodel.getModelName() + "-v" + msmodel.getVersion());
754 model.put("microServiceModelsDictionaryDatas", mapper.writeValueAsString(data));
755 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
756 JSONObject j = new JSONObject(msg);
757 response.addHeader("successMapKey", "success");
758 response.addHeader("operation", "getDictionary");
759 response.getWriter().write(j.toString());
763 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
764 response.addHeader("error", "dictionaryDBQuery");
769 @RequestMapping(value={"/get_MicroServiceModelsDataByClass"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
770 public void getMicroServiceModelsDictionaryClassEntityData(HttpServletRequest request, HttpServletResponse response){
772 Map<String, Object> model = new HashMap<>();
773 ObjectMapper mapper = new ObjectMapper();
774 model.put("microServiceModelsDictionaryClassDatas", mapper.writeValueAsString(modelList));
775 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
776 JSONObject j = new JSONObject(msg);
777 response.addHeader(SUCCESSKEY, SUCCESS);
778 response.addHeader(OPERATION, GETDICTIONARY);
779 response.getWriter().write(j.toString());
783 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
784 response.addHeader(ERROR, DICTIONARYDBQUERY);
785 LOGGER.error(e.getMessage());
789 @RequestMapping(value={"/ms_dictionary/save_model"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
790 public ModelAndView saveMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
792 boolean duplicateflag = false;
793 boolean fromAPI = false;
794 this.newModel = new MicroServiceModels();
795 if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
798 ObjectMapper mapper = new ObjectMapper();
799 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
800 JsonNode root = mapper.readTree(request.getReader());
801 MicroServiceModels microServiceModels = new MicroServiceModels();
802 String userId = null;
804 if(root.has("modelType")){
805 JsonNode dataType = root.get("modelType");
806 String modelType= dataType.toString();
807 if(modelType.contains("yml")){
808 if (root.has("microServiceModelsDictionaryData")){
809 if (root.get("microServiceModelsDictionaryData").has("description")){
810 microServiceModels.setDescription(root.get("microServiceModelsDictionaryData").get("description").asText().replace("\"", ""));
812 if (root.get("microServiceModelsDictionaryData").has("modelName")){
813 microServiceModels.setModelName(root.get("microServiceModelsDictionaryData").get("modelName").asText().replace("\"", ""));
814 this.newModel.setModelName(microServiceModels.getModelName());
816 if (root.get("microServiceModelsDictionaryData").has("version")){
817 microServiceModels.setVersion(root.get("microServiceModelsDictionaryData").get("version").asText().replace("\"", ""));
818 this.newModel.setVersion(microServiceModels.getVersion());
822 MSAttributeObject mainClass = null;
823 classMap = new HashMap<>();
824 JsonNode data = root.get("classMap");
825 ObjectMapper mapper1 = new ObjectMapper();
826 String data1 = data.toString().substring(1, data.toString().length()-1);
827 data1 = data1.replace("\\", "");
828 data1=data1.replace("\"{","{");
829 data1=data1.replace("}\"","}");
830 JSONObject jsonObject = new JSONObject(data1);
831 Set<String> keys = jsonObject.keySet();
832 for(String key : keys){
833 String value = jsonObject.get(key).toString();
834 MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class);
835 classMap.put(key, msAttributeObject);
838 userId = root.get("userid").textValue();
839 mainClass = classMap.get(this.newModel.getModelName());
840 this.newModel.setDependency("[]");
841 String value = new Gson().toJson(mainClass.getSubClass());
842 this.newModel.setSub_attributes(value);
843 String attributes= mainClass.getAttribute().toString().replace("{", "").replace("}", "");
844 int equalsIndexForAttributes= attributes.indexOf("=");
845 String atttributesAfterFirstEquals= attributes.substring(equalsIndexForAttributes+1);
846 this.newModel.setAttributes(atttributesAfterFirstEquals);
847 String refAttributes= mainClass.getRefAttribute().toString().replace("{", "").replace("}", "");
848 int equalsIndex= refAttributes.indexOf("=");
849 String refAttributesAfterFirstEquals= refAttributes.substring(equalsIndex+1);
850 this.newModel.setRef_attributes(refAttributesAfterFirstEquals);
851 this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
852 this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
856 microServiceModels = (MicroServiceModels)mapper.readValue(root.get("dictionaryFields").toString(), MicroServiceModels.class);
859 //check if update operation or create, get id for data to be updated and update attributeData
860 if (request.getParameter(OPERATION).equals("update")) {
861 String checkName = microServiceModels.getModelName() + ":" + microServiceModels.getVersion();
862 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkName, "modelName:version", MicroServiceModels.class);
864 for (int i=0; i< duplicateData.size(); i++){
865 MicroServiceModels data = (MicroServiceModels) duplicateData.get(0);
868 microServiceModels.setId(id);
869 microServiceModels.setUserCreatedBy(this.getUserInfo(userId));
873 if (root.has("microServiceModelsDictionaryData")){
874 if (root.get("microServiceModelsDictionaryData").has("description")){
875 microServiceModels.setDescription(root.get("microServiceModelsDictionaryData").get("description").asText().replace("\"", ""));
877 if (root.get("microServiceModelsDictionaryData").has("modelName")){
878 microServiceModels.setModelName(root.get("microServiceModelsDictionaryData").get("modelName").asText().replace("\"", ""));
879 this.newModel.setModelName(microServiceModels.getModelName());
881 if (root.get("microServiceModelsDictionaryData").has("version")){
882 microServiceModels.setVersion(root.get("microServiceModelsDictionaryData").get("version").asText().replace("\"", ""));
883 this.newModel.setVersion(microServiceModels.getVersion());
886 if(root.has("classMap")){
887 classMap = new HashMap<>();
888 JsonNode data = root.get("classMap");
889 ObjectMapper mapper1 = new ObjectMapper();
890 String data1 = data.toString().substring(1, data.toString().length()-1);
891 data1 = data1.replace("\\", "");
892 JSONObject jsonObject = new JSONObject(data1);
893 Set<String> keys = jsonObject.keySet();
894 for(String key : keys){
895 String value = jsonObject.get(key).toString();
896 MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class);
897 classMap.put(key, msAttributeObject);
900 userId = root.get("userid").textValue();
901 addValuesToNewModel(classMap);
906 microServiceModels.setAttributes(this.newModel.getAttributes());
907 microServiceModels.setRef_attributes(this.newModel.getRef_attributes());
908 microServiceModels.setDependency(this.newModel.getDependency());
909 microServiceModels.setModelName(this.newModel.getModelName());
910 microServiceModels.setSub_attributes(this.newModel.getSub_attributes());
911 microServiceModels.setVersion(this.newModel.getVersion());
912 microServiceModels.setEnumValues(this.newModel.getEnumValues());
913 microServiceModels.setAnnotation(this.newModel.getAnnotation());
915 if(microServiceModels.getId() == 0){
916 String checkName = microServiceModels.getModelName() + ":" + microServiceModels.getVersion();
917 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkName, "modelName:version", MicroServiceModels.class);
918 if(!duplicateData.isEmpty()){
919 duplicateflag = true;
921 microServiceModels.setUserCreatedBy(this.getUserInfo(userId));
922 commonClassDao.save(microServiceModels);
925 commonClassDao.update(microServiceModels);
927 String responseString = "";
929 responseString = "Duplicate";
931 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceModels.class));
935 if (responseString!=null && !responseString.equals("Duplicate")) {
936 responseString = "Success";
938 ModelAndView result = new ModelAndView();
939 result.setViewName(responseString);
942 response.setCharacterEncoding("UTF-8");
943 response.setContentType("application / json");
944 request.setCharacterEncoding("UTF-8");
946 PrintWriter out = response.getWriter();
947 JSONObject j = new JSONObject("{microServiceModelsDictionaryDatas: " + responseString + "}");
948 out.write(j.toString());
951 }catch (Exception e){
952 response.setCharacterEncoding("UTF-8");
953 request.setCharacterEncoding("UTF-8");
954 PrintWriter out = response.getWriter();
955 out.write(e.getMessage());
960 @RequestMapping(value={"/ms_dictionary/remove_msModel"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
961 public ModelAndView removeMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
963 ObjectMapper mapper = new ObjectMapper();
964 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
965 JsonNode root = mapper.readTree(request.getReader());
966 MicroServiceModels microServiceModels = (MicroServiceModels)mapper.readValue(root.get("data").toString(), MicroServiceModels.class);
967 commonClassDao.delete(microServiceModels);
968 response.setCharacterEncoding("UTF-8");
969 response.setContentType("application / json");
970 request.setCharacterEncoding("UTF-8");
972 PrintWriter out = response.getWriter();
974 String responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceModels.class));
975 JSONObject j = new JSONObject("{microServiceModelsDictionaryDatas: " + responseString + "}");
976 out.write(j.toString());
982 response.setCharacterEncoding("UTF-8");
983 request.setCharacterEncoding("UTF-8");
984 PrintWriter out = response.getWriter();
985 out.write(e.getMessage());
990 private void addValuesToNewModel(HashMap<String,MSAttributeObject > classMap) {
991 new MicroServiceModels();
992 //Loop through the classmap and pull out the required info for the new file.
993 MSAttributeObject mainClass = null;
994 ArrayList<String> dependency = null;
995 String subAttribute = null;
997 mainClass = classMap.get(this.newModel.getModelName());
999 if (mainClass !=null){
1000 String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[]{"[", "]", " "}, new String[]{"", "", ""});
1001 dependency = new ArrayList<String>(Arrays.asList(dependTemp.split(",")));
1002 dependency = getFullDependencyList(dependency);
1003 for (String element : dependency){
1004 MSAttributeObject temp = new MSAttributeObject();
1005 temp = classMap.get(element);
1007 mainClass.addAllRefAttribute(temp.getRefAttribute());
1008 mainClass.addAllAttribute(temp.getAttribute());
1011 subAttribute = utils.createSubAttributes(dependency, classMap, this.newModel.getModelName());
1013 subAttribute = "{}";
1014 this.newModel.setDependency("");
1017 if (mainClass != null && mainClass.getDependency()==null){
1018 mainClass.setDependency("");
1020 if(mainClass != null){
1021 this.newModel.setDependency(mainClass.getDependency());
1022 this.newModel.setSub_attributes(subAttribute);
1023 this.newModel.setAttributes(mainClass.getAttribute().toString().replace("{", "").replace("}", ""));
1024 this.newModel.setRef_attributes(mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""));
1025 this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
1026 this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
1030 private ArrayList<String> getFullDependencyList(ArrayList<String> dependency) {
1031 ArrayList<String> returnList = new ArrayList<>();
1032 ArrayList<String> workingList = new ArrayList<>();
1033 returnList.addAll(dependency);
1034 for (String element : dependency ){
1035 if (classMap.containsKey(element)){
1036 MSAttributeObject value = classMap.get(element);
1037 String rawValue = StringUtils.replaceEach(value.getDependency(), new String[]{"[", "]"}, new String[]{"", ""});
1038 workingList = new ArrayList<String>(Arrays.asList(rawValue.split(",")));
1039 for(String depend : workingList){
1040 if (!returnList.contains(depend) && !depend.isEmpty()){
1041 returnList.add(depend.trim());
1042 //getFullDepedency(workingList)