View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.plugins.campagnebp.service;
35  
36  import java.util.ArrayList;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import javax.servlet.http.HttpServletRequest;
42  
43  import org.apache.commons.lang.StringUtils;
44  
45  import fr.paris.lutece.portal.service.security.LuteceUser;
46  
47  /**
48   * 
49   * MyInfosListenerService
50   *
51   */
52  public class MyInfosListenerService
53  {
54  
55      private static List<IMyInfosListener> _lisListeners = new ArrayList<IMyInfosListener>( );
56      private static boolean _bHasListeners;
57      private static Map<String, List<IMyInfosListener>> _mapListeners = new HashMap<String, List<IMyInfosListener>>( );
58      /**
59       * 
60       */
61      public static final String Document_PROPERTY_RESOURCE_TYPE = "document";
62  
63      /**
64       * Register a comment listener.
65       * 
66       * @param listener
67       *            The listener to register
68       */
69      public static synchronized void registerListener( IMyInfosListener listener )
70      {
71          _lisListeners.add( listener );
72          _bHasListeners = true;
73      }
74  
75      /**
76       * Register a comment listener.
77       * 
78       * @param strExtendableResourceType
79       *            The extendable resource type associated with the listener.
80       * @param listener
81       *            The listener to register
82       */
83      public static synchronized void registerListener( String strExtendableResourceType, IMyInfosListener listener )
84      {
85          List<IMyInfosListener> listListeners = _mapListeners.get( strExtendableResourceType );
86          if ( listListeners == null )
87          {
88              listListeners = new ArrayList<IMyInfosListener>( );
89              _mapListeners.put( strExtendableResourceType, listListeners );
90          }
91          listListeners.add( listener );
92          _bHasListeners = true;
93      }
94  
95      /**
96       * Check if there is listeners to notify
97       * 
98       * @return True if there is at last one listener, false otherwise
99       */
100     public static boolean hasListener( )
101     {
102         return _bHasListeners;
103     }
104 
105     /**
106      * Notify to listeners new follow. Only listeners associated with the extendable resource type of the comment are notified.
107      * 
108      * @param strLuteceUserName
109      *            Data
110      * @param strNickName
111      *            Data
112      */
113     public static void updateNickName( String strLuteceUserName, String strNickName )
114     {
115 
116         for ( IMyInfosListener listener : _lisListeners )
117         {
118             listener.updateNickName( strLuteceUserName, strNickName );
119         }
120 
121     }
122 
123     /**
124      * Notify to listeners new follow. Only listeners associated with the extendable resource type of the comment are notified.
125      * 
126      * @param strLuteceUserName
127      *            Data
128      * @param strNickName
129      *            Data
130      */
131     public static void createNickName( String strLuteceUserName, String strNickName )
132     {
133 
134         for ( IMyInfosListener listener : _lisListeners )
135         {
136             listener.createNickName( strLuteceUserName, strNickName );
137         }
138 
139     }
140 
141     /**
142      * Can change arrondissement by user.
143      * 
144      * @param user
145      *            The user
146      * 
147      * @return result
148      */
149     public static int canChangeArrond( LuteceUser user )
150     {
151         int bRes = 0;
152 
153         for ( IMyInfosListener listener : _mapListeners.get( Document_PROPERTY_RESOURCE_TYPE ) )
154         {
155             bRes = listener.canChangeArrond( user );
156             // bres=0 -->default value
157             if ( bRes != 0 )
158             {
159                 return bRes;
160             }
161         }
162         return bRes;
163     }
164 
165     /**
166      * Do delete votes after changing arrondissement
167      * 
168      * @param request
169      *            the HttpServletRequest
170      * @return result
171      */
172     public static String deleteVotes( HttpServletRequest request )
173     {
174         String strRes = "";
175         for ( IMyInfosListener listener : _mapListeners.get( Document_PROPERTY_RESOURCE_TYPE ) )
176         {
177             strRes = listener.deleteVotes( request );
178             if ( !StringUtils.isEmpty( strRes ) )
179             {
180 
181                 return strRes;
182             }
183         }
184         return strRes;
185     }
186 
187 }