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