Unit/SONAR/Checkstyle in ONAP-REST
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / MicroServiceDictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2019 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.controller;
22
23 import com.fasterxml.jackson.databind.DeserializationFeature;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.gson.Gson;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
39
40 import org.apache.commons.lang.StringUtils;
41 import org.json.JSONObject;
42 import org.onap.policy.common.logging.flexlogger.FlexLogger;
43 import org.onap.policy.common.logging.flexlogger.Logger;
44 import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
45 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
46 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
47 import org.onap.policy.rest.dao.CommonClassDao;
48 import org.onap.policy.rest.jpa.DcaeUuid;
49 import org.onap.policy.rest.jpa.DictionaryData;
50 import org.onap.policy.rest.jpa.MicroServiceAttribute;
51 import org.onap.policy.rest.jpa.MicroServiceConfigName;
52 import org.onap.policy.rest.jpa.MicroServiceLocation;
53 import org.onap.policy.rest.jpa.MicroServiceModels;
54 import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults;
55 import org.onap.policy.rest.jpa.PrefixList;
56 import org.onap.policy.rest.jpa.UserInfo;
57 import org.onap.policy.rest.util.MSAttributeObject;
58 import org.onap.policy.rest.util.MSModelUtils;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.http.MediaType;
61 import org.springframework.stereotype.Controller;
62 import org.springframework.web.bind.annotation.RequestMapping;
63 import org.springframework.web.bind.annotation.RequestMethod;
64 import org.springframework.web.servlet.ModelAndView;
65
66 @Controller
67 public class MicroServiceDictionaryController {
68     private static final Logger LOGGER = FlexLogger.getLogger(MicroServiceDictionaryController.class);
69
70     private static CommonClassDao commonClassDao;
71
72     private static String successMapKey = "successMapKey";
73     private static String successMsg = "success";
74     private static String operation = "operation";
75     private static String getDictionary = "getDictionary";
76     private static String errorMsg = "error";
77     private static String dictionaryDBQuery = "dictionaryDBQuery";
78     private LinkedHashMap<String, MSAttributeObject> classMap;
79     private List<String> modelList = new ArrayList<>();
80     private static String dictionaryFields = "dictionaryFields";
81     private static String duplicateResponseString = "Duplicate";
82     private static String microServiceModelsDictionaryDatas = "microServiceModelsDictionaryDatas";
83     private static String modelName = "modelName";
84     private static String microServiceModelsDictionaryData = "microServiceModelsDictionaryData";
85     private static String description = "description";
86     private static String version = "version";
87     private static String classMapData = "classMap";
88     private static String dcaeUUIDDatas = "dcaeUUIDDictionaryDatas";
89     private static String microServiceConfigNameDatas = "microServiceConfigNameDictionaryDatas";
90     private static String microServiceLocationDatas = "microServiceLocationDictionaryDatas";
91     private static String microServiceAttributeDatas = "microServiceAttributeDictionaryDatas";
92     private static String microServiceHeaderDefaultDatas = "microServiceHeaderDefaultDatas";
93     private static final String MICROSERVICEDICTNAMEDATAS = "microServiceDictionaryDatas";
94
95     public MicroServiceDictionaryController() {
96         super();
97     }
98
99     private DictionaryUtils getDictionaryUtilsInstance() {
100         return DictionaryUtils.getDictionaryUtils();
101     }
102
103     @Autowired
104     public MicroServiceDictionaryController(CommonClassDao commonClassDao) {
105         MicroServiceDictionaryController.commonClassDao = commonClassDao;
106     }
107
108     public static void setCommonClassDao(CommonClassDao commonClassDao) {
109         MicroServiceDictionaryController.commonClassDao = commonClassDao;
110     }
111
112     MSModelUtils utils = new MSModelUtils(XACMLPapServlet.getMsOnapName(), XACMLPapServlet.getMsPolicyName());
113
114     private MicroServiceModels newModel;
115
116     @RequestMapping(
117             value = {"/get_DCAEUUIDDataByName"},
118             method = {RequestMethod.GET},
119             produces = MediaType.APPLICATION_JSON_VALUE)
120     public void getDCAEUUIDDictionaryByNameEntityData(HttpServletResponse response) {
121         DictionaryUtils utils = getDictionaryUtilsInstance();
122         utils.getDataByEntity(response, dcaeUUIDDatas, "name", DcaeUuid.class);
123     }
124
125     @RequestMapping(
126             value = {"/get_DCAEUUIDData"},
127             method = {RequestMethod.GET},
128             produces = MediaType.APPLICATION_JSON_VALUE)
129     public void getDCAEUUIDDictionaryEntityData(HttpServletResponse response) {
130         DictionaryUtils utils = getDictionaryUtilsInstance();
131         utils.getData(response, dcaeUUIDDatas, DcaeUuid.class);
132     }
133
134     @RequestMapping(value = {"/ms_dictionary/save_dcaeUUID"}, method = {RequestMethod.POST})
135     public ModelAndView saveDCAEUUIDDictionary(HttpServletRequest request, HttpServletResponse response)
136             throws IOException {
137         DictionaryUtils utils = getDictionaryUtilsInstance();
138         try {
139             boolean fromAPI = utils.isRequestFromAPI(request);
140             ObjectMapper mapper = new ObjectMapper();
141             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
142             JsonNode root = mapper.readTree(request.getReader());
143             DcaeUuid dCAEuuid;
144             if (fromAPI) {
145                 dCAEuuid = mapper.readValue(root.get(dictionaryFields).toString(), DcaeUuid.class);
146             } else {
147                 dCAEuuid = mapper.readValue(root.get("dcaeUUIDDictionaryData").toString(), DcaeUuid.class);
148             }
149
150             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(dCAEuuid.getName(), "name", DcaeUuid.class);
151             boolean duplicateflag = false;
152             if (duplicateData != null && !duplicateData.isEmpty()) {
153                 DcaeUuid data = (DcaeUuid) duplicateData.get(0);
154                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
155                     dCAEuuid.setId(data.getId());
156                 } else if ((request.getParameter(operation) != null
157                         && !"update".equals(request.getParameter(operation)))
158                         || (request.getParameter(operation) == null && (data.getId() != dCAEuuid.getId()))) {
159                     duplicateflag = true;
160                 }
161             }
162             String responseString = null;
163             if (!duplicateflag) {
164                 if (dCAEuuid.getId() == 0) {
165                     commonClassDao.save(dCAEuuid);
166                 } else {
167                     commonClassDao.update(dCAEuuid);
168                 }
169                 responseString = mapper.writeValueAsString(commonClassDao.getData(DcaeUuid.class));
170             } else {
171                 responseString = duplicateResponseString;
172             }
173             if (fromAPI) {
174                 return utils.getResultForApi(responseString);
175             } else {
176                 utils.setResponseData(response, dcaeUUIDDatas, responseString);
177             }
178         } catch (Exception e) {
179             utils.setErrorResponseData(response, e);
180         }
181         return null;
182     }
183
184     @RequestMapping(value = {"/ms_dictionary/remove_dcaeuuid"}, method = {RequestMethod.POST})
185     public void removeDCAEUUIDDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException {
186         DictionaryUtils utils = getDictionaryUtilsInstance();
187         utils.removeData(request, response, dcaeUUIDDatas, DcaeUuid.class);
188     }
189
190     @RequestMapping(
191             value = {"/get_MicroServiceConfigNameDataByName"},
192             method = {RequestMethod.GET},
193             produces = MediaType.APPLICATION_JSON_VALUE)
194     public void getMicroServiceConfigNameByNameDictionaryEntityData(HttpServletResponse response) {
195         DictionaryUtils utils = getDictionaryUtilsInstance();
196         utils.getDataByEntity(response, microServiceConfigNameDatas, "name", MicroServiceConfigName.class);
197     }
198
199     @RequestMapping(
200             value = {"/get_MicroServiceConfigNameData"},
201             method = {RequestMethod.GET},
202             produces = MediaType.APPLICATION_JSON_VALUE)
203     public void getMicroServiceConfigNameDictionaryEntityData(HttpServletResponse response) {
204         DictionaryUtils utils = getDictionaryUtilsInstance();
205         utils.getData(response, microServiceConfigNameDatas, MicroServiceConfigName.class);
206     }
207
208     @RequestMapping(
209             value = {"/get_MicroServiceDictData"},
210             method = {RequestMethod.GET},
211             produces = MediaType.APPLICATION_JSON_VALUE)
212     public void getMicroServiceDictNameDictionaryEntityData(HttpServletResponse response) {
213         DictionaryUtils utils = getDictionaryUtilsInstance();
214         utils.getData(response, MICROSERVICEDICTNAMEDATAS, DictionaryData.class);
215     }
216
217     /**
218      * Save dictionary data.
219      *
220      * @param request the request
221      * @param response the response
222      * @return the model and view
223      * @throws IOException Signals that an I/O exception has occurred.
224      */
225     // save_DictionaryData
226     @RequestMapping(value = {"/ms_dictionary/save_DictionaryData"}, method = {RequestMethod.POST})
227     public ModelAndView saveDictionaryData(HttpServletRequest request, HttpServletResponse response)
228             throws IOException {
229         DictionaryUtils dictUtil = getDictionaryUtilsInstance();
230         try {
231             boolean fromApi = dictUtil.isRequestFromAPI(request);
232             ObjectMapper mapper = new ObjectMapper();
233             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
234             JsonNode root = mapper.readTree(request.getReader());
235             DictionaryData dictionaryData;
236             if (fromApi) {
237                 dictionaryData = mapper.readValue(root.get(dictionaryFields).toString(), DictionaryData.class);
238             } else {
239                 dictionaryData = mapper.readValue(root.get(MICROSERVICEDICTNAMEDATAS).toString(), DictionaryData.class);
240             }
241             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(dictionaryData.getDictionaryName(),
242                     "dictionaryName", DictionaryData.class);
243             boolean duplicateflag = false;
244             if (duplicateData != null && !duplicateData.isEmpty()) {
245                 DictionaryData data = (DictionaryData) duplicateData.get(0);
246                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
247                     dictionaryData.setId(data.getId());
248                 } else if ((request.getParameter(operation) != null
249                         && !"update".equals(request.getParameter(operation)))
250                         || (request.getParameter(operation) == null && (data.getId() != dictionaryData.getId()))) {
251                     duplicateflag = true;
252                 }
253             }
254             String responseString = null;
255             if (!duplicateflag) {
256                 if (dictionaryData.getId() == 0) {
257                     commonClassDao.save(dictionaryData);
258                 } else {
259                     commonClassDao.update(dictionaryData);
260                 }
261                 responseString = mapper.writeValueAsString(commonClassDao.getData(DictionaryData.class));
262             } else {
263                 responseString = duplicateResponseString;
264             }
265             if (fromApi) {
266                 return dictUtil.getResultForApi(responseString);
267             } else {
268                 dictUtil.setResponseData(response, MICROSERVICEDICTNAMEDATAS, responseString);
269             }
270         } catch (Exception e) {
271             dictUtil.setErrorResponseData(response, e);
272         }
273         return null;
274     }
275
276     @RequestMapping(value = {"/ms_dictionary/remove_msDictionaryData"}, method = {RequestMethod.POST})
277     public void removeMicroServiceDictionaryData(HttpServletRequest request, HttpServletResponse response)
278             throws IOException {
279         DictionaryUtils utils = getDictionaryUtilsInstance();
280         utils.removeData(request, response, MICROSERVICEDICTNAMEDATAS, DictionaryData.class);
281     }
282
283     //
284     @RequestMapping(value = {"/ms_dictionary/save_configName"}, method = {RequestMethod.POST})
285     public ModelAndView saveMicroServiceConfigNameDictionary(HttpServletRequest request, HttpServletResponse response)
286             throws IOException {
287         DictionaryUtils utils = getDictionaryUtilsInstance();
288         try {
289             boolean fromAPI = utils.isRequestFromAPI(request);
290             ObjectMapper mapper = new ObjectMapper();
291             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
292             JsonNode root = mapper.readTree(request.getReader());
293             MicroServiceConfigName microServiceConfigName;
294             if (fromAPI) {
295                 microServiceConfigName =
296                         mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceConfigName.class);
297             } else {
298                 microServiceConfigName = mapper.readValue(root.get("microServiceConfigNameDictionaryData").toString(),
299                         MicroServiceConfigName.class);
300             }
301             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceConfigName.getName(), "name",
302                     MicroServiceConfigName.class);
303             boolean duplicateflag = false;
304             if (duplicateData != null && !duplicateData.isEmpty()) {
305                 MicroServiceConfigName data = (MicroServiceConfigName) duplicateData.get(0);
306                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
307                     microServiceConfigName.setId(data.getId());
308                 } else if ((request.getParameter(operation) != null
309                         && !"update".equals(request.getParameter(operation)))
310                         || (request.getParameter(operation) == null
311                                 && (data.getId() != microServiceConfigName.getId()))) {
312                     duplicateflag = true;
313                 }
314             }
315             String responseString = null;
316             if (!duplicateflag) {
317                 if (microServiceConfigName.getId() == 0) {
318                     commonClassDao.save(microServiceConfigName);
319                 } else {
320                     commonClassDao.update(microServiceConfigName);
321                 }
322                 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class));
323             } else {
324                 responseString = duplicateResponseString;
325             }
326             if (fromAPI) {
327                 return utils.getResultForApi(responseString);
328             } else {
329                 utils.setResponseData(response, microServiceConfigNameDatas, responseString);
330             }
331         } catch (Exception e) {
332             utils.setErrorResponseData(response, e);
333         }
334         return null;
335     }
336
337     @RequestMapping(value = {"/ms_dictionary/remove_msConfigName"}, method = {RequestMethod.POST})
338     public void removeMicroServiceConfigNameDictionary(HttpServletRequest request, HttpServletResponse response)
339             throws IOException {
340         DictionaryUtils utils = getDictionaryUtilsInstance();
341         utils.removeData(request, response, microServiceConfigNameDatas, MicroServiceConfigName.class);
342     }
343
344     @RequestMapping(
345             value = {"/get_MicroServiceLocationDataByName"},
346             method = {RequestMethod.GET},
347             produces = MediaType.APPLICATION_JSON_VALUE)
348     public void getMicroServiceLocationByNameDictionaryEntityData(HttpServletResponse response) {
349         DictionaryUtils utils = getDictionaryUtilsInstance();
350         utils.getDataByEntity(response, microServiceLocationDatas, "name", MicroServiceLocation.class);
351     }
352
353     @RequestMapping(
354             value = {"/get_MicroServiceLocationData"},
355             method = {RequestMethod.GET},
356             produces = MediaType.APPLICATION_JSON_VALUE)
357     public void getMicroServiceLocationDictionaryEntityData(HttpServletResponse response) {
358         DictionaryUtils utils = getDictionaryUtilsInstance();
359         utils.getData(response, microServiceLocationDatas, MicroServiceLocation.class);
360     }
361
362     @RequestMapping(value = {"/ms_dictionary/save_location"}, method = {RequestMethod.POST})
363     public ModelAndView saveMicroServiceLocationDictionary(HttpServletRequest request, HttpServletResponse response)
364             throws IOException {
365         DictionaryUtils utils = getDictionaryUtilsInstance();
366         try {
367             boolean fromAPI = utils.isRequestFromAPI(request);
368             ObjectMapper mapper = new ObjectMapper();
369             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
370             JsonNode root = mapper.readTree(request.getReader());
371             MicroServiceLocation microServiceLocation;
372             if (fromAPI) {
373                 microServiceLocation =
374                         mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceLocation.class);
375             } else {
376                 microServiceLocation = mapper.readValue(root.get("microServiceLocationDictionaryData").toString(),
377                         MicroServiceLocation.class);
378             }
379
380             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(microServiceLocation.getName(), "name",
381                     MicroServiceLocation.class);
382             boolean duplicateflag = false;
383             if (duplicateData != null && !duplicateData.isEmpty()) {
384                 MicroServiceLocation data = (MicroServiceLocation) duplicateData.get(0);
385                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
386                     microServiceLocation.setId(data.getId());
387                 } else if ((request.getParameter(operation) != null
388                         && !"update".equals(request.getParameter(operation)))
389                         || (request.getParameter(operation) == null
390                                 && (data.getId() != microServiceLocation.getId()))) {
391                     duplicateflag = true;
392                 }
393             }
394             String responseString = null;
395             if (!duplicateflag) {
396                 if (microServiceLocation.getId() == 0) {
397                     commonClassDao.save(microServiceLocation);
398                 } else {
399                     commonClassDao.update(microServiceLocation);
400                 }
401                 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class));
402             } else {
403                 responseString = duplicateResponseString;
404             }
405             if (fromAPI) {
406                 return utils.getResultForApi(responseString);
407             } else {
408                 utils.setResponseData(response, microServiceLocationDatas, responseString);
409             }
410         } catch (Exception e) {
411             utils.setErrorResponseData(response, e);
412         }
413         return null;
414     }
415
416     @RequestMapping(value = {"/ms_dictionary/remove_msLocation"}, method = {RequestMethod.POST})
417     public void removeMicroServiceLocationDictionary(HttpServletRequest request, HttpServletResponse response)
418             throws IOException {
419         DictionaryUtils utils = getDictionaryUtilsInstance();
420         utils.removeData(request, response, microServiceLocationDatas, MicroServiceLocation.class);
421     }
422
423     @RequestMapping(
424             value = {"/get_MicroServiceAttributeDataByName"},
425             method = {RequestMethod.GET},
426             produces = MediaType.APPLICATION_JSON_VALUE)
427     public void getMicroServiceAttributeByNameDictionaryEntityData(HttpServletResponse response) {
428         DictionaryUtils utils = getDictionaryUtilsInstance();
429         utils.getDataByEntity(response, microServiceAttributeDatas, "name", MicroServiceAttribute.class);
430     }
431
432     @RequestMapping(
433             value = {"/get_MicroServiceAttributeData"},
434             method = {RequestMethod.GET},
435             produces = MediaType.APPLICATION_JSON_VALUE)
436     public void getMicroServiceAttributeDictionaryEntityData(HttpServletResponse response) {
437         DictionaryUtils utils = getDictionaryUtilsInstance();
438         utils.getData(response, microServiceAttributeDatas, MicroServiceAttribute.class);
439     }
440
441     @RequestMapping(value = {"/ms_dictionary/save_modelAttribute"}, method = {RequestMethod.POST})
442     public ModelAndView saveMicroServiceAttributeDictionary(HttpServletRequest request, HttpServletResponse response)
443             throws IOException {
444         DictionaryUtils utils = getDictionaryUtilsInstance();
445         try {
446             boolean fromAPI = utils.isRequestFromAPI(request);
447             ObjectMapper mapper = new ObjectMapper();
448             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
449             JsonNode root = mapper.readTree(request.getReader());
450
451             MicroServiceAttribute microServiceAttribute;
452             String checkValue;
453             if (fromAPI) {
454                 microServiceAttribute =
455                         mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceAttribute.class);
456             } else {
457                 microServiceAttribute = mapper.readValue(root.get("modelAttributeDictionaryData").toString(),
458                         MicroServiceAttribute.class);
459             }
460             checkValue = microServiceAttribute.getName() + ":" + microServiceAttribute.getValue() + ":"
461                     + microServiceAttribute.getModelName();
462             List<Object> duplicateData =
463                     commonClassDao.checkDuplicateEntry(checkValue, "name:value:modelName", MicroServiceAttribute.class);
464             boolean duplicateflag = false;
465             if (duplicateData != null && !duplicateData.isEmpty()) {
466                 MicroServiceAttribute data = (MicroServiceAttribute) duplicateData.get(0);
467                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
468                     microServiceAttribute.setId(data.getId());
469                 } else if ((request.getParameter(operation) != null
470                         && !"update".equals(request.getParameter(operation)))
471                         || (request.getParameter(operation) == null
472                                 && (data.getId() != microServiceAttribute.getId()))) {
473                     duplicateflag = true;
474                 }
475             }
476             String responseString = null;
477             if (!duplicateflag) {
478                 if (microServiceAttribute.getId() == 0) {
479                     commonClassDao.save(microServiceAttribute);
480                 } else {
481                     commonClassDao.update(microServiceAttribute);
482                 }
483                 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceAttribute.class));
484             } else {
485                 responseString = duplicateResponseString;
486             }
487             if (fromAPI) {
488                 return utils.getResultForApi(responseString);
489             } else {
490                 utils.setResponseData(response, microServiceAttributeDatas, responseString);
491             }
492         } catch (Exception e) {
493             utils.setErrorResponseData(response, e);
494         }
495         return null;
496     }
497
498     @RequestMapping(value = {"/ms_dictionary/remove_modelAttribute"}, method = {RequestMethod.POST})
499     public void removeMicroServiceAttributeDictionary(HttpServletRequest request, HttpServletResponse response)
500             throws IOException {
501         DictionaryUtils utils = getDictionaryUtilsInstance();
502         utils.removeData(request, response, microServiceAttributeDatas, MicroServiceAttribute.class);
503     }
504
505     @RequestMapping(
506             value = {"/get_MicroServiceModelsDataByName"},
507             method = {RequestMethod.GET},
508             produces = MediaType.APPLICATION_JSON_VALUE)
509     public void getMicroServiceModelsDictionaryByNameEntityData(HttpServletResponse response) {
510         DictionaryUtils utils = getDictionaryUtilsInstance();
511         utils.getDataByEntity(response, microServiceModelsDictionaryDatas, modelName, MicroServiceModels.class);
512     }
513
514     @RequestMapping(
515             value = {"/get_MicroServiceModelsDataByVersion"},
516             method = {RequestMethod.GET},
517             produces = MediaType.APPLICATION_JSON_VALUE)
518     public void getMicroServiceModelsDictionaryByVersionEntityData(HttpServletRequest request,
519             HttpServletResponse response) {
520         try {
521             Map<String, Object> model = new HashMap<>();
522             ObjectMapper mapper = new ObjectMapper();
523             JsonNode root = mapper.readTree(request.getReader());
524             String modelName = null;
525             if (root.get(microServiceModelsDictionaryData).has(modelName)) {
526                 modelName = root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", "");
527             }
528             if (modelName != null) {
529                 model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(
530                         commonClassDao.getDataById(MicroServiceModels.class, modelName, modelName)));
531             } else {
532                 model.put(errorMsg, "No model name given");
533             }
534             JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
535             JSONObject j = new JSONObject(msg);
536             response.getWriter().write(j.toString());
537         } catch (Exception e) {
538             LOGGER.error(e);
539         }
540     }
541
542     @RequestMapping(
543             value = {"/get_MicroServiceModelsData"},
544             method = {RequestMethod.GET},
545             produces = MediaType.APPLICATION_JSON_VALUE)
546     public void getMicroServiceModelsDictionaryEntityData(HttpServletResponse response) {
547         DictionaryUtils utils = getDictionaryUtilsInstance();
548         utils.getData(response, microServiceModelsDictionaryDatas, MicroServiceModels.class);
549     }
550
551     @RequestMapping(
552             value = {"/get_MicroServiceModelsDataServiceVersion"},
553             method = {RequestMethod.GET},
554             produces = MediaType.APPLICATION_JSON_VALUE)
555     public void getMicroServiceModelsDictionaryEntityDataServiceVersion(HttpServletResponse response) {
556         try {
557             Map<String, Object> model = new HashMap<>();
558             ObjectMapper mapper = new ObjectMapper();
559             List<String> data = new ArrayList<>();
560             List<Object> datas = commonClassDao.getData(MicroServiceModels.class);
561             for (int i = 0; i < datas.size(); i++) {
562                 MicroServiceModels msmodel = (MicroServiceModels) datas.get(i);
563                 if (!data.contains(msmodel.getModelName())) {
564                     data.add(msmodel.getModelName() + "-v" + msmodel.getVersion());
565                 }
566             }
567             model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(data));
568             JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
569             JSONObject j = new JSONObject(msg);
570             response.addHeader("successMapKey", "success");
571             response.addHeader("operation", "getDictionary");
572             response.getWriter().write(j.toString());
573
574         } catch (Exception e) {
575             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
576             response.addHeader("error", "dictionaryDBQuery");
577             LOGGER.error(e);
578         }
579     }
580
581     @RequestMapping(
582             value = {"/get_MicroServiceModelsDataByClass"},
583             method = {RequestMethod.GET},
584             produces = MediaType.APPLICATION_JSON_VALUE)
585     public void getMicroServiceModelsDictionaryClassEntityData(HttpServletResponse response) {
586         try {
587             Map<String, Object> model = new HashMap<>();
588             ObjectMapper mapper = new ObjectMapper();
589             model.put("microServiceModelsDictionaryClassDatas", mapper.writeValueAsString(modelList));
590             JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
591             JSONObject j = new JSONObject(msg);
592             response.addHeader(successMapKey, successMsg);
593             response.addHeader(operation, getDictionary);
594             response.getWriter().write(j.toString());
595
596         } catch (Exception e) {
597             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
598             response.addHeader(errorMsg, dictionaryDBQuery);
599             LOGGER.error(e);
600         }
601     }
602
603     @RequestMapping(value = {"/ms_dictionary/save_model"}, method = {RequestMethod.POST})
604     public ModelAndView saveMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response)
605             throws IOException {
606         DictionaryUtils utils = getDictionaryUtilsInstance();
607         try {
608             this.newModel = new MicroServiceModels();
609             boolean fromAPI = utils.isRequestFromAPI(request);
610             ObjectMapper mapper = new ObjectMapper();
611             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
612             JsonNode root = mapper.readTree(request.getReader());
613             MicroServiceModels microServiceModels = new MicroServiceModels();
614             String userId = null;
615
616             String dataOrderInfo = null;
617             if (root.has("dataOrderInfo")) {
618                 dataOrderInfo = root.get("dataOrderInfo").toString();
619             }
620
621             if (root.has("modelType")) {
622                 JsonNode dataType = root.get("modelType");
623                 String modelType = dataType.toString();
624                 if (modelType.contains("yml")) {
625                     if (root.has(microServiceModelsDictionaryData)) {
626                         if (root.get(microServiceModelsDictionaryData).has(description)) {
627                             microServiceModels.setDescription(root.get(microServiceModelsDictionaryData)
628                                     .get(description).asText().replace("\"", ""));
629                         }
630                         if (root.get(microServiceModelsDictionaryData).has(modelName)) {
631                             microServiceModels.setModelName(root.get(microServiceModelsDictionaryData).get(modelName)
632                                     .asText().replace("\"", ""));
633                             this.newModel.setModelName(microServiceModels.getModelName());
634                         }
635                         if (root.get(microServiceModelsDictionaryData).has(version)) {
636                             microServiceModels.setVersion(
637                                     root.get(microServiceModelsDictionaryData).get(version).asText().replace("\"", ""));
638                             this.newModel.setVersion(microServiceModels.getVersion());
639                         }
640                     }
641
642                     classMap = new LinkedHashMap<>();
643                     JsonNode data = root.get(classMapData);
644                     ObjectMapper mapper1 = new ObjectMapper();
645                     String data1 = data.toString().substring(1, data.toString().length() - 1);
646                     data1 = data1.replace("\\", "");
647                     data1 = data1.replace("\"{", "{");
648                     data1 = data1.replace("}\"", "}");
649                     JSONObject jsonObject = new JSONObject(data1);
650                     Set<String> keys = jsonObject.keySet();
651                     for (String key : keys) {
652                         String value = jsonObject.get(key).toString();
653                         MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class);
654                         classMap.put(key, msAttributeObject);
655                     }
656
657                     userId = root.get("userid").textValue();
658                     MSAttributeObject mainClass = classMap.get(this.newModel.getModelName());
659                     this.newModel.setDependency("[]");
660                     String value = new Gson().toJson(mainClass.getSubClass());
661                     this.newModel.setSubAttributes(value);
662                     String attributes = mainClass.getAttribute().toString().replace("{", "").replace("}", "");
663                     int equalsIndexForAttributes = attributes.indexOf('=');
664                     String atttributesAfterFirstEquals = attributes.substring(equalsIndexForAttributes + 1);
665                     this.newModel.setAttributes(atttributesAfterFirstEquals);
666                     String refAttributes = mainClass.getRefAttribute().toString().replace("{", "").replace("}", "");
667                     int equalsIndex = refAttributes.indexOf("=");
668                     String refAttributesAfterFirstEquals = refAttributes.substring(equalsIndex + 1);
669                     this.newModel.setRefAttributes(refAttributesAfterFirstEquals);
670                     this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
671                     this.newModel
672                             .setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
673
674                 } else {
675                     if (fromAPI) {
676                         microServiceModels =
677                                 mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceModels.class);
678                         userId = "API";
679                     } else {
680                         if (root.has(microServiceModelsDictionaryData)) {
681                             if (root.get(microServiceModelsDictionaryData).has(description)) {
682                                 microServiceModels.setDescription(root.get(microServiceModelsDictionaryData)
683                                         .get(description).asText().replace("\"", ""));
684                             }
685                             if (root.get(microServiceModelsDictionaryData).has(modelName)) {
686                                 microServiceModels.setModelName(root.get(microServiceModelsDictionaryData)
687                                         .get(modelName).asText().replace("\"", ""));
688                                 this.newModel.setModelName(microServiceModels.getModelName());
689                             }
690                             if (root.get(microServiceModelsDictionaryData).has(version)) {
691                                 microServiceModels.setVersion(root.get(microServiceModelsDictionaryData).get(version)
692                                         .asText().replace("\"", ""));
693                                 this.newModel.setVersion(microServiceModels.getVersion());
694                             }
695                         }
696                         if (root.has(classMapData)) {
697                             classMap = new LinkedHashMap<>();
698                             JsonNode data = root.get(classMapData);
699                             ObjectMapper mapper1 = new ObjectMapper();
700                             String data1 = data.toString().substring(1, data.toString().length() - 1);
701                             data1 = data1.replace("\\", "");
702                             JSONObject jsonObject = new JSONObject(data1);
703                             Set<String> keys = jsonObject.keySet();
704                             for (String key : keys) {
705                                 String value = jsonObject.get(key).toString();
706                                 MSAttributeObject msAttributeObject = mapper1.readValue(value, MSAttributeObject.class);
707                                 classMap.put(key, msAttributeObject);
708                             }
709                         }
710                         userId = root.get("userid").textValue();
711                         addValuesToNewModel(classMap);
712                     }
713                 }
714             }
715             microServiceModels.setAttributes(this.newModel.getAttributes());
716             microServiceModels.setRefAttributes(this.newModel.getRefAttributes());
717             microServiceModels.setDependency(this.newModel.getDependency());
718             microServiceModels.setModelName(this.newModel.getModelName());
719             microServiceModels.setSubAttributes(this.newModel.getSubAttributes());
720             microServiceModels.setVersion(this.newModel.getVersion());
721             microServiceModels.setEnumValues(this.newModel.getEnumValues());
722             microServiceModels.setAnnotation(this.newModel.getAnnotation());
723             if (dataOrderInfo != null) {
724                 microServiceModels.setDataOrderInfo(dataOrderInfo);
725             }
726             String checkName = microServiceModels.getModelName() + ":" + microServiceModels.getVersion();
727             List<Object> duplicateData =
728                     commonClassDao.checkDuplicateEntry(checkName, "modelName:version", MicroServiceModels.class);
729             boolean duplicateflag = false;
730             if (duplicateData != null && !duplicateData.isEmpty()) {
731                 MicroServiceModels data = (MicroServiceModels) duplicateData.get(0);
732                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
733                     microServiceModels.setId(data.getId());
734                 } else if ((request.getParameter(operation) != null
735                         && !"update".equals(request.getParameter(operation)))
736                         || (request.getParameter(operation) == null && (data.getId() != microServiceModels.getId()))) {
737                     duplicateflag = true;
738                 }
739             }
740             UserInfo userInfo = utils.getUserInfo(userId);
741
742             String responseString = null;
743             if (!duplicateflag) {
744                 microServiceModels.setUserCreatedBy(userInfo);
745                 if (microServiceModels.getId() == 0) {
746                     commonClassDao.save(microServiceModels);
747                 } else {
748                     commonClassDao.update(microServiceModels);
749                 }
750                 responseString = mapper.writeValueAsString(commonClassDao.getData(PrefixList.class));
751             } else {
752                 responseString = duplicateResponseString;
753             }
754             if (fromAPI) {
755                 return utils.getResultForApi(responseString);
756             } else {
757                 utils.setResponseData(response, microServiceModelsDictionaryDatas, responseString);
758             }
759         } catch (Exception e) {
760             utils.setErrorResponseData(response, e);
761         }
762         return null;
763     }
764
765     @RequestMapping(value = {"/ms_dictionary/remove_msModel"}, method = {RequestMethod.POST})
766     public void removeMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response)
767             throws IOException {
768         DictionaryUtils utils = getDictionaryUtilsInstance();
769         utils.removeData(request, response, microServiceModelsDictionaryDatas, MicroServiceModels.class);
770     }
771
772     private void addValuesToNewModel(HashMap<String, MSAttributeObject> classMap) {
773         // Loop through the classmap and pull out the required info for the new file.
774         String subAttribute = null;
775
776         MSAttributeObject mainClass = classMap.get(this.newModel.getModelName());
777
778         if (mainClass != null) {
779             String dependTemp = StringUtils.replaceEach(mainClass.getDependency(), new String[] {"[", "]", " "},
780                     new String[] {"", "", ""});
781             ArrayList<String> dependency = new ArrayList<>(Arrays.asList(dependTemp.split(",")));
782             dependency = getFullDependencyList(dependency);
783             for (String element : dependency) {
784                 MSAttributeObject temp = classMap.get(element);
785                 if (temp != null) {
786                     mainClass.addAllRefAttribute(temp.getRefAttribute());
787                     mainClass.addAllAttribute(temp.getAttribute());
788                 }
789             }
790             subAttribute = utils.createSubAttributes(dependency, classMap, this.newModel.getModelName());
791         } else {
792             subAttribute = "{}";
793             this.newModel.setDependency("");
794         }
795
796         if (mainClass != null && mainClass.getDependency() == null) {
797             mainClass.setDependency("");
798         }
799         if (mainClass != null) {
800             this.newModel.setDependency(mainClass.getDependency());
801             this.newModel.setSubAttributes(subAttribute);
802             this.newModel.setAttributes(mainClass.getAttribute().toString().replace("{", "").replace("}", ""));
803             this.newModel.setRefAttributes(mainClass.getRefAttribute().toString().replace("{", "").replace("}", ""));
804             this.newModel.setEnumValues(mainClass.getEnumType().toString().replace("{", "").replace("}", ""));
805             this.newModel.setAnnotation(mainClass.getMatchingSet().toString().replace("{", "").replace("}", ""));
806         }
807     }
808
809     private ArrayList<String> getFullDependencyList(ArrayList<String> dependency) {
810         ArrayList<String> returnList = new ArrayList<>();
811         ArrayList<String> workingList = new ArrayList<>();
812         returnList.addAll(dependency);
813         for (String element : dependency) {
814             if (classMap.containsKey(element)) {
815                 MSAttributeObject value = classMap.get(element);
816                 String rawValue =
817                         StringUtils.replaceEach(value.getDependency(), new String[] {"[", "]"}, new String[] {"", ""});
818                 workingList = new ArrayList<>(Arrays.asList(rawValue.split(",")));
819                 for (String depend : workingList) {
820                     if (!returnList.contains(depend) && !depend.isEmpty()) {
821                         returnList.add(depend.trim());
822                         // getFullDepedency(workingList)
823                     }
824                 }
825             }
826         }
827
828         return returnList;
829     }
830
831     @RequestMapping(
832             value = {"/get_MicroServiceHeaderDefaultsDataByName"},
833             method = {RequestMethod.GET},
834             produces = MediaType.APPLICATION_JSON_VALUE)
835     public void getMicroServiceHeaderDefaultsEntityDataByName(HttpServletResponse response) {
836         DictionaryUtils utils = getDictionaryUtilsInstance();
837         utils.getDataByEntity(response, microServiceHeaderDefaultDatas, "modelName", MicroserviceHeaderdeFaults.class);
838     }
839
840     @RequestMapping(
841             value = {"/get_MicroServiceHeaderDefaultsData"},
842             method = {RequestMethod.GET},
843             produces = MediaType.APPLICATION_JSON_VALUE)
844     public void getMicroServiceHeaderDefaultsEntityData(HttpServletResponse response) {
845         DictionaryUtils utils = getDictionaryUtilsInstance();
846         utils.getData(response, microServiceHeaderDefaultDatas, MicroserviceHeaderdeFaults.class);
847     }
848
849     @RequestMapping(value = {"/ms_dictionary/save_headerDefaults"}, method = {RequestMethod.POST})
850     public ModelAndView saveMicroServiceHeaderDefaultValues(HttpServletRequest request, HttpServletResponse response)
851             throws IOException {
852         DictionaryUtils utils = getDictionaryUtilsInstance();
853         try {
854             boolean fromAPI = utils.isRequestFromAPI(request);
855             ObjectMapper mapper = new ObjectMapper();
856             mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
857             JsonNode root = mapper.readTree(request.getReader());
858
859             MicroserviceHeaderdeFaults msHeaderdeFaults;
860             if (fromAPI) {
861                 msHeaderdeFaults =
862                         mapper.readValue(root.get(dictionaryFields).toString(), MicroserviceHeaderdeFaults.class);
863             } else {
864                 msHeaderdeFaults = mapper.readValue(root.get("modelAttributeDictionaryData").toString(),
865                         MicroserviceHeaderdeFaults.class);
866             }
867
868             List<Object> duplicateData = commonClassDao.checkDuplicateEntry(msHeaderdeFaults.getModelName(),
869                     "modelName", MicroserviceHeaderdeFaults.class);
870             boolean duplicateflag = false;
871             if (duplicateData != null && !duplicateData.isEmpty()) {
872                 MicroserviceHeaderdeFaults data = (MicroserviceHeaderdeFaults) duplicateData.get(0);
873                 if (request.getParameter(operation) != null && "update".equals(request.getParameter(operation))) {
874                     msHeaderdeFaults.setId(data.getId());
875                 } else if ((request.getParameter(operation) != null
876                         && !"update".equals(request.getParameter(operation)))
877                         || (request.getParameter(operation) == null && (data.getId() != msHeaderdeFaults.getId()))) {
878                     duplicateflag = true;
879                 }
880             }
881
882             String responseString = null;
883             if (!duplicateflag) {
884                 if (msHeaderdeFaults.getId() == 0) {
885                     commonClassDao.save(msHeaderdeFaults);
886                 } else {
887                     commonClassDao.update(msHeaderdeFaults);
888                 }
889                 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroserviceHeaderdeFaults.class));
890             } else {
891                 responseString = duplicateResponseString;
892             }
893             if (fromAPI) {
894                 return utils.getResultForApi(responseString);
895             } else {
896                 utils.setResponseData(response, microServiceHeaderDefaultDatas, responseString);
897             }
898         } catch (Exception e) {
899             utils.setErrorResponseData(response, e);
900         }
901         return null;
902     }
903
904     @RequestMapping(value = {"/ms_dictionary/remove_headerDefaults"}, method = {RequestMethod.POST})
905     public void removeMicroServiceHeaderDefaults(HttpServletRequest request, HttpServletResponse response)
906             throws IOException {
907         DictionaryUtils utils = getDictionaryUtilsInstance();
908         utils.removeData(request, response, microServiceHeaderDefaultDatas, MicroserviceHeaderdeFaults.class);
909     }
910 }