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.onap.portalsdk.core.controller.RestrictedBaseController;
41 import org.onap.portalsdk.core.web.support.JsonMessage;
42 import org.onap.portalsdk.core.web.support.UserUtils;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.MediaType;
45 import org.springframework.stereotype.Controller;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.servlet.ModelAndView;
49 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
50 import com.fasterxml.jackson.annotation.PropertyAccessor;
51 import com.fasterxml.jackson.databind.DeserializationFeature;
52 import com.fasterxml.jackson.databind.JsonNode;
53 import com.fasterxml.jackson.databind.ObjectMapper;
57 public class PolicyRolesController extends RestrictedBaseController{
59 private static final Logger LOGGER = FlexLogger.getLogger(PolicyRolesController.class);
62 CommonClassDao commonClassDao;
64 List<String> scopelist;
66 @RequestMapping(value={"/get_RolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
67 public void getPolicyRolesEntityData(HttpServletRequest request, HttpServletResponse response){
69 Map<String, Object> model = new HashMap<>();
70 ObjectMapper mapper = new ObjectMapper();
71 model.put("rolesDatas", mapper.writeValueAsString(commonClassDao.getUserRoles()));
72 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
73 JSONObject j = new JSONObject(msg);
74 response.getWriter().write(j.toString());
77 LOGGER.error("Exception Occured"+e);
81 @RequestMapping(value={"/save_NonSuperRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
82 public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response){
84 StringBuilder scopeName = new StringBuilder();
85 ObjectMapper mapper = new ObjectMapper();
86 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
87 String userId = UserUtils.getUserSession(request).getOrgUserId();
88 JsonNode root = mapper.readTree(request.getReader());
89 ReadScopes adapter = mapper.readValue(root.get("editRoleData").toString(), ReadScopes.class);
90 for(int i = 0; i < adapter.getScope().size(); i++){
92 scopeName.append(adapter.getScope().get(0));
94 scopeName.append("," + adapter.getScope().get(i));
97 LOGGER.info("****************************************Logging UserID for Roles Function********************************************************");
98 LOGGER.info("UserId: " + userId + "Updating the Scope for following user" + adapter.getLoginId() + "ScopeNames" + adapter.getScope());
99 LOGGER.info("*********************************************************************************************************************************");
100 PolicyRoles roles = new PolicyRoles();
101 roles.setId(adapter.getId());
102 roles.setLoginId(adapter.getLoginId());
103 roles.setRole(adapter.getRole());
104 roles.setScope(scopeName.toString());
105 commonClassDao.update(roles);
106 response.setCharacterEncoding("UTF-8");
107 response.setContentType("application / json");
108 request.setCharacterEncoding("UTF-8");
110 PrintWriter out = response.getWriter();
111 String responseString = mapper.writeValueAsString(commonClassDao.getUserRoles());
112 JSONObject j = new JSONObject("{rolesDatas: " + responseString + "}");
114 out.write(j.toString());
117 LOGGER.error("Exception Occured"+e);
122 @RequestMapping(value={"/get_PolicyRolesScopeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
123 public void getPolicyScopesEntityData(HttpServletRequest request, HttpServletResponse response){
125 scopelist = new ArrayList<>();
126 Map<String, Object> model = new HashMap<>();
127 ObjectMapper mapper = new ObjectMapper();
128 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
129 List<String> scopesData = commonClassDao.getDataByColumn(PolicyEditorScopes.class, "scopeName");
130 model.put("scopeDatas", mapper.writeValueAsString(scopesData));
131 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
132 JSONObject j = new JSONObject(msg);
133 response.getWriter().write(j.toString());
136 LOGGER.error("Exception Occured"+e);
143 private UserInfo loginId;
145 private List<String> scope;
150 public void setId(int id) {
153 public UserInfo getLoginId() {
156 public void setLoginId(UserInfo loginId) {
157 this.loginId = loginId;
159 public String getRole() {
162 public void setRole(String role) {
165 public List<String> getScope() {
168 public void setScope(List<String> scope) {