Policy 1707 commit to LF
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / controller / PolicyRolesController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP 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.openecomp.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.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;
45
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;
51
52 @Controller
53 @RequestMapping("/")
54 public class PolicyRolesController extends RestrictedBaseController{
55         
56         @Autowired
57         CommonClassDao commonClassDao;
58         
59         List<String> scopelist;
60         
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){
63                 try{
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());
70                 }
71                 catch (Exception e){
72                         e.printStackTrace();
73                 }
74         }
75         
76         @RequestMapping(value={"/save_NonSuperRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
77         public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response){
78                 try{
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++){
85                         if(i == 0){
86                                 scopeName       =       adapter.getScope().get(0);
87                         }else{
88                                 scopeName       =       scopeName + "," + adapter.getScope().get(i);
89                         }       
90                     }
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");
100                 
101                         PrintWriter out = response.getWriter();
102                         String responseString = mapper.writeValueAsString(commonClassDao.getUserRoles());
103                         JSONObject j = new JSONObject("{rolesDatas: " + responseString + "}");
104
105                         out.write(j.toString());
106                 }
107                 catch (Exception e){
108                         e.printStackTrace();
109                 }
110                 return null;
111         }
112         
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){
115                 try{
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());
125                 }
126                 catch (Exception e){
127                         e.printStackTrace();
128                 }
129         }
130 }
131
132 class ReadScopes{
133         private int id;
134         private UserInfo loginId;
135         private String role;
136         private ArrayList<String> scope;
137         
138         public int getId() {
139                 return id;
140         }
141         public void setId(int id) {
142                 this.id = id;
143         }
144         public UserInfo getLoginId() {
145                 return loginId;
146         }
147         public void setLoginId(UserInfo loginId) {
148                 this.loginId = loginId;
149         }
150         public String getRole() {
151                 return role;
152         }
153         public void setRole(String role) {
154                 this.role = role;
155         }
156         public ArrayList<String> getScope() {
157                 return scope;
158         }
159         public void setScope(ArrayList<String> scope) {
160                 this.scope = scope;
161         }
162
163 }