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.service.mailinglist;
35  
36  import fr.paris.lutece.portal.business.mailinglist.MailingList;
37  import fr.paris.lutece.portal.business.mailinglist.MailingListFilter;
38  import fr.paris.lutece.portal.business.mailinglist.MailingListHome;
39  import fr.paris.lutece.portal.business.mailinglist.MailingListUsersFilter;
40  import fr.paris.lutece.portal.business.mailinglist.Recipient;
41  import fr.paris.lutece.portal.business.user.AdminUser;
42  import fr.paris.lutece.portal.business.user.AdminUserHome;
43  import fr.paris.lutece.portal.business.workgroup.AdminWorkgroupHome;
44  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
45  import fr.paris.lutece.util.ReferenceItem;
46  import fr.paris.lutece.util.ReferenceList;
47  
48  import java.util.ArrayList;
49  import java.util.Collection;
50  import java.util.List;
51  
52  
53  /**
54   * AdminMailingListService
55   */
56  public final class AdminMailingListService
57  {
58      public static final String ALL_ROLES = "*";
59  
60      /**
61       * Private COnstructor
62       */
63      private AdminMailingListService(  )
64      {
65      }
66  
67      /**
68       * Initialize
69       */
70      public static void init(  )
71      {
72          // Initialize mailing list
73          MailingList.init(  );
74      }
75  
76      /**
77       * Returns a list of all mailing list visible by the user
78       * @param user The user
79       * @return The list as a ReferenceList
80       */
81      public static ReferenceList getMailingLists( AdminUser user )
82      {
83          ReferenceList list = new ReferenceList(  );
84  
85          for ( MailingList mailinglist : getUserMailingLists( user ) )
86          {
87              list.addItem( mailinglist.getId(  ), mailinglist.getName(  ) );
88          }
89  
90          return list;
91      }
92  
93      /**
94       * Returns a list of all mailing list visible by the user
95       * @param user The user
96       * @return The list as a mailinglist Collection
97       */
98      public static Collection<MailingList> getUserMailingLists( AdminUser user )
99      {
100         Collection<MailingList> listMailinglists = new ArrayList<MailingList>(  );
101 
102         // Add all global mailing lists
103         listMailinglists.addAll( MailingListHome.findByWorkgroup( AdminWorkgroupService.ALL_GROUPS ) );
104 
105         // Add mailing list of the user's workgroups
106         ReferenceList listWorkgroups = AdminWorkgroupHome.getUserWorkgroups( user );
107 
108         for ( ReferenceItem workgroup : listWorkgroups )
109         {
110             listMailinglists.addAll( MailingListHome.findByWorkgroup( workgroup.getCode(  ) ) );
111         }
112 
113         return listMailinglists;
114     }
115 
116     /**
117      * Gets the user mailing lists by filter.
118      *
119      * @param user the user
120      * @param filter the filter
121      * @return the user mailing lists by filter
122      */
123     public static List<MailingList> getUserMailingListsByFilter( AdminUser user, MailingListFilter filter )
124     {
125         MailingListFilter mailingListFilter = new MailingListFilter( filter );
126         // First get mailinglist by workgroup 'all'
127         mailingListFilter.setWorkgroup( AdminWorkgroupService.ALL_GROUPS );
128 
129         List<MailingList> listMailingLists = MailingListHome.findsByFilter( mailingListFilter );
130 
131         // Add mailing list of the user's workgroups
132         ReferenceList listWorkgroups = AdminWorkgroupHome.getUserWorkgroups( user );
133 
134         for ( ReferenceItem workgroup : listWorkgroups )
135         {
136             mailingListFilter = new MailingListFilter( filter );
137             mailingListFilter.setWorkgroup( workgroup.getCode(  ) );
138             listMailingLists.addAll( MailingListHome.findsByFilter( mailingListFilter ) );
139         }
140 
141         return listMailingLists;
142     }
143 
144     /**
145      * Returns all the recipient of a given mailing list
146      * @param nIdMailingList The mailing list Id
147      * @return The list
148      */
149     public static Collection<Recipient> getRecipients( int nIdMailingList )
150     {
151         Collection<Recipient> listRecipients = new ArrayList<Recipient>(  );
152         MailingList mailinglist = MailingListHome.findByPrimaryKey( nIdMailingList );
153 
154         if ( mailinglist != null )
155         {
156             for ( MailingListUsersFilter filter : mailinglist.getFilters(  ) )
157             {
158                 listRecipients.addAll( getRecipients( filter.getWorkgroup(  ), filter.getRole(  ) ) );
159             }
160         }
161 
162         return listRecipients;
163     }
164 
165     /**
166      * Gets all recipients corresponding to a filter based on a Workgroup and a role
167      * @param strWorkgroup The workgroup
168      * @param strRole The role
169      * @return A collection of recipient
170      */
171     public static Collection<Recipient> getRecipients( String strWorkgroup, String strRole )
172     {
173         Collection<Recipient> listRecipients = new ArrayList<Recipient>(  );
174         Collection<AdminUser> listUsers;
175 
176         if ( ( strWorkgroup != null ) && ( !strWorkgroup.equals( AdminWorkgroupService.ALL_GROUPS ) ) )
177         {
178             listUsers = AdminWorkgroupHome.getUserListForWorkgroup( strWorkgroup );
179         }
180         else
181         {
182             listUsers = AdminUserHome.findUserList(  );
183         }
184 
185         for ( AdminUser user : listUsers )
186         {
187             if ( ( strRole != null ) && ( !strRole.equals( ALL_ROLES ) ) )
188             {
189                 if ( !user.isInRole( strRole ) )
190                 {
191                     // skip this user if it isn't in the role
192                     continue;
193                 }
194             }
195 
196             Recipient recipient = new Recipient(  );
197             recipient.setName( user.getFirstName(  ) + " " + user.getLastName(  ) );
198             recipient.setEmail( user.getEmail(  ) );
199             listRecipients.add( recipient );
200         }
201 
202         return listRecipients;
203     }
204 
205     /**
206      * Check if the filter already exists or not in a mailing list
207      * @param filter the filter
208      * @param nIdMailingList the id mailing list
209      * @return true if it already exists, false otherwise
210      */
211     public static boolean checkFilter( MailingListUsersFilter filter, int nIdMailingList )
212     {
213         return MailingListHome.checkFilter( filter, nIdMailingList );
214     }
215 }