db710961f696b508bbcf35ab235391e74a848fc2
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / controller / DescriptiveDictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.xacml.rest.controller;
22
23 import java.io.IOException;
24 import java.util.Date;
25 import java.util.List;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.onap.policy.pap.xacml.rest.adapters.GridData;
31 import org.onap.policy.pap.xacml.rest.util.DictionaryUtils;
32 import org.onap.policy.rest.dao.CommonClassDao;
33 import org.onap.policy.rest.jpa.DescriptiveScope;
34 import org.onap.policy.rest.jpa.UserInfo;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.MediaType;
37 import org.springframework.stereotype.Controller;
38 import org.springframework.web.bind.annotation.RequestMapping;
39 import org.springframework.web.bind.annotation.RequestMethod;
40 import org.springframework.web.servlet.ModelAndView;
41
42 import com.fasterxml.jackson.databind.DeserializationFeature;
43 import com.fasterxml.jackson.databind.JsonNode;
44 import com.fasterxml.jackson.databind.ObjectMapper;
45
46 @Controller
47 public class DescriptiveDictionaryController {
48         
49         private static CommonClassDao commonClassDao;
50         private static String operation = "operation";
51         private static String dScopeName = "descriptiveScopeName";
52         private static String descriptiveDatas = "descriptiveScopeDictionaryDatas";
53         
54         @Autowired
55         public DescriptiveDictionaryController(CommonClassDao commonClassDao){
56                 DescriptiveDictionaryController.commonClassDao = commonClassDao;
57         }
58         
59         public void setCommonClassDao(CommonClassDao commonClassDao){
60                 DescriptiveDictionaryController.commonClassDao = commonClassDao;
61         }
62         
63         public DescriptiveDictionaryController(){
64                 super();
65         }
66
67         private DictionaryUtils getDictionaryUtilsInstance(){
68                 return DictionaryUtils.getDictionaryUtils();
69         }
70         
71         @RequestMapping(value={"/get_DescriptiveScopeByName"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
72         public void getDescriptiveDictionaryByNameEntityData(HttpServletResponse response){
73                 DictionaryUtils utils = getDictionaryUtilsInstance();
74                 utils.getDataByEntity(response, descriptiveDatas, dScopeName, DescriptiveScope.class);
75         }
76         
77         @RequestMapping(value={"/get_DescriptiveScope"}, method={RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
78         public void getDescriptiveDictionaryEntityData(HttpServletResponse response){
79                 DictionaryUtils utils = getDictionaryUtilsInstance();
80                 utils.getData(response, descriptiveDatas, DescriptiveScope.class);
81         }
82         
83         @RequestMapping(value={"/descriptive_dictionary/save_descriptive"}, method={RequestMethod.POST})
84         public ModelAndView saveDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response)throws IOException{
85                 DictionaryUtils utils = getDictionaryUtilsInstance();
86                 try {
87                         boolean fromAPI = utils.isRequestFromAPI(request);
88                         ObjectMapper mapper = new ObjectMapper();
89                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
90                         JsonNode root = mapper.readTree(request.getReader());
91                         DescriptiveScope descriptiveScope;
92                         GridData data;
93                         String userId = null;
94                         if(fromAPI){
95                                 descriptiveScope = mapper.readValue(root.get("dictionaryFields").toString(), DescriptiveScope.class);
96                                 data = mapper.readValue(root.get("dictionaryFields").toString(), GridData.class);
97                                 userId = "API";
98                         }else{
99                                 descriptiveScope = mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), DescriptiveScope.class);
100                                 data = mapper.readValue(root.get("descriptiveScopeDictionaryData").toString(), GridData.class);
101                                 userId = root.get("userid").textValue();
102                         }
103                         descriptiveScope.setSearch(utils.appendKeyValue(data.getAttributes(), "AND", ":"));
104                         UserInfo userInfo = utils.getUserInfo(userId);
105                         List<Object> duplicateData =  commonClassDao.checkDuplicateEntry(descriptiveScope.getScopeName(), dScopeName, DescriptiveScope.class);
106                         boolean duplicateflag = false;
107                         if(!duplicateData.isEmpty()){
108                                 DescriptiveScope data1 = (DescriptiveScope) duplicateData.get(0);
109                                 if(request.getParameter(operation) != null && "update".equals(request.getParameter(operation))){
110                                         descriptiveScope.setId(data1.getId());
111                                 }else if((request.getParameter(operation) != null && !"update".equals(request.getParameter(operation))) || 
112                                                 (request.getParameter(operation) == null && (data1.getId() != descriptiveScope.getId()))){
113                                         duplicateflag = true;
114                                 }
115                         }
116                         String responseString = null;
117                         if(!duplicateflag){
118                                 descriptiveScope.setUserModifiedBy(userInfo);
119                                 if(descriptiveScope.getId() == 0){
120                                         descriptiveScope.setUserCreatedBy(userInfo);
121                                         commonClassDao.save(descriptiveScope);
122                                 }else{
123                                         descriptiveScope.setModifiedDate(new Date());
124                                         commonClassDao.update(descriptiveScope); 
125                                 } 
126                                 responseString = mapper.writeValueAsString(commonClassDao.getData(DescriptiveScope.class));
127                         }else{
128                                 responseString = "Duplicate";
129                         }
130                         if(fromAPI){
131                                 return utils.getResultForApi(responseString);
132                         }else{
133                                 utils.setResponseData(response, descriptiveDatas, responseString);
134                         }
135                 }catch (Exception e){
136                         utils.setErrorResponseData(response, e);
137                 }
138                 return null;
139         }
140
141         @RequestMapping(value={"/descriptive_dictionary/remove_descriptiveScope"}, method={RequestMethod.POST})
142         public void removeDescriptiveDictionary(HttpServletRequest request, HttpServletResponse response) throws IOException{
143                 DictionaryUtils utils = getDictionaryUtilsInstance();
144                 utils.removeData(request, response, descriptiveDatas, DescriptiveScope.class);
145         }
146 }