1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.parisconnect.web;
35
36 import fr.paris.lutece.plugins.parisconnect.business.Alert;
37 import fr.paris.lutece.plugins.parisconnect.business.AlertHome;
38 import fr.paris.lutece.plugins.parisconnect.business.Category;
39 import fr.paris.lutece.plugins.parisconnect.business.CategoryHome;
40 import fr.paris.lutece.plugins.parisconnect.business.Newsletter;
41 import fr.paris.lutece.plugins.parisconnect.service.ParisConnectService;
42 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
43 import fr.paris.lutece.portal.web.xpages.XPage;
44 import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
45 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
46 import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
47 import fr.paris.lutece.util.ReferenceList;
48 import fr.paris.lutece.util.url.UrlItem;
49 import fr.paris.lutece.portal.service.message.SiteMessageService;
50 import fr.paris.lutece.portal.service.message.SiteMessage;
51 import fr.paris.lutece.portal.service.message.SiteMessageException;
52 import fr.paris.lutece.portal.service.security.LuteceUser;
53 import fr.paris.lutece.portal.service.security.SecurityService;
54 import fr.paris.lutece.portal.service.security.UserNotSignedException;
55
56 import java.util.ArrayList;
57 import java.util.HashMap;
58 import java.util.List;
59 import java.util.Map;
60 import javax.servlet.http.HttpServletRequest;
61
62 import org.apache.commons.lang.StringUtils;
63
64
65
66
67 @Controller( xpageName = "alert" , pageTitleI18nKey = "parisconnect.xpage.alert.pageTitle" , pagePathI18nKey = "parisconnect.xpage.alert.pagePathLabel" )
68 public class AlertXPage extends MVCApplication
69 {
70
71 private static final String TEMPLATE_MANAGE_ALERTS="/skin/plugins/parisconnect/manage_alerts.html";
72
73
74
75 private static final String PARAMETER_ID_ALERT="id";
76 private static final String KEY_NO_CATEGORY = "no_category";
77
78
79
80 private static final String MARK_ALERT_LIST = "alert_list";
81 private static final String MARK_USER_ALERT_HASH = "user_alert_hash";
82 private static final String MARK_CATEGORY_LIST = "category_list";
83 private static final String MARK_CATEGORY_MAP = "category_map";
84 private static final String MARK_CATEGORY_LIST_ALERT_HASH = "category_list_alert_hash";
85
86
87
88
89
90 private static final String VIEW_MANAGE_ALERTS = "manageAlerts";
91
92
93
94 private static final String ACTION_SUBSCRIBE_ALERT = "subscribeAlert";
95 private static final String ACTION_UNSUBSCRIBE_ALERT = "unSubscribeAlert";
96
97
98
99
100 @View( value = VIEW_MANAGE_ALERTS, defaultView = true )
101 public XPage getManageAlerts( HttpServletRequest request )
102 {
103
104 Map<String, Object> model = getModel( );
105
106 LuteceUser user = SecurityService.getInstance( ).getRegisteredUser(request);
107
108
109
110 HashMap<String,Alert> userAlertMap=new HashMap<>();
111
112 if(user!=null)
113 {
114 List<Alert> listUserAlert=ParisConnectService.getInstance().getUserAlerts(user.getName());
115
116
117 if(listUserAlert != null)
118 {
119 for(Alert alertUser:listUserAlert)
120 {
121 userAlertMap.put(alertUser.getIdPc(),alertUser);
122 }
123 }
124 }
125
126 List<Alert> listAlert=ParisConnectService.getInstance().getAlerts();
127 HashMap<String ,List<Alert>> mapCategoryListAlert=new HashMap<>();
128 mapCategoryListAlert.put(KEY_NO_CATEGORY, new ArrayList<Alert>());
129
130 List<Category> listCategory=CategoryHome.getCategorysList();
131
132
133 if(listCategory!=null && listCategory.size() > 0)
134 {
135 HashMap<String, Category> mapCategory=new HashMap<>();
136 for(Category category:listCategory)
137 {
138 mapCategory.put(category.getCode(), category);
139 }
140 model.put( MARK_CATEGORY_MAP,mapCategory);
141 }
142
143 for(Alert alert:listAlert)
144 {
145
146 if(alert.getCategory()!=null)
147 {
148 if(!mapCategoryListAlert.containsKey(alert.getCategory()))
149 {
150 mapCategoryListAlert.put(alert.getCategory(), new ArrayList<Alert>());
151 }
152 mapCategoryListAlert.get(alert.getCategory()).add(alert);
153 }
154 else
155 {
156 mapCategoryListAlert.get(KEY_NO_CATEGORY).add(alert);
157 }
158
159 }
160
161
162
163
164 model.put( MARK_USER_ALERT_HASH, userAlertMap);
165 model.put( MARK_ALERT_LIST, listAlert);
166 model.put( MARK_CATEGORY_LIST,listCategory );
167 model.put(MARK_CATEGORY_LIST_ALERT_HASH,mapCategoryListAlert);
168
169
170 return getXPage( TEMPLATE_MANAGE_ALERTS, request.getLocale( ), model );
171 }
172
173
174
175
176
177
178
179
180
181 @Action( ACTION_SUBSCRIBE_ALERT )
182 public XPage doSubscribe( HttpServletRequest request ) throws UserNotSignedException
183 {
184 LuteceUser user = SecurityService.getInstance( ).getRemoteUser(request);
185
186
187 if(user==null)
188 {
189 throw new UserNotSignedException( );
190 }
191 String strIdAlert= request.getParameter( PARAMETER_ID_ALERT) ;
192 if(!StringUtils.isEmpty(strIdAlert))
193 {
194 ParisConnectService.getInstance().subscribeAlert(user.getName(), strIdAlert);
195
196 }
197
198 return redirectView( request, VIEW_MANAGE_ALERTS );
199 }
200
201
202
203
204
205
206
207 @Action( ACTION_UNSUBSCRIBE_ALERT )
208 public XPage doUnSubscribe( HttpServletRequest request ) throws UserNotSignedException
209 {
210 LuteceUser user = SecurityService.getInstance( ).getRemoteUser(request);
211
212
213 if(user==null)
214 {
215 throw new UserNotSignedException( );
216 }
217 String strIdAlert= request.getParameter( PARAMETER_ID_ALERT) ;
218 if(!StringUtils.isEmpty(strIdAlert))
219 {
220 ParisConnectService.getInstance().unSubscribeAlert(user.getName(), strIdAlert);
221
222 }
223
224 return redirectView( request, VIEW_MANAGE_ALERTS );
225 }
226
227
228 }