Rev 1704 | Rev 1814 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1616 | jmachado | 1 | package pt.estgp.estgweb.web.controllers.widgetmodel; |
2 | |||
3 | import jomm.dao.impl.AbstractDao; |
||
4 | import org.apache.struts.action.ActionForm; |
||
5 | import org.json.JSONArray; |
||
6 | import org.json.JSONException; |
||
7 | import org.json.JSONObject; |
||
8 | import pt.estgp.estgweb.domain.SigesUser; |
||
9 | import pt.estgp.estgweb.domain.Student; |
||
10 | import pt.estgp.estgweb.domain.Teacher; |
||
11 | import pt.estgp.estgweb.domain.User; |
||
12 | import pt.estgp.estgweb.domain.dao.DaoFactory; |
||
1704 | jmachado | 13 | import pt.estgp.estgweb.domain.views.UserView; |
1730 | jmachado | 14 | import pt.estgp.estgweb.filters.exceptions.NotFoundException; |
1616 | jmachado | 15 | import pt.estgp.estgweb.web.controllers.utils.AbstractWidgetAjaxController; |
16 | import pt.estgp.estgweb.web.form.widgetmodel.WidgetModelForm; |
||
1617 | jmachado | 17 | import pt.estgp.estgweb.web.utils.RequestUtils; |
18 | import pt.utl.ist.berserk.logic.serviceManager.IServiceManager; |
||
19 | import pt.utl.ist.berserk.logic.serviceManager.ServiceManager; |
||
1616 | jmachado | 20 | |
21 | import javax.servlet.http.HttpServletRequest; |
||
22 | import javax.servlet.http.HttpServletResponse; |
||
23 | import java.util.List; |
||
24 | |||
25 | /** |
||
26 | * Created by jorgemachado on 06/01/17. |
||
27 | */ |
||
28 | public class ModelWidgetController extends AbstractWidgetAjaxController |
||
29 | { |
||
30 | |||
31 | public JSONObject findUser(ActionForm form,HttpServletRequest request, HttpServletResponse response) throws JSONException { |
||
32 | |||
33 | WidgetModelForm frm = (WidgetModelForm) form; |
||
34 | 1.5.0/docs/api/java/lang/String.html">String query = frm.getQ(); |
||
35 | |||
36 | JSONObject result = new JSONObject(); |
||
37 | JSONArray usersArray = new JSONArray(); |
||
38 | result.put("results",usersArray); |
||
39 | |||
40 | if(query==null || query.trim().length() == 0) |
||
41 | return result; |
||
42 | |||
43 | AbstractDao.getCurrentSession().beginTransaction(); |
||
44 | |||
1620 | jmachado | 45 | //result.put("results",usersArray); |
1616 | jmachado | 46 | List<User> users = DaoFactory.getUserDaoImpl().findUsers(query); |
1617 | jmachado | 47 | getUsersInJson(usersArray, users); |
48 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
49 | return result; |
||
50 | } |
||
51 | |||
1704 | jmachado | 52 | |
53 | |||
54 | |||
1730 | jmachado | 55 | public JSONObject getUserForUsername(ActionForm form,HttpServletRequest request, HttpServletResponse response) throws JSONException, NotFoundException { |
1617 | jmachado | 56 | |
1620 | jmachado | 57 | WidgetModelForm frm = (WidgetModelForm) form; |
58 | 1.5.0/docs/api/java/lang/String.html">String username = frm.getQ(); |
||
59 | |||
60 | JSONObject result; |
||
61 | |||
62 | AbstractDao.getCurrentSession().beginTransaction(); |
||
63 | |||
64 | // |
||
65 | User user = DaoFactory.getUserDaoImpl().loadByUsername(username); |
||
1730 | jmachado | 66 | if(user != null) |
67 | result = getJsonUser(user); |
||
68 | else |
||
69 | throw new NotFoundException("Username: " + username + " not found"); |
||
1620 | jmachado | 70 | AbstractDao.getCurrentSession().getTransaction().commit(); |
71 | return result; |
||
72 | } |
||
73 | |||
74 | |||
75 | |||
76 | |||
1617 | jmachado | 77 | public JSONObject findUserRoles(ActionForm form,HttpServletRequest request, HttpServletResponse response) throws JSONException { |
78 | |||
79 | WidgetModelForm frm = (WidgetModelForm) form; |
||
80 | 1.5.0/docs/api/java/lang/String.html">String query = frm.getQ(); |
||
81 | |||
82 | JSONObject result = new JSONObject(); |
||
83 | JSONArray usersArray = new JSONArray(); |
||
84 | result.put("results",usersArray); |
||
85 | |||
86 | if(query==null || query.trim().length() == 0) |
||
87 | return result; |
||
88 | |||
89 | AbstractDao.getCurrentSession().beginTransaction(); |
||
90 | |||
91 | result.put("results",usersArray); |
||
92 | List<User> users = DaoFactory.getUserDaoImpl().loadRoleUsers(query); |
||
93 | getUsersInJson(usersArray, users); |
||
94 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
95 | return result; |
||
96 | } |
||
97 | |||
98 | private void getUsersInJson(JSONArray usersArray, List<User> users) throws JSONException { |
||
1616 | jmachado | 99 | for(User u: users) |
100 | { |
||
1620 | jmachado | 101 | JSONObject user = getJsonUser(u); |
1616 | jmachado | 102 | usersArray.put(user); |
103 | } |
||
104 | } |
||
1617 | jmachado | 105 | |
1667 | jmachado | 106 | public static JSONObject getJsonUser(User u) throws JSONException { |
1620 | jmachado | 107 | JSONObject user = new JSONObject(); |
108 | user.put("id",u.getId()); |
||
109 | user.put("username",u.getUsername()); |
||
110 | user.put("name",u.getName()); |
||
111 | if(u instanceof SigesUser) |
||
112 | { |
||
113 | user.put("sigesCode",((SigesUser)u).getSigesCode()); |
||
114 | } |
||
115 | if(u instanceof Teacher) |
||
116 | { |
||
117 | user.put("type","teacher"); |
||
118 | } |
||
119 | if(u instanceof Student) |
||
120 | { |
||
121 | user.put("type","student"); |
||
122 | } |
||
123 | return user; |
||
124 | } |
||
1617 | jmachado | 125 | |
1620 | jmachado | 126 | |
1617 | jmachado | 127 | /** |
128 | * q json object: |
||
129 | * { |
||
130 | * userId:Long, |
||
131 | * role:String, |
||
132 | * } |
||
133 | * Adds a role to a user |
||
134 | * |
||
135 | * @param form |
||
136 | * @param request |
||
137 | * @param response |
||
138 | * @return Users in Json |
||
139 | * @throws JSONException |
||
140 | */ |
||
141 | public JSONObject addRole2User(ActionForm form,HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable { |
||
142 | |||
143 | WidgetModelForm frm = (WidgetModelForm) form; |
||
144 | 1.5.0/docs/api/java/lang/String.html">String query = frm.getQ(); |
||
145 | JSONObject roleUser = new JSONObject(query); |
||
146 | 1.5.0/docs/api/java/lang/Long.html">Long userId = roleUser.getLong("userId"); |
||
147 | 1.5.0/docs/api/java/lang/String.html">String role = roleUser.getString("role"); |
||
148 | |||
149 | IServiceManager sm = ServiceManager.getInstance(); |
||
150 | 1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"userId","role_2_addRemove"}; //colocacao do nome de parametro role_2_add obrigatoria para validacao no filtro do serviço |
||
151 | 5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{userId,role}; |
||
152 | sm.execute(RequestUtils.getRequester(request, response), "AddRole2User", args, names); |
||
153 | |||
154 | frm.setQ(role); |
||
155 | addMessageAsString(request,"Utilizador adicionado com sucesso ao papel institucional"); |
||
156 | return findUserRoles(frm,request,response); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * q json object: |
||
161 | * { |
||
162 | * userId:Long, |
||
163 | * role:String, |
||
164 | * } |
||
165 | * Adds a role to a user |
||
166 | * |
||
167 | * @param form |
||
168 | * @param request |
||
169 | * @param response |
||
170 | * @return Users in Json |
||
171 | * @throws JSONException |
||
172 | */ |
||
173 | public JSONObject removeRole2User(ActionForm form,HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable { |
||
174 | |||
175 | WidgetModelForm frm = (WidgetModelForm) form; |
||
176 | 1.5.0/docs/api/java/lang/String.html">String query = frm.getQ(); |
||
177 | JSONObject roleUser = new JSONObject(query); |
||
178 | 1.5.0/docs/api/java/lang/Long.html">Long userId = roleUser.getLong("userId"); |
||
179 | 1.5.0/docs/api/java/lang/String.html">String role = roleUser.getString("role"); |
||
180 | |||
181 | IServiceManager sm = ServiceManager.getInstance(); |
||
182 | 1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{"userId","role_2_addRemove"}; //colocacao do nome de parametro role_2_add obrigatoria para validacao no filtro do serviço |
||
183 | 5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{userId,role}; |
||
184 | sm.execute(RequestUtils.getRequester(request, response), "RemoveRole2User", args, names); |
||
185 | |||
186 | frm.setQ(role); |
||
187 | addMessageAsString(request,"Utilizador removido com sucesso do papel institucional"); |
||
188 | return findUserRoles(frm,request,response); |
||
189 | } |
||
1704 | jmachado | 190 | |
191 | |||
192 | |||
193 | |||
194 | /** |
||
195 | * For Json Services |
||
196 | * @param form |
||
197 | * @param request |
||
198 | * @param response |
||
199 | * @return |
||
200 | * @throws Throwable |
||
201 | */ |
||
202 | public boolean wsLoginMobileChallenge(ActionForm form,HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable { |
||
203 | |||
204 | 1.5.0/docs/api/java/lang/String.html">String username = request.getParameter("username"); |
||
205 | 1.5.0/docs/api/java/lang/String.html">String chaveAppsMoveis = request.getParameter("chaveAppsMoveis"); |
||
206 | IServiceManager sm = ServiceManager.getInstance(); |
||
207 | 1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{}; |
||
208 | 5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{username,chaveAppsMoveis}; |
||
209 | UserView u = (UserView) sm.execute(RequestUtils.getRequester(request, response), "WSAPILoginChallenge", args, names); |
||
210 | return u != null; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * * For Json Services |
||
215 | * @param form |
||
216 | * @param request |
||
217 | * @param response |
||
218 | * @return |
||
219 | * @throws Throwable |
||
220 | */ |
||
221 | public JSONObject wsListStudents(ActionForm form,HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable { |
||
222 | |||
223 | IServiceManager sm = ServiceManager.getInstance(); |
||
224 | 1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{}; |
||
225 | 5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{}; |
||
226 | JSONObject result = (JSONObject) sm.execute(RequestUtils.getRequester(request, response), "WSAPIListStudents", args, names); |
||
227 | return result; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * * For Json Services |
||
232 | * @param form |
||
233 | * @param request |
||
234 | * @param response |
||
235 | * @return |
||
236 | * @throws Throwable |
||
237 | */ |
||
238 | public JSONObject wsListTeachers(ActionForm form,HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable { |
||
239 | |||
240 | IServiceManager sm = ServiceManager.getInstance(); |
||
241 | 1.5.0/docs/api/java/lang/String.html">String[] names = new 1.5.0/docs/api/java/lang/String.html">String[]{}; |
||
242 | 5+0%2Fdocs%2Fapi+Object">Object[] args = new 5+0%2Fdocs%2Fapi+Object">Object[]{}; |
||
243 | JSONObject result = (JSONObject) sm.execute(RequestUtils.getRequester(request, response), "WSAPIListTeachers", args, names); |
||
244 | return result; |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * * For Json Services |
||
249 | * @param form |
||
250 | * @param request |
||
251 | * @param response |
||
252 | * @return |
||
253 | * @throws Throwable |
||
254 | */ |
||
255 | public JSONObject wsListServices(ActionForm form,HttpServletRequest request, HttpServletResponse response) throws 1.5.0/docs/api/java/lang/Throwable.html">Throwable { |
||
256 | |||
257 | JSONObject result = new JSONObject(); |
||
258 | JSONObject wsServices = new JSONObject(); |
||
259 | result.put("ws-services",wsServices); |
||
260 | wsServices.put("info-Developers","Precisa de uma chave de API fornecida pelos administradores de sistema, com a chave poderá autênticar-se com o serviço ws-authenticate cada vez que a sua APP comunicar"); |
||
261 | wsServices.put("info-Users","O utilizador final necessita de ser encaminhado via WEB para gerar a sua chaveAppsMoveis entrando no PAE/BACO no seguinte URL /startGenerateChaveApps.do"); |
||
262 | |||
263 | JSONObject wsAuthenticate = new JSONObject(); |
||
264 | wsAuthenticate.put("service","/wsjson/api/app/ws-authenticate"); |
||
265 | wsAuthenticate.put("parameter1","apikey"); |
||
266 | wsAuthenticate.put("return1","certificate: este serviço devolve a chave de comunicação seguinte que deverá ser usada como certificado"); |
||
267 | wsAuthenticate.put("return2","certificateExpire: este serviço devolve a data em que o seu certificado de API expira"); |
||
268 | |||
269 | JSONObject wsLoginChallenge = new JSONObject(); |
||
270 | wsLoginChallenge.put("service","/wsjson/api/app/secure/ws-login-challenge"); |
||
271 | wsLoginChallenge.put("parameter1","certificate"); |
||
272 | wsLoginChallenge.put("parameter2","username"); |
||
273 | wsLoginChallenge.put("parameter3","chaveAppsMoveis"); |
||
274 | wsLoginChallenge.put("descricao","usa o certificado para garantir autenticidade da APP e verifica se o utilizador e a chave fornecida pelo mesmo estão correctas"); |
||
275 | wsLoginChallenge.put("return1","se estiver correcto devolve -> service-simple-response\",\"ok\");"); |
||
276 | wsLoginChallenge.put("return2","se estiver incorreto devolve -> service-simple-response\",\"fail\");"); |
||
277 | |||
278 | JSONObject wsLoginListStudents = new JSONObject(); |
||
279 | wsLoginListStudents.put("service","/wsjson/api/app/secure/ws-student-list"); |
||
280 | wsLoginListStudents.put("parameter1","certificate"); |
||
281 | wsLoginListStudents.put("descricao","usa o certificado para garantir autenticidade da APP e devolve a lista total de estudantes do sistema no parametro response"); |
||
282 | wsLoginListStudents.put("return1","lista total de estudantes do sistema no parametro response"); |
||
283 | |||
284 | |||
285 | JSONObject wsLoginListTeachers = new JSONObject(); |
||
286 | wsLoginListTeachers.put("service","/wsjson/api/app/secure/ws-teacher-list"); |
||
287 | wsLoginListTeachers.put("parameter","certificate"); |
||
288 | wsLoginListTeachers.put("descricao","usa o certificado para garantir autenticidade da APP e devolve a lista total de professores do sistema no parametro response"); |
||
289 | wsLoginListTeachers.put("return1","devolve a lista total de professores do sistema no parametro response"); |
||
290 | |||
291 | JSONArray services = new JSONArray(); |
||
292 | services.put(wsAuthenticate); |
||
293 | services.put(wsLoginChallenge); |
||
294 | services.put(wsLoginListStudents); |
||
295 | services.put(wsLoginListTeachers); |
||
296 | wsServices.put("services",services); |
||
297 | |||
298 | return result; |
||
299 | } |
||
1616 | jmachado | 300 | } |