Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / controller / DecisionPolicyDictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.policy.pap.xacml.rest.controller;
22
23 import java.io.PrintWriter;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 import org.json.JSONObject;
32 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
33 import org.openecomp.policy.rest.dao.DecisionPolicyDao;
34 import org.openecomp.policy.rest.dao.UserInfoDao;
35 import org.openecomp.policy.rest.jpa.Datatype;
36 import org.openecomp.policy.rest.jpa.DecisionSettings;
37 import org.openecomp.policy.rest.jpa.UserInfo;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.MediaType;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.servlet.ModelAndView;
43
44 import com.fasterxml.jackson.databind.DeserializationFeature;
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47
48 @Controller
49 public class DecisionPolicyDictionaryController {
50
51         @Autowired
52         DecisionPolicyDao decisionPolicyDao;
53         
54         @Autowired
55         UserInfoDao userInfoDao;
56         
57         public UserInfo getUserInfo(String loginId){
58                 UserInfo name = userInfoDao.getUserInfoByLoginId(loginId);
59                 return name;    
60         }
61         
62         @RequestMapping(value={"/get_SettingsDictionaryDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
63         public void getSettingsDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
64                 try{
65                         Map<String, Object> model = new HashMap<String, Object>();
66                         ObjectMapper mapper = new ObjectMapper();
67                         model.put("settingsDictionaryDatas", mapper.writeValueAsString(decisionPolicyDao.getDecisionDataByName()));
68                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
69                         JSONObject j = new JSONObject(msg);
70                         response.getWriter().write(j.toString());
71                 }
72                 catch (Exception e){
73                         e.printStackTrace();
74                 }
75         }
76
77         
78         @RequestMapping(value={"/get_SettingsDictionaryData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
79         public void getSettingsDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
80                 try{
81                         Map<String, Object> model = new HashMap<String, Object>();
82                         ObjectMapper mapper = new ObjectMapper();
83                         model.put("settingsDictionaryDatas", mapper.writeValueAsString(decisionPolicyDao.getDecisionSettingsData()));
84                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
85                         JSONObject j = new JSONObject(msg);
86                         response.getWriter().write(j.toString());
87                 }
88                 catch (Exception e){
89                         e.printStackTrace();
90                 }
91         }
92         
93         @RequestMapping(value={"/decision_dictionary/save_Settings.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
94         public ModelAndView saveSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
95                 try {
96                         boolean duplicateflag = false;
97                         ObjectMapper mapper = new ObjectMapper();
98                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
99                         JsonNode root = mapper.readTree(request.getReader());
100                         DecisionSettings decisionSettings = (DecisionSettings)mapper.readValue(root.get("settingsDictionaryData").toString(), DecisionSettings.class);
101                     String userId = root.get("loginId").textValue();
102                         if(decisionSettings.getDatatypeBean().getShortName() != null){
103                                 String datatype = decisionSettings.getDatatypeBean().getShortName();
104                                 Datatype a = new Datatype();
105                                 if(datatype.equalsIgnoreCase("string")){
106                                         a.setId(26);    
107                                 }else if(datatype.equalsIgnoreCase("integer")){
108                                         a.setId(12);    
109                                 }else if(datatype.equalsIgnoreCase("boolean")){
110                                         a.setId(18);    
111                                 }else if(datatype.equalsIgnoreCase("double")){
112                                         a.setId(25);    
113                                 }else if(datatype.equalsIgnoreCase("user")){
114                                         a.setId(29);    
115                                 }
116                                 decisionSettings.setDatatypeBean(a);
117                         }
118                         if(decisionSettings.getId() == 0){
119                                 CheckDictionaryDuplicateEntries entry = new CheckDictionaryDuplicateEntries();
120                                 List<Object> duplicateData =  entry.CheckDuplicateEntry(decisionSettings.getXacmlId(), "xacmlId", DecisionSettings.class);
121                                 if(!duplicateData.isEmpty()){
122                                         duplicateflag = true;
123                                 }else{
124                                         decisionSettings.setUserCreatedBy(this.getUserInfo(userId));
125                                         decisionSettings.setUserModifiedBy(this.getUserInfo(userId));
126                                         decisionPolicyDao.Save(decisionSettings);
127                                 }
128                         }else{
129                                 decisionSettings.setUserModifiedBy(this.getUserInfo(userId));
130                                 decisionPolicyDao.update(decisionSettings); 
131                         } 
132                         response.setCharacterEncoding("UTF-8");
133                         response.setContentType("application / json");
134                         request.setCharacterEncoding("UTF-8");
135
136                         PrintWriter out = response.getWriter();
137                         String responseString = "";
138                         if(duplicateflag){
139                                 responseString = "Duplicate";
140                         }else{
141                                 responseString =  mapper.writeValueAsString(this.decisionPolicyDao.getDecisionSettingsData());
142                         }
143                         JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}");
144
145                         out.write(j.toString());
146
147                         return null;
148                 }
149                 catch (Exception e){
150                         response.setCharacterEncoding("UTF-8");
151                         request.setCharacterEncoding("UTF-8");
152                         PrintWriter out = response.getWriter();
153                         out.write(e.getMessage());
154                 }
155                 return null;
156         }
157
158         @RequestMapping(value={"/settings_dictionary/remove_settings.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
159         public ModelAndView removeSettingsDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
160                 try{
161                         ObjectMapper mapper = new ObjectMapper();
162                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
163                         JsonNode root = mapper.readTree(request.getReader());
164                         DecisionSettings decisionSettings = (DecisionSettings)mapper.readValue(root.get("data").toString(), DecisionSettings.class);
165                         decisionPolicyDao.delete(decisionSettings);
166                         response.setCharacterEncoding("UTF-8");
167                         response.setContentType("application / json");
168                         request.setCharacterEncoding("UTF-8");
169
170                         PrintWriter out = response.getWriter();
171
172                         String responseString = mapper.writeValueAsString(this.decisionPolicyDao.getDecisionSettingsData());
173                         JSONObject j = new JSONObject("{settingsDictionaryDatas: " + responseString + "}");
174                         out.write(j.toString());
175
176                         return null;
177                 }
178                 catch (Exception e){
179                         System.out.println(e);
180                         response.setCharacterEncoding("UTF-8");
181                         request.setCharacterEncoding("UTF-8");
182                         PrintWriter out = response.getWriter();
183                         out.write(e.getMessage());
184                 }
185                 return null;
186         }
187         
188 }