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 public void setCommonClassDao(CommonClassDao commonClassDao) {
65 this.commonClassDao = commonClassDao;
68 List<String> scopelist;
70 @RequestMapping(value={"/get_RolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
71 public void getPolicyRolesEntityData(HttpServletRequest request, HttpServletResponse response){
73 Map<String, Object> model = new HashMap<>();
74 ObjectMapper mapper = new ObjectMapper();
75 model.put("rolesDatas", mapper.writeValueAsString(commonClassDao.getUserRoles()));
76 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
77 JSONObject j = new JSONObject(msg);
78 response.getWriter().write(j.toString());
81 LOGGER.error("Exception Occured"+e);
85 @RequestMapping(value={"/save_NonSuperRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
86 public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response){
88 StringBuilder scopeName = new StringBuilder();
89 ObjectMapper mapper = new ObjectMapper();
90 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
91 String userId = UserUtils.getUserSession(request).getOrgUserId();
92 JsonNode root = mapper.readTree(request.getReader());
93 ReadScopes adapter = mapper.readValue(root.get("editRoleData").toString(), ReadScopes.class);
94 for(int i = 0; i < adapter.getScope().size(); i++){
96 scopeName.append(adapter.getScope().get(0));
98 scopeName.append("," + adapter.getScope().get(i));
101 LOGGER.info("****************************************Logging UserID for Roles Function********************************************************");
102 LOGGER.info("UserId: " + userId + "Updating the Scope for following user" + adapter.getLoginId() + "ScopeNames" + adapter.getScope());
103 LOGGER.info("*********************************************************************************************************************************");
104 PolicyRoles roles = new PolicyRoles();
105 roles.setId(adapter.getId());
106 roles.setLoginId(adapter.getLoginId());
107 roles.setRole(adapter.getRole());
108 roles.setScope(scopeName.toString());
109 commonClassDao.update(roles);
110 response.setCharacterEncoding("UTF-8");
111 response.setContentType("application / json");
112 request.setCharacterEncoding("UTF-8");
114 PrintWriter out = response.getWriter();
115 String responseString = mapper.writeValueAsString(commonClassDao.getUserRoles());
116 JSONObject j = new JSONObject("{rolesDatas: " + responseString + "}");
118 out.write(j.toString());
121 LOGGER.error("Exception Occured"+e);
126 @RequestMapping(value={"/get_PolicyRolesScopeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
127 public void getPolicyScopesEntityData(HttpServletRequest request, HttpServletResponse response){
129 scopelist = new ArrayList<>();
130 Map<String, Object> model = new HashMap<>();
131 ObjectMapper mapper = new ObjectMapper();
132 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
133 List<String> scopesData = commonClassDao.getDataByColumn(PolicyEditorScopes.class, "scopeName");
134 model.put("scopeDatas", mapper.writeValueAsString(scopesData));
135 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
136 JSONObject j = new JSONObject(msg);
137 response.getWriter().write(j.toString());
140 LOGGER.error("Exception Occured"+e);
147 private UserInfo loginId;
149 private List<String> scope;
154 public void setId(int id) {
157 public UserInfo getLoginId() {
160 public void setLoginId(UserInfo loginId) {
161 this.loginId = loginId;
163 public String getRole() {
166 public void setRole(String role) {
169 public List<String> getScope() {
172 public void setScope(List<String> scope) {