View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.plugins.unittree.modules.notification.service;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.lang.StringUtils;
43  import org.apache.commons.validator.routines.EmailValidator;
44  
45  import fr.paris.lutece.plugins.unittree.business.unit.IUnitAttribute;
46  import fr.paris.lutece.plugins.unittree.business.unit.Unit;
47  import fr.paris.lutece.plugins.unittree.modules.notification.business.Notification;
48  import fr.paris.lutece.plugins.unittree.modules.notification.business.NotificationHome;
49  import fr.paris.lutece.plugins.unittree.modules.notification.business.NotificationUnitAttribute;
50  import fr.paris.lutece.plugins.unittree.service.UnitErrorException;
51  import fr.paris.lutece.plugins.unittree.service.unit.IUnitService;
52  import fr.paris.lutece.plugins.unittree.service.unit.IUnitUserService;
53  import fr.paris.lutece.portal.business.user.AdminUser;
54  import fr.paris.lutece.portal.service.spring.SpringContextService;
55  
56  /**
57   *
58   */
59  public class NotificationService implements INotificationService
60  {
61      public static final String BEAN_NAME = "unittree-notification.notificationService";
62  
63      // Markers
64      public static final String MARK_UNIT_NOTIF = "unit_notif";
65      public static final String MARK_UNIT_EMAIL = "unit_email";
66  
67      // Constant
68      private static final String NOTIF_NO = "no";
69      private static final String NOTIF_EMAIL = "email";
70      private static final String NOTIF_LIST = "list";
71      private static final String NOTIF_BOTH = "both";
72  
73      // Message
74      private static final String MESSAGE_ERROR_EMAIL_MANDATORY = "module.unittree.notification.message.error.email.mandatory";
75      private static final String MESSAGE_ERROR_EMAIL_FORMAT = "module.unittree.notification.message.error.email.format";
76  
77      // BEAN
78      private static final String BEAN_UNIT_USER_SERVICE = "unittree.unitUserService";
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public void doCreateUnit( Unit unit, HttpServletRequest request )
85      {
86          // the unit in paramater HAS BEEN populate by the request before this call
87          // but the id of the unit is not set in the UnitAttribute because the unit wasn't created at the populate call.
88          if ( unit.getAttribute( NotificationUnitAttribute.ATTRIBUTE_NAME ) != null )
89          {
90              Notification notif = (Notification) unit.getAttribute( NotificationUnitAttribute.ATTRIBUTE_NAME ).getAttribute( );
91              notif.setIdUnit( unit.getIdUnit( ) );
92              NotificationHome.mergeConfiguration( notif );
93          }
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      @Override
100     public void doModifyUnit( Unit unit, HttpServletRequest request )
101     {
102         // the unit in parameter HAS BEEN populate by the request before this call
103         if ( unit.getAttribute( NotificationUnitAttribute.ATTRIBUTE_NAME ) != null )
104         {
105             Notification notif = (Notification) unit.getAttribute( NotificationUnitAttribute.ATTRIBUTE_NAME ).getAttribute( );
106             NotificationHome.mergeConfiguration( notif );
107         }
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public void doRemoveUnit( int nIdUnit, HttpServletRequest request )
115     {
116         NotificationHome.removeForUnit( nIdUnit );
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public void populate( Unit unit )
124     {
125         if ( unit == null )
126         {
127             return;
128         }
129 
130         NotificationUnitAttribute notifUnitAtt = new NotificationUnitAttribute( );
131         Notification notificationUnit = NotificationHome.loadByUnit( unit );
132         if ( notificationUnit == null )
133         {
134             notificationUnit = new Notification( );
135             notificationUnit.setIdUnit( unit.getIdUnit( ) );
136         }
137         notifUnitAtt.setAttribute( notificationUnit );
138 
139         unit.addAttribute( notifUnitAtt );
140     }
141 
142     /**
143      * {@inheritDoc}
144      */
145     @Override
146     public void populate( Unit unit, HttpServletRequest request ) throws UnitErrorException
147     {
148         // use to control request and populate unit before calling dao
149         String strUnitEmail = request.getParameter( MARK_UNIT_EMAIL );
150         String strUnitNotif = request.getParameter( MARK_UNIT_NOTIF );
151 
152         if ( StringUtils.isEmpty( strUnitEmail ) && ( NOTIF_BOTH.equals( strUnitNotif ) || NOTIF_EMAIL.equals( strUnitNotif ) ) )
153         {
154             throw new UnitErrorException( MESSAGE_ERROR_EMAIL_MANDATORY );
155         }
156         if ( StringUtils.isNotEmpty( strUnitEmail ) )
157         {
158             EmailValidator validator = EmailValidator.getInstance( );
159             if ( !validator.isValid( strUnitEmail ) )
160             {
161                 throw new UnitErrorException( MESSAGE_ERROR_EMAIL_FORMAT );
162             }
163         }
164 
165         Notification notif = new Notification( );
166         notif.setIdUnit( unit.getIdUnit( ) );
167         notif.setEmail( strUnitEmail );
168         notif.setHasNotif( !NOTIF_NO.equals( strUnitNotif ) );
169         notif.setUseEmail( NOTIF_EMAIL.equals( strUnitNotif ) || NOTIF_BOTH.equals( strUnitNotif ) );
170         notif.setUseList( NOTIF_LIST.equals( strUnitNotif ) || NOTIF_BOTH.equals( strUnitNotif ) );
171 
172         NotificationUnitAttribute notifAttribute = new NotificationUnitAttribute( );
173         notifAttribute.setAttribute( notif );
174         unit.addAttribute( notifAttribute );
175     }
176 
177     /**
178      * {@inheritDoc}
179      */
180     @Override
181     public boolean canCreateSubUnit( int nIdUnit )
182     {
183         return true;
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     @Override
190     public void moveSubTree( Unit unitToMove, Unit newUnitParent )
191     {
192         // NOTHING TO DO, email is linked to the unit no correlation with parent
193     }
194 
195     /**
196      * {@inheritDoc}
197      */
198     @Override
199     public List<String> getUnitUsersEmail( int nUnitId )
200     {
201         IUnitService unitService = SpringContextService.getBean( IUnitService.BEAN_UNIT_SERVICE );
202 
203         Unit unit = unitService.getUnit( nUnitId, true );
204 
205         List<String> listMail = new ArrayList<String>( );
206         IUnitAttribute<Notification> notifUnitAtt = unit.getAttribute( NotificationUnitAttribute.ATTRIBUTE_NAME );
207 
208         if ( notifUnitAtt != null )
209         {
210             Notification notification = notifUnitAtt.getAttribute( );
211 
212             if ( notification != null && notification.getHasNotif( ) )
213             {
214                 if ( notification.getUseEmail( ) )
215                 {
216                     listMail.add( notification.getEmail( ) );
217                 }
218 
219                 if ( notification.getUseList( ) )
220                 {
221                     IUnitUserService _unitUserService = SpringContextService.getBean( BEAN_UNIT_USER_SERVICE );
222                     List<AdminUser> listUsers = _unitUserService.getUsers( notification.getIdUnit( ), new HashMap<String, Unit>( ), false );
223 
224                     for ( AdminUser adminUser : listUsers )
225                     {
226                         listMail.add( adminUser.getEmail( ) );
227                     }
228                 }
229             }
230         }
231 
232         return listMail;
233     }
234 }