View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.document.web;
35  
36  import fr.paris.lutece.plugins.document.business.Document;
37  import fr.paris.lutece.plugins.document.business.DocumentFilter;
38  import fr.paris.lutece.plugins.document.business.DocumentHome;
39  import fr.paris.lutece.plugins.document.business.DocumentMassAction;
40  import fr.paris.lutece.plugins.document.business.DocumentPageTemplate;
41  import fr.paris.lutece.plugins.document.business.DocumentPageTemplateHome;
42  import fr.paris.lutece.plugins.document.business.DocumentType;
43  import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
44  import fr.paris.lutece.plugins.document.business.IndexerAction;
45  import fr.paris.lutece.plugins.document.business.spaces.DocumentSpace;
46  import fr.paris.lutece.plugins.document.business.spaces.DocumentSpaceHome;
47  import fr.paris.lutece.plugins.document.business.spaces.SpaceAction;
48  import fr.paris.lutece.plugins.document.business.spaces.SpaceActionHome;
49  import fr.paris.lutece.plugins.document.business.workflow.DocumentAction;
50  import fr.paris.lutece.plugins.document.business.workflow.DocumentActionHome;
51  import fr.paris.lutece.plugins.document.business.workflow.DocumentState;
52  import fr.paris.lutece.plugins.document.business.workflow.DocumentStateHome;
53  import fr.paris.lutece.plugins.document.service.DocumentException;
54  import fr.paris.lutece.plugins.document.service.DocumentMassActionResourceService;
55  import fr.paris.lutece.plugins.document.service.DocumentService;
56  import fr.paris.lutece.plugins.document.service.DocumentTypeResourceIdService;
57  import fr.paris.lutece.plugins.document.service.category.CategoryService;
58  import fr.paris.lutece.plugins.document.service.category.CategoryService.CategoryDisplay;
59  import fr.paris.lutece.plugins.document.service.metadata.MetadataHandler;
60  import fr.paris.lutece.plugins.document.service.publishing.PublishingService;
61  import fr.paris.lutece.plugins.document.service.search.DocumentIndexer;
62  import fr.paris.lutece.plugins.document.service.spaces.DocumentSpacesService;
63  import fr.paris.lutece.plugins.document.utils.DocumentIndexerUtils;
64  import fr.paris.lutece.plugins.document.utils.IntegerUtils;
65  import fr.paris.lutece.portal.business.resourceenhancer.ResourceEnhancer;
66  import fr.paris.lutece.portal.business.right.Right;
67  import fr.paris.lutece.portal.business.right.RightHome;
68  import fr.paris.lutece.portal.business.user.AdminUser;
69  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
70  import fr.paris.lutece.portal.service.admin.PasswordResetException;
71  import fr.paris.lutece.portal.service.html.XmlTransformerService;
72  import fr.paris.lutece.portal.service.i18n.I18nService;
73  import fr.paris.lutece.portal.service.mailinglist.AdminMailingListService;
74  import fr.paris.lutece.portal.service.message.AdminMessage;
75  import fr.paris.lutece.portal.service.message.AdminMessageService;
76  import fr.paris.lutece.portal.service.plugin.PluginService;
77  import fr.paris.lutece.portal.service.rbac.RBACService;
78  import fr.paris.lutece.portal.service.search.IndexationService;
79  import fr.paris.lutece.portal.service.template.AppTemplateService;
80  import fr.paris.lutece.portal.service.util.AppLogService;
81  import fr.paris.lutece.portal.service.util.AppPathService;
82  import fr.paris.lutece.portal.service.util.AppPropertiesService;
83  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
84  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
85  import fr.paris.lutece.portal.web.constants.Messages;
86  import fr.paris.lutece.portal.web.constants.Parameters;
87  import fr.paris.lutece.portal.web.pluginaction.DefaultPluginActionResult;
88  import fr.paris.lutece.portal.web.pluginaction.IPluginActionResult;
89  import fr.paris.lutece.portal.web.resource.ExtendableResourcePluginActionManager;
90  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
91  import fr.paris.lutece.portal.web.util.LocalizedPaginator;
92  import fr.paris.lutece.util.ReferenceList;
93  import fr.paris.lutece.util.UniqueIDGenerator;
94  import fr.paris.lutece.util.date.DateUtil;
95  import fr.paris.lutece.util.html.HtmlTemplate;
96  import fr.paris.lutece.util.html.Paginator;
97  import fr.paris.lutece.util.sort.AttributeComparator;
98  import fr.paris.lutece.util.url.UrlItem;
99  
100 import org.apache.commons.collections.CollectionUtils;
101 import org.apache.commons.lang3.ArrayUtils;
102 import org.apache.commons.lang3.StringUtils;
103 
104 import java.text.DateFormat;
105 import java.text.ParseException;
106 import java.text.SimpleDateFormat;
107 
108 import java.util.ArrayList;
109 import java.util.Collection;
110 import java.util.Collections;
111 import java.util.Date;
112 import java.util.HashMap;
113 import java.util.List;
114 import java.util.Map;
115 
116 import javax.servlet.http.HttpServletRequest;
117 
118 import javax.xml.transform.Source;
119 
120 
121 /**
122  * JspBean for document management
123  */
124 public class DocumentJspBean extends PluginAdminPageJspBean
125 {
126     public static final String RIGHT_DOCUMENT_MANAGEMENT = "DOCUMENT_MANAGEMENT";
127     public static final String PARAMETER_SPACE_ID_FILTER = "id_space_filter";
128     private static final long serialVersionUID = 3884593136805763150L;
129     private static final String SPACE_TREE_XSL_UNIQUE_PREFIX = UniqueIDGenerator.getNewId(  ) + "SpacesTree";
130     private static final String DOCUMENT_STYLE_PREFIX_ID = UniqueIDGenerator.getNewId(  ) + "document-";
131 
132     // Templates
133     private static final String TEMPLATE_MANAGE_DOCUMENTS = "admin/plugins/document/manage_documents.html";
134     private static final String TEMPLATE_CREATE_DOCUMENT = "admin/plugins/document/create_document.html";
135     private static final String TEMPLATE_MODIFY_DOCUMENT = "admin/plugins/document/modify_document.html";
136     private static final String TEMPLATE_PREVIEW_DOCUMENT = "admin/plugins/document/preview_document.html";
137     private static final String TEMPLATE_MOVE_DOCUMENT = "admin/plugins/document/move_document.html";
138     private static final String TEMPLATE_DOCUMENT_PAGE_TEMPLATE_ROW = "admin/plugins/document/page_template_list_row.html";
139     private static final String TEMPLATE_FORM_CATEGORY = "admin/plugins/document/category/list_category.html";
140     private static final String TEMPLATE_MASS_ARCHIVAL = "admin/plugins/document/mass_archival.html";
141 
142     // Markers
143     private static final String MARK_DOCUMENT = "document";
144     private static final String MARK_PREVIEW = "preview";
145     private static final String MARK_DOCUMENTS_LIST = "documents_list";
146     private static final String MARK_DOCUMENT_TYPES_LIST = "document_types_list";
147     private static final String MARK_MASS_ACTION = "mass_action";
148     private static final String MARK_DOCUMENT_TYPES_FILTER_LIST = "document_types_filter_list";
149     private static final String MARK_STATES_FILTER_LIST = "states_filter_list";
150     private static final String MARK_SPACES_TREE = "spaces_tree";
151     private static final String MARK_CURRENT_SPACE_ID = "current_space_id";
152     private static final String MARK_SPACE_ACTIONS_LIST = "space_actions_list";
153     private static final String MARK_SPACE = "space";
154     private static final String MARK_STATE_ID = "id_state";
155     private static final String MARK_CHILD_SPACES_LIST = "child_spaces_list";
156     private static final String MARK_FIELDS = "fields";
157     private static final String MARK_CURRENT_DATE = "current_date";
158     private static final String MARK_DOCUMENT_TYPE = "document_type";
159     private static final String MARK_DATE_MIN = "date_min";
160     private static final String MARK_DATE_MAX = "date_max";
161     private static final String MARK_DEFAULT_DOCUMENT_TYPE = "default_document_type";
162     private static final String MARK_DEFAULT_STATE = "default_state";
163     private static final String MARK_PAGINATOR = "paginator";
164     private static final String MARK_NB_ITEMS_PER_PAGE = "nb_items_per_page";
165     private static final String MARK_VIEW_TYPE = "view_type";
166     private static final String MARK_VIEW_TYPES_LIST = "view_types_list";
167     private static final String MARK_WEBAPP_URL = "webapp_url";
168     private static final String MARK_LOCALE = "locale";
169     private static final String MARK_CATEGORY = "category";
170     private static final String MARK_CATEGORY_LIST = "category_list";
171     private static final String MARK_METADATA = "metadata";
172     private static final String MARK_SUBMIT_BUTTON_DISABLED = "submit_button_disabled";
173     private static final String MARK_DATE_VALIDITY_BEGIN = "date_validity_begin";
174     private static final String MARK_DATE_VALIDITY_END = "date_validity_end";
175     private static final String MARK_MAILINGLISTS_LIST = "mailinglists_list";
176     private static final String MARK_DOCUMENT_PAGE_TEMPLATES_LIST = "page_template_list";
177     private static final String MARK_DOCUMENT_PAGE_TEMPLATE = "document_page_template";
178     private static final String MARK_INDEX_ROW = "index_row";
179     private static final String MARK_DOCUMENT_PAGE_TEMPLATE_CHECKED = "checked";
180     private static final String MARK_SPACES_BROWSER = "spaces_browser";
181     private static final String MARK_SELECTED_CRITERIA = "selected_criteria";
182     private static final String MARK_IS_FILES2DOCS_PLUGIN_ACTIVE = "is_files2docs_plugin_active";
183 
184     // Parameters
185     private static final String PARAMETER_DOCUMENT_TYPE_CODE = "document_type_code";
186     private static final String PARAMETER_DATE_MIN = "date_min";
187     private static final String PARAMETER_DATE_MAX = "date_max";
188     private static final String PARAMETER_DOCUMENT_ID = "id_document";
189     private static final String PARAMETER_STATE_ID = "id_state";
190     private static final String PARAMETER_ACTION_ID = "id_action";
191     private static final String PARAMETER_VIEW_TYPE = "view_type";
192     private static final String PARAMETER_DOCUMENT_TYPE_CODE_FILTER = "document_type_code_filter";
193     private static final String PARAMETER_STATE_ID_FILTER = "id_state_filter";
194     private static final String PARAMETER_SELECTION = "selection";
195     private static final String PARAMETER_DOCUMENT_SELECTION = "document_selection";
196     private static final String PARAMETER_HEADER_REFERER = "referer";
197     private static final String PARAMETER_FROM_URL = "fromUrl";
198     private static final String PARAMETER_ARCHIVAL_CRITERIA = "archival_criteria";
199     private static final String PARAMETER_ACTION = "action";
200 
201     // Properties
202     private static final String PROPERTY_FILTER_ALL = "document.manage_documents.filter.labelAll";
203     private static final String PROPERTY_DOCUMENTS_PER_PAGE = "document.documentsPerPage";
204     private static final String PROPERTY_DEFAULT_VIEW_TYPE = "document.manageDocuments.defaultViewType";
205     private static final String PROPERTY_PREVIEW_DOCUMENT_PAGE_TITLE = "document.preview_document.pageTitle";
206     private static final String PROPERTY_MOVE_DOCUMENT_PAGE_TITLE = "document.move_document.pageTitle";
207     private static final String PROPERTY_RESOURCE_TYPE = "document";
208     private static final String PROPERTY_WORKFLOW_AUTOVALIDATION = "document.workflow.auto_validation";
209 
210     // Jsp
211     private static final String JSP_DELETE_DOCUMENT = "DoDeleteDocument.jsp";
212     private static final String JSP_PREVIEW_DOCUMENT = "PreviewDocument.jsp";
213     private static final String JSP_ARCHIVE_DOCUMENT = "DoArchiveDocument.jsp";
214     private static final String JSP_DO_REMOVE_SELECTION = "jsp/admin/plugins/document/DoRemoveSelection.jsp";
215     private static final String JSP_DO_ARCHIVE_SELECTION = "DoArchiveSelection.jsp";
216 
217     // Messages
218     private static final String MESSAGE_CONFIRM_DELETE = "document.message.confirmDeleteDocument";
219     private static final String MESSAGE_CONFIRM_MASS_ARCHIVE = "document.message.confirmMassArchivalDocument";
220     private static final String MESSAGE_NO_MASS_ARCHIVE = "document.message.noMassArchivalDocument";
221     private static final String MESSAGE_INVALID_DOCUMENT_ID = "document.message.invalidDocumentId";
222     private static final String MESSAGE_DOCUMENT_NOT_FOUND = "document.message.documentNotFound";
223     private static final String MESSAGE_DOCUMENT_NOT_AUTHORIZED = "document.message.documentNotAuthorized";
224     private static final String MESSAGE_MOVING_NOT_AUTHORIZED = "document.message.movingNotAuthorized";
225     private static final String MESSAGE_DOCUMENT_IS_PUBLISHED = "document.message.documentIsPublished";
226     private static final String MESSAGE_ERROR_DOCUMENT_IS_PUBLISHED = "document.message.errorDocumentIsPublished";
227     private static final String MESSAGE_DOCUMENT_ERROR = "document.message.documentError"; //TODO message erreur
228     private static final String MESSAGE_CONFIRM_DELETE_SELECTION = "document.message.confirmDeleteSelection";
229     private static final String MESSAGE_CONFIRM_ARCHIVE_SELECTION = "document.message.selectionPublished";
230     private static final String MESSAGE_ERROR_DOCUMENT_SELECTED_IS_PUBLISHED = "document.message.errorDocumentSelectedIsPublished";
231 
232     //constants
233     private static final String CONSTANT_REMOVE = "remove";
234     private static final String CONSTANT_VALIDATE = "validate";
235     private static final String CONSTANT_ARCHIVE = "archive";
236     private static final String CONSTANT_REFUSE = "refuse";
237     private static final String CONSTANT_UNARCHIVE = "unarchive";
238     private static final String CONSTANT_SUBMIT = "submit";
239     private static final String CONSTANT_AND = "&";
240     private static final String CONSTANT_AND_HTML = "%26";
241     private static final String PATH_JSP = "jsp/admin/plugins/document/";
242     private static final String XSL_PARAMETER_CURRENT_SPACE = "current-space-id";
243     private static final String FILTER_ALL = "-1";
244     private static final String PAGE_INDEX_FIRST = "1";
245     private static final String CONSTANT_DATE_FORMAT = "dd/MM/yyyy";    
246     private static final String CONSTANT_FILES2DOC_PLUGIN_NAME = "files2docs" ;
247     
248     private String _strCurrentDocumentTypeFilter;
249     private String _strCurrentStateFilter;
250     private String _strCurrentSpaceId;
251     private String _strCurrentPageIndex;
252     private int _nItemsPerPage;
253     private String _strViewType;
254     private int _nDefaultItemsPerPage;
255     private String[] _multiSelectionValues;
256     private String _strFeatureUrl;
257     private String _strSavedReferer;
258 
259     /**
260      * Constructor
261      */
262     public DocumentJspBean(  )
263     {
264         _nDefaultItemsPerPage = AppPropertiesService.getPropertyInt( PROPERTY_DOCUMENTS_PER_PAGE, 10 );
265     }
266 
267     /**
268      * Gets the document management page
269      * @param request The HTTP request
270      * @return The document management page
271      */
272     public String getManageDocuments( HttpServletRequest request )
273     {
274         this._strSavedReferer = null;
275         setPageTitleProperty( null );
276 
277         AdminUser user = getUser(  );
278 
279         // Gets new criterias or space changes from the request
280         DocumentFilternt/business/DocumentFilter.html#DocumentFilter">DocumentFilter filter = new DocumentFilter(  );
281         _strCurrentPageIndex = Paginator.getPageIndex( request, Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex );
282         _strCurrentDocumentTypeFilter = getDocumentType( request, filter );
283         _strCurrentStateFilter = getState( request, filter );
284         _strCurrentSpaceId = getSpaceId( request, filter );
285         _nItemsPerPage = Paginator.getItemsPerPage( request, Paginator.PARAMETER_ITEMS_PER_PAGE, _nItemsPerPage,
286                 _nDefaultItemsPerPage );
287         _strViewType = getViewType( request );
288 
289         //Check if space is authorized, change space to default space else
290         if ( !DocumentSpacesService.getInstance(  )
291                                        .isAuthorizedViewByWorkgroup( IntegerUtils.convert( _strCurrentSpaceId ), user ) ||
292                 !DocumentSpacesService.getInstance(  )
293                                           .isAuthorizedViewByRole( IntegerUtils.convert( _strCurrentSpaceId ), user ) )
294         {
295             filter.setIdSpace( DocumentSpacesService.getInstance(  ).getUserDefaultSpace( user ) );
296             _strCurrentSpaceId = Integer.toString( filter.getIdSpace(  ) );
297         }
298 
299         // Build document list according criterias
300         Collection<Integer> listDocumentIds = DocumentHome.findPrimaryKeysByFilter( filter, getLocale(  ) );
301 
302         // Spaces
303         String strXmlSpaces = DocumentSpacesService.getInstance(  ).getXmlSpacesList( user );
304         Source sourceXsl = DocumentSpacesService.getInstance(  ).getTreeXsl(  );
305         Map<String, String> htXslParameters = new HashMap<String, String>(  );
306         htXslParameters.put( XSL_PARAMETER_CURRENT_SPACE, _strCurrentSpaceId );
307 
308         XmlTransformerService xmlTransformerService = new XmlTransformerService(  );
309         String strSpacesTree = xmlTransformerService.transformBySourceWithXslCache( strXmlSpaces, sourceXsl,
310                 SPACE_TREE_XSL_UNIQUE_PREFIX, htXslParameters, null );
311 
312         List<SpaceAction> listSpaceActions = SpaceActionHome.getActionsList( getLocale(  ) );
313         int nCurrentSpaceId = IntegerUtils.convert( _strCurrentSpaceId );
314         DocumentSpace currentSpace = DocumentSpaceHome.findByPrimaryKey( nCurrentSpaceId );
315         listSpaceActions = (List<SpaceAction>) RBACService.getAuthorizedActionsCollection( listSpaceActions,
316                 currentSpace, getUser(  ) );
317 
318         // Build filter combos
319         // Document Types
320         ReferenceList listDocumentTypes = DocumentSpaceHome.getAllowedDocumentTypes( IntegerUtils.convert( 
321                     _strCurrentSpaceId ) );
322         listDocumentTypes = RBACService.getAuthorizedReferenceList( listDocumentTypes, DocumentType.RESOURCE_TYPE,
323                 DocumentTypeResourceIdService.PERMISSION_VIEW, user );
324         listDocumentTypes.addItem( FILTER_ALL, I18nService.getLocalizedString( PROPERTY_FILTER_ALL, getLocale(  ) ) );
325 
326         // Documents States
327         ReferenceList listStates = DocumentStateHome.getDocumentStatesList( getLocale(  ) );
328         listStates.addItem( FILTER_ALL, I18nService.getLocalizedString( PROPERTY_FILTER_ALL, getLocale(  ) ) );
329 
330         // Childs spaces
331         Collection<DocumentSpace> listChildSpaces = DocumentSpaceHome.findChilds( nCurrentSpaceId );
332         listChildSpaces = AdminWorkgroupService.getAuthorizedCollection( listChildSpaces, user );
333 
334         // Creation document types list for the current space
335         ReferenceList listCreateDocumentTypes = DocumentSpaceHome.getAllowedDocumentTypes( IntegerUtils.convert( 
336                     _strCurrentSpaceId ) );
337         listCreateDocumentTypes = RBACService.getAuthorizedReferenceList( listCreateDocumentTypes,
338                 DocumentType.RESOURCE_TYPE, DocumentTypeResourceIdService.PERMISSION_CREATE, user );
339 
340         LocalizedPaginator<Integer> paginator = new LocalizedPaginator<Integer>( (List<Integer>) listDocumentIds,
341                 _nItemsPerPage, getHomeUrl( request ), Paginator.PARAMETER_PAGE_INDEX, _strCurrentPageIndex,
342                 getLocale(  ) );
343 
344         List<Document> listDocuments = new ArrayList<Document>(  );
345 
346         for ( Integer documentId : paginator.getPageItems(  ) )
347         {
348             Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( documentId );
349 
350             if ( document != null )
351             {
352                 document.setLocale( getLocale(  ) );
353                 DocumentService.getInstance(  ).getActions( document, getLocale(  ), getUser(  ) );
354 
355                 DocumentService.getInstance(  ).getPublishedStatus( document );
356                 listDocuments.add( document );
357             }
358         }
359 
360         String strSortedAttributeName = request.getParameter( Parameters.SORTED_ATTRIBUTE_NAME );
361         String strAscSort = null;
362 
363         if ( strSortedAttributeName != null )
364         {
365             strAscSort = request.getParameter( Parameters.SORTED_ASC );
366 
367             boolean bIsAscSort = Boolean.parseBoolean( strAscSort );
368 
369             Collections.sort( listDocuments, new AttributeComparator( strSortedAttributeName, bIsAscSort ) );
370         }
371 
372         HashMap<String, Object> model = new HashMap<String, Object>(  );
373         model.put( MARK_SPACES_TREE, strSpacesTree );
374         model.put( MARK_SPACE_ACTIONS_LIST, listSpaceActions );
375         model.put( MARK_SPACE, currentSpace );
376         model.put( MARK_PAGINATOR, paginator );
377         model.put( MARK_NB_ITEMS_PER_PAGE, StringUtils.EMPTY + _nItemsPerPage );
378         model.put( MARK_VIEW_TYPES_LIST, DocumentSpaceHome.getViewTypeList( getLocale(  ) ) );
379         model.put( MARK_VIEW_TYPE, _strViewType );
380         model.put( MARK_DOCUMENTS_LIST, listDocuments );
381         model.put( MARK_DOCUMENT_TYPES_FILTER_LIST, listDocumentTypes );
382         model.put( MARK_DEFAULT_DOCUMENT_TYPE, _strCurrentDocumentTypeFilter );
383         model.put( MARK_STATES_FILTER_LIST, listStates );
384         model.put( MARK_DEFAULT_STATE, _strCurrentStateFilter );
385         model.put( MARK_CHILD_SPACES_LIST, listChildSpaces );
386         model.put( MARK_DOCUMENT_TYPES_LIST, listCreateDocumentTypes );
387         model.put( MARK_CURRENT_SPACE_ID, _strCurrentSpaceId );
388         model.put( MARK_MASS_ACTION,
389             RBACService.isAuthorized( new DocumentMassAction(  ),
390                 DocumentMassActionResourceService.PERMISSION_MASS_ARCHIVE, user ) );
391         
392         model.put( MARK_IS_FILES2DOCS_PLUGIN_ACTIVE , PluginService.isPluginEnable( CONSTANT_FILES2DOC_PLUGIN_NAME )  );
393 
394         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_DOCUMENTS, getLocale(  ), model );
395 
396         return getAdminPage( template.getHtml(  ) );
397     }
398 
399     /**
400      * Gets the document creation page
401      * @param request The HTTP request
402      * @return The document creation page
403      */
404     public String getCreateDocument( HttpServletRequest request )
405     {
406         saveReferer( request );
407 
408         String strDocumentTypeCode = request.getParameter( PARAMETER_DOCUMENT_TYPE_CODE );
409 
410         if ( ( _strCurrentSpaceId == null ) ||
411                 ( !DocumentService.getInstance(  )
412                                       .isAuthorizedAdminDocument( IntegerUtils.convert( _strCurrentSpaceId ),
413                     strDocumentTypeCode, DocumentTypeResourceIdService.PERMISSION_CREATE, getUser(  ) ) ) )
414         {
415             return getManageDocuments( request );
416         }
417 
418         DocumentType documentType = DocumentTypeHome.findByPrimaryKey( strDocumentTypeCode );
419         DateFormat dateFormat = new SimpleDateFormat( CONSTANT_DATE_FORMAT, getLocale(  ) );
420         String strCurrentDate = dateFormat.format( new Date(  ) );
421         Map<String, Object> model = new HashMap<String, Object>(  );
422         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
423         model.put( MARK_LOCALE, getLocale(  ).getLanguage(  ) );
424         model.put( MARK_DOCUMENT_TYPE, documentType.getCode(  ) );
425 
426         model.put( MARK_CATEGORY, getCategoryCreateForm( request ) );
427         model.put( MARK_METADATA, getMetadataCreateForm( request, strDocumentTypeCode ) );
428         model.put( MARK_FIELDS,
429             DocumentService.getInstance(  )
430                            .getCreateForm( strDocumentTypeCode, getLocale(  ), AppPathService.getBaseUrl( request ) ) );
431         model.put( MARK_CURRENT_DATE, strCurrentDate );
432 
433         // PageTemplate
434         int nIndexRow = 1;
435         StringBuffer strPageTemplatesRow = new StringBuffer(  );
436 
437         // Scan of the list
438         for ( DocumentPageTemplate documentPageTemplate : DocumentPageTemplateHome.getPageTemplatesList(  ) )
439         {
440             strPageTemplatesRow.append( getTemplatesPageList( documentPageTemplate.getId(  ), 0,
441                     Integer.toString( nIndexRow ) ) );
442             nIndexRow++;
443         }
444 
445         // additionnal create info
446         ResourceEnhancer.getCreateResourceModelAddOn( model );
447 
448         model.put( MARK_DOCUMENT_PAGE_TEMPLATES_LIST, strPageTemplatesRow );
449 
450         ReferenceList listMailingLists = AdminMailingListService.getMailingLists( getUser(  ) );
451         model.put( MARK_MAILINGLISTS_LIST, listMailingLists );
452 
453         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_CREATE_DOCUMENT, getLocale(  ), model );
454 
455         return getAdminPage( template.getHtml(  ) );
456     }
457 
458     /**
459      * Return the part of creation form corresponding to Category
460      * @param request The http request
461      * @return The html form
462      */
463     private String getCategoryCreateForm( HttpServletRequest request )
464     {
465         AdminUser user = getUser(  );
466         HashMap<String, Collection<CategoryDisplay>> model = new HashMap<String, Collection<CategoryDisplay>>(  );
467         model.put( MARK_CATEGORY_LIST, CategoryService.getAllCategoriesDisplay( user ) );
468 
469         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_FORM_CATEGORY, getLocale(  ), model );
470 
471         return template.getHtml(  );
472     }
473 
474     /**
475      * Return the part of modification form corresponding to Category
476      * @param request The http request
477      * @param document The Document object to modify
478      * @return The html form
479      */
480     private String getCategoryModifyForm( HttpServletRequest request, Document document )
481     {
482         HashMap<String, Collection<CategoryDisplay>> model = new HashMap<String, Collection<CategoryDisplay>>(  );
483         Collection<CategoryDisplay> listCategoryDisplay = CategoryService.getAllCategoriesDisplay( document.getCategories(  ),
484                 getUser(  ) );
485 
486         model.put( MARK_CATEGORY_LIST, listCategoryDisplay );
487 
488         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_FORM_CATEGORY, getLocale(  ), model );
489 
490         return template.getHtml(  );
491     }
492 
493     /**
494      * Return the metadata creation form
495      * @param request The http request
496      * @param strDocumentTypeCode The Document type code
497      * @return The html form
498      */
499     private String getMetadataCreateForm( HttpServletRequest request, String strDocumentTypeCode )
500     {
501         MetadataHandler hMetadata = getMetadataHandler( strDocumentTypeCode );
502 
503         return ( hMetadata != null ) ? hMetadata.getCreateForm( request ) : StringUtils.EMPTY;
504     }
505 
506     /**
507      * Return the metadata modify form
508      * @param request The http request
509      * @param document The Document
510      * @return The html form
511      */
512     private String getMetadataModifyForm( HttpServletRequest request, Document document )
513     {
514         MetadataHandler hMetadata = getMetadataHandler( document.getCodeDocumentType(  ) );
515 
516         return ( hMetadata != null ) ? hMetadata.getModifyForm( request, document.getXmlMetadata(  ) ) : StringUtils.EMPTY;
517     }
518 
519     /**
520      * Return the metadata handler
521      * @param strDocumentTypeCode The Document type code
522      * @return The metadata handler for a document type
523      */
524     private MetadataHandler getMetadataHandler( String strDocumentTypeCode )
525     {
526         DocumentType documentType = DocumentTypeHome.findByPrimaryKey( strDocumentTypeCode );
527 
528         return documentType.metadataHandler(  );
529     }
530 
531     /**
532      * Perform the creation
533      * @param request The HTTP request
534      * @return The URL to go after performing the action
535      */
536     public String doCreateDocument( HttpServletRequest request )
537     {
538         saveReferer( request );
539 
540         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
541 
542         AdminUser user = getUser(  );
543 
544         String strDocumentTypeCode = request.getParameter( PARAMETER_DOCUMENT_TYPE_CODE );
545 
546         if ( !DocumentService.getInstance(  )
547                                  .isAuthorizedAdminDocument( IntegerUtils.convert( _strCurrentSpaceId ),
548                     strDocumentTypeCode, DocumentTypeResourceIdService.PERMISSION_CREATE, user ) )
549         {
550             return getHomeUrl( request );
551         }
552 
553         Documentcument/business/Document.html#Document">Document document = new Document(  );
554         document.setCodeDocumentType( strDocumentTypeCode );
555 
556         String strError = DocumentService.getInstance(  ).getDocumentData( multipartRequest, document, getLocale(  ) );
557 
558         if ( strError != null )
559         {
560             return strError;
561         }
562 
563         document.setSpaceId( IntegerUtils.convert( _strCurrentSpaceId ) );
564         document.setStateId( 1 );
565         document.setCreatorId( getUser(  ).getUserId(  ) );
566 
567         try
568         {
569             DocumentService.getInstance(  ).createDocument( document, getUser(  ) );
570         }
571         catch ( DocumentException e )
572         {
573             return getErrorMessageUrl( request, e.getI18nMessage(  ) );
574         }
575 
576         // process
577         ResourceEnhancer.doCreateResourceAddOn( request, PROPERTY_RESOURCE_TYPE, document.getId(  ) );
578 
579         //Here we simulate clicking on the validation buttons. This could should do the same
580         //as having request to doStateChange for submitting and doValidateDocument for validating
581         if ( AppPropertiesService.getPropertyBoolean( PROPERTY_WORKFLOW_AUTOVALIDATION, false ) )
582         {
583             String strDocumentId = Integer.toString( document.getId(  ) );
584 
585             try
586             {
587                 DocumentService.getInstance(  )
588                                .changeDocumentState( document, getUser(  ), DocumentState.STATE_WAITING_FOR_APPROVAL );
589 
590                 //Reload document in case listeners have modified it in the database
591                 document = DocumentHome.findByPrimaryKeyWithoutBinaries( IntegerUtils.convert( strDocumentId ) );
592 
593                 DocumentService.getInstance(  )
594                                .validateDocument( document, getUser(  ), DocumentState.STATE_VALIDATE );
595             }
596             catch ( DocumentException e )
597             {
598                 return getErrorMessageUrl( request, e.getI18nMessage(  ) );
599             }
600 
601             IndexationService.addIndexerAction( strDocumentId, DocumentIndexer.INDEXER_NAME, IndexerAction.TASK_MODIFY,
602                 IndexationService.ALL_DOCUMENT );
603 
604             DocumentIndexerUtils.addIndexerAction( strDocumentId, IndexerAction.TASK_MODIFY, IndexationService.ALL_DOCUMENT );
605         }
606 
607         return getHomeUrl( request );
608     }
609 
610     /**
611      * Gets the document modification page
612      * @param request The HTTP request
613      * @return The URL to go after performing the action
614      */
615     public String getModifyDocument( HttpServletRequest request )
616     {
617         saveReferer( request );
618 
619         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
620         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( IntegerUtils.convert( strDocumentId ) );
621 
622         if ( ( document == null ) ||
623                 !DocumentService.getInstance(  )
624                                     .isAuthorizedAdminDocument( document.getSpaceId(  ),
625                     document.getCodeDocumentType(  ), DocumentTypeResourceIdService.PERMISSION_MODIFY, getUser(  ) ) )
626         {
627             return getManageDocuments( request );
628         }
629 
630         String strStateId = ( request.getParameter( PARAMETER_STATE_ID ) != null )
631             ? request.getParameter( PARAMETER_STATE_ID ) : StringUtils.EMPTY;
632         Map<String, Object> model = new HashMap<String, Object>(  );
633         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
634         model.put( MARK_LOCALE, getLocale(  ).getLanguage(  ) );
635         model.put( MARK_DOCUMENT, document );
636 
637         // Date Management
638         model.put( MARK_DATE_VALIDITY_BEGIN,
639             ( document.getDateValidityBegin(  ) == null ) ? StringUtils.EMPTY
640                                                           : DateUtil.getDateString( 
641                 new Date( document.getDateValidityBegin(  ).getTime(  ) ), getLocale(  ) ) );
642         model.put( MARK_DATE_VALIDITY_END,
643             ( document.getDateValidityEnd(  ) == null ) ? StringUtils.EMPTY
644                                                         : DateUtil.getDateString( 
645                 new Date( document.getDateValidityEnd(  ).getTime(  ) ), getLocale(  ) ) );
646 
647         // PageTemplate
648         int nIndexRow = 1;
649         StringBuffer strPageTemplatesRow = new StringBuffer(  );
650 
651         // Scan of the list
652         for ( DocumentPageTemplate documentPageTemplate : DocumentPageTemplateHome.getPageTemplatesList(  ) )
653         {
654             strPageTemplatesRow.append( getTemplatesPageList( documentPageTemplate.getId(  ),
655                     document.getPageTemplateDocumentId(  ), Integer.toString( nIndexRow ) ) );
656             nIndexRow++;
657         }
658 
659         model.put( MARK_DOCUMENT_PAGE_TEMPLATES_LIST, strPageTemplatesRow );
660 
661         ReferenceList listMailingLists = AdminMailingListService.getMailingLists( getUser(  ) );
662         model.put( MARK_MAILINGLISTS_LIST, listMailingLists );
663 
664         model.put( MARK_CATEGORY, getCategoryModifyForm( request, document ) );
665         model.put( MARK_METADATA, getMetadataModifyForm( request, document ) );
666         model.put( MARK_FIELDS,
667             DocumentService.getInstance(  ).getModifyForm( document, getLocale(  ), AppPathService.getBaseUrl( request ) ) );
668         model.put( MARK_STATE_ID, strStateId );
669 
670         ExtendableResourcePluginActionManager.fillModel( request, getUser(  ), model, strDocumentId,
671             Document.PROPERTY_RESOURCE_TYPE );
672 
673         ResourceEnhancer.getModifyResourceModelAddOn( model, PROPERTY_RESOURCE_TYPE, document.getId(  ) );
674 
675         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MODIFY_DOCUMENT, getLocale(  ), model );
676 
677         return getAdminPage( template.getHtml(  ) );
678     }
679 
680     /**
681      * Perform the modification
682      * @param request The HTTP request
683      * @return The URL to go after performing the action
684      */
685     public String doModifyDocument( HttpServletRequest request )
686     {
687         saveReferer( request );
688 
689         MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
690         String strDocumentId = multipartRequest.getParameter( PARAMETER_DOCUMENT_ID );
691         Document document = DocumentHome.findByPrimaryKey( IntegerUtils.convert( strDocumentId ) );
692 
693         if ( ( document == null ) ||
694                 !DocumentService.getInstance(  )
695                                     .isAuthorizedAdminDocument( document.getSpaceId(  ),
696                     document.getCodeDocumentType(  ), DocumentTypeResourceIdService.PERMISSION_MODIFY, getUser(  ) ) )
697         {
698             return getHomeUrl( request );
699         }
700 
701         String strError = DocumentService.getInstance(  ).getDocumentData( multipartRequest, document, getLocale(  ) );
702 
703         if ( strError != null )
704         {
705             return strError;
706         }
707 
708         try
709         {
710             DocumentService.getInstance(  ).modifyDocument( document, getUser(  ) );
711         }
712         catch ( DocumentException e )
713         {
714             return getErrorMessageUrl( request, e.getI18nMessage(  ) );
715         }
716 
717         // If a state is defined, it should be set after to the document after the modification
718         String strStateId = multipartRequest.getParameter( PARAMETER_STATE_ID );
719 
720         if ( IntegerUtils.isNumeric( strStateId ) )
721         {
722             int nStateId = IntegerUtils.convert( strStateId );
723 
724             try
725             {
726                 DocumentService.getInstance(  ).changeDocumentState( document, getUser(  ), nStateId );
727             }
728             catch ( DocumentException e )
729             {
730                 return getErrorMessageUrl( request, e.getI18nMessage(  ) );
731             }
732         }
733 
734         ResourceEnhancer.doModifyResourceAddOn( request, PROPERTY_RESOURCE_TYPE, document.getId(  ) );
735 
736         //Here we simulate clicking on the validation buttons. This could should do the same
737         //as having request to doStateChange for changesubmitting and doValidateDocument for revalidating
738         if ( AppPropertiesService.getPropertyBoolean( PROPERTY_WORKFLOW_AUTOVALIDATION, false ) )
739         {
740             try
741             {
742                 DocumentService.getInstance(  )
743                                .changeDocumentState( document, getUser(  ), DocumentState.STATE_WAITING_FOR_APPROVAL );
744 
745                 //Reload document in case listeners have modified it in the database
746                 document = DocumentHome.findByPrimaryKeyWithoutBinaries( IntegerUtils.convert( strDocumentId ) );
747 
748                 DocumentService.getInstance(  )
749                                .validateDocument( document, getUser(  ), DocumentState.STATE_VALIDATE );
750             }
751             catch ( DocumentException e )
752             {
753                 return getErrorMessageUrl( request, e.getI18nMessage(  ) );
754             }
755 
756             IndexationService.addIndexerAction( strDocumentId, DocumentIndexer.INDEXER_NAME, IndexerAction.TASK_MODIFY,
757                 IndexationService.ALL_DOCUMENT );
758 
759             DocumentIndexerUtils.addIndexerAction( strDocumentId, IndexerAction.TASK_MODIFY, IndexationService.ALL_DOCUMENT );
760         }
761 
762         return getHomeUrl( request );
763     }
764 
765     /**
766      * Confirm the deletion
767      * @param request The HTTP request
768      * @return The URL to go after performing the confirmation
769      */
770     public String deleteDocument( HttpServletRequest request )
771     {
772         saveReferer( request );
773 
774         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
775         int nDocumentId = IntegerUtils.convert( strDocumentId );
776         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocumentId );
777 
778         if ( !DocumentService.getInstance(  )
779                                  .isAuthorizedAdminDocument( document.getSpaceId(  ), document.getCodeDocumentType(  ),
780                     DocumentTypeResourceIdService.PERMISSION_DELETE, getUser(  ) ) )
781         {
782             return getHomeUrl( request );
783         }
784 
785         Object[] messageArgs = { document.getTitle(  ) };
786         UrlItem url = new UrlItem( PATH_JSP + JSP_DELETE_DOCUMENT );
787         url.addParameter( PARAMETER_DOCUMENT_ID, nDocumentId );
788 
789         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE, messageArgs, url.getUrl(  ),
790             AdminMessage.TYPE_CONFIRMATION );
791     }
792 
793     /**
794      * Perform the deletion
795      * @param request The HTTP request
796      * @return The URL to go after performing the action
797      */
798     public String doDeleteDocument( HttpServletRequest request )
799     {
800         saveReferer( request );
801 
802         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
803         int nDocumentId = IntegerUtils.convert( strDocumentId );
804         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocumentId );
805 
806         if ( ( document != null ) &&
807                 !DocumentService.getInstance(  )
808                                     .isAuthorizedAdminDocument( document.getSpaceId(  ),
809                     document.getCodeDocumentType(  ), DocumentTypeResourceIdService.PERMISSION_DELETE, getUser(  ) ) )
810         {
811             return getHomeUrl( request );
812         }
813 
814         // Test if the document is published or assigned
815         boolean bPublishedDocument = PublishingService.getInstance(  ).isAssigned( nDocumentId );
816 
817         if ( bPublishedDocument )
818         {
819             return AdminMessageService.getMessageUrl( request, MESSAGE_ERROR_DOCUMENT_IS_PUBLISHED,
820                 AdminMessage.TYPE_STOP );
821         }
822 
823         DocumentHome.remove( nDocumentId );
824 
825         ResourceEnhancer.doDeleteResourceAddOn( request, PROPERTY_RESOURCE_TYPE, nDocumentId );
826 
827         return getHomeUrl( request );
828     }
829 
830     /**
831      * Perform the changing of state
832      * @param request The HTTP request
833      * @return The URL to go after performing the action
834      */
835     public String doChangeState( HttpServletRequest request )
836     {
837         saveReferer( request );
838 
839         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
840         String strActionId = request.getParameter( PARAMETER_ACTION_ID );
841         int nDocumentId = -1;
842         int nActionId = -1;
843 
844         try
845         {
846             nDocumentId = Integer.parseInt( strDocumentId );
847             nActionId = Integer.parseInt( strActionId );
848         }
849         catch ( NumberFormatException ne )
850         {
851             AppLogService.error( ne );
852         }
853 
854         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocumentId );
855         DocumentAction action = DocumentActionHome.findByPrimaryKey( nActionId );
856 
857         if ( ( action == null ) || ( action.getFinishDocumentState(  ) == null ) || ( document == null ) ||
858                 !DocumentService.getInstance(  )
859                                     .isAuthorizedAdminDocument( document.getSpaceId(  ),
860                     document.getCodeDocumentType(  ), action.getPermission(  ), getUser(  ) ) )
861         {
862             return getHomeUrl( request );
863         }
864 
865         try
866         {
867             DocumentService.getInstance(  )
868                            .changeDocumentState( document, getUser(  ), action.getFinishDocumentState(  ).getId(  ) );
869         }
870         catch ( DocumentException e )
871         {
872             return getErrorMessageUrl( request, e.getI18nMessage(  ) );
873         }
874 
875         return getHomeUrl( request );
876     }
877 
878     /**
879      * Perform the document validation
880      * @param request The HTTP request
881      * @return The URL to go after performing the action
882      */
883     public String doValidateDocument( HttpServletRequest request )
884     {
885         saveReferer( request );
886 
887         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
888         String strActionId = request.getParameter( PARAMETER_ACTION_ID );
889         int nDocumentId = -1;
890         int nActionId = -1;
891 
892         try
893         {
894             nDocumentId = Integer.parseInt( strDocumentId );
895             nActionId = Integer.parseInt( strActionId );
896         }
897         catch ( NumberFormatException ne )
898         {
899             AppLogService.error( ne );
900         }
901 
902         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocumentId );
903         DocumentAction action = DocumentActionHome.findByPrimaryKey( nActionId );
904 
905         if ( ( action == null ) || ( action.getFinishDocumentState(  ) == null ) || ( document == null ) ||
906                 !DocumentService.getInstance(  )
907                                     .isAuthorizedAdminDocument( document.getSpaceId(  ),
908                     document.getCodeDocumentType(  ), action.getPermission(  ), getUser(  ) ) )
909         {
910             return getHomeUrl( request );
911         }
912 
913         try
914         {
915             DocumentService.getInstance(  )
916                            .validateDocument( document, getUser(  ), action.getFinishDocumentState(  ).getId(  ) );
917         }
918         catch ( DocumentException e )
919         {
920             return getErrorMessageUrl( request, e.getI18nMessage(  ) );
921         }
922 
923         String strIdDocument = Integer.toString( nDocumentId );
924         IndexationService.addIndexerAction( strIdDocument, DocumentIndexer.INDEXER_NAME, IndexerAction.TASK_MODIFY,
925             IndexationService.ALL_DOCUMENT );
926 
927         DocumentIndexerUtils.addIndexerAction( strIdDocument, IndexerAction.TASK_MODIFY, IndexationService.ALL_DOCUMENT );
928 
929         /*
930          * Collection<Portlet> portlets =
931          * PublishingService.getInstance().getPortletsByDocumentId
932          * (Integer.toString(nDocumentId));
933          * for(Portlet portlet : portlets)
934          * {
935          * if(PublishingService.getInstance().isPublished(nDocumentId,
936          * portlet.getId()))
937          * {
938          * IndexationService.getInstance().addIndexerAction(portlet.getPageId(),
939          * PageIndexer.INDEXER_NAME, IndexerAction.TASK_MODIFY);
940          * }
941          *
942          * }
943          */
944         return getHomeUrl( request );
945     }
946 
947     /**
948      * Confirm the filing
949      * @param request The HTTP servlet Request
950      * @return The url of filing jsp
951      */
952     public String doConfirmArchiveDocument( HttpServletRequest request )
953     {
954         saveReferer( request );
955 
956         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
957         String strActionId = request.getParameter( PARAMETER_ACTION_ID );
958         int nDocumentId = -1;
959         int nActionId = -1;
960 
961         try
962         {
963             nDocumentId = Integer.parseInt( strDocumentId );
964             nActionId = Integer.parseInt( strActionId );
965         }
966         catch ( NumberFormatException ne )
967         {
968             AppLogService.error( ne );
969         }
970 
971         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocumentId );
972         DocumentAction action = DocumentActionHome.findByPrimaryKey( nActionId );
973 
974         if ( ( action == null ) || ( action.getFinishDocumentState(  ) == null ) || ( document == null ) ||
975                 !DocumentService.getInstance(  )
976                                     .isAuthorizedAdminDocument( document.getSpaceId(  ),
977                     document.getCodeDocumentType(  ), action.getPermission(  ), getUser(  ) ) )
978         {
979             return getHomeUrl( request );
980         }
981 
982         UrlItem url = new UrlItem( JSP_ARCHIVE_DOCUMENT );
983         url.addParameter( PARAMETER_DOCUMENT_ID, nDocumentId );
984         url.addParameter( PARAMETER_ACTION_ID, nActionId );
985 
986         // Test if the document is published or assigned
987         boolean bPublishedDocument = PublishingService.getInstance(  ).isAssigned( nDocumentId );
988 
989         if ( bPublishedDocument )
990         {
991             return AdminMessageService.getMessageUrl( request, MESSAGE_DOCUMENT_IS_PUBLISHED,
992                 PATH_JSP + url.getUrl(  ), AdminMessage.TYPE_QUESTION );
993         }
994 
995         return url.getUrl(  );
996     }
997 
998     /**
999      * Remove selection
1000      * @param request The HTTP request
1001      * @return The forward url
1002      */
1003     public String doRemoveSelection( HttpServletRequest request )
1004     {
1005         int nDocumentId;
1006 
1007         for ( String strIdDocument : _multiSelectionValues )
1008         {
1009             try
1010             {
1011                 nDocumentId = Integer.parseInt( strIdDocument );
1012 
1013                 Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocumentId );
1014 
1015                 if ( !DocumentService.getInstance(  )
1016                                          .isAuthorizedAdminDocument( document.getSpaceId(  ),
1017                             document.getCodeDocumentType(  ), DocumentTypeResourceIdService.PERMISSION_DELETE,
1018                             getUser(  ) ) )
1019                 {
1020                     return getHomeUrl( request );
1021                 }
1022 
1023                 if ( ( document.getStateId(  ) == DocumentState.STATE_WRITING ) ||
1024                         ( document.getStateId(  ) == DocumentState.STATE_ARCHIVED ) )
1025                 {
1026                     // Test if the document is published or assigned
1027                     boolean bPublishedDocument = PublishingService.getInstance(  ).isAssigned( nDocumentId );
1028 
1029                     if ( bPublishedDocument )
1030                     {
1031                         return AdminMessageService.getMessageUrl( request,
1032                             MESSAGE_ERROR_DOCUMENT_SELECTED_IS_PUBLISHED, AdminMessage.TYPE_STOP );
1033                     }
1034 
1035                     DocumentHome.remove( nDocumentId );
1036                 }
1037             }
1038             catch ( NumberFormatException ne )
1039             {
1040                 AppLogService.error( ne );
1041             }
1042         }
1043 
1044         return getHomeUrl( request );
1045     }
1046 
1047     /**
1048      * Unpublish and archive selected documents
1049      * @param request The Http request
1050      * @return The URL to redirect to
1051      */
1052     public String doArchiveSelection( HttpServletRequest request )
1053     {
1054         int nDocumentId;
1055 
1056         for ( String strIdDocument : _multiSelectionValues )
1057         {
1058             try
1059             {
1060                 nDocumentId = Integer.parseInt( strIdDocument );
1061 
1062                 Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocumentId );
1063 
1064                 if ( isAuthorized( DocumentAction.ACTION_ARCHIVE, document ) )
1065                 {
1066                     try
1067                     {
1068                         DocumentService.getInstance(  )
1069                                        .archiveDocument( document, getUser(  ), DocumentState.STATE_ARCHIVED );
1070                     }
1071                     catch ( DocumentException e )
1072                     {
1073                         return getErrorMessageUrl( request, e.getI18nMessage(  ) );
1074                     }
1075                 }
1076             }
1077             catch ( NumberFormatException ne )
1078             {
1079                 AppLogService.error( ne );
1080             }
1081         }
1082 
1083         return getHomeUrl( request );
1084     }
1085 
1086     /**
1087      * Processes document action for multiselection
1088      * @param request The Http request
1089      * @return The URL to redirect to
1090      */
1091     public String doActionSelectionDocument( HttpServletRequest request )
1092     {
1093         String strAction = request.getParameter( PARAMETER_SELECTION );
1094         String[] strIdDocuments = (String[]) request.getParameterMap(  ).get( PARAMETER_DOCUMENT_SELECTION );
1095         _multiSelectionValues = strIdDocuments;
1096 
1097         int nbDocumentsAffected = 0;
1098 
1099         if ( !ArrayUtils.isEmpty( strIdDocuments ) )
1100         {
1101             if ( strAction.equals( CONSTANT_REMOVE ) )
1102             {
1103                 UrlItem url = new UrlItem( JSP_DO_REMOVE_SELECTION );
1104 
1105                 return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_DELETE_SELECTION, url.getUrl(  ),
1106                     AdminMessage.TYPE_CONFIRMATION );
1107             }
1108             else if ( strAction.equals( CONSTANT_ARCHIVE ) )
1109             {
1110                 UrlItem url = new UrlItem( JSP_DO_ARCHIVE_SELECTION );
1111 
1112                 for ( String strIdDocument : _multiSelectionValues )
1113                 {
1114                     int nIdDocument = IntegerUtils.convert( strIdDocument );
1115 
1116                     Document document = DocumentHome.findByPrimaryKey( nIdDocument );
1117 
1118                     // Test if the document is published or assigned
1119                     boolean bPublishedDocument = PublishingService.getInstance(  ).isAssigned( nIdDocument );
1120 
1121                     if ( ( document != null ) && ( document.getStateId(  ) == DocumentState.STATE_VALIDATE ) &&
1122                             ( bPublishedDocument ) )
1123                     {
1124                         return AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_ARCHIVE_SELECTION,
1125                             PATH_JSP + url.getUrl(  ), AdminMessage.TYPE_QUESTION );
1126                     }
1127                 }
1128 
1129                 for ( String strIdDocument : _multiSelectionValues )
1130                 {
1131                     int nIdDocument = IntegerUtils.convert( strIdDocument );
1132 
1133                     Document document = DocumentHome.findByPrimaryKey( nIdDocument );
1134 
1135                     try
1136                     {
1137                         if ( ( document != null ) && ( document.getStateId(  ) == DocumentState.STATE_VALIDATE ) &&
1138                                 isAuthorized( DocumentAction.ACTION_ARCHIVE, document ) )
1139                         {
1140                             DocumentService.getInstance(  )
1141                                            .archiveDocument( document, getUser(  ), DocumentState.STATE_ARCHIVED );
1142                         }
1143                     }
1144                     catch ( DocumentException e )
1145                     {
1146                         return getErrorMessageUrl( request, e.getI18nMessage(  ) );
1147                     }
1148                 }
1149             }
1150             else
1151             {
1152                 for ( String strIdDocument : strIdDocuments )
1153                 {
1154                     int nIdDocument = IntegerUtils.convert( strIdDocument );
1155                     int nActionId = -1;
1156                     Document document = DocumentHome.findByPrimaryKey( nIdDocument );
1157 
1158                     if ( document != null )
1159                     {
1160                         int stateId = document.getStateId(  );
1161 
1162                         if ( ( strAction.equals( CONSTANT_VALIDATE ) ) &&
1163                                 ( ( stateId == DocumentState.STATE_WAITING_FOR_APPROVAL ) ||
1164                                 ( stateId == DocumentState.STATE_WAITING_FOR_CHANGE_APPROVAL ) ) )
1165                         {
1166                             try
1167                             {
1168                                 //set the action
1169                                 if ( stateId == DocumentState.STATE_WAITING_FOR_APPROVAL )
1170                                 {
1171                                     nActionId = DocumentAction.ACTION_VALIDATE;
1172                                 }
1173                                 else if ( stateId == DocumentState.STATE_WAITING_FOR_CHANGE_APPROVAL )
1174                                 {
1175                                     nActionId = DocumentAction.ACTION_VALIDATE_CHANGE;
1176                                 }
1177 
1178                                 if ( isAuthorized( nActionId, document ) )
1179                                 {
1180                                     DocumentService.getInstance(  )
1181                                                    .validateDocument( document, getUser(  ),
1182                                         DocumentState.STATE_VALIDATE );
1183                                     nbDocumentsAffected++;
1184                                 }
1185                             }
1186                             catch ( DocumentException e )
1187                             {
1188                                 return getErrorMessageUrl( request, e.getI18nMessage(  ) );
1189                             }
1190                         }
1191                         else if ( ( strAction.equals( CONSTANT_REFUSE ) ) &&
1192                                 ( ( stateId == DocumentState.STATE_WAITING_FOR_APPROVAL ) ||
1193                                 ( stateId == DocumentState.STATE_WAITING_FOR_CHANGE_APPROVAL ) ) )
1194                         {
1195                             try
1196                             {
1197                                 if ( stateId == DocumentState.STATE_WAITING_FOR_APPROVAL )
1198                                 {
1199                                     nActionId = DocumentAction.ACTION_REFUSE;
1200 
1201                                     if ( isAuthorized( nActionId, document ) )
1202                                     {
1203                                         DocumentService.getInstance(  )
1204                                                        .changeDocumentState( document, getUser(  ),
1205                                             DocumentState.STATE_REJECTED );
1206                                     }
1207                                 }
1208                                 else if ( stateId == DocumentState.STATE_WAITING_FOR_CHANGE_APPROVAL )
1209                                 {
1210                                     nActionId = DocumentAction.ACTION_REFUSE_CHANGE;
1211 
1212                                     if ( isAuthorized( nActionId, document ) )
1213                                     {
1214                                         DocumentService.getInstance(  )
1215                                                        .changeDocumentState( document, getUser(  ),
1216                                             DocumentState.STATE_IN_CHANGE );
1217                                     }
1218                                 }
1219 
1220                                 nbDocumentsAffected++;
1221                             }
1222                             catch ( DocumentException e )
1223                             {
1224                                 return getErrorMessageUrl( request, e.getI18nMessage(  ) );
1225                             }
1226                         }
1227                         else if ( ( strAction.equals( CONSTANT_UNARCHIVE ) ) &&
1228                                 ( stateId == DocumentState.STATE_ARCHIVED ) )
1229                         {
1230                             nActionId = DocumentAction.ACTION_UNARCHIVE;
1231 
1232                             try
1233                             {
1234                                 if ( isAuthorized( nActionId, document ) )
1235                                 {
1236                                     DocumentService.getInstance(  )
1237                                                    .changeDocumentState( document, getUser(  ),
1238                                         DocumentState.STATE_VALIDATE );
1239                                     nbDocumentsAffected++;
1240                                 }
1241                             }
1242                             catch ( DocumentException e )
1243                             {
1244                                 return getErrorMessageUrl( request, e.getI18nMessage(  ) );
1245                             }
1246                         }
1247                         else if ( strAction.equals( CONSTANT_SUBMIT ) )
1248                         {
1249                             try
1250                             {
1251                                 if ( ( stateId == DocumentState.STATE_WRITING ) ||
1252                                         ( stateId == DocumentState.STATE_REJECTED ) )
1253                                 {
1254                                     nActionId = DocumentAction.ACTION_SUBMIT;
1255                                     DocumentService.getInstance(  )
1256                                                    .changeDocumentState( document, getUser(  ),
1257                                         DocumentState.STATE_WAITING_FOR_APPROVAL );
1258                                     nbDocumentsAffected++;
1259                                 }
1260                                 else if ( ( stateId == DocumentState.STATE_IN_CHANGE ) )
1261                                 {
1262                                     nActionId = DocumentAction.ACTION_SUBMIT_CHANGE;
1263 
1264                                     if ( isAuthorized( nActionId, document ) )
1265                                     {
1266                                         DocumentService.getInstance(  )
1267                                                        .changeDocumentState( document, getUser(  ),
1268                                             DocumentState.STATE_WAITING_FOR_CHANGE_APPROVAL );
1269                                     }
1270 
1271                                     nbDocumentsAffected++;
1272                                 }
1273                             }
1274                             catch ( DocumentException e )
1275                             {
1276                                 return getErrorMessageUrl( request, e.getI18nMessage(  ) );
1277                             }
1278                         }
1279                     }
1280                 }
1281             }
1282         }
1283 
1284         return getHomeUrl( request );
1285     }
1286 
1287     /**
1288      * Perform the document filing
1289      * @param request The HTTP request
1290      * @return The URL to go after performing the action
1291      */
1292     public String doArchiveDocument( HttpServletRequest request )
1293     {
1294         saveReferer( request );
1295 
1296         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
1297         String strActionId = request.getParameter( PARAMETER_ACTION_ID );
1298 
1299         int nDocumentId = -1;
1300         int nActionId = -1;
1301 
1302         try
1303         {
1304             nDocumentId = Integer.parseInt( strDocumentId );
1305             nActionId = Integer.parseInt( strActionId );
1306         }
1307         catch ( NumberFormatException ne )
1308         {
1309             AppLogService.error( ne );
1310         }
1311 
1312         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocumentId );
1313         DocumentAction action = DocumentActionHome.findByPrimaryKey( nActionId );
1314 
1315         if ( ( action == null ) || ( action.getFinishDocumentState(  ) == null ) || ( document == null ) ||
1316                 !DocumentService.getInstance(  )
1317                                     .isAuthorizedAdminDocument( document.getSpaceId(  ),
1318                     document.getCodeDocumentType(  ), action.getPermission(  ), getUser(  ) ) )
1319         {
1320             return getHomeUrl( request );
1321         }
1322 
1323         try
1324         {
1325             DocumentService.getInstance(  )
1326                            .archiveDocument( document, getUser(  ), action.getFinishDocumentState(  ).getId(  ) );
1327         }
1328         catch ( DocumentException e )
1329         {
1330             return getErrorMessageUrl( request, e.getI18nMessage(  ) );
1331         }
1332 
1333         return getHomeUrl( request );
1334     }
1335 
1336     /**
1337      * Perform a document search by Id
1338      * @param request The HTTP request
1339      * @return The URL to go after performing the action
1340      */
1341     public String doSearchDocumentById( HttpServletRequest request )
1342     {
1343         saveReferer( request );
1344 
1345         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
1346         int nDocumentId;
1347 
1348         try
1349         {
1350             nDocumentId = Integer.parseInt( strDocumentId );
1351         }
1352         catch ( NumberFormatException e )
1353         {
1354             return AdminMessageService.getMessageUrl( request, MESSAGE_INVALID_DOCUMENT_ID, AdminMessage.TYPE_STOP );
1355         }
1356 
1357         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocumentId );
1358 
1359         if ( document == null )
1360         {
1361             return AdminMessageService.getMessageUrl( request, MESSAGE_DOCUMENT_NOT_FOUND, AdminMessage.TYPE_STOP );
1362         }
1363 
1364         if ( !DocumentService.getInstance(  )
1365                                  .isAuthorizedAdminDocument( document.getSpaceId(  ), document.getCodeDocumentType(  ),
1366                     DocumentTypeResourceIdService.PERMISSION_VIEW, getUser(  ) ) )
1367         {
1368             return AdminMessageService.getMessageUrl( request, MESSAGE_DOCUMENT_NOT_AUTHORIZED, AdminMessage.TYPE_STOP );
1369         }
1370 
1371         UrlItem url = new UrlItem( JSP_PREVIEW_DOCUMENT );
1372         url.addParameter( PARAMETER_DOCUMENT_ID, nDocumentId );
1373 
1374         return url.getUrl(  );
1375     }
1376 
1377     /**
1378      * Get the preview page
1379      * @param request The HTTP request
1380      * @return The preview page
1381      */
1382     public String getPreviewDocument( HttpServletRequest request )
1383     {
1384         saveReferer( request );
1385         setPageTitleProperty( PROPERTY_PREVIEW_DOCUMENT_PAGE_TITLE );
1386 
1387         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
1388         int nDocumentId = IntegerUtils.convert( strDocumentId );
1389 
1390         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nDocumentId );
1391 
1392         if ( ( document == null ) ||
1393                 !DocumentService.getInstance(  )
1394                                     .isAuthorizedAdminDocument( document.getSpaceId(  ),
1395                     document.getCodeDocumentType(  ), DocumentTypeResourceIdService.PERMISSION_VIEW, getUser(  ) ) )
1396         {
1397             return getManageDocuments( request );
1398         }
1399 
1400         document.setLocale( getLocale(  ) );
1401         DocumentService.getInstance(  ).getActions( document, getLocale(  ), getUser(  ) );
1402 
1403         DocumentType type = DocumentTypeHome.findByPrimaryKey( document.getCodeDocumentType(  ) );
1404 
1405         XmlTransformerService xmlTransformerService = new XmlTransformerService(  );
1406         String strPreview = xmlTransformerService.transformBySourceWithXslCache( document.getXmlWorkingContent(  ),
1407                 type.getAdminXslSource(  ), DOCUMENT_STYLE_PREFIX_ID + type.getAdminStyleSheetId(  ), null, null );
1408 
1409         Map<String, Object> model = new HashMap<String, Object>(  );
1410         model.put( MARK_DOCUMENT, document );
1411         model.put( MARK_PREVIEW, strPreview );
1412 
1413         ExtendableResourcePluginActionManager.fillModel( request, getUser(  ), model, strDocumentId,
1414             Document.PROPERTY_RESOURCE_TYPE );
1415 
1416         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_PREVIEW_DOCUMENT, getLocale(  ), model );
1417 
1418         return getAdminPage( template.getHtml(  ) );
1419     }
1420 
1421     /**
1422      * Perform the changing of space
1423      * @param request The HTTP request
1424      * @return The document move page
1425      */
1426     public String getMoveDocument( HttpServletRequest request )
1427     {
1428         saveReferer( request );
1429         setPageTitleProperty( PROPERTY_MOVE_DOCUMENT_PAGE_TITLE );
1430 
1431         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
1432         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( IntegerUtils.convert( strDocumentId ) );
1433 
1434         if ( ( document == null ) ||
1435                 !DocumentService.getInstance(  )
1436                                     .isAuthorizedAdminDocument( document.getSpaceId(  ),
1437                     document.getCodeDocumentType(  ), DocumentTypeResourceIdService.PERMISSION_MOVE, getUser(  ) ) )
1438         {
1439             return getManageDocuments( request );
1440         }
1441 
1442         Map<String, Object> model = new HashMap<String, Object>(  );
1443         String strSpaceId = request.getParameter( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID );
1444         boolean bSubmitButtonDisabled = Boolean.TRUE;
1445 
1446         if ( ( strSpaceId != null ) && !strSpaceId.equals( StringUtils.EMPTY ) )
1447         {
1448             bSubmitButtonDisabled = Boolean.FALSE;
1449         }
1450 
1451         // Spaces browser
1452         model.put( MARK_SPACES_BROWSER,
1453             DocumentSpacesService.getInstance(  ).getSpacesBrowser( request, getUser(  ), getLocale(  ), true, true ) );
1454         model.put( MARK_DOCUMENT, document );
1455         model.put( MARK_SUBMIT_BUTTON_DISABLED, bSubmitButtonDisabled );
1456 
1457         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MOVE_DOCUMENT, getLocale(  ), model );
1458 
1459         return getAdminPage( template.getHtml(  ) );
1460     }
1461 
1462     /**
1463      * Perform the document moving
1464      * @param request The HTTP request
1465      * @return The URL to go after performing the action
1466      */
1467     public String doMoveDocument( HttpServletRequest request )
1468     {
1469         saveReferer( request );
1470 
1471         boolean bTypeAllowed = Boolean.FALSE;
1472         String strSpaceId = request.getParameter( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID );
1473 
1474         if ( strSpaceId == null )
1475         {
1476             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
1477         }
1478 
1479         int nSpaceIdDestination = IntegerUtils.convert( strSpaceId );
1480         DocumentSpace space = DocumentSpaceHome.findByPrimaryKey( nSpaceIdDestination );
1481         String strDocumentId = request.getParameter( PARAMETER_DOCUMENT_ID );
1482         Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( IntegerUtils.convert( strDocumentId ) );
1483 
1484         if ( document == null )
1485         {
1486             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_ERROR );
1487         }
1488 
1489         // Check if user have rights to create a document into this space
1490         if ( !DocumentService.getInstance(  )
1491                                  .isAuthorizedAdminDocument( document.getSpaceId(  ), document.getCodeDocumentType(  ),
1492                     DocumentTypeResourceIdService.PERMISSION_MOVE, getUser(  ) ) ||
1493                 !DocumentService.getInstance(  )
1494                                     .isAuthorizedAdminDocument( nSpaceIdDestination, document.getCodeDocumentType(  ),
1495                     DocumentTypeResourceIdService.PERMISSION_MOVE, getUser(  ) ) )
1496         {
1497             return getHomeUrl( request );
1498         }
1499 
1500         for ( String documentType : space.getAllowedDocumentTypes(  ) )
1501         {
1502             if ( document.getCodeDocumentType(  ).equals( documentType ) )
1503             {
1504                 bTypeAllowed = Boolean.TRUE;
1505             }
1506         }
1507 
1508         if ( bTypeAllowed )
1509         {
1510             document.setSpaceId( nSpaceIdDestination );
1511             DocumentHome.update( document, false );
1512 
1513             return getHomeUrl( request );
1514         }
1515 
1516         return AdminMessageService.getMessageUrl( request, MESSAGE_MOVING_NOT_AUTHORIZED, AdminMessage.TYPE_STOP );
1517     }
1518 
1519     /**
1520      * Gets the document creation page
1521      * @param request The HTTP request
1522      * @return The document creation page
1523      */
1524     public IPluginActionResult getMassArchivalDocument( HttpServletRequest request )
1525     {
1526         IPluginActionResult result = new DefaultPluginActionResult(  );
1527 
1528         if ( !RBACService.isAuthorized( new DocumentMassAction(  ),
1529                     DocumentMassActionResourceService.PERMISSION_MASS_ARCHIVE, getUser(  ) ) )
1530         {
1531             result.setHtmlContent( getManageDocuments( request ) );
1532 
1533             return result;
1534         }
1535 
1536         // Empty previous just in case
1537         request.getSession(  ).removeAttribute( "to_archive" );
1538 
1539         String strAction = request.getParameter( DocumentJspBean.PARAMETER_ACTION );
1540         String strCriteria = request.getParameter( PARAMETER_ARCHIVAL_CRITERIA );
1541         String strTypeCode = request.getParameter( PARAMETER_DOCUMENT_TYPE_CODE );
1542         String strDateMin = request.getParameter( PARAMETER_DATE_MIN );
1543         String strDateMax = request.getParameter( PARAMETER_DATE_MAX );
1544 
1545         if ( StringUtils.isBlank( strCriteria ) )
1546         {
1547             strCriteria = (String) request.getSession(  ).getAttribute( PARAMETER_ARCHIVAL_CRITERIA );
1548             request.getSession(  ).removeAttribute( PARAMETER_ARCHIVAL_CRITERIA );
1549         }
1550 
1551         if ( StringUtils.isBlank( strTypeCode ) )
1552         {
1553             strTypeCode = (String) request.getSession(  ).getAttribute( PARAMETER_DOCUMENT_TYPE_CODE );
1554             request.getSession(  ).removeAttribute( PARAMETER_DOCUMENT_TYPE_CODE );
1555         }
1556 
1557         if ( StringUtils.isBlank( strDateMin ) )
1558         {
1559             strDateMin = (String) request.getSession(  ).getAttribute( PARAMETER_DATE_MIN );
1560             request.getSession(  ).removeAttribute( PARAMETER_DATE_MIN );
1561         }
1562 
1563         if ( StringUtils.isBlank( strDateMax ) )
1564         {
1565             strDateMax = (String) request.getSession(  ).getAttribute( PARAMETER_DATE_MAX );
1566             request.getSession(  ).removeAttribute( PARAMETER_DATE_MAX );
1567         }
1568 
1569         if ( "apply".equals( strAction ) )
1570         {
1571             DocumentFilternt/business/DocumentFilter.html#DocumentFilter">DocumentFilter filter = new DocumentFilter(  );
1572             filter.setIsPublished( false );
1573 
1574             request.getSession(  ).setAttribute( PARAMETER_ARCHIVAL_CRITERIA, strCriteria );
1575 
1576             if ( "date".equals( strCriteria ) )
1577             {
1578                 if ( StringUtils.isBlank( strDateMin ) && StringUtils.isBlank( strDateMax ) )
1579                 {
1580                     result.setRedirect( AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS,
1581                             "jsp/admin/plugins/document/MassArchivalDocument.jsp", AdminMessage.TYPE_ERROR ) );
1582                 }
1583                 else
1584                 {
1585                     try
1586                     {
1587                         SimpleDateFormat sdf = new SimpleDateFormat( "dd/MM/yyyy" );
1588                         SimpleDateFormat sqlSdf = new SimpleDateFormat( "yyyy-MM-dd" );
1589 
1590                         if ( StringUtils.isNotBlank( strDateMin ) )
1591                         {
1592                             Date dateMin;
1593                             dateMin = sdf.parse( strDateMin );
1594                             filter.setDateMin( sqlSdf.format( dateMin ) );
1595                         }
1596 
1597                         if ( StringUtils.isNotBlank( strDateMax ) )
1598                         {
1599                             Date dateMax = sdf.parse( strDateMax );
1600                             filter.setDateMax( sqlSdf.format( dateMax ) );
1601                         }
1602 
1603                         request.getSession(  ).setAttribute( PARAMETER_DATE_MIN, strDateMin );
1604                         request.getSession(  ).setAttribute( PARAMETER_DATE_MAX, strDateMax );
1605                     }
1606                     catch ( ParseException e )
1607                     {
1608                         AppLogService.error( e );
1609                     }
1610                 }
1611             }
1612             else if ( "space".equals( strCriteria ) )
1613             {
1614                 String strSpaceId = request.getParameter( DocumentSpacesService.PARAMETER_BROWSER_SELECTED_SPACE_ID );
1615 
1616                 if ( StringUtils.isBlank( strSpaceId ) || !StringUtils.isNumeric( strSpaceId ) )
1617                 {
1618                     result.setRedirect( AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS,
1619                             "jsp/admin/plugins/document/MassArchivalDocument.jsp", AdminMessage.TYPE_ERROR ) );
1620                 }
1621 
1622                 filter.setIdSpace( Integer.valueOf( strSpaceId ) );
1623             }
1624             else if ( "type".equals( strCriteria ) )
1625             {
1626                 request.getSession(  ).setAttribute( PARAMETER_DOCUMENT_TYPE_CODE, strTypeCode );
1627                 filter.setCodeDocumentType( strTypeCode );
1628             }
1629             else
1630             {
1631                 result.setRedirect( AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS,
1632                         "jsp/admin/plugins/document/MassArchivalDocument.jsp", AdminMessage.TYPE_ERROR ) );
1633             }
1634 
1635             if ( result.getRedirect(  ) == null )
1636             {
1637                 List<Document> documents = DocumentHome.findByFilter( filter, getLocale(  ) );
1638                 List<Document> toArchive = new ArrayList<Document>(  );
1639 
1640                 for ( Document doc : documents )
1641                 {
1642                     if ( isAuthorized( DocumentAction.ACTION_ARCHIVE, doc ) )
1643                     {
1644                         toArchive.add( doc );
1645                     }
1646                 }
1647 
1648                 if ( CollectionUtils.isNotEmpty( toArchive ) )
1649                 {
1650                     request.getSession(  ).setAttribute( "to_archive", toArchive );
1651                     result.setRedirect( AdminMessageService.getMessageUrl( request, MESSAGE_CONFIRM_MASS_ARCHIVE,
1652                             new String[] { toArchive.size(  ) + "" },
1653                             "jsp/admin/plugins/document/DoMassArchivalDocument.jsp", AdminMessage.TYPE_CONFIRMATION ) );
1654                 }
1655                 else
1656                 {
1657                     result.setRedirect( AdminMessageService.getMessageUrl( request, MESSAGE_NO_MASS_ARCHIVE,
1658                             "jsp/admin/plugins/document/MassArchivalDocument.jsp", AdminMessage.TYPE_ERROR ) );
1659                 }
1660             }
1661         }
1662         else
1663         {
1664             Map<String, Object> model = new HashMap<String, Object>(  );
1665             model.put( MARK_SELECTED_CRITERIA, StringUtils.defaultString( strCriteria, "date" ) );
1666             model.put( MARK_LOCALE, getLocale(  ).getLanguage(  ) );
1667             model.put( MARK_SPACES_BROWSER,
1668                 DocumentSpacesService.getInstance(  ).getSpacesBrowser( request, getUser(  ), getLocale(  ), false, true ) );
1669             model.put( MARK_DOCUMENT_TYPES_LIST, DocumentTypeHome.findAll(  ) );
1670             model.put( MARK_DOCUMENT_TYPE, StringUtils.defaultString( strTypeCode ) );
1671             model.put( MARK_DATE_MIN, StringUtils.defaultString( strDateMin ) );
1672             model.put( MARK_DATE_MAX, StringUtils.defaultString( strDateMax ) );
1673 
1674             HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MASS_ARCHIVAL, getLocale(  ), model );
1675             result.setHtmlContent( getAdminPage( template.getHtml(  ) ) );
1676         }
1677 
1678         return result;
1679     }
1680 
1681     /**
1682      * Gets the document creation page
1683      * @param request The HTTP request
1684      * @return The document creation page
1685      * @throws DocumentException
1686      */
1687     public String doMassArchivalDocument( HttpServletRequest request )
1688         throws DocumentException
1689     {
1690         List<Document> documents = (List<Document>) request.getSession(  ).getAttribute( "to_archive" );
1691         request.getSession(  ).removeAttribute( "to_archive" );
1692 
1693         for ( Document doc : documents )
1694         {
1695             DocumentService.getInstance(  ).archiveDocument( doc, getUser(  ), DocumentState.STATE_ARCHIVED );
1696         }
1697 
1698         return getManageDocuments( request );
1699     }
1700 
1701     ////////////////////////////////////////////////////////////////////////////
1702     // Private implementation to manage UI parameters
1703     private String getViewType( HttpServletRequest request )
1704     {
1705         String strViewType = request.getParameter( PARAMETER_VIEW_TYPE );
1706 
1707         if ( strViewType == null )
1708         {
1709             if ( _strViewType != null )
1710             {
1711                 strViewType = _strViewType;
1712             }
1713             else
1714             {
1715                 strViewType = AppPropertiesService.getProperty( PROPERTY_DEFAULT_VIEW_TYPE );
1716             }
1717         }
1718         else
1719         {
1720             updateSpaceView( strViewType );
1721         }
1722 
1723         return strViewType;
1724     }
1725 
1726     /**
1727      * View action
1728      * @param nSpaceId the space identifier
1729      */
1730     private void setView( int nSpaceId )
1731     {
1732         DocumentSpace space = DocumentSpaceHome.findByPrimaryKey( nSpaceId );
1733 
1734         if ( space != null )
1735         {
1736             _strViewType = space.getViewType(  );
1737         }
1738     }
1739 
1740     /**
1741      * Return document type filter
1742      * @param request The HttpServletRequest
1743      * @param filter The DocumentFilter object
1744      * @return the document type
1745      */
1746     private String getDocumentType( HttpServletRequest request, DocumentFilter filter )
1747     {
1748         // Filter for document type
1749         String strCodeDocumentTypeFilter = request.getParameter( PARAMETER_DOCUMENT_TYPE_CODE_FILTER );
1750 
1751         if ( strCodeDocumentTypeFilter == null )
1752         {
1753             if ( _strCurrentDocumentTypeFilter != null )
1754             {
1755                 strCodeDocumentTypeFilter = _strCurrentDocumentTypeFilter;
1756             }
1757             else
1758             {
1759                 strCodeDocumentTypeFilter = FILTER_ALL;
1760             }
1761         }
1762 
1763         if ( !strCodeDocumentTypeFilter.equals( FILTER_ALL ) )
1764         {
1765             filter.setCodeDocumentType( strCodeDocumentTypeFilter );
1766         }
1767 
1768         if ( !strCodeDocumentTypeFilter.equals( _strCurrentDocumentTypeFilter ) )
1769         {
1770             resetPageIndex(  );
1771         }
1772 
1773         return strCodeDocumentTypeFilter;
1774     }
1775 
1776     /**
1777      * Return State
1778      * @param request The HttpsServletRequest
1779      * @param filter The DocumentFilter object
1780      * @return the space identifier
1781      */
1782     private String getState( HttpServletRequest request, DocumentFilter filter )
1783     {
1784         String strStateId = request.getParameter( PARAMETER_STATE_ID_FILTER );
1785 
1786         if ( strStateId == null )
1787         {
1788             if ( _strCurrentStateFilter != null )
1789             {
1790                 strStateId = _strCurrentStateFilter;
1791             }
1792             else
1793             {
1794                 strStateId = FILTER_ALL;
1795             }
1796         }
1797 
1798         if ( !strStateId.equals( FILTER_ALL ) )
1799         {
1800             int nStateId = IntegerUtils.convert( strStateId );
1801             filter.setIdState( nStateId );
1802         }
1803 
1804         if ( !strStateId.equals( _strCurrentStateFilter ) )
1805         {
1806             resetPageIndex(  );
1807         }
1808 
1809         return strStateId;
1810     }
1811 
1812     /**
1813      * Return Space identifier
1814      * @param request The HttpsServletRequest
1815      * @param filter The DocumentFilter object
1816      * @return the space identifier
1817      */
1818     private String getSpaceId( HttpServletRequest request, DocumentFilter filter )
1819     {
1820         String strSpaceId = request.getParameter( PARAMETER_SPACE_ID_FILTER );
1821 
1822         if ( strSpaceId == null )
1823         {
1824             if ( ( _strCurrentSpaceId != null ) && !_strCurrentSpaceId.equals( "-1" ) )
1825             {
1826                 strSpaceId = _strCurrentSpaceId;
1827             }
1828             else
1829             {
1830                 int nSpaceId = DocumentSpacesService.getInstance(  ).getUserDefaultSpace( getUser(  ) );
1831                 strSpaceId = Integer.toString( nSpaceId );
1832             }
1833         }
1834 
1835         int nSpaceId = IntegerUtils.convert( strSpaceId );
1836         filter.setIdSpace( nSpaceId );
1837 
1838         if ( !strSpaceId.equals( _strCurrentSpaceId ) )
1839         {
1840             // Reset the page index
1841             resetPageIndex(  );
1842 
1843             // Sets the view corresponding to the new space
1844             setView( nSpaceId );
1845         }
1846 
1847         return strSpaceId;
1848     }
1849 
1850     /**
1851      * Reset Page Index
1852      */
1853     private void resetPageIndex(  )
1854     {
1855         _strCurrentPageIndex = PAGE_INDEX_FIRST;
1856     }
1857 
1858     /**
1859      * Update Space view
1860      * @param strViewType The type of view
1861      */
1862     private void updateSpaceView( String strViewType )
1863     {
1864         int nSpaceId = IntegerUtils.convert( _strCurrentSpaceId );
1865         DocumentSpace space = DocumentSpaceHome.findByPrimaryKey( nSpaceId );
1866 
1867         if ( space != null )
1868         {
1869             space.setViewType( strViewType );
1870             DocumentSpaceHome.update( space );
1871         }
1872     }
1873 
1874     /**
1875      * return admin message url for generic error with specific action message
1876      * @param request The HTTPrequest
1877      * @param strI18nMessage The i18n message
1878      * @return The admin message url
1879      */
1880     private String getErrorMessageUrl( HttpServletRequest request, String strI18nMessage )
1881     {
1882         return AdminMessageService.getMessageUrl( request, MESSAGE_DOCUMENT_ERROR,
1883             new String[] { I18nService.getLocalizedString( strI18nMessage, getLocale(  ) ) }, AdminMessage.TYPE_ERROR );
1884     }
1885 
1886     /**
1887      * Gets an html template displaying the patterns list available in the
1888      * portal for the layout
1889      *
1890      * @param nTemplatePageId The identifier of the layout to select in the list
1891      * @param nPageTemplateDocumentId The page template id
1892      * @param nIndexRow the index row
1893      * @return The html code of the list
1894      */
1895     private String getTemplatesPageList( int nTemplatePageId, int nPageTemplateDocumentId, String nIndexRow )
1896     {
1897         Map<String, Object> model = new HashMap<String, Object>(  );
1898         DocumentPageTemplate documentPageTemplate = DocumentPageTemplateHome.findByPrimaryKey( nTemplatePageId );
1899         model.put( MARK_DOCUMENT_PAGE_TEMPLATE, documentPageTemplate );
1900 
1901         model.put( MARK_INDEX_ROW, nIndexRow );
1902 
1903         String strChecked = ( documentPageTemplate.getId(  ) == nPageTemplateDocumentId ) ? "checked=\"checked\"" : "";
1904         model.put( MARK_DOCUMENT_PAGE_TEMPLATE_CHECKED, strChecked );
1905 
1906         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_DOCUMENT_PAGE_TEMPLATE_ROW, getLocale(  ),
1907                 model );
1908 
1909         return template.getHtml(  );
1910     }
1911 
1912     private boolean isAuthorized( int nActionId, Document document )
1913     {
1914         DocumentAction action = DocumentActionHome.findByPrimaryKey( nActionId );
1915 
1916         if ( ( action == null ) || ( action.getFinishDocumentState(  ) == null ) || ( document == null ) ||
1917                 !DocumentService.getInstance(  )
1918                                     .isAuthorizedAdminDocument( document.getSpaceId(  ),
1919                     document.getCodeDocumentType(  ), action.getPermission(  ), getUser(  ) ) )
1920         {
1921             return false;
1922         }
1923 
1924         return true;
1925     }
1926 
1927     /**
1928      * {@inheritDoc}
1929      * @return Return the URL of the menu if the user come from the menu, of the
1930      *         URL of the main page of this feature if he comes from anywhere
1931      *         else
1932      */
1933     @Override
1934     public String getHomeUrl( HttpServletRequest request )
1935     {
1936         if ( StringUtils.isNotBlank( _strSavedReferer ) )
1937         {
1938             return AppPathService.getBaseUrl( request ) + _strSavedReferer;
1939         }
1940 
1941         return super.getHomeUrl( request );
1942     }
1943 
1944     /**
1945      * {@inheritDoc}
1946      */
1947     @Override
1948     public void init( HttpServletRequest request, String strRight )
1949         throws AccessDeniedException, PasswordResetException
1950     {
1951         Right right = RightHome.findByPrimaryKey( strRight );
1952         _strFeatureUrl = right.getUrl(  );
1953         super.init( request, strRight );
1954     }
1955 
1956     /**
1957      * Saves the referer to redirect after the action is performed
1958      * @param request The request
1959      */
1960     private void saveReferer( HttpServletRequest request )
1961     {
1962         String strFromUrl = request.getParameter( PARAMETER_FROM_URL );
1963 
1964         if ( StringUtils.isNotBlank( strFromUrl ) )
1965         {
1966             _strSavedReferer = strFromUrl.replace( CONSTANT_AND_HTML, CONSTANT_AND );
1967         }
1968         else
1969         {
1970             String strReferer = request.getHeader( PARAMETER_HEADER_REFERER );
1971             String strAdminMenuUrl = AppPathService.getAdminMenuUrl(  );
1972 
1973             if ( StringUtils.contains( strReferer, strAdminMenuUrl ) )
1974             {
1975                 _strSavedReferer = strAdminMenuUrl;
1976             }
1977             else if ( StringUtils.contains( strReferer, _strFeatureUrl ) )
1978             {
1979                 _strSavedReferer = _strFeatureUrl;
1980             }
1981         }
1982     }
1983 }