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
35 package fr.paris.lutece.plugins.campaign.web;
36
37 import fr.paris.lutece.portal.service.message.AdminMessage;
38 import fr.paris.lutece.portal.service.message.AdminMessageService;
39 import fr.paris.lutece.portal.service.security.SecurityTokenService;
40 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
41 import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
42 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
43 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
44 import fr.paris.lutece.util.url.UrlItem;
45
46 import java.util.List;
47 import java.util.Map;
48 import javax.servlet.http.HttpServletRequest;
49 import fr.paris.lutece.plugins.campaign.business.Campaign;
50 import fr.paris.lutece.plugins.campaign.business.CampaignHome;
51
52
53
54
55 @Controller( controllerJsp = "ManageCampaigns.jsp", controllerPath = "jsp/admin/plugins/campaign/", right = "CAMPAIGN_MANAGEMENT" )
56 public class CampaignJspBean extends AbstractManageCampaignsJspBean
57 {
58
59
60
61 private static final long serialVersionUID = 2485209123338980294L;
62
63 private static final String TEMPLATE_MANAGE_CAMPAIGNS = "/admin/plugins/campaign/manage_campaigns.html";
64 private static final String TEMPLATE_CREATE_CAMPAIGN = "/admin/plugins/campaign/create_campaign.html";
65 private static final String TEMPLATE_MODIFY_CAMPAIGN = "/admin/plugins/campaign/modify_campaign.html";
66
67
68 private static final String PARAMETER_ID_CAMPAIGN = "id";
69
70
71 private static final String PROPERTY_PAGE_TITLE_MANAGE_CAMPAIGNS = "campaign.manage_campaigns.pageTitle";
72 private static final String PROPERTY_PAGE_TITLE_MODIFY_CAMPAIGN = "campaign.modify_campaign.pageTitle";
73 private static final String PROPERTY_PAGE_TITLE_CREATE_CAMPAIGN = "campaign.create_campaign.pageTitle";
74
75
76 private static final String MARK_CAMPAIGN_LIST = "campaign_list";
77 private static final String MARK_CAMPAIGN = "campaign";
78
79 private static final String JSP_MANAGE_CAMPAIGNS = "jsp/admin/plugins/campaign/ManageCampaigns.jsp";
80
81
82 private static final String MESSAGE_CONFIRM_REMOVE_CAMPAIGN = "campaign.message.confirmRemoveCampaign";
83
84
85 private static final String VALIDATION_ATTRIBUTES_PREFIX = "campaign.model.entity.campaign.attribute.";
86
87
88 private static final String VIEW_MANAGE_CAMPAIGNS = "manageCampaigns";
89 private static final String VIEW_CREATE_CAMPAIGN = "createCampaign";
90 private static final String VIEW_MODIFY_CAMPAIGN = "modifyCampaign";
91
92
93 private static final String ACTION_CREATE_CAMPAIGN = "createCampaign";
94 private static final String ACTION_MODIFY_CAMPAIGN = "modifyCampaign";
95 private static final String ACTION_REMOVE_CAMPAIGN = "removeCampaign";
96 private static final String ACTION_CONFIRM_REMOVE_CAMPAIGN = "confirmRemoveCampaign";
97
98
99 private static final String INFO_CAMPAIGN_CREATED = "campaign.info.campaign.created";
100 private static final String INFO_CAMPAIGN_UPDATED = "campaign.info.campaign.updated";
101 private static final String INFO_CAMPAIGN_REMOVED = "campaign.info.campaign.removed";
102
103 private static final String ERROR_CAMPAIGN_CODE_ALREADY_USED = "campaign.error.campaign_code.already.used";
104
105
106 private Campaign _campaign;
107
108
109
110
111
112
113
114
115 @View( value = VIEW_MANAGE_CAMPAIGNS, defaultView = true )
116 public String getManageCampaigns( HttpServletRequest request )
117 {
118 _campaign = null;
119 List<Campaign> listCampaigns = CampaignHome.getCampaignsList( );
120 Map<String, Object> model = getPaginatedListModel( request, MARK_CAMPAIGN_LIST, listCampaigns, JSP_MANAGE_CAMPAIGNS );
121
122 return getPage( PROPERTY_PAGE_TITLE_MANAGE_CAMPAIGNS, TEMPLATE_MANAGE_CAMPAIGNS, model );
123 }
124
125
126
127
128
129
130
131
132 @View( VIEW_CREATE_CAMPAIGN )
133 public String getCreateCampaign( HttpServletRequest request )
134 {
135 _campaign = ( _campaign != null ) ? _campaign : new Campaign( );
136
137 Map<String, Object> model = getModel( );
138 model.put( MARK_CAMPAIGN, _campaign );
139 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_CREATE_CAMPAIGN ) );
140
141 return getPage( PROPERTY_PAGE_TITLE_CREATE_CAMPAIGN, TEMPLATE_CREATE_CAMPAIGN, model );
142 }
143
144
145
146
147
148
149
150
151
152 @Action( ACTION_CREATE_CAMPAIGN )
153 public String doCreateCampaign( HttpServletRequest request ) throws AccessDeniedException
154 {
155 populate( _campaign, request, getLocale( ) );
156
157 if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_CREATE_CAMPAIGN ) )
158 {
159 throw new AccessDeniedException( "Invalid security token" );
160 }
161
162
163 if ( !validateBean( _campaign, VALIDATION_ATTRIBUTES_PREFIX ) )
164 {
165 return redirectView( request, VIEW_CREATE_CAMPAIGN );
166 }
167 if ( CampaignHome.findByCampaignCode( _campaign.getCampaignCode( ) ) != null )
168 {
169
170 addError( ERROR_CAMPAIGN_CODE_ALREADY_USED, getLocale( ) );
171 return redirectView( request, VIEW_CREATE_CAMPAIGN );
172 }
173 CampaignHome.create( _campaign );
174 addInfo( INFO_CAMPAIGN_CREATED, getLocale( ) );
175
176 return redirectView( request, VIEW_MANAGE_CAMPAIGNS );
177 }
178
179
180
181
182
183
184
185
186 @Action( ACTION_CONFIRM_REMOVE_CAMPAIGN )
187 public String getConfirmRemoveCampaign( HttpServletRequest request )
188 {
189 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGN ) );
190 UrlItem url = new UrlItem( getActionUrl( ACTION_REMOVE_CAMPAIGN ) );
191 url.addParameter( PARAMETER_ID_CAMPAIGN, nId );
192
193 String strMessageUrl = AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_CAMPAIGN, url.getUrl( ), AdminMessage.TYPE_CONFIRMATION );
194
195 return redirect( request, strMessageUrl );
196 }
197
198
199
200
201
202
203
204
205 @Action( ACTION_REMOVE_CAMPAIGN )
206 public String doRemoveCampaign( HttpServletRequest request )
207 {
208 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGN ) );
209 CampaignHome.remove( nId );
210 addInfo( INFO_CAMPAIGN_REMOVED, getLocale( ) );
211
212 return redirectView( request, VIEW_MANAGE_CAMPAIGNS );
213 }
214
215
216
217
218
219
220
221
222 @View( VIEW_MODIFY_CAMPAIGN )
223 public String getModifyCampaign( HttpServletRequest request )
224 {
225 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_CAMPAIGN ) );
226
227 if ( _campaign == null || ( _campaign.getId( ) != nId ) )
228 {
229 _campaign = CampaignHome.findByPrimaryKey( nId );
230 }
231
232 Map<String, Object> model = getModel( );
233 model.put( MARK_CAMPAIGN, _campaign );
234 model.put( SecurityTokenService.MARK_TOKEN, SecurityTokenService.getInstance( ).getToken( request, ACTION_MODIFY_CAMPAIGN ) );
235
236 return getPage( PROPERTY_PAGE_TITLE_MODIFY_CAMPAIGN, TEMPLATE_MODIFY_CAMPAIGN, model );
237 }
238
239
240
241
242
243
244
245
246
247 @Action( ACTION_MODIFY_CAMPAIGN )
248 public String doModifyCampaign( HttpServletRequest request ) throws AccessDeniedException
249 {
250 populate( _campaign, request, getLocale( ) );
251
252 if ( !SecurityTokenService.getInstance( ).validate( request, ACTION_MODIFY_CAMPAIGN ) )
253 {
254 throw new AccessDeniedException( "Invalid security token" );
255 }
256
257
258 if ( !validateBean( _campaign, VALIDATION_ATTRIBUTES_PREFIX ) )
259 {
260 return redirect( request, VIEW_MODIFY_CAMPAIGN, PARAMETER_ID_CAMPAIGN, _campaign.getId( ) );
261 }
262
263 CampaignHome.update( _campaign );
264 addInfo( INFO_CAMPAIGN_UPDATED, getLocale( ) );
265
266 return redirectView( request, VIEW_MANAGE_CAMPAIGNS );
267 }
268 }