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.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
34 import javax.servlet.http.HttpServletRequest;
35 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.policy.utils.PolicyUtils;
46 import org.onap.portalsdk.core.controller.RestrictedBaseController;
47 import org.onap.portalsdk.core.web.support.JsonMessage;
48 import org.onap.portalsdk.core.web.support.UserUtils;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.http.MediaType;
51 import org.springframework.stereotype.Controller;
52 import org.springframework.web.bind.annotation.RequestMapping;
53 import org.springframework.web.servlet.ModelAndView;
57 public class PolicyRolesController extends RestrictedBaseController {
59 private static final Logger LOGGER = FlexLogger.getLogger(PolicyRolesController.class);
62 CommonClassDao commonClassDao;
64 public void setCommonClassDao(CommonClassDao commonClassDao) {
65 this.commonClassDao = commonClassDao;
68 List<String> scopelist;
71 * Gets the policy roles entity data.
73 * @param request the request
74 * @param response the response
77 value = {"/get_RolesData"},
78 method = {org.springframework.web.bind.annotation.RequestMethod.GET},
79 produces = MediaType.APPLICATION_JSON_VALUE)
80 public void getPolicyRolesEntityData(HttpServletRequest request, HttpServletResponse response) {
82 Map<String, Object> model = new HashMap<>();
83 ObjectMapper mapper = new ObjectMapper();
84 model.put("rolesDatas", mapper.writeValueAsString(commonClassDao.getUserRoles()));
85 response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString());
86 } catch (Exception e) {
87 LOGGER.error("Exception Occured" + e);
92 * Save roles and Mechid entity data.
94 * @param request the request
95 * @param response the response
96 * @return the model and view
99 value = {"/save_NonSuperRolesData"},
100 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
101 public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response) {
103 StringBuilder scopeName = new StringBuilder();
104 ObjectMapper mapper = new ObjectMapper();
105 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
106 String userId = UserUtils.getUserSession(request).getOrgUserId();
107 JsonNode root = mapper.readTree(request.getReader());
108 ReadScopes adapter = mapper.readValue(root.get("editRoleData").toString(), ReadScopes.class);
109 for (int i = 0; i < adapter.getScope().size(); i++) {
111 scopeName.append(adapter.getScope().get(0));
113 scopeName.append("," + adapter.getScope().get(i));
117 "*************************Logging UserID for Roles Function***********************************");
118 LOGGER.info("UserId: " + userId + "Updating the Scope for following user" + adapter.getLoginId()
119 + "ScopeNames" + adapter.getScope());
121 "*********************************************************************************************");
122 UserInfo userInfo = new UserInfo();
123 userInfo.setUserLoginId(adapter.getLoginId().getUserName());
124 userInfo.setUserName(adapter.getLoginId().getUserName());
126 boolean checkNew = false;
127 if (adapter.getId() == 0 && "mechid".equals(adapter.getRole())) {
128 // Save new mechid scopes entity data.
130 "*********************Logging UserID for New Mechid Function********************************");
131 LOGGER.info("UserId:" + userId + "Adding new mechid-scopes for following user" + adapter.getLoginId()
132 + "ScopeNames " + adapter.getScope());
134 "*******************************************************************************************");
135 // First add the mechid to userinfo
136 commonClassDao.save(userInfo);
140 PolicyRoles roles = new PolicyRoles();
141 roles.setId(adapter.getId());
142 roles.setLoginId(adapter.getLoginId());
143 roles.setRole(adapter.getRole());
144 roles.setScope(scopeName.toString());
146 roles.setLoginId(userInfo);
147 commonClassDao.save(roles);
149 commonClassDao.update(roles);
151 response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
152 response.setContentType(PolicyUtils.APPLICATION_JSON);
153 request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
154 response.getWriter().write(new JSONObject("{rolesDatas: "
155 + mapper.writeValueAsString(commonClassDao.getUserRoles()) + "}").toString());
156 } catch (Exception e) {
157 LOGGER.error("Exception Occured" + e);
163 * Gets the policy scopes entity data.
165 * @param request the request
166 * @param response the response
169 value = {"/get_PolicyRolesScopeData"},
170 method = {org.springframework.web.bind.annotation.RequestMethod.GET},
171 produces = MediaType.APPLICATION_JSON_VALUE)
172 public void getPolicyScopesEntityData(HttpServletRequest request, HttpServletResponse response) {
174 scopelist = new ArrayList<>();
175 Map<String, Object> model = new HashMap<>();
176 ObjectMapper mapper = new ObjectMapper();
177 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
178 List<String> scopesData = commonClassDao.getDataByColumn(PolicyEditorScopes.class, "scopeName");
179 model.put("scopeDatas", mapper.writeValueAsString(scopesData));
180 response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString());
181 } catch (Exception e) {
182 LOGGER.error("Exception Occured" + e);
191 private UserInfo loginId;
193 private List<String> scope;