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          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE ) )
70          {
71              daoUtil.setInt( 1, nPortletId );
72              daoUtil.setInt( 2, nSpaceId );
73              daoUtil.executeUpdate( );
74          }
75      }
76  
77      /**
78       * Delete records from a portlet
79       *
80       * @param nPortletId
81       *            the portlet identifier
82       */
83      public void deleteAllSpaces( int nPortletId )
84      {
85          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_ALL_SPACES ) )
86          {
87              daoUtil.setInt( 1, nPortletId );
88              daoUtil.executeUpdate( );
89          }
90      }
91  
92      /**
93       * Insert a new record in the table document_auto_publication
94       *
95       * @param documentAutoPublication
96       *            the instance of the DocumentAutoPublication object to insert
97       */
98      public void insert( DocumentAutoPublication documentAutoPublication )
99      {
100         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT ) )
101         {
102             daoUtil.setInt( 1, documentAutoPublication.getIdPortlet( ) );
103             daoUtil.setInt( 2, documentAutoPublication.getIdSpace( ) );
104 
105             daoUtil.executeUpdate( );
106         }
107     }
108 
109     /**
110      * Loads the data of Document Auto Publication whose identifier is specified in parameter
111      *
112      * @param nPortletId
113      *            The {@link Portlet} identifier
114      * @param nSpaceId
115      *            The {@link DocumentSpace} identifier
116      * @return The {@link DocumentAutoPublication} object
117      */
118     public DocumentAutoPublication load( int nPortletId, int nSpaceId )
119     {
120         DocumentAutoPublication documentAutoPublication = null;
121 
122         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_PRIMARY_KEY ) )
123         {
124             daoUtil.setInt( 1, nPortletId );
125             daoUtil.setInt( 2, nSpaceId );
126             daoUtil.executeQuery( );
127 
128             if ( daoUtil.next( ) )
129             {
130                 documentAutoPublication = new DocumentAutoPublication( );
131                 documentAutoPublication.setIdPortlet( daoUtil.getInt( 1 ) );
132                 documentAutoPublication.setIdSpace( daoUtil.getInt( 2 ) );
133             }
134         }
135         return documentAutoPublication;
136     }
137 
138     /**
139      * Loads all data Document Auto Publication
140      *
141      * @return The {@link Collection} of {@link DocumentAutoPublication} object
142      */
143     public Collection<DocumentAutoPublication> load( )
144     {
145         Collection<DocumentAutoPublication> listDocumentAutoPublication = new ArrayList<>( );
146         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL ) )
147         {
148             daoUtil.executeQuery( );
149 
150             while ( daoUtil.next( ) )
151             {
152                 DocumentAutoPublicationtion/DocumentAutoPublication.html#DocumentAutoPublication">DocumentAutoPublication documentAutoPublication = new DocumentAutoPublication( );
153                 documentAutoPublication.setIdPortlet( daoUtil.getInt( 1 ) );
154                 documentAutoPublication.setIdSpace( daoUtil.getInt( 2 ) );
155                 listDocumentAutoPublication.add( documentAutoPublication );
156             }
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         Collection<DocumentAutoPublication> listDocumentAutoPublication = new ArrayList<>( );
171         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_PORTLET_ID ) )
172         {
173             daoUtil.setInt( 1, nPortletId );
174             daoUtil.executeQuery( );
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         return listDocumentAutoPublication;
185     }
186 
187     /**
188      * Load the list of Document Auto Publication whose {@link DocumentSpace} identifier is specified in parameter
189      *
190      * @param nSpaceId
191      *            The {@link DocumentSpace} identifier
192      * @return The {@link DocumentAutoPublication} object
193      */
194     public Collection<DocumentAutoPublication> selectBySpaceId( int nSpaceId )
195     {
196         Collection<DocumentAutoPublication> listDocumentAutoPublication = new ArrayList<>( );
197         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_SPACE_ID ) )
198         {
199             daoUtil.setInt( 1, nSpaceId );
200             daoUtil.executeQuery( );
201 
202             while ( daoUtil.next( ) )
203             {
204                 DocumentAutoPublicationtion/DocumentAutoPublication.html#DocumentAutoPublication">DocumentAutoPublication documentAutoPublication = new DocumentAutoPublication( );
205                 documentAutoPublication.setIdPortlet( daoUtil.getInt( 1 ) );
206                 documentAutoPublication.setIdSpace( nSpaceId );
207                 listDocumentAutoPublication.add( documentAutoPublication );
208             }
209         }
210         return listDocumentAutoPublication;
211     }
212 
213     /**
214      * Update the record in the table
215      *
216      * @param documentAutoPublication
217      *            The DocumentAutoPublication to update
218      */
219     public void store( DocumentAutoPublication documentAutoPublication )
220     {
221         // DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
222         //
223         // daoUtil.setInt( 1, documentAutoPublication.getIdPortlet( ) );
224         // daoUtil.setInt( 2, documentAutoPublication.getIdSpace( ) );
225         //
226         // daoUtil.executeUpdate( );
227         //
228         // daoUtil.free( );
229     }
230 }