Policy TestSuite Enabled
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / controller / AdminTabController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP 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.openecomp.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.openecomp.policy.common.logging.flexlogger.FlexLogger;
33 import org.openecomp.policy.common.logging.flexlogger.Logger;
34 import org.openecomp.policy.rest.dao.CommonClassDao;
35 import org.openecomp.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         @Autowired
55         CommonClassDao commonClassDao;
56                 
57         @RequestMapping(value={"/get_LockDownData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
58         public void getAdminTabEntityData(HttpServletRequest request, HttpServletResponse response){
59                 try{
60                         Map<String, Object> model = new HashMap<String, Object>();
61                         ObjectMapper mapper = new ObjectMapper();
62                         model.put("lockdowndata", mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class)));
63                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
64                         JSONObject j = new JSONObject(msg);
65                         response.getWriter().write(j.toString());
66                 }
67                 catch (Exception e){
68                         LOGGER.error("Exception Occured"+e);
69                 }
70         }
71         
72         @RequestMapping(value={"/adminTabController/save_LockDownValue.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
73         public ModelAndView saveAdminTabLockdownValue(HttpServletRequest request, HttpServletResponse response) throws Exception{
74                 try {
75                         ObjectMapper mapper = new ObjectMapper();
76                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
77                         JsonNode root = mapper.readTree(request.getReader());
78                         GlobalRoleSettings globalRole = mapper.readValue(root.get("lockdowndata").toString(), GlobalRoleSettings.class);
79                         globalRole.setRole("super-admin");
80                         commonClassDao.update(globalRole);
81                         
82                         response.setCharacterEncoding("UTF-8");
83                         response.setContentType("application / json");
84                         request.setCharacterEncoding("UTF-8");
85
86                         PrintWriter out = response.getWriter();
87                         String responseString = mapper.writeValueAsString(commonClassDao.getData(GlobalRoleSettings.class));
88                         JSONObject j = new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString + "}");
89
90                         out.write(j.toString());
91
92                         return null;
93                 }
94                 catch (Exception e){
95                         response.setCharacterEncoding("UTF-8");
96                         request.setCharacterEncoding("UTF-8");
97                         PrintWriter out = response.getWriter();
98                         out.write(e.getMessage());
99                 }
100                 return null;
101         }
102 }