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.autopublication;
35  
36  import fr.paris.lutece.plugins.document.business.spaces.DocumentSpace;
37  import fr.paris.lutece.portal.business.portlet.Portlet;
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 DocumentAutoPublication objects
45   *
46   */
47  public class DocumentAutoPublicationDAO implements IDocumentAutoPublicationDAO
48  {
49      private static final String SQL_QUERY_INSERT = "INSERT INTO document_auto_publication ( id_portlet , id_space ) VALUES ( ? , ? )";
50      private static final String SQL_QUERY_SELECT_BY_PRIMARY_KEY = "SELECT id_portlet, id_space FROM document_auto_publication WHERE id_portlet = ? AND id_space = ?";
51      private static final String SQL_QUERY_SELECT_ALL = "SELECT id_portlet, id_space FROM document_auto_publication ";
52      private static final String SQL_QUERY_SELECT_BY_PORTLET_ID = "SELECT id_space FROM document_auto_publication WHERE id_portlet = ? ";
53      private static final String SQL_QUERY_SELECT_BY_SPACE_ID = "SELECT id_portlet FROM document_auto_publication WHERE id_space = ? ";
54  
55      // private static final String SQL_QUERY_UPDATE = "UPDATE document_auto_publication SET WHERE id_portlet = ? AND id_space = ? ";
56      private static final String SQL_QUERY_DELETE = "DELETE FROM document_auto_publication WHERE id_portlet= ? AND id_space= ? ";
57      private static final String SQL_QUERY_DELETE_ALL_SPACES = "DELETE FROM document_auto_publication WHERE id_portlet= ? ";
58  
59      /**
60       * Deletes records from a Document Auto Publication object identifier in the table document_auto_publication
61       *
62       * @param nPortletId
63       *            the portlet identifier
64       * @param nSpaceId
65       *            the {@link DocumentSpace} identifier
66       */
67      public void delete( int nPortletId, int nSpaceId )
68      {
69          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
70          daoUtil.setInt( 1, nPortletId );
71          daoUtil.setInt( 2, nSpaceId );
72          daoUtil.executeUpdate( );
73          daoUtil.free( );
74      }
75  
76      /**
77       * Delete records from a portlet
78       *
79       * @param nPortletId
80       *            the portlet identifier
81       */
82      public void deleteAllSpaces( int nPortletId )
83      {
84          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ALL_SPACES );
85          daoUtil.setInt( 1, nPortletId );
86          daoUtil.executeUpdate( );
87          daoUtil.free( );
88      }
89  
90      /**
91       * Insert a new record in the table document_auto_publication
92       *
93       * @param documentAutoPublication
94       *            the instance of the DocumentAutoPublication object to insert
95       */
96      public void insert( DocumentAutoPublication documentAutoPublication )
97      {
98          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
99          daoUtil.setInt( 1, documentAutoPublication.getIdPortlet( ) );
100         daoUtil.setInt( 2, documentAutoPublication.getIdSpace( ) );
101 
102         daoUtil.executeUpdate( );
103         daoUtil.free( );
104     }
105 
106     /**
107      * Loads the data of Document Auto Publication whose identifier is specified in parameter
108      *
109      * @param nPortletId
110      *            The {@link Portlet} identifier
111      * @param nSpaceId
112      *            The {@link DocumentSpace} identifier
113      * @return The {@link DocumentAutoPublication} object
114      */
115     public DocumentAutoPublication load( int nPortletId, int nSpaceId )
116     {
117         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_PRIMARY_KEY );
118         daoUtil.setInt( 1, nPortletId );
119         daoUtil.setInt( 2, nSpaceId );
120         daoUtil.executeQuery( );
121 
122         DocumentAutoPublication documentAutoPublication = null;
123 
124         if ( daoUtil.next( ) )
125         {
126             documentAutoPublication = new DocumentAutoPublication( );
127             documentAutoPublication.setIdPortlet( daoUtil.getInt( 1 ) );
128             documentAutoPublication.setIdSpace( daoUtil.getInt( 2 ) );
129         }
130 
131         daoUtil.free( );
132 
133         return documentAutoPublication;
134     }
135 
136     /**
137      * Loads all data Document Auto Publication
138      *
139      * @return The {@link Collection} of {@link DocumentAutoPublication} object
140      */
141     public Collection<DocumentAutoPublication> load( )
142     {
143         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL );
144         daoUtil.executeQuery( );
145 
146         Collection<DocumentAutoPublication> listDocumentAutoPublication = new ArrayList<DocumentAutoPublication>( );
147 
148         while ( daoUtil.next( ) )
149         {
150             DocumentAutoPublicationtion/DocumentAutoPublication.html#DocumentAutoPublication">DocumentAutoPublication documentAutoPublication = new DocumentAutoPublication( );
151             documentAutoPublication.setIdPortlet( daoUtil.getInt( 1 ) );
152             documentAutoPublication.setIdSpace( daoUtil.getInt( 2 ) );
153             listDocumentAutoPublication.add( documentAutoPublication );
154         }
155 
156         daoUtil.free( );
157 
158         return listDocumentAutoPublication;
159     }
160 
161     /**
162      * Load the list of Document Auto Publication whose portlet identifier is specified in parameter
163      *
164      * @param nPortletId
165      *            The {@link Portlet} identifier
166      * @return The {@link DocumentAutoPublication} object
167      */
168     public Collection<DocumentAutoPublication> selectByPortletId( int nPortletId )
169     {
170         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_PORTLET_ID );
171         daoUtil.setInt( 1, nPortletId );
172         daoUtil.executeQuery( );
173 
174         Collection<DocumentAutoPublication> listDocumentAutoPublication = new ArrayList<DocumentAutoPublication>( );
175 
176         while ( daoUtil.next( ) )
177         {
178             DocumentAutoPublicationtion/DocumentAutoPublication.html#DocumentAutoPublication">DocumentAutoPublication documentAutoPublication = new DocumentAutoPublication( );
179             documentAutoPublication.setIdPortlet( nPortletId );
180             documentAutoPublication.setIdSpace( daoUtil.getInt( 1 ) );
181             listDocumentAutoPublication.add( documentAutoPublication );
182         }
183 
184         daoUtil.free( );
185 
186         return listDocumentAutoPublication;
187     }
188 
189     /**
190      * Load the list of Document Auto Publication whose {@link DocumentSpace} identifier is specified in parameter
191      *
192      * @param nSpaceId
193      *            The {@link DocumentSpace} identifier
194      * @return The {@link DocumentAutoPublication} object
195      */
196     public Collection<DocumentAutoPublication> selectBySpaceId( int nSpaceId )
197     {
198         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_SPACE_ID );
199         daoUtil.setInt( 1, nSpaceId );
200         daoUtil.executeQuery( );
201 
202         Collection<DocumentAutoPublication> listDocumentAutoPublication = new ArrayList<DocumentAutoPublication>( );
203 
204         while ( daoUtil.next( ) )
205         {
206             DocumentAutoPublicationtion/DocumentAutoPublication.html#DocumentAutoPublication">DocumentAutoPublication documentAutoPublication = new DocumentAutoPublication( );
207             documentAutoPublication.setIdPortlet( daoUtil.getInt( 1 ) );
208             documentAutoPublication.setIdSpace( nSpaceId );
209             listDocumentAutoPublication.add( documentAutoPublication );
210         }
211 
212         daoUtil.free( );
213 
214         return listDocumentAutoPublication;
215     }
216 
217     /**
218      * Update the record in the table
219      *
220      * @param documentAutoPublication
221      *            The DocumentAutoPublication to update
222      */
223     public void store( DocumentAutoPublication documentAutoPublication )
224     {
225         // DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
226         //
227         // daoUtil.setInt( 1, documentAutoPublication.getIdPortlet( ) );
228         // daoUtil.setInt( 2, documentAutoPublication.getIdSpace( ) );
229         //
230         // daoUtil.executeUpdate( );
231         //
232         // daoUtil.free( );
233     }
234 }