View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de 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.modules.rest.rs;
35  
36  import fr.paris.lutece.plugins.document.business.Document;
37  import fr.paris.lutece.plugins.document.business.DocumentHome;
38  import fr.paris.lutece.plugins.document.business.DocumentType;
39  import fr.paris.lutece.plugins.document.business.DocumentTypeHome;
40  import fr.paris.lutece.plugins.document.business.IndexerAction;
41  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
42  import fr.paris.lutece.plugins.document.business.category.Category;
43  import fr.paris.lutece.plugins.document.business.category.CategoryHome;
44  import fr.paris.lutece.plugins.document.business.portlet.DocumentListPortletHome;
45  import fr.paris.lutece.plugins.document.business.portlet.DocumentPortletHome;
46  import fr.paris.lutece.plugins.document.business.portlet.PortletFilter;
47  import fr.paris.lutece.plugins.document.business.portlet.PortletOrder;
48  import fr.paris.lutece.plugins.document.business.publication.DocumentPublication;
49  import fr.paris.lutece.plugins.document.business.publication.DocumentPublicationHome;
50  import fr.paris.lutece.plugins.document.business.spaces.DocumentSpace;
51  import fr.paris.lutece.plugins.document.business.spaces.DocumentSpaceHome;
52  import fr.paris.lutece.plugins.document.business.workflow.DocumentAction;
53  import fr.paris.lutece.plugins.document.business.workflow.DocumentActionHome;
54  import fr.paris.lutece.plugins.document.modules.rest.util.builderxml.AddHeaderXml;
55  import fr.paris.lutece.plugins.document.modules.rest.util.builderxml.FieldsToCreateDocumentBuilderXml;
56  import fr.paris.lutece.plugins.document.modules.rest.util.builderxml.ResponseActionBuilderXml;
57  import fr.paris.lutece.plugins.document.modules.rest.util.constants.DocumentRestConstants;
58  import fr.paris.lutece.plugins.document.service.AttributeManager;
59  import fr.paris.lutece.plugins.document.service.AttributeService;
60  import fr.paris.lutece.plugins.document.service.DocumentException;
61  import fr.paris.lutece.plugins.document.service.DocumentPlugin;
62  import fr.paris.lutece.plugins.document.service.DocumentService;
63  import fr.paris.lutece.plugins.document.service.DocumentTypeResourceIdService;
64  import fr.paris.lutece.plugins.document.service.DocumentTypeService;
65  import fr.paris.lutece.plugins.document.service.attributes.DocumentAttributesService;
66  import fr.paris.lutece.plugins.document.service.metadata.MetadataHandler;
67  import fr.paris.lutece.plugins.document.service.publishing.PublishingService;
68  import fr.paris.lutece.plugins.document.service.search.DocumentIndexer;
69  import fr.paris.lutece.plugins.document.service.spaces.DocumentSpacesService;
70  import fr.paris.lutece.plugins.document.utils.DocumentIndexerUtils;
71  import fr.paris.lutece.plugins.rest.service.RestConstants;
72  import fr.paris.lutece.portal.business.resourceenhancer.ResourceEnhancer;
73  import fr.paris.lutece.portal.business.user.AdminUser;
74  import fr.paris.lutece.portal.business.user.AdminUserHome;
75  import fr.paris.lutece.portal.service.search.IndexationService;
76  import fr.paris.lutece.portal.service.template.AppTemplateService;
77  import fr.paris.lutece.portal.service.util.AppPathService;
78  import fr.paris.lutece.portal.service.util.AppPropertiesService;
79  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
80  import fr.paris.lutece.util.ReferenceItem;
81  import fr.paris.lutece.util.date.DateUtil;
82  import fr.paris.lutece.util.html.HtmlTemplate;
83  import fr.paris.lutece.util.http.MultipartUtil;
84  import fr.paris.lutece.util.string.StringUtil;
85  
86  import java.sql.Timestamp;
87  import java.util.ArrayList;
88  import java.util.Date;
89  import java.util.HashMap;
90  import java.util.List;
91  import java.util.Locale;
92  import java.util.Map;
93  
94  import javax.servlet.http.HttpServletRequest;
95  import javax.ws.rs.Consumes;
96  import javax.ws.rs.DELETE;
97  import javax.ws.rs.FormParam;
98  import javax.ws.rs.GET;
99  import javax.ws.rs.POST;
100 import javax.ws.rs.Path;
101 import javax.ws.rs.PathParam;
102 import javax.ws.rs.Produces;
103 import javax.ws.rs.core.Context;
104 import javax.ws.rs.core.MediaType;
105 
106 import org.apache.commons.fileupload.FileItem;
107 import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
108 import org.apache.commons.fileupload.FileUploadException;
109 import org.apache.commons.lang.StringUtils;
110 
111 /**
112  * DocumentRest
113  */
114 @Path( RestConstants.BASE_PATH + DocumentPlugin.PLUGIN_NAME )
115 public class DocumentRest
116 {
117     /**
118      * Get the wadl.xml content
119      * 
120      * @param request
121      *            {@link HttpServletRequest}
122      * @return the content of wadl.xml
123      */
124     @GET
125     @Path( DocumentRestConstants.PATH_WADL )
126     @Produces( MediaType.APPLICATION_XML )
127     public String getWADL( @Context HttpServletRequest request )
128     {
129         StringBuilder sbBase = new StringBuilder( AppPathService.getBaseUrl( request ) );
130 
131         if ( sbBase.toString( ).endsWith( DocumentRestConstants.SLASH ) )
132         {
133             sbBase.deleteCharAt( sbBase.length( ) - 1 );
134         }
135 
136         sbBase.append( RestConstants.BASE_PATH + DocumentPlugin.PLUGIN_NAME );
137 
138         Map<String, Object> model = new HashMap<String, Object>( );
139         model.put( DocumentRestConstants.MARK_BASE_URL, sbBase.toString( ) );
140 
141         HtmlTemplate t = AppTemplateService.getTemplate( DocumentRestConstants.TEMPLATE_WADL, request.getLocale( ), model );
142 
143         return t.getHtml( );
144     }
145 
146     /**
147      * Get document spaces by id user
148      * 
149      * @param strIdUser
150      *            the id user
151      * @return the xml of spaces
152      */
153     @GET
154     @Path( DocumentRestConstants.PATH_GET_USER_SPACES )
155     @Produces( MediaType.APPLICATION_XML )
156     public String getDocumentSpaces( @PathParam( DocumentRestConstants.PARAMETER_ID_USER ) String strIdUser )
157     {
158         if ( StringUtils.isNotBlank( strIdUser ) && StringUtils.isNumeric( strIdUser ) )
159         {
160             // get AdminUser
161             int nIdUser = Integer.parseInt( strIdUser );
162             AdminUser user = AdminUserHome.findByPrimaryKey( nIdUser );
163 
164             if ( user != null )
165             {
166                 user.setRights( AdminUserHome.getRightsListForUser( nIdUser ) );
167                 user.setRoles( AdminUserHome.getRolesListForUser( nIdUser ) );
168 
169                 // Spaces
170                 String strXmlSpaces = DocumentSpacesService.getInstance( ).getXmlSpacesList( user );
171 
172                 return AddHeaderXml.addHeaderXml( strXmlSpaces );
173             }
174         }
175 
176         return ResponseActionBuilderXml.getFailureResponseActionXML( );
177     }
178 
179     /**
180      * Get list of all spaces selected by a specific code document type
181      * 
182      * @param strCodeType
183      *            The specific code document type
184      * @return the xml of spaces by code document type
185      */
186     @GET
187     @Path( DocumentRestConstants.PATH_GET_SPACES_ALLOWING_DOCUMENT_CREATION_BY_CODE_DOCUMENT_TYPE )
188     @Produces( MediaType.APPLICATION_XML )
189     public String getDocumentSpacesByCodeType( @PathParam( DocumentRestConstants.PARAMETER_CODE_DOCUMENT_TYPE ) String strCodeType )
190     {
191         if ( StringUtils.isNotBlank( strCodeType ) )
192         {
193             // get AdminUser
194             int nIdUser = 1;
195             AdminUser user = AdminUserHome.findByPrimaryKey( nIdUser );
196 
197             if ( user != null )
198             {
199                 user.setRights( AdminUserHome.getRightsListForUser( nIdUser ) );
200                 user.setRoles( AdminUserHome.getRolesListForUser( nIdUser ) );
201 
202                 // Spaces
203                 String strXmlSpaces = DocumentSpacesService.getInstance( ).getXmlSpacesList( user, strCodeType );
204 
205                 return AddHeaderXml.addHeaderXml( strXmlSpaces );
206             }
207         }
208 
209         return ResponseActionBuilderXml.getFailureResponseActionXML( );
210     }
211 
212     /**
213      * Get all atributes of a selected document type
214      * 
215      * @param strCodeType
216      *            The code document type
217      * @return A xml list of all attributes of the document type
218      */
219     @GET
220     @Path( DocumentRestConstants.PATH_GET_ATTRIBUTE_BY_DOCUMENT_TYPE )
221     @Produces( MediaType.APPLICATION_XML )
222     public String getDocumentAttributesByCodeType( @PathParam( DocumentRestConstants.PARAMETER_CODE_DOCUMENT_TYPE ) String strCodeType )
223     {
224         if ( StringUtils.isNotBlank( strCodeType ) )
225         {
226             // Attributes
227             String strXmlAttributes = DocumentAttributesService.getInstance( ).getXmlAttributesList( strCodeType );
228 
229             return AddHeaderXml.addHeaderXml( strXmlAttributes );
230         }
231 
232         return ResponseActionBuilderXml.getFailureResponseActionXML( );
233     }
234 
235     /**
236      * Get space by id
237      * 
238      * @param strIdSpace
239      *            the id space
240      * @return the xml of space informations
241      */
242     @GET
243     @Path( DocumentRestConstants.PATH_GET_SPACE )
244     @Produces( MediaType.APPLICATION_XML )
245     public List<DocumentSpace> getSpaceById( @PathParam( DocumentRestConstants.PARAMETER_ID_SPACE ) String strIdSpace )
246     {
247         if ( StringUtils.isNotBlank( strIdSpace ) && StringUtils.isNumeric( strIdSpace ) )
248         {
249             int nIdSpace = Integer.parseInt( strIdSpace );
250             List<DocumentSpace> documentSpaceList = new ArrayList<DocumentSpace>( );
251             DocumentSpace documentSpace = DocumentSpaceHome.findByPrimaryKey( nIdSpace );
252             documentSpaceList.add( documentSpace );
253 
254             return documentSpaceList;
255         }
256 
257         return null;
258     }
259 
260     /**
261      * Get documents list by id space
262      * 
263      * @param strIdSpace
264      *            the id space
265      * @return the xml of documents list
266      */
267     @GET
268     @Path( DocumentRestConstants.PATH_GET_DOCUMENTS_LIST_BY_SPACE )
269     @Produces( MediaType.APPLICATION_XML )
270     public List<Document> getDocumentsList( @PathParam( DocumentRestConstants.PARAMETER_ID_SPACE ) String strIdSpace )
271     {
272         if ( StringUtils.isNotBlank( strIdSpace ) && StringUtils.isNumeric( strIdSpace ) )
273         {
274             int nIdSpace = Integer.parseInt( strIdSpace );
275             List<Document> documentList = DocumentHome.findBySpaceKey( nIdSpace );
276 
277             return documentList;
278         }
279 
280         return null;
281     }
282 
283     /**
284      * Get document by id document
285      * 
286      * @param strIdDocument
287      *            the id document
288      * @return the xml of document
289      */
290     @GET
291     @Path( DocumentRestConstants.PATH_GET_DOCUMENT )
292     @Produces( MediaType.APPLICATION_XML )
293     public List<Document> getDocument( @PathParam( DocumentRestConstants.PARAMETER_ID_DOCUMENT ) String strIdDocument )
294     {
295         if ( StringUtils.isNotBlank( strIdDocument ) && StringUtils.isNumeric( strIdDocument ) )
296         {
297             int nIdDocument = Integer.parseInt( strIdDocument );
298             List<Document> documentList = new ArrayList<Document>( );
299             Document document = DocumentHome.findByPrimaryKey( nIdDocument );
300             documentList.add( document );
301 
302             return documentList;
303         }
304 
305         return null;
306     }
307 
308     /**
309      * Get different type document fields to create it
310      * 
311      * @param strCodeDocumentType
312      *            type document code
313      * @return
314      */
315     @GET
316     @Path( DocumentRestConstants.PATH_CREATE_GET_FIELDS )
317     @Produces( MediaType.APPLICATION_XML )
318     public String getCreatingDocumentFields( @PathParam( DocumentRestConstants.PARAMETER_CODE_DOCUMENT_TYPE ) String strCodeDocumentType )
319     {
320         if ( StringUtils.isNotBlank( strCodeDocumentType ) )
321         {
322             return FieldsToCreateDocumentBuilderXml.buildXml( strCodeDocumentType );
323         }
324 
325         return ResponseActionBuilderXml.getFailureResponseActionXML( );
326     }
327 
328     /**
329      * Get list of different types document
330      * 
331      * @return the list of Document Type on XML file
332      */
333     @GET
334     @Path( DocumentRestConstants.PATH_GET_LIST_DOCUMENT_TYPE )
335     @Produces( MediaType.APPLICATION_XML )
336     public String getListDocumentTypes( )
337     {
338         // Types
339         String strXmlTypesList = DocumentTypeService.getInstance( ).getXmlDocumentTypesList( );
340 
341         return AddHeaderXml.addHeaderXml( strXmlTypesList );
342     }
343 
344     /**
345      * Remove the document by id
346      * 
347      * @param strIdDocument
348      *            id document
349      * @return
350      */
351     @DELETE
352     @Path( DocumentRestConstants.PATH_REMOVE_DOCUMENT )
353     @Produces( MediaType.APPLICATION_XML )
354     public String getDeletingDocument( @PathParam( DocumentRestConstants.PARAMETER_ID_DOCUMENT ) String strIdDocument )
355     {
356         if ( StringUtils.isNotBlank( strIdDocument ) && StringUtils.isNumeric( strIdDocument ) )
357         {
358             int nDocumentId = Integer.parseInt( strIdDocument );
359             DocumentHome.remove( nDocumentId );
360 
361             return ResponseActionBuilderXml.getSuccessResponseActionXML( );
362         }
363 
364         return ResponseActionBuilderXml.getFailureResponseActionXML( );
365     }
366 
367     /**
368      * Get the list of document portlets
369      * 
370      * @param strIdDocument
371      *            id document
372      * @return listPortlet portlets list (will be formatted in xml format)
373      */
374     @GET
375     @Path( DocumentRestConstants.PATH_PORTLETS_DOCUMENT )
376     @Produces( MediaType.APPLICATION_XML )
377     public List<ReferenceItem> getPortletsToAssignDocument( @PathParam( DocumentRestConstants.PARAMETER_ID_DOCUMENT ) String strIdDocument )
378     {
379         if ( StringUtils.isNotBlank( strIdDocument ) && StringUtils.isNumeric( strIdDocument ) )
380         {
381             int nDocumentId = Integer.parseInt( strIdDocument );
382             PortletOrder pOrder = new PortletOrder( );
383             PortletFilter pFilter = new PortletFilter( );
384             List<ReferenceItem> listPortlet = (List<ReferenceItem>) DocumentPortletHome.findByCodeDocumentTypeAndCategory( nDocumentId, DocumentHome
385                     .findByPrimaryKey( nDocumentId ).getCodeDocumentType( ), pOrder, pFilter );
386 
387             return listPortlet;
388         }
389 
390         return null;
391     }
392 
393     /**
394      * Get the list of list documents portlets
395      * 
396      * @param strIdDocument
397      * @return listPortlet portlets list (will be formatted in xml format)
398      */
399     @GET
400     @Path( DocumentRestConstants.PATH_PORTLETS_LIST_DOCUMENT )
401     @Produces( MediaType.APPLICATION_XML )
402     public List<ReferenceItem> getPortletsListToAssignDocument( @PathParam( DocumentRestConstants.PARAMETER_ID_DOCUMENT ) String strIdDocument )
403     {
404         if ( StringUtils.isNotBlank( strIdDocument ) && StringUtils.isNumeric( strIdDocument ) )
405         {
406             int nDocumentId = Integer.parseInt( strIdDocument );
407             PortletOrder pOrder = new PortletOrder( );
408             PortletFilter pFilter = new PortletFilter( );
409             List<ReferenceItem> listPortletList = (List<ReferenceItem>) DocumentListPortletHome.findByCodeDocumentTypeAndCategory( nDocumentId, DocumentHome
410                     .findByPrimaryKey( nDocumentId ).getCodeDocumentType( ), pOrder, pFilter );
411 
412             return listPortletList;
413         }
414 
415         return null;
416     }
417 
418     /**
419      * Get the list of document/list documents portlets where the document is assigned
420      * 
421      * @param strIdDocument
422      *            id document
423      * @return listDocumentPublication DocumentPublication list (will be formatted in xml format)
424      */
425     @GET
426     @Path( DocumentRestConstants.PATH_PORTLETS_DOCUMENT_ASSIGNED )
427     @Produces( MediaType.APPLICATION_XML )
428     public List<DocumentPublication> getPortletsToPublishDocument( @PathParam( DocumentRestConstants.PARAMETER_ID_DOCUMENT ) String strIdDocument )
429     {
430         if ( StringUtils.isNotBlank( strIdDocument ) && StringUtils.isNumeric( strIdDocument ) )
431         {
432             int nDocumentId = Integer.parseInt( strIdDocument );
433 
434             List<DocumentPublication> listDocumentPublication = (List<DocumentPublication>) DocumentPublicationHome.findByDocumentId( nDocumentId );
435 
436             return listDocumentPublication;
437         }
438 
439         return null;
440     }
441 
442     /**
443      * Method to submit or not a document to validate it
444      * 
445      * @param strIdDocument
446      *            id document
447      * @param strIdAction
448      *            id action
449      * @param strIdUser
450      *            id user
451      * @param request
452      *            request
453      * @return response xml
454      */
455     @POST
456     @Path( DocumentRestConstants.PATH_DOCUMENT_SUBMIT )
457     @Produces( MediaType.APPLICATION_XML )
458     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
459     public String doSubmitOrUnsubmitDocumentToValidate( @FormParam( DocumentRestConstants.PARAMETER_ID_DOCUMENT ) String strIdDocument,
460             @FormParam( DocumentRestConstants.PARAMETER_ID_ACTION ) String strIdAction, @FormParam( DocumentRestConstants.PARAMETER_ID_USER ) String strIdUser,
461             @Context HttpServletRequest request )
462     {
463         if ( StringUtils.isNotBlank( strIdUser ) && StringUtils.isNumeric( strIdUser ) && StringUtils.isNotBlank( strIdAction )
464                 && StringUtils.isNumeric( strIdAction ) && StringUtils.isNotBlank( strIdDocument ) && StringUtils.isNumeric( strIdDocument ) )
465         {
466             int nIdAction = Integer.parseInt( strIdAction );
467             int nIdDocument = Integer.parseInt( strIdDocument );
468             int nIdUser = Integer.parseInt( strIdUser );
469             AdminUser user = AdminUserHome.findByPrimaryKey( nIdUser );
470 
471             if ( user != null )
472             {
473                 user.setRights( AdminUserHome.getRightsListForUser( nIdUser ) );
474                 user.setRoles( AdminUserHome.getRolesListForUser( nIdUser ) );
475 
476                 Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nIdDocument );
477                 DocumentAction action = DocumentActionHome.findByPrimaryKey( nIdAction );
478 
479                 if ( ( action == null )
480                         || ( action.getFinishDocumentState( ) == null )
481                         || ( document == null )
482                         || !DocumentService.getInstance( ).isAuthorizedAdminDocument( document.getSpaceId( ), document.getCodeDocumentType( ),
483                                 action.getPermission( ), user ) )
484                 {
485                     return ResponseActionBuilderXml.getFailureResponseActionXML( );
486                 }
487 
488                 try
489                 {
490                     DocumentService.getInstance( ).changeDocumentState( document, user, action.getFinishDocumentState( ).getId( ) );
491                 }
492                 catch( DocumentException e )
493                 {
494                     return ResponseActionBuilderXml.getFailureResponseActionXML( );
495                 }
496 
497                 return ResponseActionBuilderXml.getSuccessResponseActionXML( );
498             }
499         }
500 
501         return ResponseActionBuilderXml.getFailureResponseActionXML( );
502     }
503 
504     /**
505      * Validate a document
506      * 
507      * @param strIdDocument
508      *            id document
509      * @param strIdAction
510      *            id action
511      * @param strIdUser
512      *            id user
513      * @param request
514      *            request
515      * @return response
516      */
517     @POST
518     @Path( DocumentRestConstants.PATH_DOCUMENT_VALIDATE )
519     @Produces( MediaType.APPLICATION_XML )
520     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
521     public String doValidateDocument( @FormParam( DocumentRestConstants.PARAMETER_ID_DOCUMENT ) String strIdDocument,
522             @FormParam( DocumentRestConstants.PARAMETER_ID_ACTION ) String strIdAction, @FormParam( DocumentRestConstants.PARAMETER_ID_USER ) String strIdUser,
523             @Context HttpServletRequest request )
524     {
525         if ( StringUtils.isNotBlank( strIdUser ) && StringUtils.isNumeric( strIdUser ) && StringUtils.isNotBlank( strIdAction )
526                 && StringUtils.isNumeric( strIdAction ) && StringUtils.isNotBlank( strIdDocument ) && StringUtils.isNumeric( strIdDocument ) )
527         {
528             int nIdAction = Integer.parseInt( strIdAction );
529             int nIdDocument = Integer.parseInt( strIdDocument );
530             int nIdUser = Integer.parseInt( strIdUser );
531             AdminUser user = AdminUserHome.findByPrimaryKey( nIdUser );
532 
533             if ( user != null )
534             {
535                 user.setRights( AdminUserHome.getRightsListForUser( nIdUser ) );
536                 user.setRoles( AdminUserHome.getRolesListForUser( nIdUser ) );
537 
538                 Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( nIdDocument );
539                 DocumentAction action = DocumentActionHome.findByPrimaryKey( nIdAction );
540 
541                 if ( ( action == null )
542                         || ( action.getFinishDocumentState( ) == null )
543                         || ( document == null )
544                         || !DocumentService.getInstance( ).isAuthorizedAdminDocument( document.getSpaceId( ), document.getCodeDocumentType( ),
545                                 action.getPermission( ), user ) )
546                 {
547                     return ResponseActionBuilderXml.getFailureResponseActionXML( );
548                 }
549 
550                 try
551                 {
552                     DocumentService.getInstance( ).validateDocument( document, user, action.getFinishDocumentState( ).getId( ) );
553                 }
554                 catch( DocumentException e )
555                 {
556                     return ResponseActionBuilderXml.getFailureResponseActionXML( );
557                 }
558 
559                 IndexationService.addIndexerAction( strIdDocument, DocumentIndexer.INDEXER_NAME, IndexerAction.TASK_MODIFY, IndexationService.ALL_DOCUMENT );
560 
561                 DocumentIndexerUtils.addIndexerAction( strIdDocument, IndexerAction.TASK_MODIFY, IndexationService.ALL_DOCUMENT );
562 
563                 return ResponseActionBuilderXml.getSuccessResponseActionXML( );
564             }
565             else
566             {
567                 return ResponseActionBuilderXml.getFailureResponseActionXML( );
568             }
569         }
570 
571         return ResponseActionBuilderXml.getFailureResponseActionXML( );
572     }
573 
574     /**
575      * Assign a document to the portlet
576      * 
577      * @param strIdDocument
578      *            id document
579      * @param strIdPortlet
580      *            id portlet
581      * @param request
582      *            request
583      * @return response xml
584      */
585     @POST
586     @Path( DocumentRestConstants.PATH_DOCUMENT_ASSIGN_PORTLET )
587     @Produces( MediaType.APPLICATION_XML )
588     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
589     public String doAssignDocumentToPortlet( @FormParam( DocumentRestConstants.PARAMETER_ID_DOCUMENT ) String strIdDocument,
590             @FormParam( DocumentRestConstants.PARAMETER_ID_PORTLET ) String strIdPortlet, @Context HttpServletRequest request )
591     {
592         if ( StringUtils.isNotBlank( strIdPortlet ) && StringUtils.isNumeric( strIdPortlet ) && StringUtils.isNotBlank( strIdDocument )
593                 && StringUtils.isNumeric( strIdDocument ) )
594         {
595             int nIdPortlet = Integer.parseInt( strIdPortlet );
596             int nIdDocument = Integer.parseInt( strIdDocument );
597             PublishingService.getInstance( ).assign( nIdDocument, nIdPortlet );
598 
599             return ResponseActionBuilderXml.getSuccessResponseActionXML( );
600         }
601 
602         return ResponseActionBuilderXml.getFailureResponseActionXML( );
603     }
604 
605     /**
606      * Unassign a document to the portlet
607      * 
608      * @param strIdDocument
609      *            id document
610      * @param strIdPortlet
611      *            id portlet
612      * @param request
613      *            request
614      * @return response xml
615      */
616     @POST
617     @Path( DocumentRestConstants.PATH_DOCUMENT_UNASSIGN_PORTLET )
618     @Produces( MediaType.APPLICATION_XML )
619     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
620     public String doUnassignDocumentToPortlet( @FormParam( DocumentRestConstants.PARAMETER_ID_DOCUMENT ) String strIdDocument,
621             @FormParam( DocumentRestConstants.PARAMETER_ID_PORTLET ) String strIdPortlet, @Context HttpServletRequest request )
622     {
623         if ( StringUtils.isNotBlank( strIdPortlet ) && StringUtils.isNumeric( strIdPortlet ) && StringUtils.isNotBlank( strIdDocument )
624                 && StringUtils.isNumeric( strIdDocument ) )
625         {
626             int nIdPortlet = Integer.parseInt( strIdPortlet );
627             int nIdDocument = Integer.parseInt( strIdDocument );
628             PublishingService.getInstance( ).unAssign( nIdDocument, nIdPortlet );
629 
630             return ResponseActionBuilderXml.getSuccessResponseActionXML( );
631         }
632 
633         return ResponseActionBuilderXml.getFailureResponseActionXML( );
634     }
635 
636     /**
637      * Publish a document
638      * 
639      * @param strIdDocument
640      *            id document
641      * @param strIdPortlet
642      *            id portlet
643      * @param request
644      *            request
645      * @return response xml
646      */
647     @POST
648     @Path( DocumentRestConstants.PATH_DOCUMENT_PUBLISH )
649     @Produces( MediaType.APPLICATION_XML )
650     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
651     public String doPublishDocument( @FormParam( DocumentRestConstants.PARAMETER_ID_DOCUMENT ) String strIdDocument,
652             @FormParam( DocumentRestConstants.PARAMETER_ID_PORTLET ) String strIdPortlet, @Context HttpServletRequest request )
653     {
654         if ( StringUtils.isNotBlank( strIdPortlet ) && StringUtils.isNumeric( strIdPortlet ) && StringUtils.isNotBlank( strIdDocument )
655                 && StringUtils.isNumeric( strIdDocument ) )
656         {
657             int nIdPortlet = Integer.parseInt( strIdPortlet );
658             int nIdDocument = Integer.parseInt( strIdDocument );
659             PublishingService.getInstance( ).publish( nIdDocument, nIdPortlet );
660 
661             return ResponseActionBuilderXml.getSuccessResponseActionXML( );
662         }
663 
664         return ResponseActionBuilderXml.getFailureResponseActionXML( );
665     }
666 
667     /**
668      * Unpublish a document
669      * 
670      * @param strIdDocument
671      *            id document
672      * @param strIdPortlet
673      *            id portlet
674      * @param request
675      *            request
676      * @return response xml
677      */
678     @POST
679     @Path( DocumentRestConstants.PATH_DOCUMENT_UNPUBLISH )
680     @Produces( MediaType.APPLICATION_XML )
681     @Consumes( MediaType.APPLICATION_FORM_URLENCODED )
682     public String doUnpublishDocument( @FormParam( DocumentRestConstants.PARAMETER_ID_DOCUMENT ) String strIdDocument,
683             @FormParam( DocumentRestConstants.PARAMETER_ID_PORTLET ) String strIdPortlet, @Context HttpServletRequest request )
684     {
685         if ( StringUtils.isNotBlank( strIdPortlet ) && StringUtils.isNumeric( strIdPortlet ) && StringUtils.isNotBlank( strIdDocument )
686                 && StringUtils.isNumeric( strIdDocument ) )
687         {
688             int nIdPortlet = Integer.parseInt( strIdPortlet );
689             int nIdDocument = Integer.parseInt( strIdDocument );
690             PublishingService.getInstance( ).unPublish( nIdDocument, nIdPortlet );
691 
692             return ResponseActionBuilderXml.getSuccessResponseActionXML( );
693         }
694 
695         return ResponseActionBuilderXml.getFailureResponseActionXML( );
696     }
697 
698     /**
699      * Create document
700      * 
701      * @param request
702      *            request
703      * @return response xml
704      */
705     @POST
706     @Path( DocumentRestConstants.PATH_CREATE_DOCUMENT )
707     @Consumes( MediaType.MULTIPART_FORM_DATA )
708     @Produces( {
709             MediaType.TEXT_HTML, MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON
710     } )
711     public String doCreateDocument( @Context HttpServletRequest request )
712     {
713         String strResponseXml = StringUtils.EMPTY;
714         int nSizeThreshold = AppPropertiesService.getPropertyInt( DocumentRestConstants.PROPERTY_MULTIPART_SIZE_THRESHOLD, -1 );
715         boolean bActivateNormalizeFileName = Boolean.getBoolean( AppPropertiesService
716                 .getProperty( DocumentRestConstants.PROPERTY_MULTIPART_NORMALIZE_FILE_NAME ) );
717         String strRequestSizeMax = AppPropertiesService.getProperty( DocumentRestConstants.PROPERTY_MULTIPART_REQUEST_SIZE_MAX );
718         long nRequestSizeMax = 0;
719 
720         if ( StringUtils.isNotBlank( strRequestSizeMax ) && StringUtils.isNumeric( strRequestSizeMax ) )
721         {
722             nRequestSizeMax = Long.parseLong( strRequestSizeMax );
723         }
724 
725         try
726         {
727             MultipartHttpServletRequest multipartRequest = MultipartUtil.convert( nSizeThreshold, nRequestSizeMax, bActivateNormalizeFileName, request );
728 
729             if ( multipartRequest != null )
730             {
731                 strResponseXml = createDocument( multipartRequest );
732             }
733         }
734         catch( SizeLimitExceededException e )
735         {
736             return ResponseActionBuilderXml.getFailureResponseActionXML( );
737         }
738         catch( FileUploadException e )
739         {
740             return ResponseActionBuilderXml.getFailureResponseActionXML( );
741         }
742 
743         return strResponseXml;
744     }
745 
746     private String createDocument( MultipartHttpServletRequest multipartRequest )
747     {
748         if ( StringUtils.isNotBlank( multipartRequest.getParameter( DocumentRestConstants.PARAMETER_ID_USER ) )
749                 && StringUtils.isNumeric( multipartRequest.getParameter( DocumentRestConstants.PARAMETER_ID_USER ) ) )
750         {
751             // Create AdminUser
752             int nIdUser = Integer.parseInt( multipartRequest.getParameter( DocumentRestConstants.PARAMETER_ID_USER ) );
753             AdminUser user = AdminUserHome.findByPrimaryKey( nIdUser );
754 
755             if ( user != null )
756             {
757                 user.setRights( AdminUserHome.getRightsListForUser( nIdUser ) );
758                 user.setRoles( AdminUserHome.getRolesListForUser( nIdUser ) );
759 
760                 String strDocumentTypeCode = multipartRequest.getParameter( DocumentRestConstants.PARAMETER_CODE_DOCUMENT_TYPE );
761 
762                 if ( !DocumentService.getInstance( ).isAuthorizedAdminDocument(
763                         Integer.parseInt( multipartRequest.getParameter( DocumentRestConstants.PARAMETER_ID_SPACE ) ), strDocumentTypeCode,
764                         DocumentTypeResourceIdService.PERMISSION_CREATE, user ) )
765                 {
766                     return ResponseActionBuilderXml.getFailureResponseActionXML( );
767                 }
768 
769                 Document document = new Document( );
770                 document.setCodeDocumentType( strDocumentTypeCode );
771 
772                 String strError = getDocumentData( multipartRequest, document, multipartRequest.getLocale( ) );
773 
774                 if ( strError != null )
775                 {
776                     return strError;
777                 }
778 
779                 document.setSpaceId( Integer.parseInt( multipartRequest.getParameter( DocumentRestConstants.PARAMETER_ID_SPACE ) ) );
780                 document.setStateId( 1 );
781                 document.setCreatorId( user.getUserId( ) );
782 
783                 try
784                 {
785                     DocumentService.getInstance( ).createDocument( document, user );
786                 }
787                 catch( DocumentException e )
788                 {
789                     return ResponseActionBuilderXml.getFailureResponseActionXML( );
790                 }
791 
792                 // process
793                 ResourceEnhancer.doCreateResourceAddOn( multipartRequest, DocumentRestConstants.PROPERTY_RESOURCE_TYPE, document.getId( ) );
794             }
795         }
796         else
797         {
798             return ResponseActionBuilderXml.getFailureResponseActionXML( );
799         }
800 
801         return ResponseActionBuilderXml.getSuccessResponseActionXML( );
802     }
803 
804     /**
805      * Return the data of a document object
806      * 
807      * @param multipartRequest
808      *            map of parameters
809      * @param document
810      *            The document object
811      * @return data of document object
812      */
813     public static String getDocumentData( MultipartHttpServletRequest multipartRequest, Document document, Locale locale )
814     {
815         String strDocumentTitle = multipartRequest.getParameter( DocumentRestConstants.PARAMETER_DOCUMENT_TITLE );
816         String strDocumentSummary = multipartRequest.getParameter( DocumentRestConstants.PARAMETER_DOCUMENT_SUMMARY );
817         String strDocumentComment = multipartRequest.getParameter( DocumentRestConstants.PARAMETER_DOCUMENT_COMMENT );
818         String strDateValidityBegin = multipartRequest.getParameter( DocumentRestConstants.PARAMETER_VALIDITY_BEGIN );
819         String strDateValidityEnd = multipartRequest.getParameter( DocumentRestConstants.PARAMETER_VALIDITY_END );
820         String strMailingListId = ( multipartRequest.getParameter( DocumentRestConstants.PARAMETER_MAILING_LIST ) != null ) ? multipartRequest
821                 .getParameter( DocumentRestConstants.PARAMETER_MAILING_LIST ) : "0";
822         int nMailingListId = Integer.parseInt( strMailingListId );
823         String strPageTemplateDocumentId = ( multipartRequest.getParameter( DocumentRestConstants.PARAMETER_PAGE_TEMPLATE_DOCUMENT_ID ) != null ) ? multipartRequest
824                 .getParameter( DocumentRestConstants.PARAMETER_PAGE_TEMPLATE_DOCUMENT_ID ) : "0";
825         int nPageTemplateDocumentId = Integer.parseInt( strPageTemplateDocumentId );
826         String [ ] listCategory = multipartRequest.getParameterValues( DocumentRestConstants.PARAMETER_CATEGORY );
827 
828         // Check for mandatory value
829         if ( strDocumentTitle.trim( ).equals( "" ) || strDocumentSummary.trim( ).equals( "" ) )
830         {
831             return ResponseActionBuilderXml.getFailureResponseActionXML( );
832         }
833 
834         // Check for illegal character character
835         if ( StringUtil.containsHtmlSpecialCharacters( strDocumentTitle ) || StringUtil.containsHtmlSpecialCharacters( strDocumentSummary ) )
836         {
837             return ResponseActionBuilderXml.getFailureResponseActionXML( );
838         }
839 
840         DocumentType documentType = DocumentTypeHome.findByPrimaryKey( document.getCodeDocumentType( ) );
841         List<DocumentAttribute> listAttributes = documentType.getAttributes( );
842 
843         for ( DocumentAttribute attribute : listAttributes )
844         {
845             String strAdminMessage = setAttribute( attribute, document, multipartRequest, locale );
846 
847             if ( strAdminMessage != null )
848             {
849                 return strAdminMessage;
850             }
851         }
852 
853         Timestamp dateValidityBegin = null;
854         Timestamp dateValidityEnd = null;
855 
856         if ( ( strDateValidityBegin != null ) && !strDateValidityBegin.equals( "" ) )
857         {
858             Date dateBegin = DateUtil.formatDateLongYear( strDateValidityBegin, locale );
859 
860             if ( ( dateBegin == null ) )
861             {
862                 return ResponseActionBuilderXml.getFailureResponseActionXML( );
863             }
864 
865             dateValidityBegin = new Timestamp( dateBegin.getTime( ) );
866 
867             if ( dateValidityBegin.before( new Timestamp( 0 ) ) )
868             {
869                 return ResponseActionBuilderXml.getFailureResponseActionXML( );
870             }
871         }
872 
873         if ( ( strDateValidityEnd != null ) && !strDateValidityEnd.equals( "" ) )
874         {
875             Date dateEnd = DateUtil.formatDateLongYear( strDateValidityEnd, locale );
876 
877             if ( ( dateEnd == null ) )
878             {
879                 return ResponseActionBuilderXml.getFailureResponseActionXML( );
880             }
881 
882             dateValidityEnd = new Timestamp( dateEnd.getTime( ) );
883 
884             if ( dateValidityEnd.before( new Timestamp( 0 ) ) )
885             {
886                 return ResponseActionBuilderXml.getFailureResponseActionXML( );
887             }
888         }
889 
890         // validate period (dateEnd > dateBegin )
891         if ( ( dateValidityBegin != null ) && ( dateValidityEnd != null ) )
892         {
893             if ( dateValidityEnd.before( dateValidityBegin ) )
894             {
895                 return ResponseActionBuilderXml.getFailureResponseActionXML( );
896             }
897         }
898 
899         document.setTitle( strDocumentTitle );
900         document.setSummary( strDocumentSummary );
901         document.setComment( strDocumentComment );
902         document.setDateValidityBegin( dateValidityBegin );
903         document.setDateValidityEnd( dateValidityEnd );
904         document.setMailingListId( nMailingListId );
905         document.setPageTemplateDocumentId( nPageTemplateDocumentId );
906 
907         MetadataHandler hMetadata = documentType.metadataHandler( );
908 
909         if ( hMetadata != null )
910         {
911             document.setXmlMetadata( hMetadata.getXmlMetadata( multipartRequest.getParameterMap( ) ) );
912         }
913 
914         document.setAttributes( listAttributes );
915 
916         // Categories
917         List<Category> listCategories = new ArrayList<Category>( );
918 
919         if ( listCategory != null )
920         {
921             for ( String strIdCategory : listCategory )
922             {
923                 listCategories.add( CategoryHome.find( Integer.parseInt( strIdCategory ) ) );
924             }
925         }
926 
927         document.setCategories( listCategories );
928 
929         return null; // No error
930     }
931 
932     /**
933      * Update the specify attribute with the parameters map *
934      * 
935      * @param attribute
936      *            The {@link DocumentAttribute} to update
937      * @param document
938      *            The {@link Document}
939      * @param mRequest
940      *            The multipart http request
941      * @return reponse xml
942      */
943     private static String setAttribute( DocumentAttribute attribute, Document document, MultipartHttpServletRequest multipartRequest, Locale locale )
944     {
945         String strParameterStringValue = multipartRequest.getParameter( attribute.getCode( ) );
946         FileItem fileParameterBinaryValue = multipartRequest.getFile( attribute.getCode( ) );
947         String strIsUpdatable = multipartRequest.getParameter( DocumentRestConstants.PARAMETER_ATTRIBUTE_UPDATE + attribute.getCode( ) );
948         boolean bIsUpdatable = ( ( strIsUpdatable == null ) || strIsUpdatable.equals( "" ) ) ? false : true;
949 
950         if ( attribute.isRequired( ) && ( StringUtils.isBlank( strParameterStringValue ) && ( fileParameterBinaryValue == null ) ) )
951         {
952             return ResponseActionBuilderXml.getFailureResponseActionXML( );
953         }
954 
955         if ( strParameterStringValue != null ) // If the field is a string
956         {
957             // Check for specific attribute validation
958             AttributeManager manager = AttributeService.getInstance( ).getManager( attribute.getCodeAttributeType( ) );
959             String strValidationErrorMessage = manager.validateValue( attribute.getId( ), strParameterStringValue, locale );
960 
961             if ( strValidationErrorMessage != null )
962             {
963                 // String[] listArguments = { attribute.getName( ), strValidationErrorMessage };
964                 return ResponseActionBuilderXml.getFailureResponseActionXML( );
965             }
966 
967             attribute.setTextValue( strParameterStringValue );
968         }
969         else
970             if ( fileParameterBinaryValue != null ) // If the field is a file
971             {
972                 attribute.setBinary( true );
973 
974                 String strContentType = fileParameterBinaryValue.getContentType( );
975                 byte [ ] bytes = fileParameterBinaryValue.get( );
976                 String strFileName = fileParameterBinaryValue.getName( );
977 
978                 if ( !bIsUpdatable )
979                 {
980                     // there is no new value then take the old file value
981                     DocumentAttribute oldAttribute = document.getAttribute( attribute.getCode( ) );
982 
983                     if ( ( oldAttribute != null ) && ( oldAttribute.getBinaryValue( ) != null ) && ( oldAttribute.getBinaryValue( ).length > 0 ) )
984                     {
985                         bytes = oldAttribute.getBinaryValue( );
986                         strContentType = oldAttribute.getValueContentType( );
987                         strFileName = oldAttribute.getTextValue( );
988                     }
989                 }
990 
991                 // Check for mandatory value
992                 if ( attribute.isRequired( ) && ( bytes.length == 0 ) )
993                 {
994                     return ResponseActionBuilderXml.getFailureResponseActionXML( );
995                 }
996 
997                 // Check for specific attribute validation
998                 AttributeManager manager = AttributeService.getInstance( ).getManager( attribute.getCodeAttributeType( ) );
999                 String strValidationErrorMessage = manager.validateValue( attribute.getId( ), strFileName, locale );
1000 
1001                 if ( strValidationErrorMessage != null )
1002                 {
1003                     // String[] listArguments = { attribute.getName( ), strValidationErrorMessage };
1004                     return ResponseActionBuilderXml.getFailureResponseActionXML( );
1005                 }
1006 
1007                 attribute.setBinaryValue( bytes );
1008                 attribute.setValueContentType( strContentType );
1009                 attribute.setTextValue( strFileName );
1010             }
1011 
1012         return null;
1013     }
1014 
1015     /**
1016      * Modify document
1017      * 
1018      * @param request
1019      *            request
1020      * @return response xml
1021      */
1022     @POST
1023     @Path( DocumentRestConstants.PATH_MODIFY_DOCUMENT )
1024     @Consumes( MediaType.MULTIPART_FORM_DATA )
1025     @Produces( {
1026             MediaType.TEXT_HTML, MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON
1027     } )
1028     public String doModifyDocument( @Context HttpServletRequest request )
1029     {
1030         String strResponseXml = StringUtils.EMPTY;
1031         int nSizeThreshold = AppPropertiesService.getPropertyInt( DocumentRestConstants.PROPERTY_MULTIPART_SIZE_THRESHOLD, -1 );
1032         boolean bActivateNormalizeFileName = Boolean.getBoolean( AppPropertiesService
1033                 .getProperty( DocumentRestConstants.PROPERTY_MULTIPART_NORMALIZE_FILE_NAME ) );
1034         String strRequestSizeMax = AppPropertiesService.getProperty( DocumentRestConstants.PROPERTY_MULTIPART_REQUEST_SIZE_MAX );
1035         long nRequestSizeMax = 0;
1036 
1037         if ( StringUtils.isNotBlank( strRequestSizeMax ) && StringUtils.isNumeric( strRequestSizeMax ) )
1038         {
1039             nRequestSizeMax = Long.parseLong( strRequestSizeMax );
1040         }
1041 
1042         try
1043         {
1044             MultipartHttpServletRequest multipartRequest = MultipartUtil.convert( nSizeThreshold, nRequestSizeMax, bActivateNormalizeFileName, request );
1045 
1046             if ( multipartRequest != null )
1047             {
1048                 strResponseXml = modifyDocument( multipartRequest );
1049             }
1050         }
1051         catch( SizeLimitExceededException e )
1052         {
1053             return ResponseActionBuilderXml.getFailureResponseActionXML( );
1054         }
1055         catch( FileUploadException e )
1056         {
1057             return ResponseActionBuilderXml.getFailureResponseActionXML( );
1058         }
1059 
1060         return strResponseXml;
1061     }
1062 
1063     private String modifyDocument( MultipartHttpServletRequest multipartRequest )
1064     {
1065         if ( StringUtils.isNotBlank( multipartRequest.getParameter( DocumentRestConstants.PARAMETER_ID_USER ) )
1066                 && StringUtils.isNumeric( multipartRequest.getParameter( DocumentRestConstants.PARAMETER_ID_USER ) ) )
1067         {
1068             // Create AdminUser
1069             int nIdUser = Integer.parseInt( multipartRequest.getParameter( DocumentRestConstants.PARAMETER_ID_USER ) );
1070             AdminUser user = AdminUserHome.findByPrimaryKey( nIdUser );
1071 
1072             if ( user != null )
1073             {
1074                 user.setRights( AdminUserHome.getRightsListForUser( nIdUser ) );
1075                 user.setRoles( AdminUserHome.getRolesListForUser( nIdUser ) );
1076 
1077                 String strDocumentId = multipartRequest.getParameter( DocumentRestConstants.PARAMETER_ID_DOCUMENT );
1078 
1079                 Document document = DocumentHome.findByPrimaryKeyWithoutBinaries( Integer.parseInt( strDocumentId ) );
1080 
1081                 if ( !DocumentService.getInstance( ).isAuthorizedAdminDocument( document.getSpaceId( ), document.getCodeDocumentType( ),
1082                         DocumentTypeResourceIdService.PERMISSION_MODIFY, user ) )
1083                 {
1084                     return ResponseActionBuilderXml.getFailureResponseActionXML( );
1085                 }
1086 
1087                 String strError = getDocumentData( multipartRequest, document, multipartRequest.getLocale( ) );
1088 
1089                 if ( strError != null )
1090                 {
1091                     return strError;
1092                 }
1093 
1094                 try
1095                 {
1096                     DocumentService.getInstance( ).modifyDocument( document, user );
1097                 }
1098                 catch( DocumentException e )
1099                 {
1100                     return ResponseActionBuilderXml.getFailureResponseActionXML( );
1101                 }
1102 
1103                 String strStateId = multipartRequest.getParameter( DocumentRestConstants.PARAMETER_STATE_ID );
1104 
1105                 if ( StringUtils.isNotBlank( strStateId ) && StringUtils.isNumeric( strStateId ) )
1106                 {
1107                     int nStateId = Integer.parseInt( strStateId );
1108 
1109                     try
1110                     {
1111                         DocumentService.getInstance( ).changeDocumentState( document, user, nStateId );
1112                     }
1113                     catch( DocumentException e )
1114                     {
1115                         return ResponseActionBuilderXml.getFailureResponseActionXML( );
1116                     }
1117                 }
1118 
1119                 return ResponseActionBuilderXml.getSuccessResponseActionXML( );
1120             }
1121             else
1122             {
1123                 return ResponseActionBuilderXml.getFailureResponseActionXML( );
1124             }
1125         }
1126 
1127         return ResponseActionBuilderXml.getFailureResponseActionXML( );
1128     }
1129 }