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.resource.web;
35
36 import fr.paris.lutece.plugins.resource.business.database.DatabaseResourceType;
37 import fr.paris.lutece.plugins.resource.business.database.DatabaseResourceTypeHome;
38 import fr.paris.lutece.plugins.resource.service.listeners.DatabaseResourceTypeRemovalManager;
39 import fr.paris.lutece.portal.service.message.AdminMessage;
40 import fr.paris.lutece.portal.service.message.AdminMessageService;
41 import fr.paris.lutece.portal.service.util.AppPropertiesService;
42 import fr.paris.lutece.portal.util.mvc.admin.MVCAdminJspBean;
43 import fr.paris.lutece.portal.util.mvc.admin.annotations.Controller;
44 import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
45 import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
46 import fr.paris.lutece.portal.web.util.LocalizedPaginator;
47 import fr.paris.lutece.util.html.Paginator;
48 import fr.paris.lutece.util.url.UrlItem;
49
50 import org.apache.commons.lang3.StringUtils;
51
52 import java.util.Map;
53
54 import javax.servlet.http.HttpServletRequest;
55
56
57
58
59 @Controller( controllerJsp = "ManageResourceTypes.jsp", controllerPath = "jsp/admin/plugins/resource/", right = ResourceJspBean.RIGHT_MANAGE_RESOURCES )
60 public class ResourceTypeJspBean extends MVCAdminJspBean
61 {
62 private static final long serialVersionUID = -5823475836167733365L;
63
64
65 private static final String VIEW_MANAGE_RESOURCE_TYPE = "viewManageResourceType";
66 private static final String VIEW_CREATE_RESOURCE_TYPE = "viewCreateResourceType";
67 private static final String VIEW_MODIFY_RESOURCE_TYPE = "viewModifyResourceType";
68 private static final String VIEW_REMOVE_RESOURCE_TYPE = "viewConfirmRemoveResourceType";
69
70
71 private static final String ACTION_DO_CREATE_RESOURCE_TYPE = "doCreateResourceType";
72 private static final String ACTION_DO_MODIFY_RESOURCE_TYPE = "doModifyResourceType";
73 private static final String ACTION_DO_REMOVE_RESOURCE_TYPE = "doRemoveResourceType";
74
75
76 private static final String MESSAGE_RESOURCE_TYPE_MANAGEMENT_PAGE_TITLE = "resource.resourceTypeManagement.pageTitle";
77 private static final String MESSAGE_CREATE_RESOURCE_TYPE_PAGE_TITLE = "resource.createResourceType.pageTitle";
78 private static final String MESSAGE_MODIFY_RESOURCE_TYPE_PAGE_TITLE = "resource.modifyResourceType.pageTitle";
79 private static final String MESSAGE_RESOURCE_TYPE_CREATE = "resource.createResourceType.resourceTypeCreated";
80 private static final String MESSAGE_RESOURCE_TYPE_UPDATED = "resource.modifyResourceType.resourceTypeUpdated";
81 private static final String MESSAGE_CONFIRM_REMOVE_RESOURCE_TYPE = "resource.removeResourceType.confirmRemoveResourceType";
82 private static final String MESSAGE_UNKNOWN_RESOURCE_TYPE = "resource.removeResourceType.unknownResourceType";
83 private static final String MESSAGE_RESOURCE_TYPE_REMOVED = "resource.removeResourceType.resourceTypeRemoved";
84 private static final String VALIDATION_ATTRIBUTES_PREFIX = "resource.model.entity.databaseResourceType.attribute.";
85
86
87 private static final String MARK_LIST_RESOURCE_TYPES = "list_resource_types";
88 private static final String MARK_RESOURCE_TYPE = "resource_type";
89 private static final String MARK_PAGINATOR = "paginator";
90 private static final String MARK_ITEMS_PER_PAGE = "nb_items_per_page";
91 private static final String PARAMETER_RESOURCE_TYPE_NAME = "resourceTypeName";
92
93
94 private static final String PROPERTY_DEFAULT_ITEMS_PER_PAGE = "resource.resourceManagement.defaultItemsPerPage";
95
96
97 private static final String TEMPLATE_MANAGE_RESOURCE_TYPE = "admin/plugins/resource/manage_resource_types.html";
98 private static final String TEMPLATE_CREATE_RESOURCE_TYPE = "admin/plugins/resource/create_resource_type.html";
99 private static final String TEMPLATE_MODIFY_RESOURCE_TYPE = "admin/plugins/resource/modify_resource_type.html";
100 private int _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DEFAULT_ITEMS_PER_PAGE, 10 );
101 private String _strCurrentPageIndex;
102 private int _nItemsPerPage;
103 private DatabaseResourceType _resourceType;
104
105
106
107
108
109
110
111
112 @View( value = VIEW_MANAGE_RESOURCE_TYPE, defaultView = true )
113 public String getManageResourceType( HttpServletRequest request )
114 {
115 _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
116 _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage, _nDefaultItemsPerPage );
117
118 Paginator<DatabaseResourceType> paginator = new LocalizedPaginator<>( DatabaseResourceTypeHome.findAll( ), _nItemsPerPage,
119 getViewFullUrl( VIEW_MANAGE_RESOURCE_TYPE ), Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex, getLocale( ) );
120
121 _resourceType = null;
122
123 Map<String, Object> model = getModel( );
124
125 model.put( MARK_ITEMS_PER_PAGE, Integer.toString( _nItemsPerPage ) );
126 model.put( MARK_PAGINATOR, paginator );
127 model.put( MARK_LIST_RESOURCE_TYPES, paginator.getPageItems( ) );
128
129 return getPage( MESSAGE_RESOURCE_TYPE_MANAGEMENT_PAGE_TITLE, TEMPLATE_MANAGE_RESOURCE_TYPE, model );
130 }
131
132
133
134
135
136
137
138
139 @View( value = VIEW_CREATE_RESOURCE_TYPE )
140 public String viewCreateResourceType( HttpServletRequest request )
141 {
142 Map<String, Object> model = getModel( );
143
144 if ( _resourceType != null )
145 {
146 model.put( MARK_RESOURCE_TYPE, _resourceType );
147 _resourceType = null;
148 }
149
150 return getPage( MESSAGE_CREATE_RESOURCE_TYPE_PAGE_TITLE, TEMPLATE_CREATE_RESOURCE_TYPE, model );
151 }
152
153
154
155
156
157
158
159
160 @Action( ACTION_DO_CREATE_RESOURCE_TYPE )
161 public String doCreateResourceType( HttpServletRequest request )
162 {
163 DatabaseResourceTypedatabase/DatabaseResourceType.html#DatabaseResourceType">DatabaseResourceType resourceType = new DatabaseResourceType( );
164 populate( resourceType, request );
165
166 if ( !validateBean( resourceType, VALIDATION_ATTRIBUTES_PREFIX ) )
167 {
168 _resourceType = resourceType;
169
170 return redirectView( request, VIEW_CREATE_RESOURCE_TYPE );
171 }
172
173 DatabaseResourceTypeHome.insert( resourceType );
174 addInfo( MESSAGE_RESOURCE_TYPE_CREATE, getLocale( ) );
175
176 return redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
177 }
178
179
180
181
182
183
184
185
186 @View( value = VIEW_MODIFY_RESOURCE_TYPE )
187 public String viewModifyResourceType( HttpServletRequest request )
188 {
189 Map<String, Object> model = getModel( );
190
191 DatabaseResourceType resourceType;
192
193 if ( _resourceType != null )
194 {
195 resourceType = _resourceType;
196 _resourceType = null;
197 }
198 else
199 {
200 String strResourceType = request.getParameter( PARAMETER_RESOURCE_TYPE_NAME );
201
202 if ( StringUtils.isEmpty( strResourceType ) )
203 {
204 redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
205 }
206
207 resourceType = DatabaseResourceTypeHome.findByPrimaryKey( strResourceType );
208 }
209
210 model.put( MARK_RESOURCE_TYPE, resourceType );
211
212 return getPage( MESSAGE_MODIFY_RESOURCE_TYPE_PAGE_TITLE, TEMPLATE_MODIFY_RESOURCE_TYPE, model );
213 }
214
215
216
217
218
219
220
221
222 @Action( ACTION_DO_MODIFY_RESOURCE_TYPE )
223 public String doModifyResourceType( HttpServletRequest request )
224 {
225 DatabaseResourceTypedatabase/DatabaseResourceType.html#DatabaseResourceType">DatabaseResourceType resourceType = new DatabaseResourceType( );
226 populate( resourceType, request );
227
228 if ( !validateBean( resourceType, VALIDATION_ATTRIBUTES_PREFIX ) )
229 {
230 _resourceType = resourceType;
231
232 return redirectView( request, VIEW_MODIFY_RESOURCE_TYPE );
233 }
234
235 DatabaseResourceTypeHome.update( resourceType );
236 addInfo( MESSAGE_RESOURCE_TYPE_UPDATED, getLocale( ) );
237
238 return redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
239 }
240
241
242
243
244
245
246
247
248 @View( value = VIEW_REMOVE_RESOURCE_TYPE )
249 public String getConfirmRemoveResourceType( HttpServletRequest request )
250 {
251 String strResourceType = request.getParameter( PARAMETER_RESOURCE_TYPE_NAME );
252
253 if ( StringUtils.isEmpty( strResourceType ) )
254 {
255 addError( MESSAGE_UNKNOWN_RESOURCE_TYPE, getLocale( ) );
256
257 return redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
258 }
259
260 DatabaseResourceType resourceType = DatabaseResourceTypeHome.findByPrimaryKey( strResourceType );
261
262 if ( resourceType == null )
263 {
264 addError( MESSAGE_UNKNOWN_RESOURCE_TYPE, getLocale( ) );
265
266 return redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
267 }
268
269 UrlItem urlItem = new UrlItem( getActionUrl( ACTION_DO_REMOVE_RESOURCE_TYPE ) );
270 urlItem.addParameter( PARAMETER_RESOURCE_TYPE_NAME, strResourceType );
271
272 return redirect( request,
273 AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_REMOVE_RESOURCE_TYPE, urlItem.getUrl( ), AdminMessage.TYPE_CONFIRMATION ) );
274 }
275
276
277
278
279
280
281
282
283 @Action( value = ACTION_DO_REMOVE_RESOURCE_TYPE )
284 public String doRemoveResourceType( HttpServletRequest request )
285 {
286 String strResourceType = request.getParameter( PARAMETER_RESOURCE_TYPE_NAME );
287
288 if ( StringUtils.isEmpty( strResourceType ) )
289 {
290 addError( MESSAGE_UNKNOWN_RESOURCE_TYPE, getLocale( ) );
291 }
292 else
293 {
294 DatabaseResourceType resourceType = DatabaseResourceTypeHome.findByPrimaryKey( strResourceType );
295
296 if ( resourceType == null )
297 {
298 addError( MESSAGE_UNKNOWN_RESOURCE_TYPE, getLocale( ) );
299 }
300 else
301 {
302 String strError = DatabaseResourceTypeRemovalManager.canResourceTypeBeRemoved( strResourceType, getLocale( ) );
303
304 if ( strError != null )
305 {
306 addError( strError );
307 }
308 else
309 {
310 DatabaseResourceTypeHome.delete( strResourceType );
311 addInfo( MESSAGE_RESOURCE_TYPE_REMOVED, getLocale( ) );
312 }
313 }
314 }
315
316 return redirectView( request, VIEW_MANAGE_RESOURCE_TYPE );
317 }
318 }