2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 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;
23 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
24 import com.fasterxml.jackson.annotation.PropertyAccessor;
25 import com.fasterxml.jackson.databind.DeserializationFeature;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
29 import java.io.PrintWriter;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse;
38 import org.json.JSONObject;
39 import org.onap.policy.common.logging.flexlogger.FlexLogger;
40 import org.onap.policy.common.logging.flexlogger.Logger;
41 import org.onap.policy.rest.dao.CommonClassDao;
42 import org.onap.policy.rest.jpa.PolicyEditorScopes;
43 import org.onap.policy.rest.jpa.PolicyRoles;
44 import org.onap.policy.rest.jpa.UserInfo;
45 import org.onap.portalsdk.core.controller.RestrictedBaseController;
46 import org.onap.portalsdk.core.web.support.JsonMessage;
47 import org.onap.portalsdk.core.web.support.UserUtils;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.http.MediaType;
50 import org.springframework.stereotype.Controller;
51 import org.springframework.web.bind.annotation.RequestMapping;
52 import org.springframework.web.servlet.ModelAndView;
56 public class PolicyRolesController extends RestrictedBaseController {
58 private static final Logger LOGGER = FlexLogger.getLogger(PolicyRolesController.class);
61 CommonClassDao commonClassDao;
63 public void setCommonClassDao(CommonClassDao commonClassDao) {
64 this.commonClassDao = commonClassDao;
67 List<String> scopelist;
70 * Gets the policy roles entity data.
72 * @param request the request
73 * @param response the response
76 value = {"/get_RolesData"},
77 method = {org.springframework.web.bind.annotation.RequestMethod.GET},
78 produces = MediaType.APPLICATION_JSON_VALUE)
79 public void getPolicyRolesEntityData(HttpServletRequest request, HttpServletResponse response) {
81 Map<String, Object> model = new HashMap<>();
82 ObjectMapper mapper = new ObjectMapper();
83 model.put("rolesDatas", mapper.writeValueAsString(commonClassDao.getUserRoles()));
84 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
85 JSONObject j = new JSONObject(msg);
86 response.getWriter().write(j.toString());
87 } catch (Exception e) {
88 LOGGER.error("Exception Occured" + e);
93 * Save roles and Mechid entity data.
95 * @param request the request
96 * @param response the response
97 * @return the model and view
100 value = {"/save_NonSuperRolesData"},
101 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
102 public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response) {
104 StringBuilder scopeName = new StringBuilder();
105 ObjectMapper mapper = new ObjectMapper();
106 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
107 String userId = UserUtils.getUserSession(request).getOrgUserId();
108 JsonNode root = mapper.readTree(request.getReader());
109 ReadScopes adapter = mapper.readValue(root.get("editRoleData").toString(), ReadScopes.class);
110 for (int i = 0; i < adapter.getScope().size(); i++) {
112 scopeName.append(adapter.getScope().get(0));
114 scopeName.append("," + adapter.getScope().get(i));
118 "****************************************Logging UserID for Roles Function********************************************************");
119 LOGGER.info("UserId: " + userId + "Updating the Scope for following user" + adapter.getLoginId()
120 + "ScopeNames" + adapter.getScope());
122 "*********************************************************************************************************************************");
123 UserInfo userInfo = new UserInfo();
124 userInfo.setUserLoginId(adapter.getLoginId().getUserName());
125 userInfo.setUserName(adapter.getLoginId().getUserName());
127 boolean checkNew = false;
128 if (adapter.getId() == 0 && "mechid".equals(adapter.getRole())) {
129 // Save new mechid scopes entity data.
131 "****************************************Logging UserID for New Mechid Function***************************************************");
132 LOGGER.info("UserId:" + userId + "Adding new mechid-scopes for following user" + adapter.getLoginId()
133 + "ScopeNames " + adapter.getScope());
135 "*********************************************************************************************************************************");
136 // First add the mechid to userinfo
137 commonClassDao.save(userInfo);
141 PolicyRoles roles = new PolicyRoles();
142 roles.setId(adapter.getId());
143 roles.setLoginId(adapter.getLoginId());
144 roles.setRole(adapter.getRole());
145 roles.setScope(scopeName.toString());
147 roles.setLoginId(userInfo);
148 commonClassDao.save(roles);
150 commonClassDao.update(roles);
152 response.setCharacterEncoding("UTF-8");
153 response.setContentType("application / json");
154 request.setCharacterEncoding("UTF-8");
156 PrintWriter out = response.getWriter();
157 String responseString = mapper.writeValueAsString(commonClassDao.getUserRoles());
158 JSONObject j = new JSONObject("{rolesDatas: " + responseString + "}");
160 out.write(j.toString());
161 } catch (Exception e) {
162 LOGGER.error("Exception Occured" + e);
168 * Gets the policy scopes entity data.
170 * @param request the request
171 * @param response the response
174 value = {"/get_PolicyRolesScopeData"},
175 method = {org.springframework.web.bind.annotation.RequestMethod.GET},
176 produces = MediaType.APPLICATION_JSON_VALUE)
177 public void getPolicyScopesEntityData(HttpServletRequest request, HttpServletResponse response) {
179 scopelist = new ArrayList<>();
180 Map<String, Object> model = new HashMap<>();
181 ObjectMapper mapper = new ObjectMapper();
182 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
183 List<String> scopesData = commonClassDao.getDataByColumn(PolicyEditorScopes.class, "scopeName");
184 model.put("scopeDatas", mapper.writeValueAsString(scopesData));
185 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
186 JSONObject j = new JSONObject(msg);
187 response.getWriter().write(j.toString());
188 } catch (Exception e) {
189 LOGGER.error("Exception Occured" + e);
197 private UserInfo loginId;
199 private List<String> scope;
205 public void setId(int id) {
209 public UserInfo getLoginId() {
213 public void setLoginId(UserInfo loginId) {
214 this.loginId = loginId;
217 public String getRole() {
221 public void setRole(String role) {
225 public List<String> getScope() {
229 public void setScope(List<String> scope) {