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