AdminMailingListService.java

  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. import fr.paris.lutece.portal.business.mailinglist.MailingList;
  36. import fr.paris.lutece.portal.business.mailinglist.MailingListFilter;
  37. import fr.paris.lutece.portal.business.mailinglist.MailingListHome;
  38. import fr.paris.lutece.portal.business.mailinglist.MailingListUsersFilter;
  39. import fr.paris.lutece.portal.business.mailinglist.Recipient;
  40. import fr.paris.lutece.portal.business.user.AdminUser;
  41. import fr.paris.lutece.portal.business.user.AdminUserHome;
  42. import fr.paris.lutece.portal.business.workgroup.AdminWorkgroupHome;
  43. import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
  44. import fr.paris.lutece.util.ReferenceItem;
  45. import fr.paris.lutece.util.ReferenceList;

  46. import java.util.ArrayList;
  47. import java.util.Collection;
  48. import java.util.List;

  49. /**
  50.  * AdminMailingListService
  51.  */
  52. public final class AdminMailingListService
  53. {
  54.     public static final String ALL_ROLES = "*";

  55.     /**
  56.      * Private COnstructor
  57.      */
  58.     private AdminMailingListService( )
  59.     {
  60.     }

  61.     /**
  62.      * Initialize
  63.      */
  64.     public static void init( )
  65.     {
  66.         // Initialize mailing list
  67.         MailingList.init( );
  68.     }

  69.     /**
  70.      * Returns a list of all mailing list visible by the user
  71.      *
  72.      * @param user
  73.      *            The user
  74.      * @return The list as a ReferenceList
  75.      */
  76.     public static ReferenceList getMailingLists( AdminUser user )
  77.     {
  78.         ReferenceList list = new ReferenceList( );

  79.         for ( MailingList mailinglist : getUserMailingLists( user ) )
  80.         {
  81.             list.addItem( mailinglist.getId( ), mailinglist.getName( ) );
  82.         }

  83.         return list;
  84.     }

  85.     /**
  86.      * Returns a list of all mailing list visible by the user
  87.      *
  88.      * @param user
  89.      *            The user
  90.      * @return The list as a mailinglist Collection
  91.      */
  92.     public static Collection<MailingList> getUserMailingLists( AdminUser user )
  93.     {
  94.         Collection<MailingList> listMailinglists = new ArrayList<>( );

  95.         // Add all global mailing lists
  96.         listMailinglists.addAll( MailingListHome.findByWorkgroup( AdminWorkgroupService.ALL_GROUPS ) );

  97.         // Add mailing list of the user's workgroups
  98.         ReferenceList listWorkgroups = AdminWorkgroupHome.getUserWorkgroups( user );

  99.         for ( ReferenceItem workgroup : listWorkgroups )
  100.         {
  101.             listMailinglists.addAll( MailingListHome.findByWorkgroup( workgroup.getCode( ) ) );
  102.         }

  103.         return listMailinglists;
  104.     }

  105.     /**
  106.      * Gets the user mailing lists by filter.
  107.      *
  108.      * @param user
  109.      *            the user
  110.      * @param filter
  111.      *            the filter
  112.      * @return the user mailing lists by filter
  113.      */
  114.     public static List<MailingList> getUserMailingListsByFilter( AdminUser user, MailingListFilter filter )
  115.     {
  116.         MailingListFilter mailingListFilter = new MailingListFilter( filter );
  117.         // First get mailinglist by workgroup 'all'
  118.         mailingListFilter.setWorkgroup( AdminWorkgroupService.ALL_GROUPS );

  119.         List<MailingList> listMailingLists = MailingListHome.findsByFilter( mailingListFilter );

  120.         // Add mailing list of the user's workgroups
  121.         ReferenceList listWorkgroups = AdminWorkgroupHome.getUserWorkgroups( user );

  122.         for ( ReferenceItem workgroup : listWorkgroups )
  123.         {
  124.             mailingListFilter = new MailingListFilter( filter );
  125.             mailingListFilter.setWorkgroup( workgroup.getCode( ) );
  126.             listMailingLists.addAll( MailingListHome.findsByFilter( mailingListFilter ) );
  127.         }

  128.         return listMailingLists;
  129.     }

  130.     /**
  131.      * Returns all the recipient of a given mailing list
  132.      *
  133.      * @param nIdMailingList
  134.      *            The mailing list Id
  135.      * @return The list
  136.      */
  137.     public static Collection<Recipient> getRecipients( int nIdMailingList )
  138.     {
  139.         Collection<Recipient> listRecipients = new ArrayList<>( );
  140.         MailingList mailinglist = MailingListHome.findByPrimaryKey( nIdMailingList );

  141.         if ( mailinglist != null )
  142.         {
  143.             for ( MailingListUsersFilter filter : mailinglist.getFilters( ) )
  144.             {
  145.                 listRecipients.addAll( getRecipients( filter.getWorkgroup( ), filter.getRole( ) ) );
  146.             }
  147.         }

  148.         return listRecipients;
  149.     }

  150.     /**
  151.      * Gets all recipients corresponding to a filter based on a Workgroup and a role
  152.      *
  153.      * @param strWorkgroup
  154.      *            The workgroup
  155.      * @param strRole
  156.      *            The role
  157.      * @return A collection of recipient
  158.      */
  159.     public static Collection<Recipient> getRecipients( String strWorkgroup, String strRole )
  160.     {
  161.         Collection<Recipient> listRecipients = new ArrayList<>( );
  162.         Collection<AdminUser> listUsers;

  163.         if ( ( strWorkgroup != null ) && ( !strWorkgroup.equals( AdminWorkgroupService.ALL_GROUPS ) ) )
  164.         {
  165.             listUsers = AdminWorkgroupHome.getUserListForWorkgroup( strWorkgroup );
  166.         }
  167.         else
  168.         {
  169.             listUsers = AdminUserHome.findUserList( );
  170.         }

  171.         for ( AdminUser user : listUsers )
  172.         {
  173.             if ( strRole != null && !strRole.equals( ALL_ROLES ) && !user.isInRole( strRole ) )
  174.             {
  175.                 // skip this user if it isn't in the role
  176.                 continue;
  177.             }

  178.             Recipient recipient = new Recipient( );
  179.             recipient.setName( user.getFirstName( ) + " " + user.getLastName( ) );
  180.             recipient.setEmail( user.getEmail( ) );
  181.             listRecipients.add( recipient );
  182.         }

  183.         return listRecipients;
  184.     }

  185.     /**
  186.      * Check if the filter already exists or not in a mailing list
  187.      *
  188.      * @param filter
  189.      *            the filter
  190.      * @param nIdMailingList
  191.      *            the id mailing list
  192.      * @return true if it already exists, false otherwise
  193.      */
  194.     public static boolean checkFilter( MailingListUsersFilter filter, int nIdMailingList )
  195.     {
  196.         return MailingListHome.checkFilter( filter, nIdMailingList );
  197.     }
  198. }