Convert tabs to space in ONAP PAP REST1
[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-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
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 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;
29 import java.util.Map;
30 import java.util.Set;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
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;
59
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;
64
65 @Controller
66 public class MicroServiceDictionaryController {
67     private static final Logger LOGGER  = FlexLogger.getLogger(MicroServiceDictionaryController.class);
68
69     private static CommonClassDao commonClassDao;
70
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";
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     public static void setCommonClassDao(CommonClassDao commonClassDao) {
108         MicroServiceDictionaryController.commonClassDao = commonClassDao;
109     }
110
111     MSModelUtils utils = new MSModelUtils(XACMLPapServlet.getMsOnapName(), XACMLPapServlet.getMsPolicyName());
112
113     private MicroServiceModels newModel;
114
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);
119     }
120
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);
125     }
126
127     @RequestMapping(value={"/ms_dictionary/save_dcaeUUID"}, method={RequestMethod.POST})
128     public ModelAndView saveDCAEUUIDDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
129         DictionaryUtils utils = getDictionaryUtilsInstance();
130         try {
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());
135             DCAEuuid dCAEuuid;
136             if(fromAPI){
137                 dCAEuuid = mapper.readValue(root.get(dictionaryFields).toString(), DCAEuuid.class);
138             }else{
139                 dCAEuuid = mapper.readValue(root.get("dcaeUUIDDictionaryData").toString(), DCAEuuid.class);
140             }
141             
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;
151                 }
152             }
153             String responseString = null;
154             if(!duplicateflag){
155                 if(dCAEuuid.getId() == 0){
156                     commonClassDao.save(dCAEuuid);
157                 }else{
158                     commonClassDao.update(dCAEuuid);
159                 }
160                 responseString = mapper.writeValueAsString(commonClassDao.getData(DCAEuuid.class));
161             }else{
162                 responseString = duplicateResponseString;
163             }
164             if(fromAPI){
165                 return utils.getResultForApi(responseString);
166             }else{
167                 utils.setResponseData(response, dcaeUUIDDatas, responseString);
168             }
169         }catch (Exception e){
170             utils.setErrorResponseData(response, e);
171         }
172         return null;
173     }
174
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);
179     }
180
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);
185     }
186
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);
191     }
192
193     @RequestMapping(value={"/ms_dictionary/save_configName"}, method={RequestMethod.POST})
194     public ModelAndView saveMicroServiceConfigNameDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
195         DictionaryUtils utils = getDictionaryUtilsInstance();
196         try {
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;
202             if(fromAPI){
203                 microServiceConfigName = mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceConfigName.class);
204             }else{
205                 microServiceConfigName = mapper.readValue(root.get("microServiceConfigNameDictionaryData").toString(), MicroServiceConfigName.class);
206             }
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;
216                 }
217             }
218             String responseString = null;
219             if(!duplicateflag){
220                 if(microServiceConfigName.getId() == 0){
221                     commonClassDao.save(microServiceConfigName);
222                 }else{
223                     commonClassDao.update(microServiceConfigName);
224                 }
225                 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceConfigName.class));
226             }else{
227                 responseString = duplicateResponseString;
228             }
229             if(fromAPI){
230                 return utils.getResultForApi(responseString);
231             }else{
232                 utils.setResponseData(response, microServiceConfigNameDatas, responseString);
233             }
234         }catch (Exception e){
235             utils.setErrorResponseData(response, e);
236         }
237         return null;
238     }
239
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);
244     }
245
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);
250     }
251
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);
256     }
257
258     @RequestMapping(value={"/ms_dictionary/save_location"}, method={RequestMethod.POST})
259     public ModelAndView saveMicroServiceLocationDictionary(HttpServletRequest request, HttpServletResponse response)throws IOException{
260         DictionaryUtils utils = getDictionaryUtilsInstance();
261         try {
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;
267             if(fromAPI){
268                 microServiceLocation = mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceLocation.class);
269             }else{
270                 microServiceLocation = mapper.readValue(root.get("microServiceLocationDictionaryData").toString(), MicroServiceLocation.class);
271             }
272             
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;
282                 }
283             }
284             String responseString = null;
285             if(!duplicateflag){
286                 if(microServiceLocation.getId() == 0){
287                     commonClassDao.save(microServiceLocation);
288                 }else{
289                     commonClassDao.update(microServiceLocation);
290                 }
291                 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceLocation.class));
292             }else{
293                 responseString = duplicateResponseString;
294             }
295             if(fromAPI){
296                 return utils.getResultForApi(responseString);
297             }else{
298                 utils.setResponseData(response, microServiceLocationDatas, responseString);
299             }
300         }catch (Exception e){
301             utils.setErrorResponseData(response, e);
302         }
303         return null;
304     }
305
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);
310     }
311
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);
316     }
317     
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);
322     }
323     
324     @RequestMapping(value={"/ms_dictionary/save_modelAttribute"}, method={RequestMethod.POST})
325     public ModelAndView saveMicroServiceAttributeDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
326         DictionaryUtils utils = getDictionaryUtilsInstance();
327         try {
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());
332             
333             MicroServiceAttribute microServiceAttribute;
334             String checkValue;
335             if (fromAPI) {
336                 microServiceAttribute = mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceAttribute.class);
337             } else {
338                 microServiceAttribute = mapper.readValue(root.get("modelAttributeDictionaryData").toString(), MicroServiceAttribute.class);
339             }
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;
350                 }
351             }
352             String responseString = null;
353             if(!duplicateflag){
354                 if(microServiceAttribute.getId() == 0){
355                     commonClassDao.save(microServiceAttribute);
356                 }else{
357                     commonClassDao.update(microServiceAttribute);
358                 }
359                 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroServiceAttribute.class));
360             }else{
361                 responseString = duplicateResponseString;
362             }
363             if(fromAPI){
364                 return utils.getResultForApi(responseString);
365             }else{
366                 utils.setResponseData(response, microServiceAttributeDatas, responseString);
367             }
368         }
369         catch (Exception e){
370             utils.setErrorResponseData(response, e);
371         }
372         return null;
373     }
374  
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);
379     }
380  
381
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);
386     }
387
388     @RequestMapping(value={"/get_MicroServiceModelsDataByVersion"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
389     public void getMicroServiceModelsDictionaryByVersionEntityData(HttpServletRequest request, HttpServletResponse response){
390         try{
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("\"", "");
397             }
398              if (modelName!=null){
399                     model.put(microServiceModelsDictionaryDatas, mapper.writeValueAsString(commonClassDao.getDataById(MicroServiceModels.class, modelName, modelName)));
400              } else{
401                  model.put(errorMsg, "No model name given");
402              }
403             JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
404             JSONObject j = new JSONObject(msg);
405             response.getWriter().write(j.toString());
406         }
407         catch (Exception e){
408             LOGGER.error(e);
409         }
410     }
411     
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);
416     }
417
418     @RequestMapping(value={"/get_MicroServiceModelsDataServiceVersion"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
419     public void getMicroServiceModelsDictionaryEntityDataServiceVersion(HttpServletResponse response){
420         try{
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());
429                 }
430             }
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());
437  
438         }
439         catch (Exception e){
440             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
441             response.addHeader("error", "dictionaryDBQuery");
442             LOGGER.error(e);
443         }
444     }
445     
446     @RequestMapping(value={"/get_MicroServiceModelsDataByClass"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
447     public void getMicroServiceModelsDictionaryClassEntityData(HttpServletResponse response){
448         try{
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());
457  
458         }
459         catch (Exception e){
460             response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
461             response.addHeader(errorMsg, dictionaryDBQuery);
462             LOGGER.error(e);
463         }
464     }
465     
466     @RequestMapping(value={"/ms_dictionary/save_model"}, method={RequestMethod.POST})
467     public ModelAndView saveMicroServiceModelsDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
468         DictionaryUtils utils = getDictionaryUtilsInstance();
469         try {
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;
477
478             String dataOrderInfo = null;
479             if(root.has("dataOrderInfo")){
480                 dataOrderInfo = root.get("dataOrderInfo").toString();
481             }
482
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("\"", ""));
490                         }
491                         if (root.get(microServiceModelsDictionaryData).has(modelName)){
492                             microServiceModels.setModelName(root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", ""));
493                             this.newModel.setModelName(microServiceModels.getModelName());
494                         }
495                         if (root.get(microServiceModelsDictionaryData).has(version)){
496                             microServiceModels.setVersion(root.get(microServiceModelsDictionaryData).get(version).asText().replace("\"", ""));
497                             this.newModel.setVersion(microServiceModels.getVersion());
498                         }
499                     }
500
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);
514                     }
515
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("}", ""));
531
532                 }else{
533                     if (fromAPI) {
534                         microServiceModels = mapper.readValue(root.get(dictionaryFields).toString(), MicroServiceModels.class);
535                         userId = "API";
536                     } else {
537                         if (root.has(microServiceModelsDictionaryData)){
538                             if (root.get(microServiceModelsDictionaryData).has(description)){
539                                 microServiceModels.setDescription(root.get(microServiceModelsDictionaryData).get(description).asText().replace("\"", ""));
540                             }
541                             if (root.get(microServiceModelsDictionaryData).has(modelName)){
542                                 microServiceModels.setModelName(root.get(microServiceModelsDictionaryData).get(modelName).asText().replace("\"", ""));
543                                 this.newModel.setModelName(microServiceModels.getModelName());
544                             }
545                             if (root.get(microServiceModelsDictionaryData).has(version)){
546                                 microServiceModels.setVersion(root.get(microServiceModelsDictionaryData).get(version).asText().replace("\"", ""));
547                                 this.newModel.setVersion(microServiceModels.getVersion());
548                             }
549                         }
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);
562                             }
563                         }
564                         userId = root.get("userid").textValue();
565                         addValuesToNewModel(classMap);
566                     }
567                 }
568             }
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);
579             }
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;
590                 }
591             }
592             UserInfo userInfo = utils.getUserInfo(userId);
593
594             String responseString = null;
595             if(!duplicateflag){
596                 microServiceModels.setUserCreatedBy(userInfo);
597                 if(microServiceModels.getId() == 0){
598                     commonClassDao.save(microServiceModels);
599                 }else{
600                     commonClassDao.update(microServiceModels);
601                 }
602                 responseString = mapper.writeValueAsString(commonClassDao.getData(PrefixList.class));
603             }else{
604                 responseString = duplicateResponseString;
605             }
606             if(fromAPI){
607                 return utils.getResultForApi(responseString);
608             }else{
609                 utils.setResponseData(response, microServiceModelsDictionaryDatas, responseString);
610             }
611         }catch (Exception e){
612             utils.setErrorResponseData(response, e);
613         }
614         return null;
615     }
616
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);
621     }
622
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;
626
627         MSAttributeObject mainClass = classMap.get(this.newModel.getModelName());
628
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);
635                 if (temp!=null){
636                     mainClass.addAllRefAttribute(temp.getRefAttribute());
637                     mainClass.addAllAttribute(temp.getAttribute());
638                 }
639             }
640             subAttribute = utils.createSubAttributes(dependency, classMap, this.newModel.getModelName());
641         }else{
642             subAttribute = "{}";
643             this.newModel.setDependency("");
644         }
645
646         if (mainClass != null && mainClass.getDependency()==null){
647             mainClass.setDependency("");
648         }
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("}", ""));
656         }
657     }
658
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)
672                     }
673                 }
674             }
675         }
676
677         return returnList;
678     }
679
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);
684     }
685
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);
690     }
691     
692     
693     @RequestMapping(value={"/ms_dictionary/save_headerDefaults"}, method={RequestMethod.POST})
694     public ModelAndView saveMicroServiceHeaderDefaultValues(HttpServletRequest request, HttpServletResponse response) throws IOException{
695         DictionaryUtils utils = getDictionaryUtilsInstance();
696         try {
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());
701             
702             MicroserviceHeaderdeFaults msHeaderdeFaults;
703             if(fromAPI){
704                 msHeaderdeFaults = mapper.readValue(root.get(dictionaryFields).toString(), MicroserviceHeaderdeFaults.class);
705             }else{
706                 msHeaderdeFaults = mapper.readValue(root.get("modelAttributeDictionaryData").toString(), MicroserviceHeaderdeFaults.class);
707             }
708             
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;
718                 }
719             }
720             
721             String responseString = null;
722             if(!duplicateflag){
723                 if(msHeaderdeFaults.getId() == 0){
724                     commonClassDao.save(msHeaderdeFaults);
725                 }else{
726                     commonClassDao.update(msHeaderdeFaults);
727                 }
728                 responseString = mapper.writeValueAsString(commonClassDao.getData(MicroserviceHeaderdeFaults.class));
729             }else{
730                 responseString = duplicateResponseString;
731             }
732             if(fromAPI){
733                 return utils.getResultForApi(responseString);
734             }else{
735                 utils.setResponseData(response, microServiceHeaderDefaultDatas, responseString);
736             }
737         }
738         catch (Exception e){
739             utils.setErrorResponseData(response, e);
740         }
741         return null;
742     }
743     
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);
748     }
749 }