Policy 1707 commit to LF
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / controller / EnforcerDictionaryController.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.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.json.JSONObject;
33 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
34 import org.openecomp.policy.rest.dao.CommonClassDao;
35 import org.openecomp.policy.rest.jpa.EnforcingType;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.http.MediaType;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.RequestMapping;
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 EnforcerDictionaryController {
48
49         @Autowired
50         CommonClassDao commonClassDao;
51
52         @RequestMapping(value={"/get_EnforcerTypeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
53         public void getEnforcerDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
54                 try{
55                         Map<String, Object> model = new HashMap<String, Object>();
56                         ObjectMapper mapper = new ObjectMapper();
57                         List<Object> list = commonClassDao.getData(EnforcingType.class);
58                         List<String> dictList = new ArrayList<String>();
59                         for(int i = 0; i < list.size(); i++){
60                                 EnforcingType dict = (EnforcingType) list.get(i);
61                                 dictList.add(dict.getEnforcingType());
62                         }
63                         model.put("enforcerDictionaryDatas", mapper.writeValueAsString(dictList));
64                         org.openecomp.policy.pap.xacml.rest.util.JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
65                         JSONObject j = new JSONObject(msg);
66                         response.getWriter().write(j.toString());
67                 }
68                 catch (Exception e){
69                         e.printStackTrace();
70                 }
71         }
72         
73         @RequestMapping(value={"/enforcer_dictionary/save_enforcerType"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
74         public ModelAndView saveEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
75                 try {
76                         ObjectMapper mapper = new ObjectMapper();
77                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
78                         JsonNode root = mapper.readTree(request.getReader());
79                         EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("enforcerDictionaryData").toString(), EnforcingType.class);
80                         if(enforcingType.getId() == 0){
81                                 commonClassDao.save(enforcingType);
82                         }else{
83                                 commonClassDao.update(enforcingType); 
84                         } 
85                         response.setCharacterEncoding("UTF-8");
86                         response.setContentType("application / json");
87                         request.setCharacterEncoding("UTF-8");
88
89                         PrintWriter out = response.getWriter();
90                         String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
91                         JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
92
93                         out.write(j.toString());
94
95                         return null;
96                 }
97                 catch (Exception e){
98                         response.setCharacterEncoding("UTF-8");
99                         request.setCharacterEncoding("UTF-8");
100                         PrintWriter out = response.getWriter();
101                         out.write(e.getMessage());
102                 }
103                 return null;
104         }
105
106         @RequestMapping(value={"/enforcer_dictionary/remove_enforcer"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
107         public ModelAndView removeEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
108                 try{
109                         ObjectMapper mapper = new ObjectMapper();
110                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
111                         JsonNode root = mapper.readTree(request.getReader());
112                         EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("data").toString(), EnforcingType.class);
113                         commonClassDao.delete(enforcingType);
114                         response.setCharacterEncoding("UTF-8");
115                         response.setContentType("application / json");
116                         request.setCharacterEncoding("UTF-8");
117
118                         PrintWriter out = response.getWriter();
119
120                         String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
121                         JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
122                         out.write(j.toString());
123
124                         return null;
125                 }
126                 catch (Exception e){
127                         System.out.println(e);
128                         response.setCharacterEncoding("UTF-8");
129                         request.setCharacterEncoding("UTF-8");
130                         PrintWriter out = response.getWriter();
131                         out.write(e.getMessage());
132                 }
133                 return null;
134         }
135 }