View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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  
35  package fr.paris.lutece.plugins.appcenter.modules.support.web;
36  
37  import java.util.Map;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import fr.paris.lutece.plugins.appcenter.business.Application;
42  import fr.paris.lutece.plugins.appcenter.modules.support.business.SupportDemand;
43  import fr.paris.lutece.plugins.appcenter.modules.support.business.SupportsData;
44  import fr.paris.lutece.plugins.appcenter.modules.support.business.UploadFile;
45  import fr.paris.lutece.plugins.appcenter.modules.support.service.SupportAsynchronousUploadHandler;
46  import fr.paris.lutece.plugins.appcenter.service.DemandService;
47  import fr.paris.lutece.plugins.appcenter.web.AppCenterDemandXPage;
48  import fr.paris.lutece.plugins.appcenter.web.Constants;
49  import fr.paris.lutece.portal.business.file.File;
50  import fr.paris.lutece.portal.business.file.FileHome;
51  import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
52  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
53  import fr.paris.lutece.portal.service.message.SiteMessageException;
54  import fr.paris.lutece.portal.service.security.UserNotSignedException;
55  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
56  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
57  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
58  import fr.paris.lutece.portal.web.xpages.XPage;
59  import fr.paris.lutece.util.filesystem.FileSystemUtil;
60  import java.util.ArrayList;
61  import java.util.List;
62  import org.apache.commons.fileupload.FileItem;
63  
64  /**
65   * SourcesXPage
66   */
67  @Controller( xpageName = "support", pageTitleI18nKey = "module.appcenter.support.xpage.support.pageTitle", pagePathI18nKey = "module.appcenter.support.xpage.support.pagePathLabel" )
68  public class SupportsXPage extends AppCenterDemandXPage
69  {
70      // Templates
71      private static final String TEMPLATE_MANAGE_SUPPORT = "/skin/plugins/appcenter/modules/support/manage_support.html";
72  
73      // PARAMETERS
74      private static final String PARAMETER_FIELD_NAME = "fieldname";
75  
76      // Markers
77      private static final String MARK_HANDLER = "handler";
78      private static final String MARK_FILE_NAME = "fieldName";
79      private static final String MARK_LIST_UPLOADED_FILE = "listFiles";
80  
81      // VIEW
82      private static final String VIEW_MANAGE_SUPPORT = "managesupport";
83  
84      // ACTION
85      private static final String ACTION_ADD_SUPPORT = "addSupport";
86      
87      // PERMISSIONS
88      private static final String PERMISSION_VIEW_SUPPORT_DEMANDS = "PERMISSION_VIEW_SUPPORT_DEMANDS";
89      private static final String PERMISSION_CREATE_SUPPORT_DEMAND = "PERMISSION_CREATE_SUPPORT_DEMAND";
90  
91      @View( value = VIEW_MANAGE_SUPPORT, defaultView = true )
92      public XPage getManageSupport( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
93      {
94          try
95          {
96              checkPermission( request, PERMISSION_VIEW_SUPPORT_DEMANDS, null );
97          }
98          catch ( AccessDeniedException e )
99          {
100             getUnauthorizedAccessMessage( request );
101         }
102         
103         
104         Map<String, Object> model = getModel( );
105         
106         String strFiledName = SupportAsynchronousUploadHandler.getHandler( ).buildFieldName( request );
107         
108         fillAppCenterCommons( model, request );
109         fillDisplayPermission( model, request, PERMISSION_CREATE_SUPPORT_DEMAND , null );
110         model.put( MARK_HANDLER, SupportAsynchronousUploadHandler.getHandler( ) );
111         model.put( MARK_FILE_NAME, strFiledName );
112         model.put( MARK_LIST_UPLOADED_FILE, new ArrayList<FileItem>( ) );
113         
114         return getXPage( TEMPLATE_MANAGE_SUPPORT , request.getLocale( ), model );
115     }
116 
117     @Action( ACTION_ADD_SUPPORT )
118     public XPage doAddSupport( HttpServletRequest request ) throws UserNotSignedException, SiteMessageException
119     {
120         try
121         {
122             checkPermission( request, PERMISSION_CREATE_SUPPORT_DEMAND, null );
123         }
124         catch ( AccessDeniedException e )
125         {
126             getUnauthorizedAccessMessage( request );
127         }
128         
129         Application application = getApplication( request );
130         SupportDemandter/modules/support/business/SupportDemand.html#SupportDemand">SupportDemand supportDemand = new SupportDemand( );
131         populate( supportDemand, request );
132         
133         // Check constraints
134         if ( !validateBean( supportDemand, getLocale( request ) ) )
135         {
136             return redirectView( request, VIEW_MANAGE_SUPPORT );
137         }
138 
139         String strUploadValue = request.getParameter( PARAMETER_FIELD_NAME );
140 
141         List<FileItem> listFiles = SupportAsynchronousUploadHandler.getHandler( ).getListUploadedFiles( strUploadValue, request.getSession( ) );
142         List<UploadFile> listUploadFiles = new ArrayList<>();
143 
144         if ( !listFiles.isEmpty( ) )
145         {         
146             for ( FileItem fileitem : listFiles )
147             {
148                 File file = buildFileWithPhysicalFile( fileitem );
149                 int nidFile = FileHome.create( file );
150                 
151                 
152                 UploadFileappcenter/modules/support/business/UploadFile.html#UploadFile">UploadFile uploadFile = new UploadFile( );
153                 uploadFile.setIdFile( nidFile );
154                 uploadFile.setTitle( file.getTitle( ) );
155                 uploadFile.setSize( file.getSize( ) );
156                 
157                 listUploadFiles.add( uploadFile );
158             }
159         }
160         
161         supportDemand.setListFilesSupport( listUploadFiles );
162 
163         SupportAsynchronousUploadHandler.getHandler( ).removeSessionFiles( request.getSession( ).getId( ) );
164 
165         DemandService.saveDemand( supportDemand, application );
166 
167         return redirect(request, VIEW_MANAGE_SUPPORT, Constants.PARAM_ID_APPLICATION, application.getId() );
168     }
169 
170     @Override
171     protected String getDemandType()
172     {
173         return SupportDemand.DEMAND_TYPE;
174     }
175 
176     @Override
177     protected Class getDemandClass()
178     {
179         return SupportDemand.class;
180     }
181 
182     @Override
183     protected String getDatasName()
184     {
185         return SupportsData.DATA_SUPPORTS_NAME;
186     }
187 
188     @Override
189     protected Class getDatasClass()
190     {
191         return SupportsData.class;
192     }
193     
194     /**
195      * Builds the file with physical file.
196      *
197      * @param fileItem
198      *            the file item
199      * @return the file
200      */
201     private File buildFileWithPhysicalFile( FileItem fileItem )
202     {
203         File file = new File( );
204         file.setTitle( fileItem.getName( ) );
205         file.setSize( ( fileItem.getSize( ) < Integer.MAX_VALUE ) ? (int) fileItem.getSize( ) : Integer.MAX_VALUE );
206         file.setMimeType( FileSystemUtil.getMIMEType( file.getTitle( ) ) );
207 
208         PhysicalFile physicalFile = new PhysicalFile( );
209         physicalFile.setValue( fileItem.get( ) );
210         file.setPhysicalFile( physicalFile );
211 
212         return file;
213 }
214 
215 }