View Javadoc
1   /*
2    * Copyright (c) 2002-2016, 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  
35  package fr.paris.lutece.plugins.myportal.modules.rss.business;
36  
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.util.ReferenceList;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * This class provides Data Access methods for RssConf objects
46   */
47  public final class RssConfDAO implements IRssConfDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_rss_conf ) FROM myportal_rss_advanced_conf";
51      private static final String SQL_QUERY_SELECT = "SELECT id_rss_conf, attributeUser, attributeValue, url, idCategory, idStyleSheet FROM myportal_rss_advanced_conf WHERE id_rss_conf = ?";
52      private static final String SQL_QUERY_INSERT = "INSERT INTO myportal_rss_advanced_conf ( id_rss_conf, attributeUser, attributeValue, url, idCategory, idStyleSheet ) VALUES (?, ?, ?, ?, ?, ? ) ";
53      private static final String SQL_QUERY_DELETE = "DELETE FROM myportal_rss_advanced_conf WHERE id_rss_conf = ? ";
54      private static final String SQL_QUERY_UPDATE = "UPDATE myportal_rss_advanced_conf SET id_rss_conf = ?, attributeUser = ?, attributeValue = ?, url = ?, idCategory = ?, idStyleSheet = ? WHERE id_rss_conf = ?";
55      private static final String SQL_QUERY_SELECTALL = "SELECT id_rss_conf, attributeUser, attributeValue, url, idCategory, idStyleSheet FROM myportal_rss_advanced_conf";
56      private static final String SQL_QUERY_SELECTALL_ID = "SELECT id_rss_conf FROM myportal_rss_advanced_conf";
57      private static final String SQL_QUERY_SELECT_BY_CATEGORY = "SELECT id_rss_conf, attributeUser, attributeValue, url, idCategory, idStyleSheet FROM myportal_rss_advanced_conf WHERE  idCategory = ?";
58  
59      /**
60       * Generates a new primary key
61       * @param plugin The Plugin
62       * @return The new primary key
63       */
64      public int newPrimaryKey( Plugin plugin)
65      {
66          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK , plugin  );
67          daoUtil.executeQuery( );
68          int nKey = 1;
69  
70          if( daoUtil.next( ) )
71          {
72              nKey = daoUtil.getInt( 1 ) + 1;
73          }
74  
75          daoUtil.free();
76          return nKey;
77      }
78  
79      /**
80       * {@inheritDoc }
81       */
82      @Override
83      public void insert( RssConf rssConf, Plugin plugin )
84      {
85          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
86          rssConf.setId( newPrimaryKey( plugin ) );
87          int nIndex = 1;
88          
89          daoUtil.setInt( nIndex++ , rssConf.getId( ) );
90          daoUtil.setString( nIndex++ , rssConf.getAttributeUser( ) );
91          daoUtil.setString( nIndex++ , rssConf.getAttributeValue( ) );
92          daoUtil.setString( nIndex++ , rssConf.getUrl( ) );
93          daoUtil.setInt( nIndex++ , rssConf.getIdCategory( ) );
94          daoUtil.setInt( nIndex++ , rssConf.getIdStyleSheet( ) );
95          
96          daoUtil.executeUpdate( );
97          daoUtil.free( );
98      }
99  
100     /**
101      * {@inheritDoc }
102      */
103     @Override
104     public RssConf load( int nKey, Plugin plugin )
105     {
106         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
107         daoUtil.setInt( 1 , nKey );
108         daoUtil.executeQuery( );
109         RssConf rssConf = null;
110 
111         if ( daoUtil.next( ) )
112         {
113             rssConf = new RssConf();
114             int nIndex = 1;
115             
116             rssConf.setId( daoUtil.getInt( nIndex++ ) );
117             rssConf.setAttributeUser( daoUtil.getString( nIndex++ ) );
118             rssConf.setAttributeValue( daoUtil.getString( nIndex++ ) );
119             rssConf.setUrl( daoUtil.getString( nIndex++ ) );
120             rssConf.setIdCategory( daoUtil.getInt( nIndex++ ) );
121             rssConf.setIdStyleSheet( daoUtil.getInt( nIndex++ ) );
122 
123         }
124 
125         daoUtil.free( );
126         return rssConf;
127     }
128 
129     /**
130      * {@inheritDoc }
131      */
132     @Override
133     public void delete( int nKey, Plugin plugin )
134     {
135         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
136         daoUtil.setInt( 1 , nKey );
137         daoUtil.executeUpdate( );
138         daoUtil.free( );
139     }
140 
141     /**
142      * {@inheritDoc }
143      */
144     @Override
145     public void store( RssConf rssConf, Plugin plugin )
146     {
147         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
148         int nIndex = 1;
149         
150         daoUtil.setInt( nIndex++ , rssConf.getId( ) );
151         daoUtil.setString( nIndex++ , rssConf.getAttributeUser( ) );
152         daoUtil.setString( nIndex++ , rssConf.getAttributeValue( ) );
153         daoUtil.setString( nIndex++ , rssConf.getUrl( ) );
154         daoUtil.setInt( nIndex++ , rssConf.getIdCategory( ) );
155         daoUtil.setInt( nIndex++ , rssConf.getIdStyleSheet( ) );
156 
157         daoUtil.setInt( nIndex , rssConf.getId( ) );
158 
159         daoUtil.executeUpdate( );
160         daoUtil.free( );
161     }
162 
163     /**
164      * {@inheritDoc }
165      */
166     @Override
167     public List<RssConf> selectRssConfsList( Plugin plugin )
168     {
169         List<RssConf> rssConfList = new ArrayList<RssConf>(  );
170         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
171         daoUtil.executeQuery(  );
172 
173         while ( daoUtil.next(  ) )
174         {
175             RssConf rssConf = new RssConf(  );
176             int nIndex = 1;
177             
178             rssConf.setId( daoUtil.getInt( nIndex++ ) );
179             rssConf.setAttributeUser( daoUtil.getString( nIndex++ ) );
180             rssConf.setAttributeValue( daoUtil.getString( nIndex++ ) );
181             rssConf.setUrl( daoUtil.getString( nIndex++ ) );
182             rssConf.setIdCategory( daoUtil.getInt( nIndex++ ) );
183             rssConf.setIdStyleSheet( daoUtil.getInt( nIndex++ ) );
184 
185 
186             rssConfList.add( rssConf );
187         }
188 
189         daoUtil.free( );
190         return rssConfList;
191     }
192     
193     /**
194      * {@inheritDoc }
195      */
196     @Override
197     public List<Integer> selectIdRssConfsList( Plugin plugin )
198     {
199         List<Integer> rssConfList = new ArrayList<Integer>( );
200         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL_ID, plugin );
201         daoUtil.executeQuery(  );
202 
203         while ( daoUtil.next(  ) )
204         {
205             rssConfList.add( daoUtil.getInt( 1 ) );
206         }
207 
208         daoUtil.free( );
209         return rssConfList;
210     }
211     
212     /**
213      * {@inheritDoc }
214      */
215     @Override
216     public ReferenceList selectRssConfsReferenceList( Plugin plugin )
217     {
218         ReferenceList rssConfList = new ReferenceList();
219         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL, plugin );
220         daoUtil.executeQuery(  );
221 
222         while ( daoUtil.next(  ) )
223         {
224             rssConfList.addItem( daoUtil.getInt( 1 ) , daoUtil.getString( 2 ) );
225         }
226 
227         daoUtil.free( );
228         return rssConfList;
229     }
230     
231     /**
232      * {@inheritDoc }
233      */
234     @Override
235     public List<RssConf> loadByCategory( int nIdCategory, Plugin plugin )
236     {
237     	
238         List<RssConf> rssConfList = new ArrayList<RssConf>(  );
239 
240         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_CATEGORY, plugin );
241         daoUtil.setInt( 1 , nIdCategory );
242         daoUtil.executeQuery( );
243 
244         while ( daoUtil.next(  ) )
245         {
246             RssConf rssConf = new RssConf(  );
247             int nIndex = 1;
248             
249             rssConf.setId( daoUtil.getInt( nIndex++ ) );
250             rssConf.setAttributeUser( daoUtil.getString( nIndex++ ) );
251             rssConf.setAttributeValue( daoUtil.getString( nIndex++ ) );
252             rssConf.setUrl( daoUtil.getString( nIndex++ ) );
253             rssConf.setIdCategory( daoUtil.getInt( nIndex++ ) );
254             rssConf.setIdStyleSheet( daoUtil.getInt( nIndex++ ) );
255 
256 
257             rssConfList.add( rssConf );
258         }
259 
260         daoUtil.free( );
261         return rssConfList;
262     }
263 }