Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-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.dao.GlobalRoleSettingsDao;
33 import org.openecomp.policy.rest.jpa.GlobalRoleSettings;
34 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
35 import org.openecomp.portalsdk.core.web.support.JsonMessage;
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 @RequestMapping({"/"})
48 public class AdminTabController extends RestrictedBaseController{
49
50         @Autowired
51         GlobalRoleSettingsDao globalRoleSettingsDao;
52                 
53         
54
55         @RequestMapping(value={"/get_LockDownData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
56         public void getAdminTabEntityData(HttpServletRequest request, HttpServletResponse response){
57                 try{
58                         Map<String, Object> model = new HashMap<String, Object>();
59                         ObjectMapper mapper = new ObjectMapper();
60                         model.put("lockdowndata", mapper.writeValueAsString(globalRoleSettingsDao.getGlobalRoleSettings()));
61                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
62                         JSONObject j = new JSONObject(msg);
63                         response.getWriter().write(j.toString());
64                 }
65                 catch (Exception e){
66                         e.printStackTrace();
67                 }
68         }
69         
70         @RequestMapping(value={"/adminTabController/save_LockDownValue.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
71         public ModelAndView saveAdminTabLockdownValue(HttpServletRequest request, HttpServletResponse response) throws Exception{
72                 try {
73                         ObjectMapper mapper = new ObjectMapper();
74                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
75                         JsonNode root = mapper.readTree(request.getReader());
76                         GlobalRoleSettings globalRole = mapper.readValue(root.get("lockdowndata").toString(), GlobalRoleSettings.class);
77                         globalRole.setRole("super-admin");
78                         globalRoleSettingsDao.update(globalRole);
79                         
80                         response.setCharacterEncoding("UTF-8");
81                         response.setContentType("application / json");
82                         request.setCharacterEncoding("UTF-8");
83
84                         PrintWriter out = response.getWriter();
85                         String responseString = mapper.writeValueAsString(this.globalRoleSettingsDao.getGlobalRoleSettings());
86                         JSONObject j = new JSONObject("{descriptiveScopeDictionaryDatas: " + responseString + "}");
87
88                         out.write(j.toString());
89
90                         return null;
91                 }
92                 catch (Exception e){
93                         response.setCharacterEncoding("UTF-8");
94                         request.setCharacterEncoding("UTF-8");
95                         PrintWriter out = response.getWriter();
96                         out.write(e.getMessage());
97                 }
98                 return null;
99         }
100 }