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