2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controller;
24 import java.io.PrintWriter;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
33 import org.json.JSONObject;
34 import org.onap.policy.common.logging.flexlogger.FlexLogger;
35 import org.onap.policy.common.logging.flexlogger.Logger;
36 import org.onap.policy.rest.dao.CommonClassDao;
37 import org.onap.policy.rest.jpa.PolicyEditorScopes;
38 import org.onap.policy.rest.jpa.PolicyRoles;
39 import org.onap.policy.rest.jpa.UserInfo;
40 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
41 import org.openecomp.portalsdk.core.web.support.JsonMessage;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.http.MediaType;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.servlet.ModelAndView;
48 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
49 import com.fasterxml.jackson.annotation.PropertyAccessor;
50 import com.fasterxml.jackson.databind.DeserializationFeature;
51 import com.fasterxml.jackson.databind.JsonNode;
52 import com.fasterxml.jackson.databind.ObjectMapper;
56 public class PolicyRolesController extends RestrictedBaseController{
58 private static final Logger LOGGER = FlexLogger.getLogger(PolicyRolesController.class);
61 CommonClassDao commonClassDao;
63 List<String> scopelist;
65 @RequestMapping(value={"/get_RolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
66 public void getPolicyRolesEntityData(HttpServletRequest request, HttpServletResponse response){
68 Map<String, Object> model = new HashMap<>();
69 ObjectMapper mapper = new ObjectMapper();
70 model.put("rolesDatas", mapper.writeValueAsString(commonClassDao.getUserRoles()));
71 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
72 JSONObject j = new JSONObject(msg);
73 response.getWriter().write(j.toString());
76 LOGGER.error("Exception Occured"+e);
80 @RequestMapping(value={"/save_NonSuperRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
81 public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response){
83 String scopeName = null;
84 ObjectMapper mapper = new ObjectMapper();
85 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
86 JsonNode root = mapper.readTree(request.getReader());
87 ReadScopes adapter = mapper.readValue(root.get("editRoleData").toString(), ReadScopes.class);
88 for(int i = 0; i < adapter.getScope().size(); i++){
90 scopeName = adapter.getScope().get(0);
92 scopeName = scopeName + "," + adapter.getScope().get(i);
95 PolicyRoles roles = new PolicyRoles();
96 roles.setId(adapter.getId());
97 roles.setLoginId(adapter.getLoginId());
98 roles.setRole(adapter.getRole());
99 roles.setScope(scopeName);
100 commonClassDao.update(roles);
101 response.setCharacterEncoding("UTF-8");
102 response.setContentType("application / json");
103 request.setCharacterEncoding("UTF-8");
105 PrintWriter out = response.getWriter();
106 String responseString = mapper.writeValueAsString(commonClassDao.getUserRoles());
107 JSONObject j = new JSONObject("{rolesDatas: " + responseString + "}");
109 out.write(j.toString());
112 LOGGER.error("Exception Occured"+e);
117 @RequestMapping(value={"/get_PolicyRolesScopeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
118 public void getPolicyScopesEntityData(HttpServletRequest request, HttpServletResponse response){
120 scopelist = new ArrayList<>();
121 Map<String, Object> model = new HashMap<>();
122 ObjectMapper mapper = new ObjectMapper();
123 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
124 List<String> scopesData = commonClassDao.getDataByColumn(PolicyEditorScopes.class, "scopeName");
125 model.put("scopeDatas", mapper.writeValueAsString(scopesData));
126 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
127 JSONObject j = new JSONObject(msg);
128 response.getWriter().write(j.toString());
131 LOGGER.error("Exception Occured"+e);
138 private UserInfo loginId;
140 private ArrayList<String> scope;
145 public void setId(int id) {
148 public UserInfo getLoginId() {
151 public void setLoginId(UserInfo loginId) {
152 this.loginId = loginId;
154 public String getRole() {
157 public void setRole(String role) {
160 public ArrayList<String> getScope() {
163 public void setScope(ArrayList<String> scope) {