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.directories.business;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.ReferenceList;
38  import fr.paris.lutece.util.sql.DAOUtil;
39  import java.sql.Statement;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  /**
44   * This class provides Data Access methods for DirectoryEntity objects
45   */
46  public final class DirectoryEntityDAO implements IDirectoryEntityDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_SELECT = "SELECT id_reference, id_directory, id_creator, date_creation, id_modificator, date_update, title  FROM directories_directory_entity WHERE id_reference = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO directories_directory_entity ( id_directory, id_creator, date_creation, title ) VALUES ( ?, ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM directories_directory_entity WHERE id_reference = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE directories_directory_entity SET id_reference = ?, id_directory = ?, id_creator = ?, date_creation = ?,id_modificator = ?, date_update = ?, title = ? WHERE id_reference = ?";
53      private static final String SQL_QUERY_SELECTALL = "SELECT id_reference, id_directory, id_creator, date_creation, id_modificator, date_update, title FROM directories_directory_entity";
54      private static final String SQL_QUERY_SELECTALL_BY_DIRECTORY = "SELECT id_reference, id_directory, id_creator, date_creation, id_modificator, date_update, title FROM directories_directory_entity WHERE id_directory = ?";
55      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_reference FROM directories_directory_entity";
56  
57      /**
58       * {@inheritDoc }
59       */
60      @Override
61      public void insert( DirectoryEntity directoryEntity, Plugin plugin )
62      {
63          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
64          {
65              int nIndex = 1;
66              daoUtil.setInt( nIndex++, directoryEntity.getIdDirectory( ) );
67              daoUtil.setInt( nIndex++, directoryEntity.getIdCreator( ) );
68              daoUtil.setTimestamp( nIndex++, directoryEntity.getCreation( ) );
69              daoUtil.setString( nIndex++, directoryEntity.getTitle( ) );
70              daoUtil.executeUpdate( );
71              if ( daoUtil.nextGeneratedKey( ) )
72              {
73                  directoryEntity.setId( daoUtil.getGeneratedKeyInt( 1 ) );
74              }
75          }
76      }
77  
78      /**
79       * {@inheritDoc }
80       */
81      @Override
82      public DirectoryEntity load( int nKey, Plugin plugin )
83      {
84          DirectoryEntity directoryEntity = null;
85          try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin ) )
86          {
87              daoUtil.setInt( 1, nKey );
88              daoUtil.executeQuery( );
89              if ( daoUtil.next( ) )
90              {
91                  directoryEntity = new DirectoryEntity( );
92                  int nIndex = 1;
93                  directoryEntity.setId( daoUtil.getInt( nIndex++ ) );
94                  directoryEntity.setIdDirectory( daoUtil.getInt( nIndex++ ) );
95                  directoryEntity.setIdCreator( daoUtil.getInt( nIndex++ ) );
96                  directoryEntity.setDateCreation( daoUtil.getTimestamp( nIndex++ ) );
97                  directoryEntity.setIdModificator( daoUtil.getInt( nIndex++ ) );
98                  directoryEntity.setUpdate( daoUtil.getTimestamp( nIndex++ ) );
99                  directoryEntity.setTitle( daoUtil.getString( nIndex++ ) );
100             }
101         }
102         return directoryEntity;
103     }
104 
105     /**
106      * {@inheritDoc }
107      */
108     @Override
109     public void delete( int nKey, Plugin plugin )
110     {
111         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
112         {
113             daoUtil.setInt( 1, nKey );
114             daoUtil.executeUpdate( );
115         }
116     }
117 
118     /**
119      * {@inheritDoc }
120      */
121     @Override
122     public void store( DirectoryEntity directoryEntity, Plugin plugin )
123     {
124         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
125         {
126             int nIndex = 1;
127             daoUtil.setInt( nIndex++, directoryEntity.getId( ) );
128             daoUtil.setInt( nIndex++, directoryEntity.getIdDirectory( ) );
129             daoUtil.setInt( nIndex++, directoryEntity.getIdCreator( ) );
130             daoUtil.setTimestamp( nIndex++, directoryEntity.getCreation( ) );
131             daoUtil.setInt( nIndex++, directoryEntity.getIdModificator( ) );
132             daoUtil.setTimestamp( nIndex++, directoryEntity.getUpdate( ) );
133             daoUtil.setString( nIndex++, directoryEntity.getTitle( ) );
134             daoUtil.setInt( nIndex, directoryEntity.getId( ) );
135             daoUtil.executeUpdate( );
136         }
137     }
138 
139     /**
140      * {@inheritDoc }
141      */
142     @Override
143     public List<DirectoryEntity> selectDirectoryEntitiesList( Plugin plugin )
144     {
145         List<DirectoryEntity> directoryEntityList = new ArrayList<>( );
146         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
147         {
148             daoUtil.executeQuery( );
149             while ( daoUtil.next( ) )
150             {
151                 DirectoryEntityness/DirectoryEntity.html#DirectoryEntity">DirectoryEntity directoryEntity = new DirectoryEntity( );
152                 int nIndex = 1;
153                 directoryEntity.setId( daoUtil.getInt( nIndex++ ) );
154                 directoryEntity.setIdDirectory( daoUtil.getInt( nIndex++ ) );
155                 directoryEntity.setIdCreator( daoUtil.getInt( nIndex++ ) );
156                 directoryEntity.setDateCreation( daoUtil.getTimestamp( nIndex++ ) );
157                 directoryEntity.setIdModificator( daoUtil.getInt( nIndex++ ) );
158                 directoryEntity.setUpdate( daoUtil.getTimestamp( nIndex++ ) );
159                 directoryEntity.setTitle( daoUtil.getString( nIndex++ ) );
160                 directoryEntityList.add( directoryEntity );
161             }
162         }
163         return directoryEntityList;
164     }
165 
166     /**
167      * {@inheritDoc }
168      */
169     @Override
170     public List<Integer> selectIdDirectoryEntitiesList( Plugin plugin )
171     {
172         List<Integer> directoryEntityList = new ArrayList<>( );
173         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin ) )
174         {
175             daoUtil.executeQuery( );
176             while ( daoUtil.next( ) )
177             {
178                 directoryEntityList.add( daoUtil.getInt( 1 ) );
179             }
180         }
181         return directoryEntityList;
182     }
183 
184     /**
185      * {@inheritDoc }
186      */
187     @Override
188     public ReferenceList selectDirectoryEntitiesReferenceList( Plugin plugin )
189     {
190         ReferenceList directoryEntityList = new ReferenceList( );
191         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin ) )
192         {
193             daoUtil.executeQuery( );
194             while ( daoUtil.next( ) )
195             {
196                 directoryEntityList.addItem( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
197             }
198         }
199         return directoryEntityList;
200     }
201 
202     /**
203      * {@inheritDoc }
204      */
205     @Override
206     public List<DirectoryEntity> selectDirectoryEntitiesListByIdDirectory( int nKey, Plugin plugin )
207     {
208         List<DirectoryEntity> directoryEntityList = new ArrayList<>( );
209         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_BY_DIRECTORY, plugin ) )
210         {
211             daoUtil.setInt( 1, nKey );
212             daoUtil.executeQuery( );
213             while ( daoUtil.next( ) )
214             {
215                 DirectoryEntityness/DirectoryEntity.html#DirectoryEntity">DirectoryEntity directoryEntity = new DirectoryEntity( );
216                 int nIndex = 1;
217                 directoryEntity.setId( daoUtil.getInt( nIndex++ ) );
218                 directoryEntity.setIdDirectory( daoUtil.getInt( nIndex++ ) );
219                 directoryEntity.setIdCreator( daoUtil.getInt( nIndex++ ) );
220                 directoryEntity.setDateCreation( daoUtil.getTimestamp( nIndex++ ) );
221                 directoryEntity.setIdModificator( daoUtil.getInt( nIndex++ ) );
222                 directoryEntity.setUpdate( daoUtil.getTimestamp( nIndex++ ) );
223                 directoryEntity.setTitle( daoUtil.getString( nIndex++ ) );
224                 directoryEntityList.add( directoryEntity );
225             }
226         }
227         return directoryEntityList;
228     }
229 }