Sonar fix forONAP-PAP-REST critical sonar issues
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / PolicyScopeDictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-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.onap.policy.pap.xacml.rest.controller;
22
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.io.UnsupportedEncodingException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
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.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.policy.pap.xacml.rest.util.JsonMessage;
39 import org.onap.policy.rest.dao.CommonClassDao;
40 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
41 import org.onap.policy.rest.jpa.PolicyScopeClosedLoop;
42 import org.onap.policy.rest.jpa.PolicyScopeResource;
43 import org.onap.policy.rest.jpa.PolicyScopeService;
44 import org.onap.policy.rest.jpa.PolicyScopeType;
45 import org.onap.policy.rest.jpa.UserInfo;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.MediaType;
48 import org.springframework.stereotype.Controller;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.servlet.ModelAndView;
51
52 import com.fasterxml.jackson.databind.DeserializationFeature;
53 import com.fasterxml.jackson.databind.JsonNode;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55
56 @Controller
57 public class PolicyScopeDictionaryController {
58
59         private static final Logger LOGGER  = FlexLogger.getLogger(PolicyScopeDictionaryController.class);
60
61         private static CommonClassDao commonClassDao;
62         
63         @Autowired
64         public PolicyScopeDictionaryController(CommonClassDao commonClassDao){
65                 PolicyScopeDictionaryController.commonClassDao = commonClassDao;
66         }
67         
68         public PolicyScopeDictionaryController(){}      
69
70         private static String SUCCESSMAPKEY = "successMapKey";
71
72         public UserInfo getUserInfo(String loginId){
73                 UserInfo name = (UserInfo) commonClassDao.getEntityItem(UserInfo.class, "userLoginId", loginId);
74                 return name;    
75         }
76
77         @RequestMapping(value={"/get_GroupPolicyScopeDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
78         public void getGroupPolicyScopeEntityDataByName(HttpServletRequest request, HttpServletResponse response){
79                 try{
80                         Map<String, Object> model = new HashMap<>();
81                         ObjectMapper mapper = new ObjectMapper();
82                         model.put("groupPolicyScopeListDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(GroupPolicyScopeList.class, "name")));
83                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
84                         JSONObject j = new JSONObject(msg);
85                         response.getWriter().write(j.toString());
86                 }
87                 catch (Exception e){
88                         LOGGER.error("Exception Occured"+e);
89                 }
90         }
91
92         @RequestMapping(value={"/get_GroupPolicyScopeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
93         public void getGroupPolicyScopeEntityData(HttpServletRequest request, HttpServletResponse response){
94                 try{
95                         Map<String, Object> model = new HashMap<>();
96                         ObjectMapper mapper = new ObjectMapper();
97                         model.put("groupPolicyScopeListDatas", mapper.writeValueAsString(commonClassDao.getData(GroupPolicyScopeList.class)));
98                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
99                         JSONObject j = new JSONObject(msg);
100                         response.addHeader(SUCCESSMAPKEY, "success"); 
101                         response.addHeader("operation", "getDictionary");
102                         response.getWriter().write(j.toString());
103                 }
104                 catch (Exception e){
105                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
106                         response.addHeader("error", "dictionaryDBQuery");
107                         LOGGER.error(e);
108                 }
109         }
110
111         @RequestMapping(value={"/ps_dictionary/save_psGroupPolicyScope"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
112         public ModelAndView savePSGroupScopeDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
113                 try {
114                         boolean duplicateflag = false;
115                         boolean isFakeUpdate = false;
116                         boolean duplicateGroupFlag = false;
117                         boolean fromAPI = false;
118                         if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
119                                 fromAPI = true;
120                         }
121
122                         ObjectMapper mapper = new ObjectMapper();
123                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
124                         JsonNode root = mapper.readTree(request.getReader());
125                         //GroupPolicyScopeList gpdata = (GroupPolicyScopeList)mapper.readValue(root.get("groupPolicyScopeListData").toString(), GroupPolicyScopeList.class);
126                         GroupPolicyScopeList gpdata = null;
127                         GroupPolicyScope groupData = null;
128                         if (fromAPI) {
129                                 gpdata = (GroupPolicyScopeList)mapper.readValue(root.get("dictionaryFields").toString(), GroupPolicyScopeList.class);
130                                 try{
131                                         groupData = (GroupPolicyScope)mapper.readValue(root.get("groupPolicyScopeListData1").toString(), GroupPolicyScope.class);
132                                 }catch(Exception e){
133                                         groupData = new GroupPolicyScope();
134                                         groupData.setResource(root.get("dictionaryFields").get("resource").toString().replace("\"", ""));
135                                         groupData.setClosedloop(root.get("dictionaryFields").get("closedloop").toString().replace("\"", ""));
136                                         groupData.setService(root.get("dictionaryFields").get("service").toString().replace("\"", ""));
137                                         groupData.setType(root.get("dictionaryFields").get("type").toString().replace("\"", ""));
138                                         LOGGER.error(e);
139                                 }
140
141                                 if(!gpdata.getGroupName().startsWith("PolicyScope")){
142                                         String name = "PolicyScope_" + gpdata.getGroupName();
143                                         gpdata.setGroupName(name);
144                                 }
145
146                                 //check if update operation or create, get id for data to be updated and update attributeData
147                                 if (request.getParameter("operation").equals("update")) {
148
149                                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(gpdata.getGroupName(), "name", GroupPolicyScopeList.class);
150                                         int id = 0;
151                                         for (int i =0; i< duplicateData.size(); i++){
152                                                 GroupPolicyScopeList data = (GroupPolicyScopeList) duplicateData.get(0);
153                                                 id = data.getId();
154                                         }                   
155
156                                         if(id==0){
157                                                 isFakeUpdate=true;
158                                                 gpdata.setId(1);
159                                         } else {
160                                                 gpdata.setId(id);
161                                         }
162
163                                 }
164                         } else {
165                                 gpdata = (GroupPolicyScopeList)mapper.readValue(root.get("groupPolicyScopeListData").toString(), GroupPolicyScopeList.class);
166
167                                 try{
168                                         groupData = (GroupPolicyScope)mapper.readValue(root.get("groupPolicyScopeListData1").toString(), GroupPolicyScope.class);
169                                 }catch(Exception e){
170                                         LOGGER.error(e);
171                                         groupData = new GroupPolicyScope();
172                                         groupData.setResource(root.get("groupPolicyScopeListData1").get("resource").toString().replace("\"", ""));
173                                         groupData.setClosedloop(root.get("groupPolicyScopeListData1").get("closedloop").toString().replace("\"", ""));
174                                         groupData.setService(root.get("groupPolicyScopeListData1").get("service").toString().replace("\"", ""));
175                                         groupData.setType(root.get("groupPolicyScopeListData1").get("type").toString().replace("\"", ""));
176                                 }
177                                 if(!gpdata.getGroupName().startsWith("PolicyScope")){
178                                         String name = "PolicyScope_" + gpdata.getGroupName();
179                                         gpdata.setGroupName(name);
180                                 }
181
182                         }
183                         ArrayList<String> valueList = new ArrayList<>();
184                         String list = null;
185                         String resourceValue = groupData.getResource();
186                         String typeValue = groupData.getType();
187                         String serviceValue = groupData.getService();
188                         String closedLoopValue = groupData.getClosedloop();
189                         valueList.add("resource=" + resourceValue);
190                         valueList.add("service=" + serviceValue);
191                         valueList.add("type=" + typeValue);
192                         valueList.add("closedLoopControlName="  + closedLoopValue);
193                         list = StringUtils.replaceEach(valueList.toString(), new String[]{"[", "]", " "}, new String[]{"", "", ""});
194                         gpdata.setGroupList(list);
195                         if(!gpdata.getGroupName().startsWith("PolicyScope")){
196                                 String name = "PolicyScope_" + gpdata.getGroupName();
197                                 gpdata.setGroupName(name);
198                         }
199                         if(gpdata.getId() == 0){
200                                 List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(gpdata.getGroupName(), "name", GroupPolicyScopeList.class);
201                                 if(!duplicateData.isEmpty()){
202                                         duplicateflag = true;
203                                 }else{
204                                         duplicateData =  commonClassDao.checkDuplicateEntry(gpdata.getGroupList(), "groupList", GroupPolicyScopeList.class);
205                                         if(!duplicateData.isEmpty()){
206                                                 duplicateGroupFlag = true;
207                                         }else{
208                                                 commonClassDao.save(gpdata);
209                                         }
210                                 }
211                         }else{
212                                 if(!isFakeUpdate) {
213                                         List<Object> duplicateGroupList =  commonClassDao.checkExistingGroupListforUpdate(gpdata.getGroupList(), gpdata.getGroupName());
214                                         if(!duplicateGroupList.isEmpty()) {
215                                                 duplicateGroupFlag = true;
216                                         } else {
217                                                 commonClassDao.update(gpdata); 
218                                         }
219                                 } 
220                         }
221                         String responseString = "";
222                         if(duplicateflag){
223                                 responseString = "Duplicate";
224                         }else if(duplicateGroupFlag){
225                                 responseString = "DuplicateGroup";
226                         }else{
227                                 responseString = mapper.writeValueAsString(commonClassDao.getData(GroupPolicyScopeList.class));
228                         }
229
230                         if (fromAPI) {
231                                 if (responseString!=null && !responseString.equals("Duplicate") && !responseString.equals("DuplicateGroup")) {
232                                         if(isFakeUpdate){
233                                                 responseString = "Exists";
234                                         } else {
235                                                 responseString = "Success";
236                                         }
237                                 }
238
239                                 ModelAndView result = new ModelAndView();
240                                 result.setViewName(responseString);
241                                 return result;
242                         } else {
243
244                                 response.setCharacterEncoding("UTF-8");
245                                 response.setContentType("application / json");
246                                 request.setCharacterEncoding("UTF-8");
247
248                                 PrintWriter out = response.getWriter();
249                                 JSONObject j = new JSONObject("{groupPolicyScopeListDatas: " + responseString + "}");
250                                 out.write(j.toString());
251                                 return null;
252                         }
253                 }catch (Exception e){
254                         LOGGER.error(e);
255                         response.setCharacterEncoding("UTF-8");
256                         request.setCharacterEncoding("UTF-8");
257                         PrintWriter out = response.getWriter();
258                         out.write(e.getMessage());
259                         
260                 }
261                 return null;
262         }
263
264         @RequestMapping(value={"/ps_dictionary/remove_GroupPolicyScope"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
265         public ModelAndView removePSGroupScopeDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
266                 try{
267                         ObjectMapper mapper = new ObjectMapper();
268                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
269                         JsonNode root = mapper.readTree(request.getReader());
270                         GroupPolicyScopeList onapData = (GroupPolicyScopeList)mapper.readValue(root.get("data").toString(), GroupPolicyScopeList.class);
271                         commonClassDao.delete(onapData);
272                         response.setCharacterEncoding("UTF-8");
273                         response.setContentType("application / json");
274                         request.setCharacterEncoding("UTF-8");
275
276                         PrintWriter out = response.getWriter();
277
278                         String responseString = mapper.writeValueAsString(commonClassDao.getData(GroupPolicyScopeList.class));
279                         JSONObject j = new JSONObject("{groupPolicyScopeListDatas: " + responseString + "}");
280                         out.write(j.toString());
281
282                         return null;
283                 }
284                 catch (Exception e){
285                         System.out.println(e);
286                         response.setCharacterEncoding("UTF-8");
287                         request.setCharacterEncoding("UTF-8");
288                         PrintWriter out = response.getWriter();
289                         out.write(e.getMessage());
290                 }
291                 return null;
292         }
293
294         @RequestMapping(value={"/get_PSClosedLoopDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
295         public void getPSClosedLoopEntityDataByName(HttpServletRequest request, HttpServletResponse response){
296                 try{
297                         Map<String, Object> model = new HashMap<>();
298                         ObjectMapper mapper = new ObjectMapper();
299                         model.put("psClosedLoopDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(PolicyScopeClosedLoop.class, "name")));
300                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
301                         JSONObject j = new JSONObject(msg);
302                         response.getWriter().write(j.toString());
303                 }
304                 catch (Exception e){
305                         LOGGER.error("Exception Occured"+e);
306                 }
307         }
308
309         @RequestMapping(value={"/get_PSClosedLoopData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
310         public void getPSClosedLoopEntityData(HttpServletRequest request, HttpServletResponse response){
311                 try{
312                         Map<String, Object> model = new HashMap<>();
313                         ObjectMapper mapper = new ObjectMapper();
314                         model.put("psClosedLoopDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(PolicyScopeClosedLoop.class)));
315                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
316                         JSONObject j = new JSONObject(msg);
317                         response.addHeader(SUCCESSMAPKEY, "success"); 
318                         response.addHeader("operation", "getDictionary");
319                         response.getWriter().write(j.toString());
320                 }
321                 catch (Exception e){
322                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
323                         response.addHeader("error", "dictionaryDBQuery");
324                         LOGGER.error(e);
325                 }
326         }
327
328         @RequestMapping(value={"/ps_dictionary/save_psClosedLoop"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
329         public ModelAndView savePSClosedLoopDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
330                 try {
331                         boolean duplicateflag = false;
332                         boolean isFakeUpdate = false;
333                         boolean fromAPI = false;
334                         if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
335                                 fromAPI = true;
336                         }
337                         ObjectMapper mapper = new ObjectMapper();
338                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
339                         JsonNode root = mapper.readTree(request.getReader());
340                         PolicyScopeClosedLoop onapData;
341                         if (fromAPI) {
342                                 onapData = (PolicyScopeClosedLoop)mapper.readValue(root.get("dictionaryFields").toString(), PolicyScopeClosedLoop.class);
343
344                                 //check if update operation or create, get id for data to be updated and update attributeData
345                                 if (request.getParameter("operation").equals("update")) {
346                                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeClosedLoop.class);
347                                         int id = 0;
348                                         for (int i =0; i< duplicateData.size(); i++){
349                                                 PolicyScopeClosedLoop data = (PolicyScopeClosedLoop) duplicateData.get(0);
350                                                 id = data.getId();
351                                         }
352
353                                         if(id==0){
354                                                 isFakeUpdate=true;
355                                                 onapData.setId(1);
356                                         } else {
357                                                 onapData.setId(id);
358                                         }   
359
360                                 }
361                         } else {
362                                 onapData = (PolicyScopeClosedLoop)mapper.readValue(root.get("psClosedLoopDictionaryData").toString(), PolicyScopeClosedLoop.class);
363                         }
364                         if(onapData.getId() == 0){
365                                 List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeClosedLoop.class);
366                                 if(!duplicateData.isEmpty()){
367                                         duplicateflag = true;
368                                 }else{
369                                         commonClassDao.save(onapData);
370                                 }
371                         }else{
372                                 if(!isFakeUpdate) {
373                                         commonClassDao.update(onapData); 
374                                 }
375                         } 
376                         String responseString = "";
377                         if(duplicateflag){
378                                 responseString = "Duplicate";
379                         }else{
380                                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeClosedLoop.class));
381                         }
382
383                         if (fromAPI) {
384                                 if (responseString!=null && !responseString.equals("Duplicate")) {
385                                         if(isFakeUpdate){
386                                                 responseString = "Exists";
387                                         } else {
388                                                 responseString = "Success";
389                                         }
390                                 }
391                                 ModelAndView result = new ModelAndView();
392                                 result.setViewName(responseString);
393                                 return result;
394                         } else {
395                                 response.setCharacterEncoding("UTF-8");
396                                 response.setContentType("application / json");
397                                 request.setCharacterEncoding("UTF-8");
398
399                                 PrintWriter out = response.getWriter();
400                                 JSONObject j = new JSONObject("{psClosedLoopDictionaryDatas: " + responseString + "}");
401
402                                 out.write(j.toString());
403
404                                 return null;
405                         }
406
407                 }catch (Exception e){
408                         response.setCharacterEncoding("UTF-8");
409                         request.setCharacterEncoding("UTF-8");
410                         PrintWriter out = response.getWriter();
411                         out.write(e.getMessage());
412                         LOGGER.error(e);
413                 }
414                 return null;
415         }
416
417         @RequestMapping(value={"/ps_dictionary/remove_PSClosedLoop"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
418         public ModelAndView removePSClosedLoopDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
419                 try{
420                         ObjectMapper mapper = new ObjectMapper();
421                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
422                         JsonNode root = mapper.readTree(request.getReader());
423                         PolicyScopeClosedLoop onapData = (PolicyScopeClosedLoop)mapper.readValue(root.get("data").toString(), PolicyScopeClosedLoop.class);
424                         commonClassDao.delete(onapData);
425                         response.setCharacterEncoding("UTF-8");
426                         response.setContentType("application / json");
427                         request.setCharacterEncoding("UTF-8");
428
429                         PrintWriter out = response.getWriter();
430
431                         String responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeClosedLoop.class));
432                         JSONObject j = new JSONObject("{psClosedLoopDictionaryDatas: " + responseString + "}");
433                         out.write(j.toString());
434
435                         return null;
436                 }
437                 catch (Exception e){
438                         System.out.println(e);
439                         response.setCharacterEncoding("UTF-8");
440                         request.setCharacterEncoding("UTF-8");
441                         PrintWriter out = response.getWriter();
442                         out.write(e.getMessage());
443                 }
444                 return null;
445         }
446
447         @RequestMapping(value={"/get_PSServiceDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
448         public void getPSServiceEntityDataByName(HttpServletRequest request, HttpServletResponse response){
449                 try{
450                         Map<String, Object> model = new HashMap<>();
451                         ObjectMapper mapper = new ObjectMapper();
452                         model.put("psServiceDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(PolicyScopeService.class, "name")));
453                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
454                         JSONObject j = new JSONObject(msg);
455                         response.getWriter().write(j.toString());
456                 }
457                 catch (Exception e){
458                         LOGGER.error("Exception Occured"+e);
459                 }
460         }
461
462         @RequestMapping(value={"/get_PSServiceData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
463         public void getPSServiceEntityData(HttpServletRequest request, HttpServletResponse response){
464                 try{
465                         Map<String, Object> model = new HashMap<>();
466                         ObjectMapper mapper = new ObjectMapper();
467                         model.put("psServiceDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(PolicyScopeService.class)));
468                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
469                         JSONObject j = new JSONObject(msg);
470                         response.addHeader(SUCCESSMAPKEY, "success"); 
471                         response.addHeader("operation", "getDictionary");
472                         response.getWriter().write(j.toString());
473                 }
474                 catch (Exception e){
475                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
476                         response.addHeader("error", "dictionaryDBQuery");
477                         LOGGER.error(e);
478                 }
479         }
480
481         @RequestMapping(value={"/ps_dictionary/save_psService"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
482         public ModelAndView savePSServiceDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
483                 try {
484                         boolean duplicateflag = false;
485                         boolean isFakeUpdate = false;
486                         boolean fromAPI = false;
487                         if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
488                                 fromAPI = true;
489                         }
490                         ObjectMapper mapper = new ObjectMapper();
491                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
492                         JsonNode root = mapper.readTree(request.getReader());
493                         PolicyScopeService onapData;
494                         if (fromAPI) {
495                                 onapData = (PolicyScopeService)mapper.readValue(root.get("dictionaryFields").toString(), PolicyScopeService.class);
496
497                                 //check if update operation or create, get id for data to be updated and update attributeData
498                                 if (request.getParameter("operation").equals("update")) {
499                                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeService.class);
500                                         int id = 0;
501                                         for (int i =0; i< duplicateData.size(); i++){
502                                                 PolicyScopeService data = (PolicyScopeService) duplicateData.get(0);
503                                                 id = data.getId();
504                                         }
505                                         if(id==0){
506                                                 isFakeUpdate=true;
507                                                 onapData.setId(1);
508                                         } else {
509                                                 onapData.setId(id);
510                                         }
511                                 }
512                         } else {
513                                 onapData = (PolicyScopeService)mapper.readValue(root.get("psServiceDictionaryData").toString(), PolicyScopeService.class);
514                         }
515                         if(onapData.getId() == 0){
516                                 List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeService.class);
517                                 if(!duplicateData.isEmpty()){
518                                         duplicateflag = true;
519                                 }else{
520                                         commonClassDao.save(onapData);
521                                 }
522                         }else{
523                                 if(!isFakeUpdate) {
524                                         commonClassDao.update(onapData); 
525                                 }
526                         } 
527
528                         String responseString = "";
529                         if(duplicateflag){
530                                 responseString = "Duplicate";
531                         }else{
532                                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeService.class));
533                         }   
534
535                         if (fromAPI) {
536                                 if (responseString!=null && !responseString.equals("Duplicate")) {
537                                         if(isFakeUpdate){
538                                                 responseString = "Exists";
539                                         } else {
540                                                 responseString = "Success";
541                                         }
542                                 }
543                                 ModelAndView result = new ModelAndView();
544                                 result.setViewName(responseString);
545                                 return result;
546
547                         } else {
548                                 response.setCharacterEncoding("UTF-8");
549                                 response.setContentType("application / json");
550                                 request.setCharacterEncoding("UTF-8");
551
552                                 PrintWriter out = response.getWriter();
553                                 JSONObject j = new JSONObject("{psServiceDictionaryDatas: " + responseString + "}");
554                                 out.write(j.toString());
555                                 return null;
556                         }
557                 }catch (Exception e){
558                         response.setCharacterEncoding("UTF-8");
559                         request.setCharacterEncoding("UTF-8");
560                         PrintWriter out = response.getWriter();
561                         out.write(e.getMessage());
562                         LOGGER.error(e);
563                 }
564                 return null;
565         }
566
567         @RequestMapping(value={"/ps_dictionary/remove_PSService"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
568         public ModelAndView removePSServiceDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
569                 try{
570                         ObjectMapper mapper = new ObjectMapper();
571                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
572                         JsonNode root = mapper.readTree(request.getReader());
573                         PolicyScopeService onapData = (PolicyScopeService)mapper.readValue(root.get("data").toString(), PolicyScopeService.class);
574                         commonClassDao.delete(onapData);
575                         response.setCharacterEncoding("UTF-8");
576                         response.setContentType("application / json");
577                         request.setCharacterEncoding("UTF-8");
578
579                         PrintWriter out = response.getWriter();
580
581                         String responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeService.class));
582                         JSONObject j = new JSONObject("{psServiceDictionaryDatas: " + responseString + "}");
583                         out.write(j.toString());
584
585                         return null;
586                 }
587                 catch (Exception e){
588                         System.out.println(e);
589                         response.setCharacterEncoding("UTF-8");
590                         request.setCharacterEncoding("UTF-8");
591                         PrintWriter out = response.getWriter();
592                         out.write(e.getMessage());
593                 }
594                 return null;
595         }
596
597         @RequestMapping(value={"/get_PSTypeDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
598         public void getPSTypeEntityDataByName(HttpServletRequest request, HttpServletResponse response){
599                 try{
600                         Map<String, Object> model = new HashMap<>();
601                         ObjectMapper mapper = new ObjectMapper();
602                         model.put("psTypeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(PolicyScopeType.class, "name")));
603                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
604                         JSONObject j = new JSONObject(msg);
605                         response.getWriter().write(j.toString());
606                 }
607                 catch (Exception e){
608                         LOGGER.error("Exception Occured"+e);
609                 }
610         }
611
612         @RequestMapping(value={"/get_PSTypeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
613         public void getPSTypeEntityData(HttpServletRequest request, HttpServletResponse response){
614                 try{
615                         Map<String, Object> model = new HashMap<>();
616                         ObjectMapper mapper = new ObjectMapper();
617                         model.put("psTypeDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(PolicyScopeType.class)));
618                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
619                         JSONObject j = new JSONObject(msg);
620                         response.addHeader(SUCCESSMAPKEY, "success"); 
621                         response.addHeader("operation", "getDictionary");
622                         response.getWriter().write(j.toString());
623                 }
624                 catch (Exception e){
625                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
626                         response.addHeader("error", "dictionaryDBQuery");
627                         LOGGER.error(e);
628                 }
629         }
630
631         @RequestMapping(value={"/ps_dictionary/save_psType"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
632         public ModelAndView savePSTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
633                 try {
634                         boolean duplicateflag = false;
635                         boolean isFakeUpdate = false;
636                         boolean fromAPI = false;
637                         if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
638                                 fromAPI = true;
639                         }
640                         ObjectMapper mapper = new ObjectMapper();
641                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
642                         JsonNode root = mapper.readTree(request.getReader());
643                         PolicyScopeType onapData;
644                         if (fromAPI) {
645                                 onapData = (PolicyScopeType)mapper.readValue(root.get("dictionaryFields").toString(), PolicyScopeType.class);
646
647                                 //check if update operation or create, get id for data to be updated and update attributeData
648                                 if (request.getParameter("operation").equals("update")) {
649                                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeType.class);
650                                         int id = 0;
651                                         for (int i =0; i< duplicateData.size(); i++){
652                                                 PolicyScopeType data = (PolicyScopeType) duplicateData.get(0);
653                                                 id = data.getId();
654                                         }
655
656                                         if(id==0){
657                                                 isFakeUpdate=true;
658                                                 onapData.setId(1);
659                                         } else {
660                                                 onapData.setId(id);
661                                         }   
662
663                                 }
664                         } else {
665                                 onapData = (PolicyScopeType)mapper.readValue(root.get("psTypeDictionaryData").toString(), PolicyScopeType.class);
666                         }
667                         if(onapData.getId() == 0){
668                                 List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeType.class);
669                                 if(!duplicateData.isEmpty()){
670                                         duplicateflag = true;
671                                 }else{
672                                         commonClassDao.save(onapData);
673                                 }
674                         }else{
675                                 if(!isFakeUpdate) {
676                                         commonClassDao.update(onapData); 
677                                 }
678                         } 
679                         String responseString = "";
680                         if(duplicateflag){
681                                 responseString = "Duplicate";
682                         }else{
683                                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeType.class));
684                         } 
685
686                         if (fromAPI) {
687                                 if (responseString!=null && !responseString.equals("Duplicate")) {
688                                         if(isFakeUpdate){
689                                                 responseString = "Exists";
690                                         } else {
691                                                 responseString = "Success";
692                                         }
693
694                                 }
695                                 ModelAndView result = new ModelAndView();
696                                 result.setViewName(responseString);
697                                 return result;
698
699                         } else {
700
701                                 response.setCharacterEncoding("UTF-8");
702                                 response.setContentType("application / json");
703                                 request.setCharacterEncoding("UTF-8");
704
705                                 PrintWriter out = response.getWriter();
706                                 JSONObject j = new JSONObject("{psTypeDictionaryDatas: " + responseString + "}");
707
708                                 out.write(j.toString());
709
710                                 return null;
711                         }
712
713                 }catch (Exception e){
714                         response.setCharacterEncoding("UTF-8");
715                         request.setCharacterEncoding("UTF-8");
716                         PrintWriter out = response.getWriter();
717                         out.write(e.getMessage());
718                         LOGGER.error(e);
719                 }
720                 return null;
721         }
722
723         @RequestMapping(value={"/ps_dictionary/remove_PSType"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
724         public ModelAndView removePSTypeDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
725                 try{
726                         ObjectMapper mapper = new ObjectMapper();
727                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
728                         JsonNode root = mapper.readTree(request.getReader());
729                         PolicyScopeType onapData = (PolicyScopeType)mapper.readValue(root.get("data").toString(), PolicyScopeType.class);
730                         commonClassDao.delete(onapData);
731                         response.setCharacterEncoding("UTF-8");
732                         response.setContentType("application / json");
733                         request.setCharacterEncoding("UTF-8");
734
735                         PrintWriter out = response.getWriter();
736
737                         String responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeType.class));
738                         JSONObject j = new JSONObject("{psTypeDictionaryDatas: " + responseString + "}");
739                         out.write(j.toString());
740
741                         return null;
742                 }
743                 catch (Exception e){
744                         System.out.println(e);
745                         response.setCharacterEncoding("UTF-8");
746                         request.setCharacterEncoding("UTF-8");
747                         PrintWriter out = response.getWriter();
748                         out.write(e.getMessage());
749                 }
750                 return null;
751         }
752
753         @RequestMapping(value={"/get_PSResourceDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
754         public void getPSResourceEntityDataByName(HttpServletRequest request, HttpServletResponse response){
755                 try{
756                         Map<String, Object> model = new HashMap<>();
757                         ObjectMapper mapper = new ObjectMapper();
758                         model.put("psResourceDictionaryDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(PolicyScopeResource.class, "name")));
759                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
760                         JSONObject j = new JSONObject(msg);
761                         response.getWriter().write(j.toString());
762                 }
763                 catch (Exception e){
764                         LOGGER.error("Exception Occured"+e);
765                 }
766         }
767
768         @RequestMapping(value={"/get_PSResourceData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
769         public void getPSResourceEntityData(HttpServletRequest request, HttpServletResponse response){
770                 try{
771                         Map<String, Object> model = new HashMap<>();
772                         ObjectMapper mapper = new ObjectMapper();
773                         model.put("psResourceDictionaryDatas", mapper.writeValueAsString(commonClassDao.getData(PolicyScopeResource.class)));
774                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
775                         JSONObject j = new JSONObject(msg);
776                         response.addHeader("successMapKey", "success"); 
777                         response.addHeader("operation", "getDictionary");
778                         response.getWriter().write(j.toString());
779                 }
780                 catch (Exception e){
781                         LOGGER.error(e);
782                         response.setStatus(HttpServletResponse.SC_BAD_REQUEST);                             
783                         response.addHeader("error", "dictionaryDBQuery");
784                         
785                 }
786         }
787
788         @RequestMapping(value={"/ps_dictionary/save_psResource"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
789         public ModelAndView savePSResourceDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException{
790                 try {
791                         boolean duplicateflag = false;
792                         boolean isFakeUpdate = false;
793                         boolean fromAPI = false;
794                         if (request.getParameter("apiflag")!=null && request.getParameter("apiflag").equalsIgnoreCase("api")) {
795                                 fromAPI = true;
796                         }
797                         ObjectMapper mapper = new ObjectMapper();
798                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
799                         JsonNode root = mapper.readTree(request.getReader());
800                         PolicyScopeResource onapData;
801                         if (fromAPI) {
802                                 onapData = (PolicyScopeResource)mapper.readValue(root.get("dictionaryFields").toString(), PolicyScopeResource.class);
803
804                                 //check if update operation or create, get id for data to be updated and update attributeData
805                                 if (request.getParameter("operation").equals("update")) {
806                                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeResource.class);
807                                         int id = 0;
808                                         PolicyScopeResource data = (PolicyScopeResource) duplicateData.get(0);
809                                         id = data.getId();
810
811                                         if(id==0){
812                                                 isFakeUpdate=true;
813                                                 onapData.setId(1);
814                                         } else {
815                                                 onapData.setId(id);
816                                         }
817                                 }
818                         } else {
819                                 onapData = (PolicyScopeResource)mapper.readValue(root.get("psResourceDictionaryData").toString(), PolicyScopeResource.class);
820                         }
821                         if(onapData.getId() == 0){
822                                 List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(onapData.getName(), "name", PolicyScopeResource.class);
823                                 if(!duplicateData.isEmpty()){
824                                         duplicateflag = true;
825                                 }else{
826                                         commonClassDao.save(onapData);
827                                 }
828                         }else{
829                                 if(!isFakeUpdate) {
830                                         commonClassDao.update(onapData); 
831                                 }
832                         } 
833                         String responseString = "";
834                         if(duplicateflag){
835                                 responseString = "Duplicate";
836                         }else{
837                                 responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeResource.class));
838                         }   
839
840                         if (fromAPI) {
841                                 if (responseString!=null && !responseString.equals("Duplicate")) {
842                                         if(isFakeUpdate){
843                                                 responseString = "Exists";
844                                         } else {
845                                                 responseString = "Success";
846                                         }
847
848                                 }
849                                 ModelAndView result = new ModelAndView();
850                                 result.setViewName(responseString);
851                                 return result;
852
853                         } else {
854
855                                 response.setCharacterEncoding("UTF-8");
856                                 response.setContentType("application / json");
857                                 request.setCharacterEncoding("UTF-8");
858
859                                 PrintWriter out = response.getWriter();
860                                 JSONObject j = new JSONObject("{psResourceDictionaryDatas: " + responseString + "}");
861
862                                 out.write(j.toString());
863
864                                 return null;
865                         }
866
867                 }catch (Exception e){
868                         LOGGER.error(e);
869                         response.setCharacterEncoding("UTF-8");
870                         request.setCharacterEncoding("UTF-8");
871                         PrintWriter out = response.getWriter();
872                         out.write(e.getMessage());
873                         
874                 }
875                 return null;
876         }
877
878         @RequestMapping(value={"/ps_dictionary/remove_PSResource"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
879         public ModelAndView removePSResourceDictionary(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException {
880                 try{
881                         ObjectMapper mapper = new ObjectMapper();
882                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
883                         JsonNode root = mapper.readTree(request.getReader());
884                         PolicyScopeResource onapData = (PolicyScopeResource)mapper.readValue(root.get("data").toString(), PolicyScopeResource.class);
885                         commonClassDao.delete(onapData);
886                         response.setCharacterEncoding("UTF-8");
887                         response.setContentType("application / json");
888                         request.setCharacterEncoding("UTF-8");
889
890                         PrintWriter out = response.getWriter();
891
892                         String responseString = mapper.writeValueAsString(commonClassDao.getData(PolicyScopeResource.class));
893                         JSONObject j = new JSONObject("{psResourceDictionaryDatas: " + responseString + "}");
894                         out.write(j.toString());
895
896                         return null;
897                 }
898                 catch (Exception e){
899                         System.out.println(e);
900                         response.setCharacterEncoding("UTF-8");
901                         request.setCharacterEncoding("UTF-8");
902                         PrintWriter out = response.getWriter();
903                         out.write(e.getMessage());
904                 }
905                 return null;
906         }
907 }
908
909 class GroupPolicyScope{
910         String resource;
911         String type;
912         String service;
913         String closedloop;
914         public String getResource() {
915                 return resource;
916         }
917         public void setResource(String resource) {
918                 this.resource = resource;
919         }
920         public String getType() {
921                 return type;
922         }
923         public void setType(String type) {
924                 this.type = type;
925         }
926         public String getService() {
927                 return service;
928         }
929         public void setService(String service) {
930                 this.service = service;
931         }
932         public String getClosedloop() {
933                 return closedloop;
934         }
935         public void setClosedloop(String closedloop) {
936                 this.closedloop = closedloop;
937         }
938
939 }