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.directories.service;
35  
36  import java.sql.Timestamp;
37  import java.time.LocalDateTime;
38  import java.util.ArrayList;
39  import java.util.List;
40  import javax.servlet.http.HttpServletRequest;
41  import org.apache.commons.fileupload.FileItem;
42  import fr.paris.lutece.plugins.directories.business.DirectoryEntity;
43  import fr.paris.lutece.plugins.directories.business.DirectoryEntityHome;
44  import fr.paris.lutece.plugins.directories.business.DirectoryResponse;
45  import fr.paris.lutece.plugins.directories.business.DirectoryResponseHome;
46  import fr.paris.lutece.plugins.directories.service.upload.DirectoriesAsynchronousUploadHandler;
47  import fr.paris.lutece.plugins.genericattributes.business.Entry;
48  import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
49  import fr.paris.lutece.plugins.genericattributes.business.GenAttFileItem;
50  import fr.paris.lutece.plugins.genericattributes.business.Response;
51  import fr.paris.lutece.plugins.genericattributes.business.ResponseHome;
52  import fr.paris.lutece.plugins.genericattributes.service.entrytype.EntryTypeServiceManager;
53  import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
54  import fr.paris.lutece.portal.business.file.File;
55  import fr.paris.lutece.portal.business.file.FileHome;
56  import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
57  import fr.paris.lutece.portal.business.physicalfile.PhysicalFileHome;
58  import fr.paris.lutece.portal.business.user.AdminUser;
59  import fr.paris.lutece.portal.service.admin.AdminUserService;
60  
61  /**
62   * This Service manages document actions (create, update, delete, validate ...) .
63   */
64  public class DirectoriesService
65  {
66      private static DirectoriesServicervice/DirectoriesService.html#DirectoriesService">DirectoriesService _singleton = new DirectoriesService( );
67  
68      /**
69       * Get the unique instance of the service
70       *
71       * @return The unique instance
72       */
73      public static DirectoriesService getInstance( )
74      {
75          return _singleton;
76      }
77  
78      /**
79       * Find and build all the response of all directory entities
80       * 
81       * 
82       * @return the list of directory entities
83       */
84      public List<DirectoryEntity> getListDocWithoutBinaries( )
85      {
86          List<DirectoryEntity> listDirectoryEntity = DirectoryEntityHome.getDirectoryEntitiesList( );
87          for ( DirectoryEntity directoryEntity : listDirectoryEntity )
88          {
89              List<DirectoryResponse> listDirectoryResponse = DirectoryResponseHome.getDirectoryResponsesListByIdEntity( directoryEntity.getId( ) );
90              List<Response> listResponse = new ArrayList<>( );
91              for ( DirectoryResponse directoryResponse : listDirectoryResponse )
92              {
93                  Response response = ResponseHome.findByPrimaryKey( directoryResponse.getIdResponse( ) );
94                  if ( response.getFile( ) == null )
95                  {
96                      listResponse.add( response );
97                  }
98              }
99              directoryEntity.setResponses( listResponse );
100         }
101         return listDirectoryEntity;
102     }
103 
104     /**
105      * Find and build all the response of an directory entity
106      * 
107      * @param nIdDirectoryEntity
108      *            the directory entity id
109      * @return the directory entity
110      */
111     public DirectoryEntity findByPrimaryKeyWithoutBinaries( int nIdDirectoryEntity )
112     {
113         DirectoryEntity directoryEntity = DirectoryEntityHome.findByPrimaryKey( nIdDirectoryEntity );
114         List<DirectoryResponse> listDirectoryResponse = DirectoryResponseHome.getDirectoryResponsesListByIdEntity( directoryEntity.getId( ) );
115         List<Response> listResponse = new ArrayList<>( );
116         for ( DirectoryResponse directoryResponse : listDirectoryResponse )
117         {
118             Response response = ResponseHome.findByPrimaryKey( directoryResponse.getId( ) );
119             if ( response.getFile( ) == null )
120             {
121                 listResponse.add( response );
122             }
123         }
124         directoryEntity.setResponses( listResponse );
125         return directoryEntity;
126     }
127 
128     /**
129      * Find and build all the response of an directory entity
130      * 
131      * @param nIdEntity
132      *            the directory entity id
133      * @param request
134      *            the request
135      * @return a list of response
136      */
137     public static List<Response> findAndBuildListResponse( HttpServletRequest request, int nIdEntity )
138     {
139         DirectoriesAsynchronousUploadHandler.getHandler( ).removeSessionFiles( request.getSession( ) );
140         List<DirectoryResponse> listDirectoryResponse = DirectoryResponseHome.getDirectoryResponsesListByIdEntity( nIdEntity );
141         List<Response> listResponse = new ArrayList<>( );
142         for ( DirectoryResponse directoryResponse : listDirectoryResponse )
143         {
144             Response response = ResponseHome.findByPrimaryKey( directoryResponse.getIdResponse( ) );
145             if ( ( response.getFile( ) != null ) && ( response.getFile( ).getIdFile( ) > 0 ) )
146             {
147                 File file = FileHome.findByPrimaryKey( response.getFile( ).getIdFile( ) );
148                 PhysicalFile physicalFile = PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ) );
149                 FileItem fileItem = new GenAttFileItem( physicalFile.getValue( ), file.getTitle( ) );
150                 DirectoriesAsynchronousUploadHandler.getHandler( ).addFileItemToUploadedFilesList( fileItem,
151                         IEntryTypeService.PREFIX_ATTRIBUTE + Integer.toString( response.getEntry( ).getIdEntry( ) ), request );
152                 response.setFile( file );
153             }
154             listResponse.add( response );
155         }
156         return listResponse;
157     }
158 
159     /**
160      * Create directory entity
161      * 
162      * @param request
163      *            the request
164      * @param nIdDirectory
165      *            the directory id
166      * @param strEntityTitle
167      *            the entity title
168      */
169     public static void createEntity( HttpServletRequest request, int nIdDirectory, String strEntityTitle )
170     {
171         List<Entry> listEntryFirstLevel = EntryService.getDirectoryEntryList( nIdDirectory, true );
172         List<Response> listResponse = new ArrayList<>( );
173         for ( Entry entry : listEntryFirstLevel )
174         {
175             entry.setFields( FieldHome.getFieldListByIdEntry( entry.getIdEntry( ) ) );
176             EntryTypeServiceManager.getEntryTypeService( entry ).getResponseData( entry, request, listResponse, request.getLocale( ) );
177         }
178         AdminUser currentUser = AdminUserService.getAdminUser( request );
179         DirectoryEntityries/business/DirectoryEntity.html#DirectoryEntity">DirectoryEntity entity = new DirectoryEntity( );
180         entity.setIdDirectory( nIdDirectory );
181         entity.setDateCreation( Timestamp.valueOf( LocalDateTime.now( ) ) );
182         entity.setIdCreator( currentUser.getUserId( ) );
183         entity.setTitle( strEntityTitle );
184         DirectoryEntityHome.create( entity );
185         createEntityResponse( listResponse, nIdDirectory, entity.getId( ) );
186         DirectoriesAsynchronousUploadHandler.getHandler( ).removeSessionFiles( request.getSession( ) );
187     }
188 
189     /**
190      * Modify directory entity
191      * 
192      * @param request
193      *            the request
194      * @param nIdDirectory
195      *            the directory id
196      * @param nIdEntity
197      *            the entity id
198      * @param strEntityTitle
199      *            the entity title
200      */
201     public static void modifyEntity( HttpServletRequest request, int nIdDirectory, int nIdEntity, String strEntityTitle )
202     {
203         List<Entry> listEntryFirstLevel = EntryService.getDirectoryEntryList( nIdDirectory, true );
204         List<Response> listResponse = new ArrayList<>( );
205         for ( Entry entry : listEntryFirstLevel )
206         {
207             entry.setFields( FieldHome.getFieldListByIdEntry( entry.getIdEntry( ) ) );
208             EntryTypeServiceManager.getEntryTypeService( entry ).getResponseData( entry, request, listResponse, request.getLocale( ) );
209         }
210         removeEntityResponse( nIdEntity );
211         createEntityResponse( listResponse, nIdDirectory, nIdEntity );
212         AdminUser currentUser = AdminUserService.getAdminUser( request );
213         DirectoryEntity entity = DirectoryEntityHome.findByPrimaryKey( nIdEntity );
214         entity.setTitle( strEntityTitle );
215         entity.setUpdate( Timestamp.valueOf( LocalDateTime.now( ) ) );
216         entity.setIdModificator( currentUser.getUserId( ) );
217         DirectoryEntityHome.update( entity );
218     }
219 
220     /**
221      * Create entity response
222      * 
223      * @param listResponse
224      * @param nIdDirectory
225      *            the directory id
226      * @param nIdEntity
227      *            the entity id
228      * @param strEntityTitle
229      *            the entity title
230      */
231     public static void createEntityResponse( List<Response> listResponse, int nIdDirectory, int nIdEntity )
232     {
233         for ( Response response : listResponse )
234         {
235             DirectoryResponse/DirectoryResponse.html#DirectoryResponse">DirectoryResponse directoryResponse = new DirectoryResponse( );
236             ResponseHome.create( response );
237             directoryResponse.setIdDirectory( nIdDirectory );
238             directoryResponse.setIdResponse( response.getIdResponse( ) );
239             directoryResponse.setIdEntity( nIdEntity );
240             DirectoryResponseHome.create( directoryResponse );
241         }
242     }
243 
244     /**
245      * Remove entity response
246      * 
247      * @param nIdEntity
248      *            the directory entity id
249      */
250     public static void removeEntityResponse( int nIdEntity )
251     {
252         List<DirectoryResponse> listDirectoryResponse = DirectoryResponseHome.getDirectoryResponsesListByIdEntity( nIdEntity );
253         for ( DirectoryResponse directoryResponse : listDirectoryResponse )
254         {
255             ResponseHome.remove( directoryResponse.getIdResponse( ) );
256             DirectoryResponseHome.remove( directoryResponse.getId( ) );
257         }
258     }
259 }