[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / AdminTabController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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.controller;
22
23
24 import java.io.PrintWriter;
25 import java.util.HashMap;
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.onap.policy.common.logging.flexlogger.FlexLogger;
33 import org.onap.policy.common.logging.flexlogger.Logger;
34 import org.onap.policy.rest.dao.CommonClassDao;
35 import org.onap.policy.rest.jpa.GlobalRoleSettings;
36 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
37 import org.openecomp.portalsdk.core.web.support.JsonMessage;
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 @RequestMapping({"/"})
50 public class AdminTabController extends RestrictedBaseController{
51
52         private static final Logger LOGGER      = FlexLogger.getLogger(AdminTabController.class);
53         
54         private static CommonClassDao commonClassDao;
55         
56         public static CommonClassDao getCommonClassDao() {
57                 return commonClassDao;
58         }
59
60         public static void setCommonClassDao(CommonClassDao commonClassDao) {
61                 AdminTabController.commonClassDao = commonClassDao;
62         }
63         
64         @Autowired
65         private AdminTabController(CommonClassDao commonClassDao){
66                 AdminTabController.commonClassDao = commonClassDao;
67         }
68                 
69         public AdminTabController() {
70                 //default constructor
71         }
72
73         @RequestMapping(value={"/get_LockDownData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
74         public void getAdminTabEntityData(HttpServletRequest request, HttpServletResponse response){
75                 try{
76                         Map<String, Object> model = new HashMap<>();
77                         ObjectMapper mapper = new ObjectMapper();
78                         model.put("lockdowndata", mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class)));
79                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
80                         JSONObject j = new JSONObject(msg);
81                         response.getWriter().write(j.toString());
82                 }
83                 catch (Exception e){
84                         LOGGER.error("Exception Occured"+e);
85                 }
86         }
87         
88         @RequestMapping(value={"/adminTabController/save_LockDownValue.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
89         public ModelAndView saveAdminTabLockdownValue(HttpServletRequest request, HttpServletResponse response) throws Exception{
90                 try {
91                         ObjectMapper mapper = new ObjectMapper();
92                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
93                         JsonNode root = mapper.readTree(request.getReader());
94                         GlobalRoleSettings globalRole = mapper.readValue(root.get("lockdowndata").toString(), GlobalRoleSettings.class);
95                         globalRole.setRole("super-admin");
96                         commonClassDao.update(globalRole);
97                         
98                         response.setCharacterEncoding("UTF-8");
99                         response.setContentType("application / json");
100                         request.setCharacterEncoding("UTF-8");
101
102                         PrintWriter out = response.getWriter();
103                         String responseString = mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class));
104                         JSONObject j = new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString + "}");
105
106                         out.write(j.toString());
107
108                         return null;
109                 }
110                 catch (Exception e){
111                         LOGGER.error("Exception Occured"+e);
112                         response.setCharacterEncoding("UTF-8");
113                         request.setCharacterEncoding("UTF-8");
114                         PrintWriter out = response.getWriter();
115                         out.write(e.getMessage());
116                 }
117                 return null;
118         }
119 }