View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.business.spaces;
35  
36  import fr.paris.lutece.portal.service.i18n.I18nService;
37  import fr.paris.lutece.util.ReferenceList;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  
40  import java.util.ArrayList;
41  import java.util.List;
42  import java.util.Locale;
43  
44  /**
45   * This class provides Data Access methods for DocumentSpace objects
46   */
47  public final class DocumentSpaceDAO implements IDocumentSpaceDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_space ) FROM document_space ";
51      private static final String SQL_QUERY_SELECT = " SELECT a.id_space, a.id_parent, a.document_space_name, a.description, a.document_space_view, a.id_space_icon, b.icon_url, a.document_creation_allowed, a.workgroup_key "
52              + " FROM document_space a, document_space_icon b " + " WHERE a.id_space_icon = b.id_space_icon AND id_space = ? ";
53      private static final String SQL_QUERY_INSERT = " INSERT INTO document_space ( id_space, id_parent, document_space_name, description, document_space_view, id_space_icon, document_creation_allowed ,workgroup_key ) VALUES ( ?, ?, ?, ?, ?, ?, ? ,?) ";
54      private static final String SQL_QUERY_DELETE = " DELETE FROM document_space WHERE id_space = ?  ";
55      private static final String SQL_QUERY_UPDATE = " UPDATE document_space SET id_space = ?, id_parent = ?, document_space_name = ?, description = ?, document_space_view = ?, id_space_icon = ?, document_creation_allowed = ? ,workgroup_key= ? WHERE id_space = ?  ";
56      private static final String SQL_QUERY_SELECT_CHILDS = " SELECT a.id_space, a.id_parent, a.document_space_name, a.description, a.document_space_view, a.id_space_icon, b.icon_url, a.document_creation_allowed ,a.workgroup_key"
57              + " FROM document_space a, document_space_icon b WHERE a.id_space_icon = b.id_space_icon AND id_parent = ? ORDER BY a.document_space_name ";
58      private static final String SQL_QUERY_SELECT_CHILDS_BY_CODE_TYPE = " SELECT a.id_space, a.id_parent, a.document_space_name, a.description, a.document_space_view, a.id_space_icon, b.icon_url, a.document_creation_allowed ,a.workgroup_key"
59              + " FROM document_space a, document_space_icon b, document_space_document_type c "
60              + " WHERE a.id_space_icon = b.id_space_icon AND id_parent = ? AND a.id_space = c.id_space AND c.code_document_type = ?"
61              + " ORDER BY a.document_space_name ";
62      private static final String SQL_QUERY_SELECT_SPACES_WITH_DOCUMENT_CREATION_IS_ALLOWED_BY_CODE_TYPE = "SELECT a.id_space, a.id_parent, a.document_space_name, a.description, a.document_space_view, a.id_space_icon, b.icon_url, a.document_creation_allowed ,a.workgroup_key"
63              + " FROM document_space a, document_space_icon b, document_space_document_type c "
64              + " WHERE a.id_space_icon = b.id_space_icon AND a.id_space = c.id_space AND c.code_document_type = ? AND a.document_creation_allowed = ?"
65              + " ORDER BY a.document_space_name ";
66      private static final String SQL_QUERY_SELECTALL = " SELECT a.id_space, a.id_parent, a.document_space_name, a.description, a.document_space_view, a.id_space_icon, b.icon_url, a.document_creation_allowed, a.workgroup_key "
67              + " FROM document_space a, document_space_icon b WHERE a.id_space_icon = b.id_space_icon ORDER BY a.document_space_name ";
68      private static final String SQL_QUERY_SELECTALL_VIEWTYPE = " SELECT code_view , name_key FROM document_view";
69      private static final String SQL_QUERY_SELECTALL_ICONS = " SELECT id_space_icon , icon_url FROM document_space_icon";
70      private static final String SQL_QUERY_INSERT_DOCUMENT_TYPE = "INSERT INTO document_space_document_type ( id_space , code_document_type ) VALUES ( ? , ? )";
71      private static final String SQL_QUERY_DELETE_DOCUMENT_TYPE = " DELETE FROM document_space_document_type WHERE id_space = ?  ";
72      private static final String SQL_QUERY_SELECT_DOCUMENT_TYPE = " SELECT code_document_type FROM document_space_document_type WHERE id_space = ?  ";
73      private static final String SQL_QUERY_SELECT_SPACE_DOCUMENT_TYPE = " SELECT a.code_document_type, a.document_type_name "
74              + " FROM document_type a, document_space_document_type b " + " WHERE a.code_document_type = b.code_document_type AND b.id_space = ?"
75              + " ORDER BY a.document_type_name";
76  
77      /**
78       * Generates a new primary key
79       * 
80       * @return The new primary key
81       */
82      private int newPrimaryKey( )
83      {
84          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
85          daoUtil.executeQuery( );
86  
87          int nKey;
88  
89          if ( !daoUtil.next( ) )
90          {
91              // if the table is empty
92              nKey = 1;
93          }
94  
95          nKey = daoUtil.getInt( 1 ) + 1;
96  
97          daoUtil.free( );
98  
99          return nKey;
100     }
101 
102     /**
103      * Insert a new record in the table.
104      *
105      * @param space
106      *            The space object
107      */
108     public synchronized void insert( DocumentSpace space )
109     {
110         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
111         space.setId( newPrimaryKey( ) );
112         daoUtil.setInt( 1, space.getId( ) );
113         daoUtil.setInt( 2, space.getIdParent( ) );
114         daoUtil.setString( 3, space.getName( ) );
115         daoUtil.setString( 4, space.getDescription( ) );
116         daoUtil.setString( 5, space.getViewType( ) );
117         daoUtil.setInt( 6, space.getIdIcon( ) );
118         daoUtil.setInt( 7, space.isDocumentCreationAllowed( ) ? 1 : 0 );
119         daoUtil.setString( 8, space.getWorkgroup( ) );
120         daoUtil.executeUpdate( );
121         daoUtil.free( );
122 
123         // insert allowed document types
124         insertAllowedDocumenTypes( space );
125     }
126 
127     /**
128      * Insert allowed document types to a space
129      * 
130      * @param space
131      *            The space
132      */
133     private void insertAllowedDocumenTypes( DocumentSpace space )
134     {
135         String [ ] doctypes = space.getAllowedDocumentTypes( );
136 
137         for ( int i = 0; i < doctypes.length; i++ )
138         {
139             DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_DOCUMENT_TYPE );
140             daoUtil.setInt( 1, space.getId( ) );
141             daoUtil.setString( 2, doctypes [i] );
142 
143             daoUtil.executeUpdate( );
144             daoUtil.free( );
145         }
146     }
147 
148     /**
149      * Load the data of DocumentSpace from the table
150      *
151      * @param nDocumentSpaceId
152      *            The identifier of DocumentSpace
153      * @return the instance of the DocumentSpace
154      */
155     public DocumentSpace load( int nDocumentSpaceId )
156     {
157         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
158         daoUtil.setInt( 1, nDocumentSpaceId );
159         daoUtil.executeQuery( );
160 
161         DocumentSpace space = null;
162 
163         if ( daoUtil.next( ) )
164         {
165             space = new DocumentSpace( );
166             space.setId( daoUtil.getInt( 1 ) );
167             space.setIdParent( daoUtil.getInt( 2 ) );
168             space.setName( daoUtil.getString( 3 ) );
169             space.setDescription( daoUtil.getString( 4 ) );
170             space.setViewType( daoUtil.getString( 5 ) );
171             space.setIdIcon( daoUtil.getInt( 6 ) );
172             space.setIconUrl( daoUtil.getString( 7 ) );
173             space.setDocumentCreationAllowed( daoUtil.getInt( 8 ) != 0 );
174             space.setWorkgroup( daoUtil.getString( 9 ) );
175         }
176 
177         daoUtil.free( );
178 
179         if ( space != null )
180         {
181             loadAllowedDocumentTypes( space );
182         }
183 
184         return space;
185     }
186 
187     /**
188      * Load allowed document types for a space
189      * 
190      * @param space
191      *            The space
192      */
193     private void loadAllowedDocumentTypes( DocumentSpace space )
194     {
195         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_DOCUMENT_TYPE );
196         daoUtil.setInt( 1, space.getId( ) );
197         daoUtil.executeQuery( );
198 
199         while ( daoUtil.next( ) )
200         {
201             space.addAllowedDocumentType( daoUtil.getString( 1 ) );
202         }
203 
204         daoUtil.free( );
205     }
206 
207     /**
208      * Delete a record from the table
209      *
210      * @param nSpaceId
211      *            The Id to delete
212      */
213     public void delete( int nSpaceId )
214     {
215         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
216         daoUtil.setInt( 1, nSpaceId );
217         daoUtil.executeUpdate( );
218 
219         daoUtil.free( );
220         deleteAllowedDocumentTypes( nSpaceId );
221     }
222 
223     /**
224      * Delete allowed document types
225      * 
226      * @param nSpaceId
227      *            The space identifier
228      */
229     private void deleteAllowedDocumentTypes( int nSpaceId )
230     {
231         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_DOCUMENT_TYPE );
232         daoUtil.setInt( 1, nSpaceId );
233 
234         daoUtil.executeUpdate( );
235         daoUtil.free( );
236     }
237 
238     /**
239      * Update the record in the table
240      * 
241      * @param space
242      *            The reference of space
243      */
244     public void store( DocumentSpace space )
245     {
246         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
247         daoUtil.setInt( 1, space.getId( ) );
248         daoUtil.setInt( 2, space.getIdParent( ) );
249         daoUtil.setString( 3, space.getName( ) );
250         daoUtil.setString( 4, space.getDescription( ) );
251         daoUtil.setString( 5, space.getViewType( ) );
252         daoUtil.setInt( 6, space.getIdIcon( ) );
253         daoUtil.setInt( 7, space.isDocumentCreationAllowed( ) ? 1 : 0 );
254         daoUtil.setString( 8, space.getWorkgroup( ) );
255         daoUtil.setInt( 9, space.getId( ) );
256 
257         daoUtil.executeUpdate( );
258         daoUtil.free( );
259 
260         deleteAllowedDocumentTypes( space.getId( ) );
261         insertAllowedDocumenTypes( space );
262     }
263 
264     /**
265      * Load the list of documentSpaces childs
266      *
267      * @param strCodeType
268      *            the document type filter if needed (null if not)
269      * @param nSpaceId
270      *            The space identifier
271      * @return The Collection of the DocumentSpaces
272      */
273     public List<DocumentSpace> selectChilds( int nSpaceId, String strCodeType )
274     {
275         List<DocumentSpace> listDocumentSpaces = new ArrayList<DocumentSpace>( );
276         DAOUtil daoUtil = null;
277 
278         if ( strCodeType != null )
279         {
280             daoUtil = new DAOUtil( SQL_QUERY_SELECT_CHILDS_BY_CODE_TYPE );
281             daoUtil.setInt( 1, nSpaceId );
282             daoUtil.setString( 2, strCodeType );
283         }
284         else
285         {
286             daoUtil = new DAOUtil( SQL_QUERY_SELECT_CHILDS );
287             daoUtil.setInt( 1, nSpaceId );
288         }
289 
290         daoUtil.executeQuery( );
291 
292         while ( daoUtil.next( ) )
293         {
294             DocumentSpaceocument/business/spaces/DocumentSpace.html#DocumentSpace">DocumentSpace space = new DocumentSpace( );
295             space.setId( daoUtil.getInt( 1 ) );
296             space.setIdParent( daoUtil.getInt( 2 ) );
297             space.setName( daoUtil.getString( 3 ) );
298             space.setDescription( daoUtil.getString( 4 ) );
299             space.setViewType( daoUtil.getString( 5 ) );
300             space.setIdIcon( daoUtil.getInt( 6 ) );
301             space.setIconUrl( daoUtil.getString( 7 ) );
302             space.setDocumentCreationAllowed( daoUtil.getInt( 8 ) != 0 );
303             space.setWorkgroup( daoUtil.getString( 9 ) );
304             listDocumentSpaces.add( space );
305         }
306 
307         daoUtil.free( );
308 
309         return listDocumentSpaces;
310     }
311 
312     /**
313      * Load the list of documentSpaces authorizing the selected document type
314      *
315      * @param strCodeType
316      *            the document type filter
317      * @return The Collection of the DocumentSpaces
318      */
319     public List<DocumentSpace> selectSpacesAllowingDocumentCreationByDocumentType( String strCodeType, int createDocumentIsAllowed )
320     {
321         List<DocumentSpace> listDocumentSpaces = new ArrayList<DocumentSpace>( );
322         DAOUtil daoUtil = null;
323 
324         daoUtil = new DAOUtil( SQL_QUERY_SELECT_SPACES_WITH_DOCUMENT_CREATION_IS_ALLOWED_BY_CODE_TYPE );
325         daoUtil.setString( 1, strCodeType );
326         daoUtil.setInt( 2, createDocumentIsAllowed );
327 
328         daoUtil.executeQuery( );
329 
330         while ( daoUtil.next( ) )
331         {
332             DocumentSpaceocument/business/spaces/DocumentSpace.html#DocumentSpace">DocumentSpace space = new DocumentSpace( );
333             space.setId( daoUtil.getInt( 1 ) );
334             space.setIdParent( daoUtil.getInt( 2 ) );
335             space.setName( daoUtil.getString( 3 ) );
336             space.setDescription( daoUtil.getString( 4 ) );
337             space.setViewType( daoUtil.getString( 5 ) );
338             space.setIdIcon( daoUtil.getInt( 6 ) );
339             space.setIconUrl( daoUtil.getString( 7 ) );
340             space.setDocumentCreationAllowed( daoUtil.getInt( 8 ) != 0 );
341             space.setWorkgroup( daoUtil.getString( 9 ) );
342             listDocumentSpaces.add( space );
343         }
344 
345         daoUtil.free( );
346 
347         return listDocumentSpaces;
348     }
349 
350     /**
351      * Load the list of documentSpaces
352      * 
353      * @return The Collection of the DocumentSpaces
354      */
355     public ReferenceList getDocumentSpaceList( )
356     {
357         ReferenceList list = new ReferenceList( );
358         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
359         daoUtil.executeQuery( );
360 
361         while ( daoUtil.next( ) )
362         {
363             DocumentSpaceocument/business/spaces/DocumentSpace.html#DocumentSpace">DocumentSpace space = new DocumentSpace( );
364             space.setId( daoUtil.getInt( 1 ) );
365             space.setName( daoUtil.getString( 3 ) );
366 
367             list.addItem( space.getId( ), space.getName( ) );
368         }
369 
370         daoUtil.free( );
371 
372         return list;
373     }
374 
375     /**
376      * Load the list of documentSpaces
377      * 
378      * @param locale
379      *            The locale
380      * @return The Collection of the DocumentSpaces
381      */
382     public ReferenceList getViewTypeList( Locale locale )
383     {
384         ReferenceList list = new ReferenceList( );
385         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_VIEWTYPE );
386         daoUtil.executeQuery( );
387 
388         while ( daoUtil.next( ) )
389         {
390             String strCodeView = daoUtil.getString( 1 );
391             String strViewNameKey = daoUtil.getString( 2 );
392             list.addItem( strCodeView, I18nService.getLocalizedString( strViewNameKey, locale ) );
393         }
394 
395         daoUtil.free( );
396 
397         return list;
398     }
399 
400     /**
401      * Gets a list of icons available or space customization
402      * 
403      * @return A list of icons
404      */
405     public ReferenceList getIconsList( )
406     {
407         ReferenceList list = new ReferenceList( );
408         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ICONS );
409         daoUtil.executeQuery( );
410 
411         while ( daoUtil.next( ) )
412         {
413             int nIconId = daoUtil.getInt( 1 );
414             String strIconUrl = daoUtil.getString( 2 );
415             list.addItem( nIconId, strIconUrl );
416         }
417 
418         daoUtil.free( );
419 
420         return list;
421     }
422 
423     /**
424      * Select all spaces
425      * 
426      * @return A collection of all spaces.
427      */
428     public List<DocumentSpace> selectAll( )
429     {
430         List<DocumentSpace> listDocumentSpaces = new ArrayList<DocumentSpace>( );
431         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
432         daoUtil.executeQuery( );
433 
434         while ( daoUtil.next( ) )
435         {
436             DocumentSpaceocument/business/spaces/DocumentSpace.html#DocumentSpace">DocumentSpace space = new DocumentSpace( );
437             space.setId( daoUtil.getInt( 1 ) );
438             space.setIdParent( daoUtil.getInt( 2 ) );
439             space.setName( daoUtil.getString( 3 ) );
440             space.setDescription( daoUtil.getString( 4 ) );
441             space.setViewType( daoUtil.getString( 5 ) );
442             space.setIdIcon( daoUtil.getInt( 6 ) );
443             space.setIconUrl( daoUtil.getString( 7 ) );
444             space.setDocumentCreationAllowed( daoUtil.getInt( 8 ) != 0 );
445             space.setWorkgroup( daoUtil.getString( 9 ) );
446             listDocumentSpaces.add( space );
447         }
448 
449         daoUtil.free( );
450 
451         return listDocumentSpaces;
452     }
453 
454     /**
455      * Returns all allowed document types for a given space
456      * 
457      * @param nSpaceId
458      *            The space Id
459      * @return Allowed documents types as a ReferenceList
460      */
461     public ReferenceList getAllowedDocumentTypes( int nSpaceId )
462     {
463         ReferenceList list = new ReferenceList( );
464         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SPACE_DOCUMENT_TYPE );
465         daoUtil.setInt( 1, nSpaceId );
466         daoUtil.executeQuery( );
467 
468         while ( daoUtil.next( ) )
469         {
470             list.addItem( daoUtil.getString( 1 ), daoUtil.getString( 2 ) );
471         }
472 
473         daoUtil.free( );
474 
475         return list;
476     }
477 }