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.openecomp.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.openecomp.policy.rest.dao.CommonClassDao;
35 import org.openecomp.policy.rest.jpa.PolicyEditorScopes;
36 import org.openecomp.policy.rest.jpa.PolicyRoles;
37 import org.openecomp.policy.rest.jpa.UserInfo;
38 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
39 import org.openecomp.portalsdk.core.web.support.JsonMessage;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.MediaType;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.servlet.ModelAndView;
46 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
47 import com.fasterxml.jackson.annotation.PropertyAccessor;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.JsonNode;
50 import com.fasterxml.jackson.databind.ObjectMapper;
54 public class PolicyRolesController extends RestrictedBaseController{
57 CommonClassDao commonClassDao;
59 List<String> scopelist;
61 @RequestMapping(value={"/get_RolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
62 public void getPolicyRolesEntityData(HttpServletRequest request, HttpServletResponse response){
64 Map<String, Object> model = new HashMap<String, Object>();
65 ObjectMapper mapper = new ObjectMapper();
66 model.put("rolesDatas", mapper.writeValueAsString(commonClassDao.getUserRoles()));
67 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
68 JSONObject j = new JSONObject(msg);
69 response.getWriter().write(j.toString());
76 @RequestMapping(value={"/save_NonSuperRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
77 public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response){
79 String scopeName = null;
80 ObjectMapper mapper = new ObjectMapper();
81 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
82 JsonNode root = mapper.readTree(request.getReader());
83 ReadScopes adapter = mapper.readValue(root.get("editRoleData").toString(), ReadScopes.class);
84 for(int i = 0; i < adapter.getScope().size(); i++){
86 scopeName = adapter.getScope().get(0);
88 scopeName = scopeName + "," + adapter.getScope().get(i);
91 PolicyRoles roles = new PolicyRoles();
92 roles.setId(adapter.getId());
93 roles.setLoginId(adapter.getLoginId());
94 roles.setRole(adapter.getRole());
95 roles.setScope(scopeName);
96 commonClassDao.update(roles);
97 response.setCharacterEncoding("UTF-8");
98 response.setContentType("application / json");
99 request.setCharacterEncoding("UTF-8");
101 PrintWriter out = response.getWriter();
102 String responseString = mapper.writeValueAsString(commonClassDao.getUserRoles());
103 JSONObject j = new JSONObject("{rolesDatas: " + responseString + "}");
105 out.write(j.toString());
113 @RequestMapping(value={"/get_PolicyRolesScopeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
114 public void getPolicyScopesEntityData(HttpServletRequest request, HttpServletResponse response){
116 scopelist = new ArrayList<String>();
117 Map<String, Object> model = new HashMap<String, Object>();
118 ObjectMapper mapper = new ObjectMapper();
119 mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
120 List<String> scopesData = commonClassDao.getDataByColumn(PolicyEditorScopes.class, "scopeName");
121 model.put("scopeDatas", mapper.writeValueAsString(scopesData));
122 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
123 JSONObject j = new JSONObject(msg);
124 response.getWriter().write(j.toString());
134 private UserInfo loginId;
136 private ArrayList<String> scope;
141 public void setId(int id) {
144 public UserInfo getLoginId() {
147 public void setLoginId(UserInfo loginId) {
148 this.loginId = loginId;
150 public String getRole() {
153 public void setRole(String role) {
156 public ArrayList<String> getScope() {
159 public void setScope(ArrayList<String> scope) {