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