Merge "Fix audit log to contain ending timestamp"
[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         List<String> scopelist;
65         
66         @RequestMapping(value={"/get_RolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
67         public void getPolicyRolesEntityData(HttpServletRequest request, HttpServletResponse response){
68                 try{
69                         Map<String, Object> model = new HashMap<>();
70                         ObjectMapper mapper = new ObjectMapper();
71                         model.put("rolesDatas", mapper.writeValueAsString(commonClassDao.getUserRoles()));
72                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
73                         JSONObject j = new JSONObject(msg);
74                         response.getWriter().write(j.toString());
75                 }
76                 catch (Exception e){
77                         LOGGER.error("Exception Occured"+e);
78                 }
79         }
80         
81         @RequestMapping(value={"/save_NonSuperRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
82         public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response){
83                 try{
84                         String scopeName = null;
85                         ObjectMapper mapper = new ObjectMapper();
86                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
87                         String userId = UserUtils.getUserSession(request).getOrgUserId();
88                     JsonNode root = mapper.readTree(request.getReader());
89                     ReadScopes adapter = mapper.readValue(root.get("editRoleData").toString(), ReadScopes.class);
90                     for(int i = 0; i < adapter.getScope().size(); i++){
91                         if(i == 0){
92                                 scopeName       =       adapter.getScope().get(0);
93                         }else{
94                                 scopeName       =       scopeName + "," + adapter.getScope().get(i);
95                         }       
96                     }
97                     LOGGER.info("****************************************Logging UserID for Roles Function********************************************************");
98                         LOGGER.info("UserId:  " + userId + "Updating the Scope for following user" + adapter.getLoginId() + "ScopeNames" + adapter.getScope());
99                         LOGGER.info("*********************************************************************************************************************************");
100                     PolicyRoles roles = new PolicyRoles();
101                     roles.setId(adapter.getId());
102                     roles.setLoginId(adapter.getLoginId());
103                     roles.setRole(adapter.getRole());
104                     roles.setScope(scopeName);
105                     commonClassDao.update(roles);
106                     response.setCharacterEncoding("UTF-8");
107                         response.setContentType("application / json");
108                         request.setCharacterEncoding("UTF-8");
109                 
110                         PrintWriter out = response.getWriter();
111                         String responseString = mapper.writeValueAsString(commonClassDao.getUserRoles());
112                         JSONObject j = new JSONObject("{rolesDatas: " + responseString + "}");
113
114                         out.write(j.toString());
115                 }
116                 catch (Exception e){
117                         LOGGER.error("Exception Occured"+e);
118                 }
119                 return null;
120         }
121         
122         @RequestMapping(value={"/get_PolicyRolesScopeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
123         public void getPolicyScopesEntityData(HttpServletRequest request, HttpServletResponse response){
124                 try{
125                         scopelist = new ArrayList<>();
126                         Map<String, Object> model = new HashMap<>();
127                         ObjectMapper mapper = new ObjectMapper();
128                         mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
129                         List<String> scopesData = commonClassDao.getDataByColumn(PolicyEditorScopes.class, "scopeName");
130                         model.put("scopeDatas", mapper.writeValueAsString(scopesData));
131                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
132                         JSONObject j = new JSONObject(msg);
133                         response.getWriter().write(j.toString());
134                 }
135                 catch (Exception e){
136                         LOGGER.error("Exception Occured"+e);
137                 }
138         }
139 }
140
141 class ReadScopes{
142         private int id;
143         private UserInfo loginId;
144         private String role;
145         private ArrayList<String> scope;
146         
147         public int getId() {
148                 return id;
149         }
150         public void setId(int id) {
151                 this.id = id;
152         }
153         public UserInfo getLoginId() {
154                 return loginId;
155         }
156         public void setLoginId(UserInfo loginId) {
157                 this.loginId = loginId;
158         }
159         public String getRole() {
160                 return role;
161         }
162         public void setRole(String role) {
163                 this.role = role;
164         }
165         public ArrayList<String> getScope() {
166                 return scope;
167         }
168         public void setScope(ArrayList<String> scope) {
169                 this.scope = scope;
170         }
171
172 }