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.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
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.DictionaryUtils;
41 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
42 import org.onap.policy.rest.dao.CommonClassDao;
43 import org.onap.policy.rest.jpa.DCAEuuid;
44 import org.onap.policy.rest.jpa.MicroServiceAttribute;
45 import org.onap.policy.rest.jpa.MicroServiceConfigName;
46 import org.onap.policy.rest.jpa.MicroServiceLocation;
47 import org.onap.policy.rest.jpa.MicroServiceModels;
48 import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults;
49 import org.onap.policy.rest.jpa.PrefixList;
50 import org.onap.policy.rest.jpa.UserInfo;
51 import org.onap.policy.rest.util.MSAttributeObject;
52 import org.onap.policy.rest.util.MSModelUtils;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.http.MediaType;
55 import org.springframework.stereotype.Controller;
56 import org.springframework.web.bind.annotation.RequestMapping;
57 import org.springframework.web.bind.annotation.RequestMethod;
58 import org.springframework.web.servlet.ModelAndView;
60 import com.fasterxml.jackson.databind.DeserializationFeature;
61 import com.fasterxml.jackson.databind.JsonNode;
62 import com.fasterxml.jackson.databind.ObjectMapper;
63 import com.google.gson.Gson;
66 public class MicroServiceDictionaryController {
67 private static final Logger LOGGER = FlexLogger.getLogger(MicroServiceDictionaryController.class);
69 private static CommonClassDao commonClassDao;
71 private static String successMapKey= "successMapKey";
72 private static String successMsg = "success";
73 private static String operation = "operation";
74 private static String getDictionary = "getDictionary";
75 private static String errorMsg = "error";
76 private static String dictionaryDBQuery = "dictionaryDBQuery";
77 private LinkedHashMap<String,MSAttributeObject > classMap;
78 private List<String> modelList = new ArrayList<>();
79 private static String apiflag = "apiflag";
80 private static String dictionaryFields ="dictionaryFields";
81 private static String update = "update";
82 private static String duplicateResponseString = "Duplicate";
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";
89 private static String dcaeUUIDDatas = "dcaeUUIDDictionaryDatas";
90 private static String microServiceConfigNameDatas = "microServiceConfigNameDictionaryDatas";
91 private static String microServiceLocationDatas = "microServiceLocationDictionaryDatas";
92 private static String microServiceAttributeDatas = "microServiceAttributeDictionaryDatas";
93 private static String microServiceHeaderDefaultDatas = "microServiceHeaderDefaultDatas";
95 public MicroServiceDictionaryController(){
99 private DictionaryUtils getDictionaryUtilsInstance(){
100 return DictionaryUtils.getDictionaryUtils();
104 public MicroServiceDictionaryController(CommonClassDao commonClassDao){
105 MicroServiceDictionaryController.commonClassDao = commonClassDao;
107 public static void setCommonClassDao(CommonClassDao commonClassDao) {
108 MicroServiceDictionaryController.commonClassDao = commonClassDao;
111 MSModelUtils utils = new MSModelUtils(XACMLPapServlet.getMsOnapName(), XACMLPapServlet.getMsPolicyName());
113 private MicroServiceModels newModel;
115 @RequestMapping(value={"/get_DCAEUUIDDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
116 public void getDCAEUUIDDictionaryByNameEntityData(HttpServletResponse response){
117 DictionaryUtils utils = getDictionaryUtilsInstance();
118 utils.getDataByEntity(response, dcaeUUIDDatas, "name", DCAEuuid.class);
121 @RequestMapping(value={"/get_DCAEUUIDData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
122 public void getDCAEUUIDDictionaryEntityData(HttpServletResponse response){
123 DictionaryUtils utils = getDictionaryUtilsInstance();
124 utils.getData(response, dcaeUUIDDatas, DCAEuuid.class);
127 @RequestMapping(value={"/ms_dictionary/save_dcaeUUID"}, method={RequestMethod.POST})
128 public ModelAndView saveDCAEUUIDDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
129 DictionaryUtils utils = getDictionaryUtilsInstance();
131 boolean fromAPI = utils.isRequestFromAPI(request);
132 ObjectMapper mapper = new ObjectMapper();
133 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
134 JsonNode root = mapper.readTree(request.getReader());
137 dCAEuuid = mapper.readValue(root.get(dictionaryFields).toString(), DCAEuuid.class);
139 dCAEuuid = mapper.readValue(root.get("dcaeUUIDDictionaryData").toString(), DCAEuuid.class);
142 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(dCAEuuid.getName(), "name", DCAEuuid.class);
143 boolean duplicateflag = false;
144 if(duplicateData != null && !duplicateData.isEmpty()){
145 DCAEuuid data = (DCAEuuid) duplicateData.get(0);
146 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
147 dCAEuuid.setId(data.getId());
148 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
149 (request.getParameter(operation) == null && (data.getId() != dCAEuuid.getId()))){
150 duplicateflag = true;
153 String responseString = null;
155 if(dCAEuuid.getId() == 0){
156 commonClassDao.save(dCAEuuid);
158 commonClassDao.update(dCAEuuid);
160 responseString = mapper.writeValueAsString(commonClassDao.getData(DCAEuuid.class));
162 responseString = duplicateResponseString;
165 return utils.getResultForApi(responseString);
167 utils.setResponseData(response, dcaeUUIDDatas, responseString);
169 }catch (Exception e){
170 utils.setErrorResponseData(response, e);
175 @RequestMapping(value={"/ms_dictionary/remove_dcaeuuid"}, method={RequestMethod.POST})
176 public void removeDCAEUUIDDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
177 DictionaryUtils utils = getDictionaryUtilsInstance();
178 utils.removeData(request, response, dcaeUUIDDatas, DCAEuuid.class);
181 @RequestMapping(value={"/get_MicroServiceConfigNameDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
182 public void getMicroServiceConfigNameByNameDictionaryEntityData(HttpServletResponse response){
183 DictionaryUtils utils = getDictionaryUtilsInstance();
184 utils.getDataByEntity(response, microServiceConfigNameDatas, "name", MicroServiceConfigName.class);
187 @RequestMapping(value={"/get_MicroServiceConfigNameData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
188 public void getMicroServiceConfigNameDictionaryEntityData(HttpServletResponse response){
189 DictionaryUtils utils = getDictionaryUtilsInstance();
190 utils.getData(response, microServiceConfigNameDatas, MicroServiceConfigName.class);
193 @RequestMapping(value={"/ms_dictionary/save_configName"}, method={RequestMethod.POST})
194 public ModelAndView saveMicroServiceConfigNameDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
195 DictionaryUtils utils = getDictionaryUtilsInstance();
197 boolean fromAPI = utils.isRequestFromAPI(request);
198 ObjectMapper mapper = new ObjectMapper();
199 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
200 JsonNode root = mapper.readTree(request.getReader());
201 MicroServiceConfigName microServiceConfigName;
203 microServiceConfigName = mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceConfigName.class);
205 microServiceConfigName = mapper.readValue(root.get("microServiceConfigNameDictionaryData").toString(), MicroServiceConfigName.class);
207 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceConfigName.getName(), "name", MicroServiceConfigName.class);
208 boolean duplicateflag = false;
209 if(duplicateData != null && !duplicateData.isEmpty()){
210 MicroServiceConfigName data = (MicroServiceConfigName) duplicateData.get(0);
211 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
212 microServiceConfigName.setId(data.getId());
213 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
214 (request.getParameter(operation) == null && (data.getId() != microServiceConfigName.getId()))){
215 duplicateflag = true;
218 String responseString = null;
220 if(microServiceConfigName.getId() == 0){
221 commonClassDao.save(microServiceConfigName);
223 commonClassDao.update(microServiceConfigName);
225 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class));
227 responseString = duplicateResponseString;
230 return utils.getResultForApi(responseString);
232 utils.setResponseData(response, microServiceConfigNameDatas, responseString);
234 }catch (Exception e){
235 utils.setErrorResponseData(response, e);
240 @RequestMapping(value={"/ms_dictionary/remove_msConfigName"}, method={RequestMethod.POST})
241 public void removeMicroServiceConfigNameDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
242 DictionaryUtils utils = getDictionaryUtilsInstance();
243 utils.removeData(request, response, microServiceConfigNameDatas, MicroServiceConfigName.class);
246 @RequestMapping(value={"/get_MicroServiceLocationDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
247 public void getMicroServiceLocationByNameDictionaryEntityData(HttpServletResponse response){
248 DictionaryUtils utils = getDictionaryUtilsInstance();
249 utils.getDataByEntity(response, microServiceLocationDatas, "name", MicroServiceLocation.class);
252 @RequestMapping(value={"/get_MicroServiceLocationData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
253 public void getMicroServiceLocationDictionaryEntityData(HttpServletResponse response){
254 DictionaryUtils utils = getDictionaryUtilsInstance();
255 utils.getData(response, microServiceLocationDatas, MicroServiceLocation.class);
258 @RequestMapping(value={"/ms_dictionary/save_location"}, method={RequestMethod.POST})
259 public ModelAndView saveMicroServiceLocationDictionary(HttpServletRequest request, HttpServletResponse response)throws IOException{
260 DictionaryUtils utils = getDictionaryUtilsInstance();
262 boolean fromAPI = utils.isRequestFromAPI(request);
263 ObjectMapper mapper = new ObjectMapper();
264 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
265 JsonNode root = mapper.readTree(request.getReader());
266 MicroServiceLocation microServiceLocation;
268 microServiceLocation = mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceLocation.class);
270 microServiceLocation = mapper.readValue(root.get("microServiceLocationDictionaryData").toString(), MicroServiceLocation.class);
273 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceLocation.getName(), "name", MicroServiceLocation.class);
274 boolean duplicateflag = false;
275 if(duplicateData != null && !duplicateData.isEmpty()){
276 MicroServiceLocation data = (MicroServiceLocation) duplicateData.get(0);
277 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
278 microServiceLocation.setId(data.getId());
279 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
280 (request.getParameter(operation) == null && (data.getId() != microServiceLocation.getId()))){
281 duplicateflag = true;
284 String responseString = null;
286 if(microServiceLocation.getId() == 0){
287 commonClassDao.save(microServiceLocation);
289 commonClassDao.update(microServiceLocation);
291 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class));
293 responseString = duplicateResponseString;
296 return utils.getResultForApi(responseString);
298 utils.setResponseData(response, microServiceLocationDatas, responseString);
300 }catch (Exception e){
301 utils.setErrorResponseData(response, e);
306 @RequestMapping(value={"/ms_dictionary/remove_msLocation"}, method={RequestMethod.POST})
307 public void removeMicroServiceLocationDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
308 DictionaryUtils utils = getDictionaryUtilsInstance();
309 utils.removeData(request, response, microServiceLocationDatas, MicroServiceLocation.class);
312 @RequestMapping(value={"/get_MicroServiceAttributeDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
313 public void getMicroServiceAttributeByNameDictionaryEntityData(HttpServletResponse response){
314 DictionaryUtils utils = getDictionaryUtilsInstance();
315 utils.getDataByEntity(response, microServiceAttributeDatas, "name", MicroServiceAttribute.class);
318 @RequestMapping(value={"/get_MicroServiceAttributeData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
319 public void getMicroServiceAttributeDictionaryEntityData(HttpServletResponse response){
320 DictionaryUtils utils = getDictionaryUtilsInstance();
321 utils.getData(response, microServiceAttributeDatas, MicroServiceAttribute.class);
324 @RequestMapping(value={"/ms_dictionary/save_modelAttribute"}, method={RequestMethod.POST})
325 public ModelAndView saveMicroServiceAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
326 DictionaryUtils utils = getDictionaryUtilsInstance();
328 boolean fromAPI = utils.isRequestFromAPI(request);
329 ObjectMapper mapper = new ObjectMapper();
330 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
331 JsonNode root = mapper.readTree(request.getReader());
333 MicroServiceAttribute microServiceAttribute;
336 microServiceAttribute = mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceAttribute.class);
338 microServiceAttribute = mapper.readValue(root.get("modelAttributeDictionaryData").toString(), MicroServiceAttribute.class);
340 checkValue = microServiceAttribute.getName() + ":" + microServiceAttribute.getValue() + ":" + microServiceAttribute.getModelName();
341 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkValue, "name:value:modelName", MicroServiceAttribute.class);
342 boolean duplicateflag = false;
343 if(duplicateData != null && !duplicateData.isEmpty()){
344 MicroServiceAttribute data = (MicroServiceAttribute) duplicateData.get(0);
345 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
346 microServiceAttribute.setId(data.getId());
347 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
348 (request.getParameter(operation) == null && (data.getId() != microServiceAttribute.getId()))){
349 duplicateflag = true;
352 String responseString = null;
354 if(microServiceAttribute.getId() == 0){
355 commonClassDao.save(microServiceAttribute);
357 commonClassDao.update(microServiceAttribute);
359 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceAttribute.class));
361 responseString = duplicateResponseString;
364 return utils.getResultForApi(responseString);
366 utils.setResponseData(response, microServiceAttributeDatas, responseString);
370 utils.setErrorResponseData(response, e);
375 @RequestMapping(value={"/ms_dictionary/remove_modelAttribute"}, method={RequestMethod.POST})
376 public void removeMicroServiceAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
377 DictionaryUtils utils = getDictionaryUtilsInstance();
378 utils.removeData(request, response, microServiceAttributeDatas, MicroServiceAttribute.class);
382 @RequestMapping(value={"/get_MicroServiceModelsDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
383 public void getMicroServiceModelsDictionaryByNameEntityData(HttpServletResponse response){
384 DictionaryUtils utils = getDictionaryUtilsInstance();
385 utils.getDataByEntity(response, microServiceModelsDictionaryDatas, modelName, MicroServiceModels.class);
388 @RequestMapping(value={"/get_MicroServiceModelsDataByVersion"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
389 public void getMicroServiceModelsDictionaryByVersionEntityData(HttpServletRequest request, HttpServletResponse response){
391 Map<String, Object> model = new HashMap<>();
392 ObjectMapper mapper = new ObjectMapper();
393 JsonNode root = mapper.readTree(request.getReader());
394 String modelName = null;
395 if (root.get(microServiceModelsDictionaryData).has(modelName)){
396 modelName = root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", "");
398 if (modelName!=null){
399 model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(commonClassDao.getDataById(MicroServiceModels.class, modelName, modelName)));
401 model.put(errorMsg, "No model name given");
403 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
404 JSONObject j = new JSONObject(msg);
405 response.getWriter().write(j.toString());
412 @RequestMapping(value={"/get_MicroServiceModelsData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
413 public void getMicroServiceModelsDictionaryEntityData(HttpServletResponse response){
414 DictionaryUtils utils = getDictionaryUtilsInstance();
415 utils.getData(response, microServiceModelsDictionaryDatas, MicroServiceModels.class);
418 @RequestMapping(value={"/get_MicroServiceModelsDataServiceVersion"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
419 public void getMicroServiceModelsDictionaryEntityDataServiceVersion(HttpServletResponse response){
421 Map<String, Object> model = new HashMap<>();
422 ObjectMapper mapper = new ObjectMapper();
423 List<String> data = new ArrayList<>();
424 List<Object> datas = commonClassDao.getData(MicroServiceModels.class);
425 for(int i = 0; i < datas.size(); i++){
426 MicroServiceModels msmodel = (MicroServiceModels) datas.get(i);
427 if (!data.contains(msmodel.getModelName())){
428 data.add(msmodel.getModelName() + "-v" + msmodel.getVersion());
431 model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(data));
432 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
433 JSONObject j = new JSONObject(msg);
434 response.addHeader("successMapKey", "success");
435 response.addHeader("operation", "getDictionary");
436 response.getWriter().write(j.toString());
440 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
441 response.addHeader("error", "dictionaryDBQuery");
446 @RequestMapping(value={"/get_MicroServiceModelsDataByClass"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
447 public void getMicroServiceModelsDictionaryClassEntityData(HttpServletResponse response){
449 Map<String, Object> model = new HashMap<>();
450 ObjectMapper mapper = new ObjectMapper();
451 model.put("microServiceModelsDictionaryClassDatas", mapper.writeValueAsString(modelList));
452 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
453 JSONObject j = new JSONObject(msg);
454 response.addHeader(successMapKey, successMsg);
455 response.addHeader(operation, getDictionary);
456 response.getWriter().write(j.toString());
460 response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
461 response.addHeader(errorMsg, dictionaryDBQuery);
466 @RequestMapping(value={"/ms_dictionary/save_model"}, method={RequestMethod.POST})
467 public ModelAndView saveMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
468 DictionaryUtils utils = getDictionaryUtilsInstance();
470 this.newModel = new MicroServiceModels();
471 boolean fromAPI = utils.isRequestFromAPI(request);
472 ObjectMapper mapper = new ObjectMapper();
473 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
474 JsonNode root = mapper.readTree(request.getReader());
475 MicroServiceModels microServiceModels = new MicroServiceModels();
476 String userId = null;
478 String dataOrderInfo = null;
479 if(root.has("dataOrderInfo")){
480 dataOrderInfo = root.get("dataOrderInfo").toString();
483 if(root.has("modelType")){
484 JsonNode dataType = root.get("modelType");
485 String modelType= dataType.toString();
486 if(modelType.contains("yml")){
487 if (root.has(microServiceModelsDictionaryData)){
488 if (root.get(microServiceModelsDictionaryData).has(description)){
489 microServiceModels.setDescription(root.get(microServiceModelsDictionaryData).get(description).asText().replace("\"", ""));
491 if (root.get(microServiceModelsDictionaryData).has(modelName)){
492 microServiceModels.setModelName(root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", ""));
493 this.newModel.setModelName(microServiceModels.getModelName());
495 if (root.get(microServiceModelsDictionaryData).has(version)){
496 microServiceModels.setVersion(root.get(microServiceModelsDictionaryData).get(version).asText().replace("\"", ""));
497 this.newModel.setVersion(microServiceModels.getVersion());
501 classMap = new LinkedHashMap<>();
502 JsonNode data = root.get(classMapData);
503 ObjectMapper mapper1 = new ObjectMapper();
504 String data1 = data.toString().substring(1, data.toString().length()-1);
505 data1 = data1.replace("\\", "");
506 data1=data1.replace("\"{","{");
507 data1=data1.replace("}\"","}");
508 JSONObject jsonObject = new JSONObject(data1);
509 Set<String> keys = jsonObject.keySet();
510 for(String key : keys){
511 String value = jsonObject.get(key).toString();
512 MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class);
513 classMap.put(key, msAttributeObject);
516 userId = root.get("userid").textValue();
517 MSAttributeObject mainClass = classMap.get(this.newModel.getModelName());
518 this.newModel.setDependency("[]");
519 String value = new Gson().toJson(mainClass.getSubClass());
520 this.newModel.setSub_attributes(value);
521 String attributes= mainClass.getAttribute().toString().replace("{", "").replace("}", "");
522 int equalsIndexForAttributes= attributes.indexOf('=');
523 String atttributesAfterFirstEquals= attributes.substring(equalsIndexForAttributes+1);
524 this.newModel.setAttributes(atttributesAfterFirstEquals);
525 String refAttributes= mainClass.getRefAttribute().toString().replace("{", "").replace("}", "");
526 int equalsIndex= refAttributes.indexOf("=");
527 String refAttributesAfterFirstEquals= refAttributes.substring(equalsIndex+1);
528 this.newModel.setRef_attributes(refAttributesAfterFirstEquals);
529 this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
530 this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
534 microServiceModels = mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceModels.class);
537 if (root.has(microServiceModelsDictionaryData)){
538 if (root.get(microServiceModelsDictionaryData).has(description)){
539 microServiceModels.setDescription(root.get(microServiceModelsDictionaryData).get(description).asText().replace("\"", ""));
541 if (root.get(microServiceModelsDictionaryData).has(modelName)){
542 microServiceModels.setModelName(root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", ""));
543 this.newModel.setModelName(microServiceModels.getModelName());
545 if (root.get(microServiceModelsDictionaryData).has(version)){
546 microServiceModels.setVersion(root.get(microServiceModelsDictionaryData).get(version).asText().replace("\"", ""));
547 this.newModel.setVersion(microServiceModels.getVersion());
550 if(root.has(classMapData)){
551 classMap = new LinkedHashMap<>();
552 JsonNode data = root.get(classMapData);
553 ObjectMapper mapper1 = new ObjectMapper();
554 String data1 = data.toString().substring(1, data.toString().length()-1);
555 data1 = data1.replace("\\", "");
556 JSONObject jsonObject = new JSONObject(data1);
557 Set<String> keys = jsonObject.keySet();
558 for(String key : keys){
559 String value = jsonObject.get(key).toString();
560 MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class);
561 classMap.put(key, msAttributeObject);
564 userId = root.get("userid").textValue();
565 addValuesToNewModel(classMap);
569 microServiceModels.setAttributes(this.newModel.getAttributes());
570 microServiceModels.setRef_attributes(this.newModel.getRef_attributes());
571 microServiceModels.setDependency(this.newModel.getDependency());
572 microServiceModels.setModelName(this.newModel.getModelName());
573 microServiceModels.setSub_attributes(this.newModel.getSub_attributes());
574 microServiceModels.setVersion(this.newModel.getVersion());
575 microServiceModels.setEnumValues(this.newModel.getEnumValues());
576 microServiceModels.setAnnotation(this.newModel.getAnnotation());
577 if(dataOrderInfo != null){
578 microServiceModels.setDataOrderInfo(dataOrderInfo);
580 String checkName = microServiceModels.getModelName() + ":" + microServiceModels.getVersion();
581 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(checkName, "modelName:version", MicroServiceModels.class);
582 boolean duplicateflag = false;
583 if(duplicateData != null && !duplicateData.isEmpty()){
584 MicroServiceModels data = (MicroServiceModels) duplicateData.get(0);
585 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
586 microServiceModels.setId(data.getId());
587 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
588 (request.getParameter(operation) == null && (data.getId() != microServiceModels.getId()))){
589 duplicateflag = true;
592 UserInfo userInfo = utils.getUserInfo(userId);
594 String responseString = null;
596 microServiceModels.setUserCreatedBy(userInfo);
597 if(microServiceModels.getId() == 0){
598 commonClassDao.save(microServiceModels);
600 commonClassDao.update(microServiceModels);
602 responseString = mapper.writeValueAsString(commonClassDao.getData(PrefixList.class));
604 responseString = duplicateResponseString;
607 return utils.getResultForApi(responseString);
609 utils.setResponseData(response, microServiceModelsDictionaryDatas, responseString);
611 }catch (Exception e){
612 utils.setErrorResponseData(response, e);
617 @RequestMapping(value={"/ms_dictionary/remove_msModel"}, method={RequestMethod.POST})
618 public void removeMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
619 DictionaryUtils utils = getDictionaryUtilsInstance();
620 utils.removeData(request, response, microServiceModelsDictionaryDatas, MicroServiceModels.class);
623 private void addValuesToNewModel(HashMap<String,MSAttributeObject > classMap) {
624 //Loop through the classmap and pull out the required info for the new file.
625 String subAttribute = null;
627 MSAttributeObject mainClass = classMap.get(this.newModel.getModelName());
629 if (mainClass !=null){
630 String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[]{"[", "]", " "}, new String[]{"", "", ""});
631 ArrayList<String> dependency = new ArrayList<>(Arrays.asList(dependTemp.split(",")));
632 dependency = getFullDependencyList(dependency);
633 for (String element : dependency){
634 MSAttributeObject temp = classMap.get(element);
636 mainClass.addAllRefAttribute(temp.getRefAttribute());
637 mainClass.addAllAttribute(temp.getAttribute());
640 subAttribute = utils.createSubAttributes(dependency, classMap, this.newModel.getModelName());
643 this.newModel.setDependency("");
646 if (mainClass != null && mainClass.getDependency()==null){
647 mainClass.setDependency("");
649 if(mainClass != null){
650 this.newModel.setDependency(mainClass.getDependency());
651 this.newModel.setSub_attributes(subAttribute);
652 this.newModel.setAttributes(mainClass.getAttribute().toString().replace("{", "").replace("}", ""));
653 this.newModel.setRef_attributes(mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""));
654 this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
655 this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
659 private ArrayList<String> getFullDependencyList(ArrayList<String> dependency) {
660 ArrayList<String> returnList = new ArrayList<>();
661 ArrayList<String> workingList = new ArrayList<>();
662 returnList.addAll(dependency);
663 for (String element : dependency ){
664 if (classMap.containsKey(element)){
665 MSAttributeObject value = classMap.get(element);
666 String rawValue = StringUtils.replaceEach(value.getDependency(), new String[]{"[", "]"}, new String[]{"", ""});
667 workingList = new ArrayList<>(Arrays.asList(rawValue.split(",")));
668 for(String depend : workingList){
669 if (!returnList.contains(depend) && !depend.isEmpty()){
670 returnList.add(depend.trim());
671 //getFullDepedency(workingList)
680 @RequestMapping(value={"/get_MicroServiceHeaderDefaultsDataByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
681 public void getMicroServiceHeaderDefaultsEntityDataByName(HttpServletResponse response){
682 DictionaryUtils utils = getDictionaryUtilsInstance();
683 utils.getDataByEntity(response, microServiceHeaderDefaultDatas, "modelName", MicroserviceHeaderdeFaults.class);
686 @RequestMapping(value={"/get_MicroServiceHeaderDefaultsData"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
687 public void getMicroServiceHeaderDefaultsEntityData(HttpServletResponse response){
688 DictionaryUtils utils = getDictionaryUtilsInstance();
689 utils.getData(response, microServiceHeaderDefaultDatas, MicroserviceHeaderdeFaults.class);
693 @RequestMapping(value={"/ms_dictionary/save_headerDefaults"}, method={RequestMethod.POST})
694 public ModelAndView saveMicroServiceHeaderDefaultValues(HttpServletRequest request, HttpServletResponse response) throws IOException{
695 DictionaryUtils utils = getDictionaryUtilsInstance();
697 boolean fromAPI = utils.isRequestFromAPI(request);
698 ObjectMapper mapper = new ObjectMapper();
699 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
700 JsonNode root = mapper.readTree(request.getReader());
702 MicroserviceHeaderdeFaults msHeaderdeFaults;
704 msHeaderdeFaults = mapper.readValue(root.get(dictionaryFields).toString(), MicroserviceHeaderdeFaults.class);
706 msHeaderdeFaults = mapper.readValue(root.get("modelAttributeDictionaryData").toString(), MicroserviceHeaderdeFaults.class);
709 List<Object> duplicateData = commonClassDao.checkDuplicateEntry(msHeaderdeFaults.getModelName(), "modelName", MicroserviceHeaderdeFaults.class);
710 boolean duplicateflag = false;
711 if(duplicateData != null && !duplicateData.isEmpty()){
712 MicroserviceHeaderdeFaults data = (MicroserviceHeaderdeFaults) duplicateData.get(0);
713 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
714 msHeaderdeFaults.setId(data.getId());
715 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) ||
716 (request.getParameter(operation) == null && (data.getId() != msHeaderdeFaults.getId()))){
717 duplicateflag = true;
721 String responseString = null;
723 if(msHeaderdeFaults.getId() == 0){
724 commonClassDao.save(msHeaderdeFaults);
726 commonClassDao.update(msHeaderdeFaults);
728 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroserviceHeaderdeFaults.class));
730 responseString = duplicateResponseString;
733 return utils.getResultForApi(responseString);
735 utils.setResponseData(response, microServiceHeaderDefaultDatas, responseString);
739 utils.setErrorResponseData(response, e);
744 @RequestMapping(value={"/ms_dictionary/remove_headerDefaults"}, method={RequestMethod.POST})
745 public void removeMicroServiceHeaderDefaults(HttpServletRequest request, HttpServletResponse response) throws IOException{
746 DictionaryUtils utils = getDictionaryUtilsInstance();
747 utils.removeData(request, response, microServiceHeaderDefaultDatas, MicroserviceHeaderdeFaults.class);