Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / controller / BRMSDictionaryController.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.io.StringWriter;
25 import java.nio.charset.StandardCharsets;
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.io.IOUtils;
34 import org.json.JSONObject;
35 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
36 import org.openecomp.policy.rest.dao.BRMSParamTemplateDao;
37 import org.openecomp.policy.rest.dao.UserInfoDao;
38 import org.openecomp.policy.rest.jpa.BRMSParamTemplate;
39 import org.openecomp.policy.rest.jpa.UserInfo;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.MediaType;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.servlet.ModelAndView;
45
46 import com.fasterxml.jackson.databind.DeserializationFeature;
47 import com.fasterxml.jackson.databind.JsonNode;
48 import com.fasterxml.jackson.databind.ObjectMapper;
49
50
51 @Controller
52 public class BRMSDictionaryController{
53
54         @Autowired
55         BRMSParamTemplateDao brmsParamTemplateDao;
56         
57         @Autowired
58         UserInfoDao userInfoDao;
59         
60         private String rule;
61         
62         public UserInfo getUserInfo(String loginId){
63                 UserInfo name = userInfoDao.getUserInfoByLoginId(loginId);
64                 return name;    
65         }
66
67         @RequestMapping(value={"/get_BRMSParamDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
68         public void getBRMSParamDictionaryByNameEntityData(HttpServletRequest request, HttpServletResponse response){
69                 try{
70                         Map<String, Object> model = new HashMap<String, Object>();
71                         ObjectMapper mapper = new ObjectMapper();
72                         model.put("brmsParamDictionaryDatas", mapper.writeValueAsString(brmsParamTemplateDao.getBRMSParamDataByName()));
73                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
74                         JSONObject j = new JSONObject(msg);
75                         response.getWriter().write(j.toString());
76                 }
77                 catch (Exception e){
78                         e.printStackTrace();
79                 }
80         }
81         
82         @RequestMapping(value={"/get_BRMSParamData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
83         public void getBRMSParamDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
84                 try{
85                         Map<String, Object> model = new HashMap<String, Object>();
86                         ObjectMapper mapper = new ObjectMapper();
87                         model.put("brmsParamDictionaryDatas", mapper.writeValueAsString(brmsParamTemplateDao.getBRMSParamTemplateData()));
88                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
89                         JSONObject j = new JSONObject(msg);
90                         response.getWriter().write(j.toString());
91                 }
92                 catch (Exception e){
93                         e.printStackTrace();
94                 }
95         }
96         
97         @RequestMapping(value={"/brms_dictionary/set_BRMSParamData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
98         public void SetRuleData(HttpServletRequest request, HttpServletResponse response) throws Exception{
99                 StringWriter writer = new StringWriter();
100                 IOUtils.copy(request.getInputStream() , writer, StandardCharsets.UTF_8);
101                 String cleanStreamBoundary =  writer.toString().replaceFirst("------(.*)(?s).*octet-stream", "");
102                 rule = cleanStreamBoundary.substring(0, cleanStreamBoundary.lastIndexOf("end")+4);
103         }
104         
105         @RequestMapping(value={"/brms_dictionary/save_BRMSParam.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
106         public ModelAndView saveBRMSParamDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
107                 try {
108                         boolean duplicateflag = false;
109                         ObjectMapper mapper = new ObjectMapper();
110                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
111                         JsonNode root = mapper.readTree(request.getReader());
112                         BRMSParamTemplate bRMSParamTemplateData = (BRMSParamTemplate)mapper.readValue(root.get("brmsParamDictionaryData").toString(), BRMSParamTemplate.class);
113                         String userId = root.get("loginId").textValue();
114                         bRMSParamTemplateData.setRule(rule);
115                         if(bRMSParamTemplateData.getId() == 0){
116                                 CheckDictionaryDuplicateEntries entry = new CheckDictionaryDuplicateEntries();
117                                 List<Object> duplicateData =  entry.CheckDuplicateEntry(bRMSParamTemplateData.getRuleName(), "ruleName", BRMSParamTemplate.class);
118                                 if(!duplicateData.isEmpty()){
119                                         duplicateflag = true;
120                                 }else{
121                                         bRMSParamTemplateData.setUserCreatedBy(this.getUserInfo(userId));
122                                         brmsParamTemplateDao.Save(bRMSParamTemplateData);
123                                 }       
124                         }else{
125                                 brmsParamTemplateDao.update(bRMSParamTemplateData); 
126                         } 
127                         response.setCharacterEncoding("UTF-8");
128                         response.setContentType("application / json");
129                         request.setCharacterEncoding("UTF-8");
130
131                         PrintWriter out = response.getWriter();
132                         String responseString = "";
133                         if(duplicateflag){
134                                 responseString = "Duplicate";
135                         }else{
136                                 responseString = mapper.writeValueAsString(this.brmsParamTemplateDao.getBRMSParamTemplateData());
137                         } 
138                         JSONObject j = new JSONObject("{brmsParamDictionaryDatas: " + responseString + "}");
139
140                         out.write(j.toString());
141
142                         return null;
143                 }
144                 catch (Exception e){
145                         response.setCharacterEncoding("UTF-8");
146                         request.setCharacterEncoding("UTF-8");
147                         PrintWriter out = response.getWriter();
148                         out.write(e.getMessage());
149                 }
150                 return null;
151         }
152
153         @RequestMapping(value={"/brms_dictionary/remove_brmsParam.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
154         public ModelAndView removeBRMSParamDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
155                 try{
156                         ObjectMapper mapper = new ObjectMapper();
157                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
158                         JsonNode root = mapper.readTree(request.getReader());
159                         BRMSParamTemplate bRMSParamTemplateData = (BRMSParamTemplate)mapper.readValue(root.get("data").toString(), BRMSParamTemplate.class);
160                         brmsParamTemplateDao.delete(bRMSParamTemplateData);
161                         response.setCharacterEncoding("UTF-8");
162                         response.setContentType("application / json");
163                         request.setCharacterEncoding("UTF-8");
164
165                         PrintWriter out = response.getWriter();
166
167                         String responseString = mapper.writeValueAsString(this.brmsParamTemplateDao.getBRMSParamTemplateData());
168                         JSONObject j = new JSONObject("{brmsParamDictionaryDatas: " + responseString + "}");
169                         out.write(j.toString());
170
171                         return null;
172                 }
173                 catch (Exception e){
174                         System.out.println(e);
175                         response.setCharacterEncoding("UTF-8");
176                         request.setCharacterEncoding("UTF-8");
177                         PrintWriter out = response.getWriter();
178                         out.write(e.getMessage());
179                 }
180                 return null;
181         }
182         
183 }