View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de 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.gru.business.feature;
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.List;
41  
42  /**
43   * This class provides Data Access methods for Feature objects
44   */
45  public final class FeatureDAO implements IFeatureDAO
46  {
47      // Constants
48      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_feature ) FROM gru_feature";
49      private static final String SQL_QUERY_SELECT = "SELECT id_feature, name, link, link_customer_params, target, id_category, id_order, display_level FROM gru_feature WHERE id_feature = ?";
50      private static final String SQL_QUERY_INSERT = "INSERT INTO gru_feature ( id_feature, name, link, link_customer_params, target, id_category, id_order, display_level ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? ) ";
51      private static final String SQL_QUERY_DELETE = "DELETE FROM gru_feature WHERE id_feature = ? ";
52      private static final String SQL_QUERY_UPDATE = "UPDATE gru_feature SET id_feature = ?, name = ?, link = ?, link_customer_params = ?, target = ?, id_category = ?, id_order = ?, display_level = ? WHERE id_feature = ?";
53      private static final String SQL_QUERY_SELECTALL = "SELECT a.id_feature, a.name, a.link, a.link_customer_params, a.target, a.id_category, b.name, a.id_order, a.display_level "
54              + " FROM gru_feature a, gru_feature_category b WHERE a.id_category = b.id_feature_category ORDER BY a.id_order";
55      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_feature FROM gru_feature";
56      private static final String SQL_QUERY_SELECT_BY_CATEGORY = "SELECT id_feature, name, link, link_customer_params, target, id_category, id_order, display_level FROM gru_feature WHERE id_category = ? ORDER BY id_order";
57  
58      /**
59       * Generates a new primary key
60       * 
61       * @param plugin
62       *            The Plugin
63       * @return The new primary key
64       */
65      public int newPrimaryKey( Plugin plugin )
66      {
67          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
68          daoUtil.executeQuery( );
69  
70          int nKey = 1;
71  
72          if ( daoUtil.next( ) )
73          {
74              nKey = daoUtil.getInt( 1 ) + 1;
75          }
76  
77          daoUtil.free( );
78  
79          return nKey;
80      }
81  
82      /**
83       * {@inheritDoc }
84       */
85      @Override
86      public void insert( Feature feature, Plugin plugin )
87      {
88          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
89  
90          feature.setId( newPrimaryKey( plugin ) );
91  
92          int nIndex = 1;
93          daoUtil.setInt( nIndex++, feature.getId( ) );
94          daoUtil.setString( nIndex++, feature.getName( ) );
95          daoUtil.setString( nIndex++, feature.getLink( ) );
96          daoUtil.setString( nIndex++, feature.getLinkCustomerParams( ) );
97          daoUtil.setInt( nIndex++, feature.getTarget( ) );
98          daoUtil.setInt( nIndex++, feature.getIdCategory( ) );
99          daoUtil.setInt( nIndex++, feature.getIdOrder( ) );
100         daoUtil.setInt( nIndex++, feature.getDisplayLevel( ) );
101 
102         daoUtil.executeUpdate( );
103         daoUtil.free( );
104     }
105 
106     /**
107      * {@inheritDoc }
108      */
109     @Override
110     public Feature load( int nKey, Plugin plugin )
111     {
112         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
113         daoUtil.setInt( 1, nKey );
114         daoUtil.executeQuery( );
115 
116         Feature feature = null;
117 
118         if ( daoUtil.next( ) )
119         {
120             feature = new Feature( );
121 
122             int nIndex = 1;
123             feature.setId( daoUtil.getInt( nIndex++ ) );
124             feature.setName( daoUtil.getString( nIndex++ ) );
125             feature.setLink( daoUtil.getString( nIndex++ ) );
126             feature.setLinkCustomerParams( daoUtil.getString( nIndex++ ) );
127             feature.setTarget( daoUtil.getInt( nIndex++ ) );
128             feature.setIdCategory( daoUtil.getInt( nIndex++ ) );
129             feature.setIdOrder( daoUtil.getInt( nIndex++ ) );
130             feature.setDisplayLevel( daoUtil.getInt( nIndex++ ) );
131         }
132 
133         daoUtil.free( );
134 
135         return feature;
136     }
137 
138     /**
139      * {@inheritDoc }
140      */
141     @Override
142     public void delete( int nKey, Plugin plugin )
143     {
144         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
145         daoUtil.setInt( 1, nKey );
146         daoUtil.executeUpdate( );
147         daoUtil.free( );
148     }
149 
150     /**
151      * {@inheritDoc }
152      */
153     @Override
154     public void store( Feature feature, Plugin plugin )
155     {
156         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
157 
158         int nIndex = 1;
159         daoUtil.setInt( nIndex++, feature.getId( ) );
160         daoUtil.setString( nIndex++, feature.getName( ) );
161         daoUtil.setString( nIndex++, feature.getLink( ) );
162         daoUtil.setString( nIndex++, feature.getLinkCustomerParams( ) );
163         daoUtil.setInt( nIndex++, feature.getTarget( ) );
164         daoUtil.setInt( nIndex++, feature.getIdCategory( ) );
165         daoUtil.setInt( nIndex++, feature.getIdOrder( ) );
166         daoUtil.setInt( nIndex++, feature.getDisplayLevel( ) );
167         daoUtil.setInt( nIndex++, feature.getId( ) );
168 
169         daoUtil.executeUpdate( );
170         daoUtil.free( );
171     }
172 
173     /**
174      * {@inheritDoc }
175      */
176     @Override
177     public List<Feature> selectFeaturesList( Plugin plugin )
178     {
179         List<Feature> featureList = new ArrayList<Feature>( );
180         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
181         daoUtil.executeQuery( );
182 
183         while ( daoUtil.next( ) )
184         {
185             Featurens/gru/business/feature/Feature.html#Feature">Feature feature = new Feature( );
186 
187             int nIndex = 1;
188             feature.setId( daoUtil.getInt( nIndex++ ) );
189             feature.setName( daoUtil.getString( nIndex++ ) );
190             feature.setLink( daoUtil.getString( nIndex++ ) );
191             feature.setLinkCustomerParams( daoUtil.getString( nIndex++ ) );
192             feature.setTarget( daoUtil.getInt( nIndex++ ) );
193             feature.setIdCategory( daoUtil.getInt( nIndex++ ) );
194             feature.setCategory( daoUtil.getString( nIndex++ ) );
195             feature.setIdOrder( daoUtil.getInt( nIndex++ ) );
196             feature.setDisplayLevel( daoUtil.getInt( nIndex++ ) );
197 
198             featureList.add( feature );
199         }
200 
201         daoUtil.free( );
202 
203         return featureList;
204     }
205 
206     /**
207      * {@inheritDoc }
208      */
209     @Override
210     public List<Feature> selectFeaturesListByCategory( int nCategory, Plugin plugin )
211     {
212         List<Feature> featureList = new ArrayList<Feature>( );
213         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_CATEGORY, plugin );
214         daoUtil.setInt( 1, nCategory );
215         daoUtil.executeQuery( );
216 
217         while ( daoUtil.next( ) )
218         {
219             Featurens/gru/business/feature/Feature.html#Feature">Feature feature = new Feature( );
220 
221             int nIndex = 1;
222             feature.setId( daoUtil.getInt( nIndex++ ) );
223             feature.setName( daoUtil.getString( nIndex++ ) );
224             feature.setLink( daoUtil.getString( nIndex++ ) );
225             feature.setLinkCustomerParams( daoUtil.getString( nIndex++ ) );
226             feature.setTarget( daoUtil.getInt( nIndex++ ) );
227             feature.setIdCategory( daoUtil.getInt( nIndex++ ) );
228             feature.setIdOrder( daoUtil.getInt( nIndex++ ) );
229             feature.setDisplayLevel( daoUtil.getInt( nIndex++ ) );
230 
231             featureList.add( feature );
232         }
233 
234         daoUtil.free( );
235 
236         return featureList;
237     }
238 
239     /**
240      * {@inheritDoc }
241      */
242     @Override
243     public List<Integer> selectIdFeaturesList( Plugin plugin )
244     {
245         List<Integer> featureList = new ArrayList<Integer>( );
246         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
247         daoUtil.executeQuery( );
248 
249         while ( daoUtil.next( ) )
250         {
251             featureList.add( daoUtil.getInt( 1 ) );
252         }
253 
254         daoUtil.free( );
255 
256         return featureList;
257     }
258 }