[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / PolicyRolesController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.controller;
22
23
24 import java.io.PrintWriter;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
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.openecomp.portalsdk.core.controller.RestrictedBaseController;
41 import org.openecomp.portalsdk.core.web.support.JsonMessage;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.http.MediaType;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.servlet.ModelAndView;
47
48 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
49 import com.fasterxml.jackson.annotation.PropertyAccessor;
50 import com.fasterxml.jackson.databind.DeserializationFeature;
51 import com.fasterxml.jackson.databind.JsonNode;
52 import com.fasterxml.jackson.databind.ObjectMapper;
53
54 @Controller
55 @RequestMapping("/")
56 public class PolicyRolesController extends RestrictedBaseController{
57         
58         private static final Logger LOGGER      = FlexLogger.getLogger(PolicyRolesController.class);
59         
60         @Autowired
61         CommonClassDao commonClassDao;
62         
63         List<String> scopelist;
64         
65         @RequestMapping(value={"/get_RolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
66         public void getPolicyRolesEntityData(HttpServletRequest request, HttpServletResponse response){
67                 try{
68                         Map<String, Object> model = new HashMap<>();
69                         ObjectMapper mapper = new ObjectMapper();
70                         model.put("rolesDatas", mapper.writeValueAsString(commonClassDao.getUserRoles()));
71                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
72                         JSONObject j = new JSONObject(msg);
73                         response.getWriter().write(j.toString());
74                 }
75                 catch (Exception e){
76                         LOGGER.error("Exception Occured"+e);
77                 }
78         }
79         
80         @RequestMapping(value={"/save_NonSuperRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
81         public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response){
82                 try{
83                         String scopeName = null;
84                         ObjectMapper mapper = new ObjectMapper();
85                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
86                     JsonNode root = mapper.readTree(request.getReader());
87                     ReadScopes adapter = mapper.readValue(root.get("editRoleData").toString(), ReadScopes.class);
88                     for(int i = 0; i < adapter.getScope().size(); i++){
89                         if(i == 0){
90                                 scopeName       =       adapter.getScope().get(0);
91                         }else{
92                                 scopeName       =       scopeName + "," + adapter.getScope().get(i);
93                         }       
94                     }
95                     PolicyRoles roles = new PolicyRoles();
96                     roles.setId(adapter.getId());
97                     roles.setLoginId(adapter.getLoginId());
98                     roles.setRole(adapter.getRole());
99                     roles.setScope(scopeName);
100                     commonClassDao.update(roles);
101                     response.setCharacterEncoding("UTF-8");
102                         response.setContentType("application / json");
103                         request.setCharacterEncoding("UTF-8");
104                 
105                         PrintWriter out = response.getWriter();
106                         String responseString = mapper.writeValueAsString(commonClassDao.getUserRoles());
107                         JSONObject j = new JSONObject("{rolesDatas: " + responseString + "}");
108
109                         out.write(j.toString());
110                 }
111                 catch (Exception e){
112                         LOGGER.error("Exception Occured"+e);
113                 }
114                 return null;
115         }
116         
117         @RequestMapping(value={"/get_PolicyRolesScopeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
118         public void getPolicyScopesEntityData(HttpServletRequest request, HttpServletResponse response){
119                 try{
120                         scopelist = new ArrayList<>();
121                         Map<String, Object> model = new HashMap<>();
122                         ObjectMapper mapper = new ObjectMapper();
123                         mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
124                         List<String> scopesData = commonClassDao.getDataByColumn(PolicyEditorScopes.class, "scopeName");
125                         model.put("scopeDatas", mapper.writeValueAsString(scopesData));
126                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
127                         JSONObject j = new JSONObject(msg);
128                         response.getWriter().write(j.toString());
129                 }
130                 catch (Exception e){
131                         LOGGER.error("Exception Occured"+e);
132                 }
133         }
134 }
135
136 class ReadScopes{
137         private int id;
138         private UserInfo loginId;
139         private String role;
140         private ArrayList<String> scope;
141         
142         public int getId() {
143                 return id;
144         }
145         public void setId(int id) {
146                 this.id = id;
147         }
148         public UserInfo getLoginId() {
149                 return loginId;
150         }
151         public void setLoginId(UserInfo loginId) {
152                 this.loginId = loginId;
153         }
154         public String getRole() {
155                 return role;
156         }
157         public void setRole(String role) {
158                 this.role = role;
159         }
160         public ArrayList<String> getScope() {
161                 return scope;
162         }
163         public void setScope(ArrayList<String> scope) {
164                 this.scope = scope;
165         }
166
167 }