View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.rss.business.portlet;
35  
36  import fr.paris.lutece.portal.business.portlet.Portlet;
37  import fr.paris.lutece.util.sql.DAOUtil;
38  
39  import java.util.ArrayList;
40  import java.util.List;
41  
42  
43  /**
44   * this class provides Data Access methods for RssPortlet objects
45   */
46  public final class RssPortletDAO implements IRssPortletDAO
47  {
48      ////////////////////////////////////////////////////////////////////////////
49      // Constants
50      private static final String SQL_QUERY_SELECT = "SELECT id_portlet, rss_feed_id FROM rss_portlet WHERE id_portlet = ? ";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO rss_portlet ( id_portlet, rss_feed_id ) VALUES ( ?, ? )";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM rss_portlet WHERE id_portlet = ? ";
53      private static final String SQL_QUERY_UPDATE = "UPDATE rss_portlet SET id_portlet = ?, rss_feed_id = ? WHERE id_portlet = ? ";
54      private static final String SQL_QUERY_LINKED_PORTLET = "SELECT id_portlet FROM rss_portlet WHERE rss_feed_id = ? ";
55  
56      ///////////////////////////////////////////////////////////////////////////////////////
57      // Access methods to data
58  
59      /**
60       * Insert a new record in the table.
61       *
62       * @param portlet The Instance of the Portlet
63       */
64      public void insert( Portlet portlet )
65      {
66          RssPortlet"../../../../../../../fr/paris/lutece/plugins/rss/business/portlet/RssPortlet.html#RssPortlet">RssPortlet p = (RssPortlet) portlet;
67          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
68          daoUtil.setInt( 1, p.getId(  ) );
69          daoUtil.setString( 2, p.getRssFeedId(  ) );
70          daoUtil.executeUpdate(  );
71          daoUtil.free(  );
72      }
73  
74      /**
75       * Delete record from table
76       *
77       * @param nPortletId The indentifier of the Portlet
78       */
79      public void delete( int nPortletId )
80      {
81          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
82          daoUtil.setInt( 1, nPortletId );
83          daoUtil.executeUpdate(  );
84          daoUtil.free(  );
85      }
86  
87      /**
88       * Update the record in the table
89       *
90       * @param portlet The reference of the portlet
91       */
92      public void store( Portlet portlet )
93      {
94          RssPortlet"../../../../../../../fr/paris/lutece/plugins/rss/business/portlet/RssPortlet.html#RssPortlet">RssPortlet p = (RssPortlet) portlet;
95          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
96          daoUtil.setInt( 1, p.getId(  ) );
97          daoUtil.setString( 2, p.getRssFeedId(  ) );
98          daoUtil.setInt( 3, p.getId(  ) );
99  
100         daoUtil.executeUpdate(  );
101         daoUtil.free(  );
102     }
103 
104     /**
105      * load the data of dbpagePortlet from the table
106      * @return portlet The instance of the object portlet
107      * @param nIdPortlet The identifier of the portlet
108      */
109     public Portlet load( int nIdPortlet )
110     {
111         RssPortletrss/business/portlet/RssPortlet.html#RssPortlet">RssPortlet portlet = new RssPortlet(  );
112         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
113         daoUtil.setInt( 1, nIdPortlet );
114         daoUtil.executeQuery(  );
115 
116         if ( daoUtil.next(  ) )
117         {
118             portlet.setId( daoUtil.getInt( 1 ) );
119             portlet.setRssFeedId( daoUtil.getString( 2 ) );
120         }
121 
122         daoUtil.free(  );
123 
124         return portlet;
125     }
126 
127     /**
128      * Checks if a feed is linked to a portlet
129      * @return A boolean
130      * @param nIdRssFeed The identifier of the Rss feed
131      */
132     public List<RssPortlet> checkLinkedPortlet( int nIdRssFeed )
133     {
134         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_LINKED_PORTLET );
135         daoUtil.setInt( 1, nIdRssFeed );
136         daoUtil.executeQuery(  );
137 
138         List<RssPortlet> listPortlet = new ArrayList<RssPortlet>(  );
139 
140         while ( daoUtil.next(  ) )
141         {
142             RssPortletrss/business/portlet/RssPortlet.html#RssPortlet">RssPortlet portlet = new RssPortlet(  );
143             portlet.setId( daoUtil.getInt( 1 ) );
144             portlet.setRssFeedId( Integer.toString( nIdRssFeed ) );
145             listPortlet.add( portlet );
146         }
147 
148         daoUtil.free(  );
149 
150         return listPortlet;
151     }
152 }