Merge "Added Junits for POLICY-SDK-APP controllers"
[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.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;
48
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;
54
55 @Controller
56 @RequestMapping("/")
57 public class PolicyRolesController extends RestrictedBaseController{
58         
59         private static final Logger LOGGER      = FlexLogger.getLogger(PolicyRolesController.class);
60         
61         @Autowired
62         CommonClassDao commonClassDao;
63         
64         public void setCommonClassDao(CommonClassDao commonClassDao) {
65                 this.commonClassDao = commonClassDao;
66         }
67         
68         List<String> scopelist;
69         
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){
72                 try{
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());
79                 }
80                 catch (Exception e){
81                         LOGGER.error("Exception Occured"+e);
82                 }
83         }
84         
85         @RequestMapping(value={"/save_NonSuperRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
86         public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response){
87                 try{
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++){
95                         if(i == 0){
96                                 scopeName.append(adapter.getScope().get(0));
97                         }else{
98                                 scopeName.append("," + adapter.getScope().get(i));
99                         }       
100                     }
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");
113                 
114                         PrintWriter out = response.getWriter();
115                         String responseString = mapper.writeValueAsString(commonClassDao.getUserRoles());
116                         JSONObject j = new JSONObject("{rolesDatas: " + responseString + "}");
117
118                         out.write(j.toString());
119                 }
120                 catch (Exception e){
121                         LOGGER.error("Exception Occured"+e);
122                 }
123                 return null;
124         }
125         
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){
128                 try{
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());
138                 }
139                 catch (Exception e){
140                         LOGGER.error("Exception Occured"+e);
141                 }
142         }
143 }
144
145 class ReadScopes{
146         private int id;
147         private UserInfo loginId;
148         private String role;
149         private List<String> scope;
150         
151         public int getId() {
152                 return id;
153         }
154         public void setId(int id) {
155                 this.id = id;
156         }
157         public UserInfo getLoginId() {
158                 return loginId;
159         }
160         public void setLoginId(UserInfo loginId) {
161                 this.loginId = loginId;
162         }
163         public String getRole() {
164                 return role;
165         }
166         public void setRole(String role) {
167                 this.role = role;
168         }
169         public List<String> getScope() {
170                 return scope;
171         }
172         public void setScope(List<String> scope) {
173                 this.scope = scope;
174         }
175
176 }