View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.blog.business;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.util.sql.DAOUtil;
41  
42  /**
43   * This class provides Data Access methods for Blog objects
44   */
45  public final class DocContentDAO implements IDocContentDAO
46  {
47  
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_document ) FROM blog_content";
50      private static final String SQL_QUERY_INSERT_CONTENT = "INSERT INTO blog_content ( id_document, id_type, text_value , binary_value, mime_type ) VALUES ( ?, ? , ? , ?, ? )";
51      private static final String SQL_QUERY_INSERT_CONTENT_IN_BLOG = "INSERT INTO blog_blog_content ( id_blog, id_document, priority ) VALUES ( ?, ?, ? )";
52      private static final String SQL_QUERY_SELECT_CONTENT = "SELECT a.id_document, id_type, text_value , binary_value, mime_type, b.priority FROM blog_content a , blog_blog_content b WHERE b.id_blog = ?  AND a.id_document = b.id_document";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM blog_blog_content WHERE id_blog = ?  ;";
54      private static final String SQL_QUERY_DELETE_BY_ID_IN_BLOG = "DELETE FROM blog_blog_content WHERE id_document = ?  ;";
55      private static final String SQL_QUERY_UPDATE = "UPDATE blog_content SET id_type= ?, text_value = ?, binary_value = ?, mime_type = ? WHERE id_document = ?";
56      private static final String SQL_QUERY_SELECT_CONTENT_BY_PRIMARY_KEY = "SELECT  id_document, id_type , text_value , binary_value, mime_type FROM blog_content WHERE id_document = ? ";
57      private static final String SQL_QUERY_DELETE_BY_ID = "DELETE FROM blog_content WHERE id_document = ?  ;";
58  
59      private static final String SQL_QUERY_SELECT_CONTENT_TYPE_BY_PRIMARY_KEY = "SELECT id_type, type_label FROM blog_content_type WHERE id_type = ? ";
60      private static final String SQL_QUERY_SELECT_CONTENT_TYPE = "SELECT id_type, type_label FROM blog_content_type ";
61  
62      /**
63       * Generates a new primary key
64       * 
65       * @param plugin
66       *            The Plugin
67       * @return The new primary key
68       */
69      public int newPrimaryKey( Plugin plugin )
70      {
71          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin ) )
72          {
73              daoUtil.executeQuery( );
74              int nKey = 1;
75  
76              if ( daoUtil.next( ) )
77              {
78                  nKey = daoUtil.getInt( 1 ) + 1;
79              }
80  
81              daoUtil.free( );
82              return nKey;
83          }
84      }
85  
86      /**
87       * {@inheritDoc }
88       */
89      @Override
90      public void insertDocContent( DocContent docContent, Plugin plugin )
91      {
92          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_CONTENT, plugin ) )
93          {
94              docContent.setId( newPrimaryKey( plugin ) );
95  
96              daoUtil.setInt( 1, docContent.getId( ) );
97              daoUtil.setInt( 2, docContent.getContentType( ).getIdContentType( ) );
98              daoUtil.setString( 3, docContent.getTextValue( ) );
99              daoUtil.setBytes( 4, docContent.getBinaryValue( ) );
100             daoUtil.setString( 5, docContent.getValueContentType( ) );
101 
102             daoUtil.executeUpdate( );
103             daoUtil.free( );
104         }
105     }
106 
107     @Override
108     public void insertDocContentInBlog( int nIdBlog, int nIdDocument, int nPriority, Plugin plugin )
109     {
110         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_CONTENT_IN_BLOG, plugin ) )
111         {
112 
113             daoUtil.setInt( 1, nIdBlog );
114             daoUtil.setInt( 2, nIdDocument );
115             daoUtil.setInt( 3, nPriority );
116 
117             daoUtil.executeUpdate( );
118             daoUtil.free( );
119         }
120     }
121 
122     /**
123      * {@inheritDoc }
124      */
125     @Override
126     public List<DocContent> loadDocContentByIdHtemldoc( int idBlog, Plugin plugin )
127     {
128         List<DocContent> listDoc = new ArrayList<>( );
129         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTENT, plugin ) )
130         {
131             daoUtil.setInt( 1, idBlog );
132             daoUtil.executeQuery( );
133 
134             while ( daoUtil.next( ) )
135             {
136                 DocContentusiness/DocContent.html#DocContent">DocContent docContent = new DocContent( );
137 
138                 docContent.setId( daoUtil.getInt( 1 ) );
139                 docContent.setContentType( loadContentType( daoUtil.getInt( 2 ), plugin ) );
140                 docContent.setTextValue( daoUtil.getString( 3 ) );
141                 docContent.setBinaryValue( daoUtil.getBytes( 4 ) );
142                 docContent.setValueContentType( daoUtil.getString( 5 ) );
143                 docContent.setPriority( daoUtil.getInt( 6 ) );
144 
145                 listDoc.add( docContent );
146             }
147             daoUtil.free( );
148         }
149         return listDoc;
150     }
151 
152     /**
153      * {@inheritDoc }
154      */
155     @Override
156     public DocContent loadDocContent( int idDocument, Plugin plugin )
157     {
158         DocContent docContent = null;
159         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTENT_BY_PRIMARY_KEY, plugin ) )
160         {
161             daoUtil.setInt( 1, idDocument );
162             daoUtil.executeQuery( );
163 
164             if ( daoUtil.next( ) )
165             {
166                 docContent = new DocContent( );
167 
168                 docContent.setId( daoUtil.getInt( 1 ) );
169                 docContent.setContentType( loadContentType( daoUtil.getInt( 2 ), plugin ) );
170                 docContent.setTextValue( daoUtil.getString( 3 ) );
171                 docContent.setBinaryValue( daoUtil.getBytes( 4 ) );
172                 docContent.setValueContentType( daoUtil.getString( 5 ) );
173 
174             }
175             daoUtil.free( );
176         }
177         return docContent;
178     }
179 
180     /**
181      * {@inheritDoc }
182      */
183     @Override
184     public void delete( int nBlogId, Plugin plugin )
185     {
186         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
187         {
188             daoUtil.setInt( 1, nBlogId );
189 
190             daoUtil.executeUpdate( );
191             daoUtil.free( );
192         }
193     }
194 
195     /**
196      * {@inheritDoc }
197      */
198     @Override
199     public void deleteById( int nDocumentId, Plugin plugin )
200     {
201         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_ID, plugin ) )
202         {
203             daoUtil.setInt( 1, nDocumentId );
204 
205             daoUtil.executeUpdate( );
206             daoUtil.free( );
207         }
208     }
209 
210     @Override
211     public void deleteInBlogById( int nDocumentId, Plugin plugin )
212     {
213         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_BY_ID_IN_BLOG, plugin ) )
214         {
215             daoUtil.setInt( 1, nDocumentId );
216 
217             daoUtil.executeUpdate( );
218             daoUtil.free( );
219         }
220     }
221 
222     /**
223      * {@inheritDoc }
224      */
225     @Override
226     public void store( DocContent docContent, Plugin plugin )
227     {
228         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
229         {
230             daoUtil.setInt( 1, docContent.getContentType( ).getIdContentType( ) );
231 
232             daoUtil.setString( 2, docContent.getTextValue( ) );
233             daoUtil.setBytes( 3, docContent.getBinaryValue( ) );
234             daoUtil.setString( 4, docContent.getValueContentType( ) );
235             daoUtil.setInt( 5, docContent.getId( ) );
236 
237             daoUtil.executeUpdate( );
238             daoUtil.free( );
239         }
240     }
241 
242     /**
243      * {@inheritDoc }
244      */
245     @Override
246     public ContentType loadContentType( int idType, Plugin plugin )
247     {
248         ContentType contentType = null;
249         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTENT_TYPE_BY_PRIMARY_KEY, plugin ) )
250         {
251             daoUtil.setInt( 1, idType );
252             daoUtil.executeQuery( );
253 
254             if ( daoUtil.next( ) )
255             {
256                 contentType = new ContentType( );
257 
258                 contentType.setIdContentType( daoUtil.getInt( 1 ) );
259                 contentType.setLabel( daoUtil.getString( 2 ) );
260 
261             }
262             daoUtil.free( );
263 
264             return contentType;
265         }
266     }
267 
268     /**
269      * {@inheritDoc }
270      */
271     @Override
272     public List<ContentType> loadListContentType( Plugin plugin )
273     {
274         List<ContentType> listcontentType = new ArrayList<>( );
275         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_CONTENT_TYPE, plugin ) )
276         {
277 
278             daoUtil.executeQuery( );
279 
280             while ( daoUtil.next( ) )
281             {
282                 ContentTypeiness/ContentType.html#ContentType">ContentType contentType = new ContentType( );
283 
284                 contentType.setIdContentType( daoUtil.getInt( 1 ) );
285                 contentType.setLabel( daoUtil.getString( 2 ) );
286                 listcontentType.add( contentType );
287 
288             }
289             daoUtil.free( );
290         }
291         return listcontentType;
292     }
293 }