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.portal.business.mailinglist;
35  
36  import fr.paris.lutece.util.sql.DAOUtil;
37  
38  import java.util.ArrayList;
39  import java.util.Collection;
40  import java.util.List;
41  
42  
43  /**
44   * This class provides Data Access methods for MailingList objects
45   */
46  public final class MailingListDAO implements IMailingListDAO
47  {
48      // Constants
49      private static final String SQL_QUERY_NEW_PK = "SELECT max( id_mailinglist ) FROM core_admin_mailinglist";
50      private static final String SQL_QUERY_SELECT = "SELECT id_mailinglist, name, description, workgroup FROM core_admin_mailinglist WHERE id_mailinglist = ?";
51      private static final String SQL_QUERY_INSERT = "INSERT INTO core_admin_mailinglist ( id_mailinglist, name, description, workgroup ) VALUES ( ?, ?, ?, ? ) ";
52      private static final String SQL_QUERY_DELETE = "DELETE FROM core_admin_mailinglist WHERE id_mailinglist = ? ";
53      private static final String SQL_QUERY_UPDATE = "UPDATE core_admin_mailinglist SET id_mailinglist = ?, name = ?, description = ?, workgroup = ? WHERE id_mailinglist = ?";
54      private static final String SQL_QUERY_SELECTALL = "SELECT id_mailinglist, name, description, workgroup FROM core_admin_mailinglist";
55      private static final String SQL_QUERY_SELECT_BY_WORKGROUP = "SELECT id_mailinglist, name, description, workgroup FROM core_admin_mailinglist WHERE workgroup = ? ";
56  
57      // filters
58      private static final String SQL_QUERY_FILTERS_INSERT = "INSERT INTO core_admin_mailinglist_filter ( id_mailinglist, workgroup, role ) VALUES ( ?, ?, ? ) ";
59      private static final String SQL_QUERY_FILTERS_DELETE = "DELETE FROM core_admin_mailinglist_filter WHERE id_mailinglist = ? ";
60      private static final String SQL_QUERY_FILTERS_DELETE_FILTER = "DELETE FROM core_admin_mailinglist_filter WHERE id_mailinglist = ? AND workgroup = ? AND role = ? ";
61      private static final String SQL_QUERY_FILTERS_SELECTALL = "SELECT id_mailinglist, workgroup, role FROM core_admin_mailinglist_filter WHERE id_mailinglist = ?";
62      private static final String SQL_QUERY_FILTERS_SELECT = "SELECT id_mailinglist, workgroup, role FROM core_admin_mailinglist_filter WHERE id_mailinglist = ? AND workgroup = ? AND role = ?";
63  
64      /**
65       * Generates a new primary key
66       *
67       * @return The new primary key
68       */
69      public int newPrimaryKey(  )
70      {
71          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK );
72          daoUtil.executeQuery(  );
73  
74          int nKey;
75  
76          if ( !daoUtil.next(  ) )
77          {
78              // if the table is empty
79              nKey = 1;
80          }
81  
82          nKey = daoUtil.getInt( 1 ) + 1;
83          daoUtil.free(  );
84  
85          return nKey;
86      }
87  
88      /**
89       * Insert a new record in the table.
90       *
91       * @param mailingList instance of the MailingList object to insert
92       */
93      @Override
94      public synchronized void insert( MailingList mailingList )
95      {
96          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT );
97  
98          mailingList.setId( newPrimaryKey(  ) );
99  
100         daoUtil.setInt( 1, mailingList.getId(  ) );
101         daoUtil.setString( 2, mailingList.getName(  ) );
102         daoUtil.setString( 3, mailingList.getDescription(  ) );
103         daoUtil.setString( 4, mailingList.getWorkgroup(  ) );
104 
105         daoUtil.executeUpdate(  );
106         daoUtil.free(  );
107     }
108 
109     /**
110      * Load the data of the mailingList from the table
111      *
112      * @param nId The identifier of the mailingList
113      * @return the instance of the MailingList
114      */
115     @Override
116     public MailingList load( int nId )
117     {
118         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT );
119         daoUtil.setInt( 1, nId );
120         daoUtil.executeQuery(  );
121 
122         MailingList mailinglist = null;
123 
124         if ( daoUtil.next(  ) )
125         {
126             mailinglist = new MailingList(  );
127 
128             mailinglist.setId( daoUtil.getInt( 1 ) );
129             mailinglist.setName( daoUtil.getString( 2 ) );
130             mailinglist.setDescription( daoUtil.getString( 3 ) );
131             mailinglist.setWorkgroup( daoUtil.getString( 4 ) );
132         }
133 
134         daoUtil.free(  );
135 
136         // load filters
137         selectMailingListUsersFilters( mailinglist );
138 
139         return mailinglist;
140     }
141 
142     /**
143      * Delete a record from the table
144      *
145      * @param nMailingListId The identifier of the mailingList
146      */
147     @Override
148     public void delete( int nMailingListId )
149     {
150         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE );
151         daoUtil.setInt( 1, nMailingListId );
152         daoUtil.executeUpdate(  );
153         daoUtil.free(  );
154 
155         // delete filters
156         deleteFilters( nMailingListId );
157     }
158 
159     /**
160      * Update the record in the table
161      *
162      * @param mailingList The reference of the mailingList
163      */
164     @Override
165     public void store( MailingList mailingList )
166     {
167         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE );
168 
169         daoUtil.setInt( 1, mailingList.getId(  ) );
170         daoUtil.setString( 2, mailingList.getName(  ) );
171         daoUtil.setString( 3, mailingList.getDescription(  ) );
172         daoUtil.setString( 4, mailingList.getWorkgroup(  ) );
173         daoUtil.setInt( 5, mailingList.getId(  ) );
174 
175         daoUtil.executeUpdate(  );
176         daoUtil.free(  );
177     }
178 
179     /**
180      * Load the data of all the mailingLists and returns them in form of a collection
181      *
182      * @return The Collection which contains the data of all the mailingLists
183      */
184     @Override
185     public Collection<MailingList> selectAll(  )
186     {
187         Collection<MailingList> mailingListList = new ArrayList<MailingList>(  );
188         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECTALL );
189         daoUtil.executeQuery(  );
190 
191         while ( daoUtil.next(  ) )
192         {
193             MailingList mailingList = new MailingList(  );
194 
195             mailingList.setId( daoUtil.getInt( 1 ) );
196             mailingList.setName( daoUtil.getString( 2 ) );
197             mailingList.setDescription( daoUtil.getString( 3 ) );
198             mailingList.setWorkgroup( daoUtil.getString( 4 ) );
199 
200             mailingListList.add( mailingList );
201         }
202 
203         daoUtil.free(  );
204 
205         return mailingListList;
206     }
207 
208     /**
209      * Returns all mailing lists having a scope restricted to a given workgroup
210      *
211      * @param strWorkgroup The workgroup
212      * @return the collection which contains the data of all the mailingLists
213      */
214     @Override
215     public Collection<MailingList> selectByWorkgroup( String strWorkgroup )
216     {
217         Collection<MailingList> mailingListList = new ArrayList<MailingList>(  );
218         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_WORKGROUP );
219         daoUtil.setString( 1, strWorkgroup );
220         daoUtil.executeQuery(  );
221 
222         while ( daoUtil.next(  ) )
223         {
224             MailingList mailingList = new MailingList(  );
225 
226             mailingList.setId( daoUtil.getInt( 1 ) );
227             mailingList.setName( daoUtil.getString( 2 ) );
228             mailingList.setDescription( daoUtil.getString( 3 ) );
229             mailingList.setWorkgroup( daoUtil.getString( 4 ) );
230 
231             mailingListList.add( mailingList );
232         }
233 
234         daoUtil.free(  );
235 
236         return mailingListList;
237     }
238 
239     /**
240      * Insert a new record in the table.
241      *
242      * @param nMailingListId The mailing list Id
243      * @param mailingListUsersFilter instance of the MailingListUsersFilter object to insert
244      */
245     @Override
246     public void insertFilter( MailingListUsersFilter mailingListUsersFilter, int nMailingListId )
247     {
248         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILTERS_INSERT );
249 
250         daoUtil.setInt( 1, nMailingListId );
251         daoUtil.setString( 2, mailingListUsersFilter.getWorkgroup(  ) );
252         daoUtil.setString( 3, mailingListUsersFilter.getRole(  ) );
253 
254         daoUtil.executeUpdate(  );
255         daoUtil.free(  );
256     }
257 
258     /**
259      * Remove an users filter from the mailing list
260      * @param nMailingListId The Id of the mailing list
261      * @param filter the filter to remove
262      */
263     @Override
264     public void deleteFilter( MailingListUsersFilter filter, int nMailingListId )
265     {
266         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILTERS_DELETE_FILTER );
267         daoUtil.setInt( 1, nMailingListId );
268         daoUtil.setString( 2, filter.getWorkgroup(  ) );
269         daoUtil.setString( 3, filter.getRole(  ) );
270         daoUtil.executeUpdate(  );
271         daoUtil.free(  );
272     }
273 
274     /**
275      * Delete a record from the table
276      *
277      * @param nMailingListUsersFilterId The identifier of the mailingListUsersFilter
278      */
279     public void deleteFilters( int nMailingListUsersFilterId )
280     {
281         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILTERS_DELETE );
282         daoUtil.setInt( 1, nMailingListUsersFilterId );
283         daoUtil.executeUpdate(  );
284         daoUtil.free(  );
285     }
286 
287     /**
288      * Load the data of all the mailingListUsersFilters and returns them in form of a collection
289      * @param mailinglist The mailing list
290      */
291     public void selectMailingListUsersFilters( MailingList mailinglist )
292     {
293         if ( mailinglist != null )
294         {
295             Collection<MailingListUsersFilter> mailingListUsersFilterList = mailinglist.getFilters(  );
296             DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILTERS_SELECTALL );
297             daoUtil.setInt( 1, mailinglist.getId(  ) );
298             daoUtil.executeQuery(  );
299 
300             while ( daoUtil.next(  ) )
301             {
302                 MailingListUsersFilter mailingListUsersFilter = new MailingListUsersFilter(  );
303 
304                 mailingListUsersFilter.setWorkgroup( daoUtil.getString( 2 ) );
305                 mailingListUsersFilter.setRole( daoUtil.getString( 3 ) );
306 
307                 mailingListUsersFilterList.add( mailingListUsersFilter );
308             }
309 
310             daoUtil.free(  );
311         }
312     }
313 
314     /**
315      * {@inheritDoc}
316      */
317     @Override
318     public boolean checkFilter( MailingListUsersFilter filter, int nId )
319     {
320         boolean bExists = false;
321         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FILTERS_SELECT );
322         daoUtil.setInt( 1, nId );
323         daoUtil.setString( 2, filter.getWorkgroup(  ) );
324         daoUtil.setString( 3, filter.getRole(  ) );
325         daoUtil.executeQuery(  );
326 
327         if ( daoUtil.next(  ) )
328         {
329             bExists = true;
330         }
331 
332         daoUtil.free(  );
333 
334         return bExists;
335     }
336 
337     /**
338          * {@inheritDoc}
339          */
340     @Override
341     public List<MailingList> selectByFilter( MailingListFilter filter )
342     {
343         List<MailingList> mailingListList = new ArrayList<MailingList>(  );
344         DAOUtil daoUtil = new DAOUtil( filter.buildSQLQuery( SQL_QUERY_SELECTALL ) );
345         filter.setFilterValues( daoUtil );
346         daoUtil.executeQuery(  );
347 
348         while ( daoUtil.next(  ) )
349         {
350             int nIndex = 1;
351             MailingList mailingList = new MailingList(  );
352 
353             mailingList.setId( daoUtil.getInt( nIndex++ ) );
354             mailingList.setName( daoUtil.getString( nIndex++ ) );
355             mailingList.setDescription( daoUtil.getString( nIndex++ ) );
356             mailingList.setWorkgroup( daoUtil.getString( nIndex ) );
357 
358             mailingListList.add( mailingList );
359         }
360 
361         daoUtil.free(  );
362 
363         return mailingListList;
364     }
365 }