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.participatorybudget.web.campaign;
35
36 import java.sql.Timestamp;
37 import java.util.Calendar;
38 import java.util.List;
39 import java.util.Map;
40
41 import javax.servlet.http.HttpServletRequest;
42
43 import fr.paris.lutece.plugins.participatorybudget.business.campaign.CampaignPhase;
44 import fr.paris.lutece.plugins.participatorybudget.business.campaign.CampaignPhaseHome;
45 import fr.paris.lutece.plugins.participatorybudget.service.campaign.CampaignService;
46 import fr.paris.lutece.portal.service.message.AdminMessage;
47 import fr.paris.lutece.portal.service.message.AdminMessageService;
48 import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
49 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
50 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
51 import fr.paris.lutece.util.url.UrlItem;
52
53
54
55
56 @Controller( controllerJsp = "ManageCampaignPhases.jsp", controllerPath = "jsp/admin/plugins/participatorybudget/campaign/", right = "CAMPAIGN_MANAGEMENT" )
57 public class CampaignPhaseJspBean extends ManageCampaignJspBean
58 {
59
60
61
62
63
64 private static final String TEMPLATE_MANAGE_CAMPAIGNPHASES = "/admin/plugins/participatorybudget/campaign/manage_campaignphases.html";
65 private static final String TEMPLATE_CREATE_CAMPAIGNPHASE = "/admin/plugins/participatorybudget/campaign/create_campaignphase.html";
66 private static final String TEMPLATE_MODIFY_CAMPAIGNPHASE = "/admin/plugins/participatorybudget/campaign/modify_campaignphase.html";
67
68
69 private static final String PARAMETER_ID_CAMPAIGNPHASE = "id";
70
71
72 private static final String PROPERTY_PAGE_TITLE_MANAGE_CAMPAIGNPHASES = "participatorybudget.manage_campaignphases.pageTitle";
73 private static final String PROPERTY_PAGE_TITLE_MODIFY_CAMPAIGNPHASE = "participatorybudget.modify_campaignphase.pageTitle";
74 private static final String PROPERTY_PAGE_TITLE_CREATE_CAMPAIGNPHASE = "participatorybudget.create_campaignphase.pageTitle";
75
76
77 private static final String MARK_CAMPAIGNPHASE_LIST = "campaignphase_list";
78 private static final String MARK_CAMPAIGNPHASE = "campaignphase";
79
80 private static final String JSP_MANAGE_CAMPAIGNPHASES = "jsp/admin/plugins/participatorybudget/campaign/ManageCampaignPhases.jsp";
81
82
83 private static final String MESSAGE_CONFIRM_REMOVE_CAMPAIGNPHASE = "participatorybudget.message.confirmRemoveCampaignPhase";
84 private static final String MESSAGE_CONFIRM_TARGET_CAMPAIGNPHASE = "participatorybudget.message.confirmTargetCampaignPhase";
85
86 private static final String VALIDATION_ATTRIBUTES_PREFIX = "participatorybudget.model.entity.campaignphase.attribute.";
87
88
89 private static final String VIEW_MANAGE_CAMPAIGNPHASES = "manageCampaignPhases";
90 private static final String VIEW_CREATE_CAMPAIGNPHASE = "createCampaignPhase";
91 private static final String VIEW_MODIFY_CAMPAIGNPHASE = "modifyCampaignPhase";
92
93
94 private static final String ACTION_CREATE_CAMPAIGNPHASE = "createCampaignPhase";
95 private static final String ACTION_MODIFY_CAMPAIGNPHASE = "modifyCampaignPhase";
96 private static final String ACTION_REMOVE_CAMPAIGNPHASE = "removeCampaignPhase";
97 private static final String ACTION_TARGET_CAMPAIGNPHASE = "targetCampaignPhase";
98 private static final String ACTION_CONFIRM_REMOVE_CAMPAIGNPHASE = "confirmRemoveCampaignPhase";
99 private static final String ACTION_CONFIRM_TARGET_CAMPAIGNPHASE = "confirmTargetCampaignPhase";
100
101
102 private static final String INFO_CAMPAIGNPHASE_CREATED = "participatorybudget.info.campaignphase.created";
103 private static final String INFO_CAMPAIGNPHASE_UPDATED = "participatorybudget.info.campaignphase.updated";
104 private static final String INFO_CAMPAIGNPHASE_REMOVED = "participatorybudget.info.campaignphase.removed";
105 private static final String INFO_CAMPAIGNPHASE_TARGETED = "participatorybudget.info.campaignphase.targeted";
106
107
108 private CampaignPhase _campaignphase;
109
110
111
112
113
114
115
116
117 @View( value = VIEW_MANAGE_CAMPAIGNPHASES, defaultView = true )
118 public String getManageCampaignPhases( HttpServletRequest request )
119 {
120 _campaignphase = null;
121 List<CampaignPhase> listCampaignPhases = (List<CampaignPhase>) CampaignPhaseHome.getCampaignPhasesList( );
122 Map<String, Object> model = getPaginatedListModel( request, MARK_CAMPAIGNPHASE_LIST, listCampaignPhases, JSP_MANAGE_CAMPAIGNPHASES );
123
124 return getPage( PROPERTY_PAGE_TITLE_MANAGE_CAMPAIGNPHASES, TEMPLATE_MANAGE_CAMPAIGNPHASES, model );
125 }
126
127
128
129
130
131
132
133
134 @View( VIEW_CREATE_CAMPAIGNPHASE )
135 public String getCreateCampaignPhase( HttpServletRequest request )
136 {
137 _campaignphase = ( _campaignphase != null ) ? _campaignphase : new CampaignPhase( );
138
139 Map<String, Object> model = getModel( );
140 model.put( MARK_CAMPAIGNPHASE, _campaignphase );
141
142 return getPage( PROPERTY_PAGE_TITLE_CREATE_CAMPAIGNPHASE, TEMPLATE_CREATE_CAMPAIGNPHASE, model );
143 }
144
145
146
147
148
149
150
151
152 @Action( ACTION_CREATE_CAMPAIGNPHASE )
153 public String doCreateCampaignPhase( HttpServletRequest request )
154 {
155 populate( _campaignphase, request );
156
157
158 if ( !validateBean( _campaignphase, VALIDATION_ATTRIBUTES_PREFIX ) )
159 {
160 return redirectView( request, VIEW_CREATE_CAMPAIGNPHASE );
161 }
162
163 CampaignPhaseHome.create( _campaignphase );
164 addInfo( INFO_CAMPAIGNPHASE_CREATED, getLocale( ) );
165
166 CampaignService.getInstance( ).reset( );
167
168 return redirectView( request, VIEW_MANAGE_CAMPAIGNPHASES );
169 }
170
171
172
173
174
175
176
177
178 @Action( ACTION_CONFIRM_REMOVE_CAMPAIGNPHASE )
179 public String getConfirmRemoveCampaignPhase( HttpServletRequest request )
180 {
181 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNPHASE ) );
182 UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CAMPAIGNPHASE ) );
183 url.addParameter( PARAMETER_ID_CAMPAIGNPHASE, nId );
184
185 String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CAMPAIGNPHASE, url.getUrl( ),
186 AdminMessage.TYPE_CONFIRMATION );
187
188 return redirect( request, strMessageUrl );
189 }
190
191
192
193
194
195
196
197
198 @Action( ACTION_CONFIRM_TARGET_CAMPAIGNPHASE )
199 public String getConfirmTargetCampaignPhase( HttpServletRequest request )
200 {
201 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNPHASE ) );
202 UrlItem url = new UrlItem( getActionUrl( ACTION_TARGET_CAMPAIGNPHASE ) );
203 url.addParameter( PARAMETER_ID_CAMPAIGNPHASE, nId );
204
205 String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_TARGET_CAMPAIGNPHASE, url.getUrl( ),
206 AdminMessage.TYPE_CONFIRMATION );
207
208 return redirect( request, strMessageUrl );
209 }
210
211
212
213
214
215
216
217
218 @Action( ACTION_REMOVE_CAMPAIGNPHASE )
219 public String doRemoveCampaignPhase( HttpServletRequest request )
220 {
221 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNPHASE ) );
222 CampaignPhaseHome.remove( nId );
223 addInfo( INFO_CAMPAIGNPHASE_REMOVED, getLocale( ) );
224
225 CampaignService.getInstance( ).reset( );
226
227 return redirectView( request, VIEW_MANAGE_CAMPAIGNPHASES );
228 }
229
230
231
232
233
234
235
236
237 @Action( ACTION_TARGET_CAMPAIGNPHASE )
238 public String doTargetCampaignPhase( HttpServletRequest request )
239 {
240 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNPHASE ) );
241 CampaignPhase targetedPhase = CampaignPhaseHome.findByPrimaryKey( nId );
242
243 List<CampaignPhase> phases = CampaignPhaseHome.getCampaignPhasesOrderedList( );
244
245
246 int index = 0;
247 for ( index = 0; index < phases.size( ); index++ )
248 {
249 if ( targetedPhase.getId( ) == phases.get( index ).getId( ) )
250 {
251 break;
252 }
253 }
254
255
256 Calendar cal = Calendar.getInstance( );
257 cal.setTimeInMillis( System.currentTimeMillis( ) );
258 cal.add( Calendar.HOUR, -( 2 * 24 ) );
259 cal.add( Calendar.DAY_OF_YEAR, -( index * 7 ) );
260
261
262 for ( CampaignPhase phase : phases )
263 {
264 phase.setStart( new Timestamp( cal.getTime( ).getTime( ) ) );
265
266 cal.add( Calendar.DAY_OF_YEAR, 7 );
267 cal.add( Calendar.SECOND, -1 );
268 phase.setEnd( new Timestamp( cal.getTime( ).getTime( ) ) );
269
270 CampaignPhaseHome.update( phase );
271
272 cal.add( Calendar.SECOND, 1 );
273 }
274
275 addInfo( INFO_CAMPAIGNPHASE_TARGETED, getLocale( ) );
276
277 CampaignService.getInstance( ).reset( );
278
279 return redirectView( request, VIEW_MANAGE_CAMPAIGNPHASES );
280 }
281
282
283
284
285
286
287
288
289 @View( VIEW_MODIFY_CAMPAIGNPHASE )
290 public String getModifyCampaignPhase( HttpServletRequest request )
291 {
292 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGNPHASE ) );
293
294 if ( _campaignphase == null || ( _campaignphase.getId( ) != nId ) )
295 {
296 _campaignphase = CampaignPhaseHome.findByPrimaryKey( nId );
297 }
298
299 Map<String, Object> model = getModel( );
300 model.put( MARK_CAMPAIGNPHASE, _campaignphase );
301
302 return getPage( PROPERTY_PAGE_TITLE_MODIFY_CAMPAIGNPHASE, TEMPLATE_MODIFY_CAMPAIGNPHASE, model );
303 }
304
305
306
307
308
309
310
311
312 @Action( ACTION_MODIFY_CAMPAIGNPHASE )
313 public String doModifyCampaignPhase( HttpServletRequest request )
314 {
315 populate( _campaignphase, request );
316
317
318 if ( !validateBean( _campaignphase, VALIDATION_ATTRIBUTES_PREFIX ) )
319 {
320 return redirect( request, VIEW_MODIFY_CAMPAIGNPHASE, PARAMETER_ID_CAMPAIGNPHASE, _campaignphase.getId( ) );
321 }
322
323 CampaignPhaseHome.update( _campaignphase );
324 addInfo( INFO_CAMPAIGNPHASE_UPDATED, getLocale( ) );
325
326 CampaignService.getInstance( ).reset( );
327
328 return redirectView( request, VIEW_MANAGE_CAMPAIGNPHASES );
329 }
330 }