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;
35  
36  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttributeHome;
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.Collection;
42  
43  /**
44   * This class provides Data Access methods for DocumentType objects
45   */
46  public final class DocumentTypeDAO implements IDocumentTypeDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_SELECT = "SELECT code_document_type, document_type_name, description, thumbnail_attr_id, default_thumbnail_url, admin_xsl, content_service_xsl, metadata_handler FROM document_type WHERE code_document_type = ?  ";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO document_type ( code_document_type, document_type_name, description, thumbnail_attr_id, default_thumbnail_url, metadata_handler  ) VALUES ( ?, ?, ?, ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM document_type WHERE code_document_type = ?  ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE document_type SET code_document_type = ?, document_type_name = ?, description = ?, thumbnail_attr_id = ?, default_thumbnail_url = ?, metadata_handler = ? WHERE code_document_type = ?  ";
53      private static final String SQL_QUERY_SELECTALL = "SELECT code_document_type, document_type_name, description, thumbnail_attr_id, default_thumbnail_url, metadata_handler  FROM document_type ";
54      private static final String SQL_QUERY_CHECK_DOCUMENTS = "SELECT id_document FROM document WHERE code_document_type = ? ";
55      private static final String SQL_QUERY_REORDER_ATTRIBUTES = "UPDATE document_type_attr SET attr_order = ? WHERE id_document_attr = ? ";
56      private static final String SQL_QUERY_UPDATE_ADMIN_STYLESHEET = "UPDATE document_type SET admin_xsl = ? WHERE code_document_type = ? ";
57      private static final String SQL_QUERY_UPDATE_CONTENT_STYLESHEET = "UPDATE document_type SET content_service_xsl = ? WHERE code_document_type = ? ";
58  
59      /**
60       * Insert a new record in the table.
61       *
62       * @param documentType
63       *            The documentType object
64       */
65      public void insert( DocumentType documentType )
66      {
67          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
68          daoUtil.setString( 1, documentType.getCode( ) );
69          daoUtil.setString( 2, documentType.getName( ) );
70          daoUtil.setString( 3, documentType.getDescription( ) );
71          daoUtil.setInt( 4, documentType.getThumbnailAttributeId( ) );
72          daoUtil.setString( 5, documentType.getDefaultThumbnailUrl( ) );
73          daoUtil.setString( 6, documentType.getMetadataHandler( ) );
74  
75          daoUtil.executeUpdate( );
76          daoUtil.free( );
77      }
78  
79      /**
80       * Load the data of DocumentType from the table
81       * 
82       * @param strDocumentTypeCode
83       *            the code
84       * @return the instance of the DocumentType
85       */
86      public DocumentType load( String strDocumentTypeCode )
87      {
88          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
89          daoUtil.setString( 1, strDocumentTypeCode );
90          daoUtil.executeQuery( );
91  
92          DocumentType documentType = null;
93  
94          if ( daoUtil.next( ) )
95          {
96              documentType = new DocumentType( );
97              documentType.setCode( daoUtil.getString( 1 ) );
98              documentType.setName( daoUtil.getString( 2 ) );
99              documentType.setDescription( daoUtil.getString( 3 ) );
100             documentType.setThumbnailAttributeId( daoUtil.getInt( 4 ) );
101             documentType.setDefaultThumbnailUrl( daoUtil.getString( 5 ) );
102             documentType.setAdminXsl( daoUtil.getBytes( 6 ) );
103             documentType.setContentServiceXsl( daoUtil.getBytes( 7 ) );
104             documentType.setMetadataHandler( daoUtil.getString( 8 ) );
105             DocumentAttributeHome.setDocumentTypeAttributes( documentType );
106         }
107 
108         daoUtil.free( );
109 
110         return documentType;
111     }
112 
113     /**
114      * Delete a record from the table
115      * 
116      * @param strCode
117      *            the code type
118      */
119     public void delete( String strCode )
120     {
121         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
122         daoUtil.setString( 1, strCode );
123 
124         daoUtil.executeUpdate( );
125         daoUtil.free( );
126     }
127 
128     /**
129      * Update the record in the table
130      * 
131      * @param documentType
132      *            The reference of documentType
133      */
134     public void store( DocumentType documentType )
135     {
136         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
137         daoUtil.setString( 1, documentType.getCode( ) );
138         daoUtil.setString( 2, documentType.getName( ) );
139         daoUtil.setString( 3, documentType.getDescription( ) );
140         daoUtil.setInt( 4, documentType.getThumbnailAttributeId( ) );
141         daoUtil.setString( 5, documentType.getDefaultThumbnailUrl( ) );
142         daoUtil.setString( 6, documentType.getMetadataHandler( ) );
143         daoUtil.setString( 7, documentType.getOldCode( ) );
144 
145         daoUtil.executeUpdate( );
146         daoUtil.free( );
147     }
148 
149     /**
150      * Load the list of documentTypes
151      * 
152      * @return The Collection of the DocumentTypes
153      */
154     public Collection<DocumentType> selectDocumentTypeList( )
155     {
156         Collection<DocumentType> listDocumentTypes = new ArrayList<DocumentType>( );
157         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
158         daoUtil.executeQuery( );
159 
160         while ( daoUtil.next( ) )
161         {
162             DocumentTypeusiness/DocumentType.html#DocumentType">DocumentType documentType = new DocumentType( );
163             documentType.setCode( daoUtil.getString( 1 ) );
164             documentType.setName( daoUtil.getString( 2 ) );
165             documentType.setDescription( daoUtil.getString( 3 ) );
166             documentType.setThumbnailAttributeId( daoUtil.getInt( 4 ) );
167             documentType.setDefaultThumbnailUrl( daoUtil.getString( 5 ) );
168             documentType.setMetadataHandler( daoUtil.getString( 6 ) );
169 
170             listDocumentTypes.add( documentType );
171         }
172 
173         daoUtil.free( );
174 
175         return listDocumentTypes;
176     }
177 
178     /**
179      * Load the Referencelist of documentTypes
180      * 
181      * @return listDocumentTypes
182      */
183     public ReferenceList getDocumentTypeList( )
184     {
185         ReferenceList listDocumentTypes = new ReferenceList( );
186         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
187         daoUtil.executeQuery( );
188 
189         while ( daoUtil.next( ) )
190         {
191             DocumentTypeusiness/DocumentType.html#DocumentType">DocumentType documentType = new DocumentType( );
192             documentType.setCode( daoUtil.getString( 1 ) );
193             documentType.setName( daoUtil.getString( 2 ) );
194             listDocumentTypes.addItem( documentType.getCode( ), documentType.getName( ) );
195         }
196 
197         daoUtil.free( );
198 
199         return listDocumentTypes;
200     }
201 
202     /**
203      *
204      * @param strCode
205      *            The code type
206      * @return bCheck the boolean
207      */
208     public boolean checkDocuments( String strCode )
209     {
210         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_CHECK_DOCUMENTS );
211         daoUtil.setString( 1, strCode );
212         daoUtil.executeQuery( );
213 
214         boolean bCheck = daoUtil.next( );
215         daoUtil.free( );
216 
217         return bCheck;
218     }
219 
220     /**
221      *
222      * @param nIdAttribute1
223      *            the attribute order
224      * @param nOrderAttribute1
225      *            the attribute order
226      * @param nIdAttribute2
227      *            the attribute order
228      * @param nOrderAttribute2
229      *            the attribute order
230      */
231     public void reorderAttributes( int nIdAttribute1, int nOrderAttribute1, int nIdAttribute2, int nOrderAttribute2 )
232     {
233         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_REORDER_ATTRIBUTES );
234         daoUtil.setInt( 1, nOrderAttribute1 );
235         daoUtil.setInt( 2, nIdAttribute1 );
236         daoUtil.executeUpdate( );
237         daoUtil.setInt( 1, nOrderAttribute2 );
238         daoUtil.setInt( 2, nIdAttribute2 );
239         daoUtil.executeUpdate( );
240         daoUtil.free( );
241     }
242 
243     /**
244      * Sets the admin stylesheet
245      * 
246      * @param baXslAdmin
247      *            The stylesheet
248      * @param strCodeType
249      *            The code type
250      */
251     public void setAdminStyleSheet( byte [ ] baXslAdmin, String strCodeType )
252     {
253         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_ADMIN_STYLESHEET );
254         daoUtil.setBytes( 1, baXslAdmin );
255         daoUtil.setString( 2, strCodeType );
256 
257         daoUtil.executeUpdate( );
258         daoUtil.free( );
259     }
260 
261     /**
262      * Sets the content service stylesheet
263      * 
264      * @param baXslContent
265      *            The stylesheet
266      * @param strCodeType
267      *            The code type
268      */
269     public void setContentStyleSheet( byte [ ] baXslContent, String strCodeType )
270     {
271         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE_CONTENT_STYLESHEET );
272         daoUtil.setBytes( 1, baXslContent );
273         daoUtil.setString( 2, strCodeType );
274 
275         daoUtil.executeUpdate( );
276         daoUtil.free( );
277     }
278 }