Rev 1617 | Go to most recent revision | Details | 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; |
||
13 | import pt.estgp.estgweb.web.controllers.utils.AbstractWidgetAjaxController; |
||
14 | import pt.estgp.estgweb.web.form.widgetmodel.WidgetModelForm; |
||
15 | |||
16 | import javax.servlet.http.HttpServletRequest; |
||
17 | import javax.servlet.http.HttpServletResponse; |
||
18 | import java.util.List; |
||
19 | |||
20 | /** |
||
21 | * Created by jorgemachado on 06/01/17. |
||
22 | */ |
||
23 | public class ModelWidgetController extends AbstractWidgetAjaxController |
||
24 | { |
||
25 | |||
26 | public JSONObject findUser(ActionForm form,HttpServletRequest request, HttpServletResponse response) throws JSONException { |
||
27 | |||
28 | WidgetModelForm frm = (WidgetModelForm) form; |
||
29 | 1.5.0/docs/api/java/lang/String.html">String query = frm.getQ(); |
||
30 | |||
31 | JSONObject result = new JSONObject(); |
||
32 | JSONArray usersArray = new JSONArray(); |
||
33 | result.put("results",usersArray); |
||
34 | |||
35 | if(query==null || query.trim().length() == 0) |
||
36 | return result; |
||
37 | |||
38 | AbstractDao.getCurrentSession().beginTransaction(); |
||
39 | |||
40 | result.put("results",usersArray); |
||
41 | List<User> users = DaoFactory.getUserDaoImpl().findUsers(query); |
||
42 | for(User u: users) |
||
43 | { |
||
44 | JSONObject user = new JSONObject(); |
||
45 | user.put("id",u.getId()); |
||
46 | user.put("username",u.getUsername()); |
||
47 | user.put("name",u.getName()); |
||
48 | if(u instanceof SigesUser) |
||
49 | { |
||
50 | user.put("sigesCode",((SigesUser)u).getSigesCode()); |
||
51 | } |
||
52 | if(u instanceof Teacher) |
||
53 | { |
||
54 | user.put("type","teacher"); |
||
55 | } |
||
56 | if(u instanceof Student) |
||
57 | { |
||
58 | user.put("type","student"); |
||
59 | } |
||
60 | usersArray.put(user); |
||
61 | } |
||
62 | AbstractDao.getCurrentSession().getTransaction().commit(); |
||
63 | return result; |
||
64 | } |
||
65 | } |