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.extend.business.extender;
35  
36  import fr.paris.lutece.portal.service.plugin.Plugin;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.Arrays;
41  import java.util.List;
42  
43  /**
44   *
45   * ExtenderResourceDAO
46   *
47   */
48  public class ResourceExtenderDAO implements IResourceExtenderDAO
49  {
50      private static final String SQL_QUERY_NEW_PK = " SELECT max( id_extender ) FROM extend_resource_extender ";
51      private static final String SQL_QUERY_SELECT_ALL = " SELECT id_extender, extender_type, id_resource, resource_type, is_active FROM extend_resource_extender ";
52      private static final String SQL_QUERY_SELECT_ID_EXTENDER = " SELECT id_extender FROM extend_resource_extender ";
53      private static final String SQL_QUERY_SELECT = SQL_QUERY_SELECT_ALL + " WHERE id_extender = ? ";
54      private static final String SQL_QUERY_UPDATE = " UPDATE extend_resource_extender SET extender_type = ?, id_resource = ?, resource_type = ?, is_active = ? WHERE id_extender = ? ";
55      private static final String SQL_QUERY_INSERT = " INSERT INTO extend_resource_extender (id_extender, extender_type, id_resource, resource_type) VALUES ( ?,?,?,? ) ";
56      private static final String SQL_QUERY_DELETE = " DELETE FROM extend_resource_extender where id_extender = ? ";
57  
58      // FILTERS
59      private static final String SQL_WHERE = " WHERE ";
60      private static final String SQL_FILTER_ID_EXTENDER = " id_extender IN ";
61  
62      // CONSTANTS
63      private static final String OPEN_BRACKET = " ( ";
64      private static final String CLOSED_BRACKET = " ) ";
65      private static final String QUESTION_MARK = " ? ";
66      private static final String COMMA = " , ";
67  
68      /**
69       * New primary key
70       * 
71       * @param plugin
72       *            the plugin
73       * @return a new primary key
74       */
75      private int newPrimaryKey( Plugin plugin )
76      {
77          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
78          daoUtil.executeQuery( );
79  
80          int nKey = 1;
81  
82          if ( daoUtil.next( ) )
83          {
84              nKey = daoUtil.getInt( 1 ) + 1;
85          }
86  
87          daoUtil.free( );
88  
89          return nKey;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public synchronized void insert( ResourceExtenderDTO extender, Plugin plugin )
97      {
98          int nIndex = 1;
99          int nIdExtender = newPrimaryKey( plugin );
100         extender.setIdExtender( nIdExtender );
101 
102         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
103         daoUtil.setInt( nIndex++, extender.getIdExtender( ) );
104         daoUtil.setString( nIndex++, extender.getExtenderType( ) );
105         daoUtil.setString( nIndex++, extender.getIdExtendableResource( ) );
106         daoUtil.setString( nIndex, extender.getExtendableResourceType( ) );
107 
108         daoUtil.executeUpdate( );
109         daoUtil.free( );
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public void store( ResourceExtenderDTO extender, Plugin plugin )
117     {
118         int nIndex = 1;
119         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
120         daoUtil.setString( nIndex++, extender.getExtenderType( ) );
121         daoUtil.setString( nIndex++, extender.getIdExtendableResource( ) );
122         daoUtil.setString( nIndex++, extender.getExtendableResourceType( ) );
123         daoUtil.setBoolean( nIndex++, extender.isIsActive( ) );
124 
125         daoUtil.setInt( nIndex, extender.getIdExtender( ) );
126 
127         daoUtil.executeUpdate( );
128         daoUtil.free( );
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public void delete( int nIdExtender, Plugin plugin )
136     {
137         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
138         daoUtil.setInt( 1, nIdExtender );
139 
140         daoUtil.executeUpdate( );
141         daoUtil.free( );
142     }
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public ResourceExtenderDTO load( int nIdExtender, Plugin plugin )
149     {
150         ResourceExtenderDTO extender = null;
151         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
152         daoUtil.setInt( 1, nIdExtender );
153         daoUtil.executeQuery( );
154 
155         if ( daoUtil.next( ) )
156         {
157             extender = populateDTO( daoUtil );
158         }
159 
160         daoUtil.free( );
161 
162         return extender;
163     }
164 
165     /**
166      * {@inheritDoc}
167      */
168     @Override
169     public List<ResourceExtenderDTO> loadAll( Plugin plugin )
170     {
171         List<ResourceExtenderDTO> listExtenders = new ArrayList<ResourceExtenderDTO>( );
172         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin );
173         daoUtil.executeQuery( );
174 
175         while ( daoUtil.next( ) )
176         {
177             ResourceExtenderDTO extender = populateDTO( daoUtil );
178             listExtenders.add( extender );
179         }
180 
181         daoUtil.free( );
182 
183         return listExtenders;
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public List<ResourceExtenderDTO> loadByFilter( ResourceExtenderDTOFilter filter, Plugin plugin )
191     {
192         List<ResourceExtenderDTO> listExtenders = new ArrayList<ResourceExtenderDTO>( );
193         DAOUtil daoUtil = new DAOUtil( filter.buildSQLQuery( SQL_QUERY_SELECT_ALL ), plugin );
194         filter.setFilterValues( daoUtil );
195         daoUtil.executeQuery( );
196 
197         while ( daoUtil.next( ) )
198         {
199             ResourceExtenderDTO extender = populateDTO( daoUtil );
200             listExtenders.add( extender );
201         }
202 
203         daoUtil.free( );
204 
205         return listExtenders;
206     }
207 
208     /**
209      * {@inheritDoc}
210      */
211     @Override
212     public List<Integer> loadIdsByFilter( ResourceExtenderDTOFilter filter, Plugin plugin )
213     {
214         List<Integer> listIdsExtender = new ArrayList<Integer>( );
215         DAOUtil daoUtil = new DAOUtil( filter.buildSQLQuery( SQL_QUERY_SELECT_ID_EXTENDER ), plugin );
216         filter.setFilterValues( daoUtil );
217         daoUtil.executeQuery( );
218 
219         while ( daoUtil.next( ) )
220         {
221             listIdsExtender.add( daoUtil.getInt( 1 ) );
222         }
223 
224         daoUtil.free( );
225 
226         return listIdsExtender;
227     }
228 
229     /**
230      * {@inheritDoc}
231      */
232     @Override
233     public List<ResourceExtenderDTO> loadByListIds( List<Integer> listIds, Plugin plugin )
234     {
235         List<ResourceExtenderDTO> listExtenders = new ArrayList<ResourceExtenderDTO>( );
236 
237         if ( ( listIds != null ) && !listIds.isEmpty( ) )
238         {
239             // array to keep order from listId
240             // because we have no way to keep it with a query
241             ResourceExtenderDTOnder/ResourceExtenderDTO.html#ResourceExtenderDTO">ResourceExtenderDTO [ ] arrayExtenders = new ResourceExtenderDTO [ listIds.size( )];
242             StringBuilder sbSQL = new StringBuilder( SQL_QUERY_SELECT_ALL );
243             sbSQL.append( SQL_WHERE ).append( SQL_FILTER_ID_EXTENDER );
244             sbSQL.append( OPEN_BRACKET );
245 
246             for ( int i = 0; i < listIds.size( ); i++ )
247             {
248                 sbSQL.append( QUESTION_MARK );
249 
250                 if ( i < ( listIds.size( ) - 1 ) )
251                 {
252                     sbSQL.append( COMMA );
253                 }
254             }
255 
256             sbSQL.append( CLOSED_BRACKET );
257 
258             DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin );
259             int nIndex = 1;
260 
261             for ( int nId : listIds )
262             {
263                 daoUtil.setInt( nIndex++, nId );
264             }
265 
266             daoUtil.executeQuery( );
267 
268             while ( daoUtil.next( ) )
269             {
270                 ResourceExtenderDTO extender = populateDTO( daoUtil );
271                 listExtenders.add( extender );
272                 // keep id order
273                 arrayExtenders [listIds.indexOf( extender.getIdExtender( ) )] = extender;
274             }
275 
276             daoUtil.free( );
277             // get list from array
278             listExtenders = Arrays.asList( arrayExtenders );
279         }
280 
281         return listExtenders;
282     }
283 
284     /**
285      * Populate dto.
286      *
287      * @param daoUtil
288      *            the dao util
289      * @return the resource extender dto
290      */
291     private ResourceExtenderDTO populateDTO( DAOUtil daoUtil )
292     {
293         int nIndex = 1;
294         ResourceExtenderDTOd/business/extender/ResourceExtenderDTO.html#ResourceExtenderDTO">ResourceExtenderDTO dto = new ResourceExtenderDTO( );
295         dto.setIdExtender( daoUtil.getInt( nIndex++ ) );
296         dto.setExtenderType( daoUtil.getString( nIndex++ ) );
297         dto.setIdExtendableResource( daoUtil.getString( nIndex++ ) );
298         dto.setExtendableResourceType( daoUtil.getString( nIndex++ ) );
299         dto.setIsActive( daoUtil.getBoolean( nIndex ) );
300 
301         return dto;
302     }
303 }