1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
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  
44  
45  public final class FeatureDAO implements IFeatureDAO
46  {
47      
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  
60  
61  
62  
63  
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  
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 
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 
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 
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 
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 
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 
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 }