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.resource;
35  
36  import fr.paris.lutece.portal.service.spring.SpringContextService;
37  
38  import java.util.HashMap;
39  import java.util.Map;
40  import java.util.Map.Entry;
41  
42  
43  /**
44   * Class to notify listeners of actions performed on resources. Listeners keep
45   * at least track of the number of actions performed over a given resource.
46   */
47  public final class ExtendableResourceActionHit
48  {
49      /**
50       * Download action
51       */
52      public static final String ACTION_DOWNLOAD = "download";
53  
54      /**
55       * Creation action
56       */
57      public static final String ACTION_CREATION = "creation";
58  
59      /**
60       * Update action
61       */
62      public static final String ACTION_UPDATE = "update";
63  
64      /**
65       * Archive action
66       */
67      public static final String ACTION_ARCHIVE = "archive";
68  
69      /**
70       * Delete action
71       */
72      public static final String ACTION_DELETE = "delete";
73      private static volatile ExtendableResourceActionHit _instance;
74  
75      /**
76       * Private constructor
77       */
78      private ExtendableResourceActionHit(  )
79      {
80      }
81  
82      /**
83       * Get the service instance
84       * @return The instance of the service
85       */
86      public static ExtendableResourceActionHit getInstance(  )
87      {
88          if ( _instance == null )
89          {
90              synchronized ( ExtendableResourceActionHit.class )
91              {
92                  _instance = new ExtendableResourceActionHit(  );
93              }
94          }
95  
96          return _instance;
97      }
98  
99      /**
100      * Get the total number of hit associated to a given action name and
101      * resource type
102      * @param strActionName The name of the action to get the number of hit of
103      * @param strExtendableResourceType The resource type to get the hit of
104      * @return The number of hit, or 0 if this action has no hit for this
105      *         resource type
106      */
107     public int getActionHit( String strActionName, String strExtendableResourceType )
108     {
109         int nResult = 0;
110 
111         for ( IExtendableResourceActionHitListener listener : SpringContextService.getBeansOfType( 
112                 IExtendableResourceActionHitListener.class ) )
113         {
114             nResult += listener.getActionHit( strActionName, strExtendableResourceType );
115         }
116 
117         return nResult;
118     }
119 
120     /**
121      * Get the list of action names associated with a number of hit for a given
122      * resource
123      * @param strExtendableResourceId The id of the resource
124      * @param strExtendableResourceType The type of the resource
125      * @return A map containing associations between action names and hit number
126      */
127     public Map<String, Integer> getResourceHit( String strExtendableResourceId, String strExtendableResourceType )
128     {
129         Map<String, Integer> mapActionHit = new HashMap<String, Integer>(  );
130 
131         for ( IExtendableResourceActionHitListener listener : SpringContextService.getBeansOfType( 
132                 IExtendableResourceActionHitListener.class ) )
133         {
134             for ( Entry<String, Integer> entry : listener.getResourceHit( strExtendableResourceId,
135                     strExtendableResourceType ).entrySet(  ) )
136             {
137                 if ( mapActionHit.containsKey( entry.getKey(  ) ) )
138                 {
139                     mapActionHit.put( entry.getKey(  ), mapActionHit.get( entry.getKey(  ) ) + entry.getValue(  ) );
140                 }
141                 else
142                 {
143                     mapActionHit.put( entry.getKey(  ), entry.getValue(  ) );
144                 }
145             }
146         }
147 
148         return mapActionHit;
149     }
150 
151     /**
152      * Get the number of hit associated with a resource and an action name
153      * @param strExtendableResourceId The id of the resource
154      * @param strExtendableResourceType The type of the resource
155      * @param strActionName The name of the action
156      * @return The number of hit, or 0 if the resource has no hit for this
157      *         action
158      */
159     public int getResourceActionHit( String strExtendableResourceId, String strExtendableResourceType,
160         String strActionName )
161     {
162         int nResult = 0;
163 
164         for ( IExtendableResourceActionHitListener listener : SpringContextService.getBeansOfType( 
165                 IExtendableResourceActionHitListener.class ) )
166         {
167             nResult += listener.getResourceActionHit( strExtendableResourceId, strExtendableResourceType, strActionName );
168         }
169 
170         return nResult;
171     }
172 
173     /**
174      * Notify every listeners that an action has been performed on a resource
175      * @param strExtendableResourceId The id of the resource the action was
176      *            performed on
177      * @param strExtendableResourceType The type of the resource the action was
178      *            performed on
179      * @param strActionName The name of the action that was performed on the
180      *            resource. Action names can be any strings, but
181      *            the class {@link ExtendableResourceActionHit} exposes standard
182      *            action names.
183      */
184     public void notifyActionOnResource( String strExtendableResourceId, String strExtendableResourceType,
185         String strActionName )
186     {
187         for ( IExtendableResourceActionHitListener listener : SpringContextService.getBeansOfType( 
188                 IExtendableResourceActionHitListener.class ) )
189         {
190             listener.notifyActionOnResource( strExtendableResourceId, strExtendableResourceType, strActionName );
191         }
192     }
193 }