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.newsletter.business.portlet;
35  
36  import fr.paris.lutece.plugins.newsletter.service.NewsletterPlugin;
37  import fr.paris.lutece.portal.business.portlet.Portlet;
38  import fr.paris.lutece.portal.service.plugin.PluginService;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  import java.util.HashSet;
42  import java.util.Set;
43  
44  /**
45   * This class provides Data Access methods for NewsLetterSubscriptionPortlet objects
46   */
47  public final class NewsLetterSubscriptionPortletDAO implements INewsLetterSubscriptionPortletDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_SELECT = "SELECT id_portlet FROM core_portlet WHERE id_portlet = ?";
51      private static final String SQL_QUERY_SELECT_SUBSCRIPTION_BY_PORTLET = "SELECT id_newsletter FROM newsletter_portlet_subscribe WHERE id_portlet = ?";
52      private static final String SQL_QUERY_INSERT_NEWSLETTER = "INSERT INTO newsletter_portlet_subscribe( id_portlet, id_newsletter ) VALUES ( ?, ? )";
53      private static final String SQL_QUERY_DELETE_NEWSLETTER = "DELETE FROM newsletter_portlet_subscribe WHERE id_portlet = ? AND id_newsletter = ?";
54      private static final String SQL_QUERY_DELETE = "DELETE FROM newsletter_portlet_subscribe WHERE id_portlet=? ";
55  
56      ///////////////////////////////////////////////////////////////////////////////////////
57      // Access methods to data
58  
59      /**
60       * Inserts a new record in the table. Not implemented.
61       * 
62       * @param portlet
63       *            the object to be inserted
64       */
65      public void insert( Portlet portlet )
66      {
67          // Not implemented.
68      }
69  
70      /**
71       * Deletes a record from the table.
72       * 
73       * @param nPortletId
74       *            the portlet id
75       * 
76       */
77      public void delete( int nPortletId )
78      {
79          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) );
80          daoUtil.setInt( 1, nPortletId );
81          daoUtil.executeUpdate( );
82          daoUtil.free( );
83      }
84  
85      /**
86       * Loads the data of the portlet from the table.
87       * 
88       * @param nPortletId
89       *            the portlet id
90       * @return the Portlet object
91       */
92      public Portlet load( int nPortletId )
93      {
94          NewsLetterSubscriptionPortlet/portlet/NewsLetterSubscriptionPortlet.html#NewsLetterSubscriptionPortlet">NewsLetterSubscriptionPortlet portlet = new NewsLetterSubscriptionPortlet( );
95  
96          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
97  
98          daoUtil.setInt( 1, nPortletId );
99  
100         daoUtil.executeQuery( );
101 
102         if ( daoUtil.next( ) )
103         {
104             portlet.setId( nPortletId );
105         }
106 
107         daoUtil.free( );
108 
109         return portlet;
110     }
111 
112     /**
113      * Updates the record in the table. Not implemented.
114      * 
115      * @param portlet
116      *            the instance of Portlet class to be updated
117      */
118     public void store( Portlet portlet )
119     {
120         // Not implemented.
121     }
122 
123     /**
124      * Associates a new subscription to a given portlet.
125      * 
126      * @param nPortletId
127      *            the identifier of the portlet.
128      * @param nNewsletterId
129      *            the identifier of the subscription.
130      */
131     public void insertSubscription( int nPortletId, int nNewsletterId )
132     {
133         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT_NEWSLETTER, PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) );
134         daoUtil.setInt( 1, nPortletId );
135         daoUtil.setInt( 2, nNewsletterId );
136 
137         daoUtil.executeUpdate( );
138         daoUtil.free( );
139     }
140 
141     /**
142      * De-associate a subscription from a given portlet.
143      * 
144      * @param nPortletId
145      *            the identifier of the portlet.
146      * @param nNewsletterId
147      *            the identifier of the subscription.
148      */
149     public void removeSubscription( int nPortletId, int nNewsletterId )
150     {
151         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE_NEWSLETTER, PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) );
152         daoUtil.setInt( 1, nPortletId );
153         daoUtil.setInt( 2, nNewsletterId );
154 
155         daoUtil.executeUpdate( );
156         daoUtil.free( );
157     }
158 
159     /**
160      * Returns all the newsletters associated to a given portlet.
161      * 
162      * @param nPortletId
163      *            the identifier of the portlet.
164      * @return a Set of Integer objects containing the identifers of the susbscriptions.
165      */
166     public Set<Integer> findSelectedNewsletters( int nPortletId )
167     {
168         HashSet<Integer> results = new HashSet<Integer>( );
169 
170         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_SUBSCRIPTION_BY_PORTLET, PluginService.getPlugin( NewsletterPlugin.PLUGIN_NAME ) );
171         daoUtil.setInt( 1, nPortletId );
172         daoUtil.executeQuery( );
173 
174         while ( daoUtil.next( ) )
175         {
176             results.add( Integer.valueOf( daoUtil.getInt( 1 ) ) );
177         }
178 
179         daoUtil.free( );
180 
181         return results;
182     }
183 }