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.example.web;
35
36 import fr.paris.lutece.plugins.example.business.Project;
37 import fr.paris.lutece.plugins.example.business.ProjectHome;
38 import fr.paris.lutece.plugins.example.service.ProjectCacheService;
39 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
40 import fr.paris.lutece.portal.web.xpages.XPage;
41 import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
42 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
43 import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
44 import fr.paris.lutece.util.url.UrlItem;
45 import fr.paris.lutece.portal.service.message.SiteMessageService;
46 import fr.paris.lutece.portal.service.message.SiteMessage;
47 import fr.paris.lutece.portal.service.message.SiteMessageException;
48 import fr.paris.lutece.portal.web.resource.ExtendableResourcePluginActionManager;
49
50 import java.util.Map;
51 import javax.servlet.http.HttpServletRequest;
52
53
54
55
56 @Controller( xpageName = "project", pageTitleI18nKey = "example.xpage.project.pageTitle", pagePathI18nKey = "example.xpage.project.pagePathLabel" )
57 public class ProjectXPage extends MVCApplication
58 {
59
60 private static final String TEMPLATE_MANAGE_PROJECTS = "/skin/plugins/example/manage_projects.html";
61 private static final String TEMPLATE_CREATE_PROJECT = "/skin/plugins/example/create_project.html";
62 private static final String TEMPLATE_MODIFY_PROJECT = "/skin/plugins/example/modify_project.html";
63 private static final String TEMPLATE_DETAIL_PROJECT = "/skin/plugins/example/detail_project.html";
64
65
66 private static final String JSP_PAGE_PORTAL = "jsp/site/Portal.jsp";
67
68
69 private static final String PARAMETER_ID_PROJECT = "id";
70 private static final String PARAM_ACTION = "action";
71 private static final String PARAM_PAGE = "page";
72
73
74 private static final String MARK_PROJECT_LIST = "project_list";
75 private static final String MARK_PROJECT = "project";
76
77
78 private static final String MESSAGE_CONFIRM_REMOVE_PROJECT = "example.message.confirmRemoveProject";
79
80
81 private static final String VIEW_MANAGE_PROJECTS = "manageProjects";
82 private static final String VIEW_CREATE_PROJECT = "createProject";
83 private static final String VIEW_MODIFY_PROJECT = "modifyProject";
84 private static final String VIEW_DETAIL_PROJECT = "detailsProject";
85
86
87 private static final String ACTION_CREATE_PROJECT = "createProject";
88 private static final String ACTION_MODIFY_PROJECT = "modifyProject";
89 private static final String ACTION_REMOVE_PROJECT = "removeProject";
90 private static final String ACTION_CONFIRM_REMOVE_PROJECT = "confirmRemoveProject";
91
92
93 private static final String INFO_PROJECT_CREATED = "example.info.project.created";
94 private static final String INFO_PROJECT_UPDATED = "example.info.project.updated";
95 private static final String INFO_PROJECT_REMOVED = "example.info.project.removed";
96
97
98 private Project _project;
99
100 @View( value = VIEW_MANAGE_PROJECTS, defaultView = true )
101 public XPage getManageProjects( HttpServletRequest request )
102 {
103 _project = null;
104 Map<String, Object> model = getModel( );
105 model.put( MARK_PROJECT_LIST, ProjectHome.getProjectsList( ) );
106
107 return getXPage( TEMPLATE_MANAGE_PROJECTS, getLocale( request ), model );
108 }
109
110
111
112
113
114
115
116
117 @View( VIEW_CREATE_PROJECT )
118 public XPage getCreateProject( HttpServletRequest request )
119 {
120 _project = ( _project != null ) ? _project : new Project( );
121
122 Map<String, Object> model = getModel( );
123 model.put( MARK_PROJECT, _project );
124
125 return getXPage( TEMPLATE_CREATE_PROJECT, getLocale( request ), model );
126 }
127
128
129
130
131
132
133
134
135 @Action( ACTION_CREATE_PROJECT )
136 public XPage doCreateProject( HttpServletRequest request )
137 {
138 populate( _project, request );
139
140
141 if ( !validateBean( _project, getLocale( request ) ) )
142 {
143 return redirectView( request, VIEW_CREATE_PROJECT );
144 }
145
146 ProjectHome.create( _project );
147 addInfo( INFO_PROJECT_CREATED, getLocale( request ) );
148
149 return redirectView( request, VIEW_MANAGE_PROJECTS );
150 }
151
152
153
154
155
156
157
158
159
160 @Action( ACTION_CONFIRM_REMOVE_PROJECT )
161 public XPage getConfirmRemoveProject( HttpServletRequest request ) throws SiteMessageException
162 {
163 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_PROJECT ) );
164 UrlItem url = new UrlItem( JSP_PAGE_PORTAL );
165 url.addParameter( PARAM_PAGE, MARK_PROJECT );
166 url.addParameter( PARAM_ACTION, ACTION_REMOVE_PROJECT );
167 url.addParameter( PARAMETER_ID_PROJECT, nId );
168
169 SiteMessageService.setMessage( request, MESSAGE_CONFIRM_REMOVE_PROJECT, SiteMessage.TYPE_CONFIRMATION, url.getUrl( ) );
170 return null;
171 }
172
173
174
175
176
177
178
179
180 @Action( ACTION_REMOVE_PROJECT )
181 public XPage doRemoveProject( HttpServletRequest request )
182 {
183 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_PROJECT ) );
184 ProjectHome.remove( nId );
185 addInfo( INFO_PROJECT_REMOVED, getLocale( request ) );
186
187 return redirectView( request, VIEW_MANAGE_PROJECTS );
188 }
189
190
191
192
193
194
195
196
197 @View( VIEW_MODIFY_PROJECT )
198 public XPage getModifyProject( HttpServletRequest request )
199 {
200 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_PROJECT ) );
201
202 if ( _project == null || ( _project.getId( ) != nId ) )
203 {
204 _project = ProjectHome.findByPrimaryKey( nId );
205 }
206
207 Map<String, Object> model = getModel( );
208 model.put( MARK_PROJECT, _project );
209
210 return getXPage( TEMPLATE_MODIFY_PROJECT, getLocale( request ), model );
211 }
212
213
214
215
216
217
218
219
220 @Action( ACTION_MODIFY_PROJECT )
221 public XPage doModifyProject( HttpServletRequest request )
222 {
223 populate( _project, request );
224
225
226 if ( !validateBean( _project, getLocale( request ) ) )
227 {
228 return redirect( request, VIEW_MODIFY_PROJECT, PARAMETER_ID_PROJECT, _project.getId( ) );
229 }
230
231 ProjectHome.update( _project );
232 addInfo( INFO_PROJECT_UPDATED, getLocale( request ) );
233
234 return redirectView( request, VIEW_MANAGE_PROJECTS );
235 }
236
237
238
239
240
241
242
243
244 @View( VIEW_DETAIL_PROJECT )
245 public XPage getDetailProject( HttpServletRequest request )
246 {
247 int nId = Integer.parseInt( request.getParameter( PARAMETER_ID_PROJECT ) );
248
249 if ( _project == null || ( _project.getId( ) != nId ) )
250 {
251 _project = ProjectCacheService.getInstance( ).getResource( String.valueOf( nId ), null );
252 }
253
254 Map<String, Object> model = getModel( );
255 model.put( MARK_PROJECT, _project );
256
257 return getXPage( TEMPLATE_DETAIL_PROJECT, getLocale( request ), model );
258 }
259
260 }