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