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.participatorybudget.service;
35  
36  import java.util.Collection;
37  import java.util.HashMap;
38  import java.util.Map;
39  
40  import fr.paris.lutece.plugins.participatorybudget.business.campaign.Campaign;
41  import fr.paris.lutece.plugins.participatorybudget.service.campaign.CampaignService;
42  import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
43  import fr.paris.lutece.portal.service.spring.SpringContextService;
44  
45  public class BudgetStaticService extends AbstractCacheableService
46  {
47  
48      private static BudgetStaticService _singleton;
49  
50      private static final String BEAN_BUDGET_STATIC_SERVICE = "participatorybudget.budgetStaticService";
51  
52      private static final String SERVICE_NAME = "Budget Static Cache";
53  
54      private static final String MARK_GLOBAL_STATIC = "global_static";
55      private static final String MARK_CAMPAIGN_STATIC = "campaign_static";
56  
57      private static final String MARK_CAMPAIGN_LIST = "campaign_list";
58      private static final String MARK_CAMPAIGN = "campaign";
59  
60      private static final String MARK_THEME_LABEL_LIST = "theme_label_list";
61      private static final String MARK_THEME_FRONT_RGB_LIST = "theme_front_rgb_list";
62  
63      public static final String CACHE_KEY = "[budgetStatic]";
64  
65      // *********************************************************************************************
66      // * SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON *
67      // * SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON *
68      // *********************************************************************************************
69  
70      public static BudgetStaticService getInstance( )
71      {
72          if ( _singleton == null )
73          {
74              _singleton = SpringContextService.getBean( BEAN_BUDGET_STATIC_SERVICE );
75          }
76          return _singleton;
77      }
78  
79      public BudgetStaticService( )
80      {
81          initCache( );
82      }
83  
84      public String getName( )
85      {
86          return SERVICE_NAME;
87      }
88  
89      // *********************************************************************************************
90      // * PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLI *
91      // * PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLIC PUBLI *
92      // *********************************************************************************************
93  
94      /**
95       * Fill model with static content for a specific campaign.
96       */
97      public void fillCampaignStaticContent( Map<String, Object> model, String strCampaignCode )
98      {
99          // Add list of campaigns
100         model.put( MARK_CAMPAIGN_LIST, CampaignService.getInstance( ).getCampaigns( ) );
101 
102         // Add static data of the specified campaign
103         @SuppressWarnings( "unchecked" )
104         Map<String, Object> cached = (Map<String, Object>) getFromCache( CACHE_KEY );
105         if ( cached == null )
106         {
107             cached = putAllStaticContentInCache( );
108         }
109         model.put( MARK_CAMPAIGN_STATIC, cached.get( strCampaignCode ) );
110     }
111 
112     /**
113      * Fill model with static content for all campaigns
114      */
115     public void fillAllCampaignsStaticContent( Map<String, Object> model )
116     {
117         // Add list of campaigns
118         model.put( MARK_CAMPAIGN_LIST, CampaignService.getInstance( ).getCampaigns( ) );
119 
120         // Add static data of all campaigns
121         @SuppressWarnings( "unchecked" )
122         Map<String, Object> cached = (Map<String, Object>) getFromCache( CACHE_KEY );
123         if ( cached == null )
124         {
125             cached = putAllStaticContentInCache( );
126         }
127         model.put( MARK_GLOBAL_STATIC, cached );
128 
129     }
130 
131     // *********************************************************************************************
132     // * PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE P *
133     // * PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE PRIVATE P *
134     // *********************************************************************************************
135 
136     /**
137      * Returns a map with data of all campaigns :
138      * 
139      * - content.Key = campaign code - content.Value = a map with data of the campaign : - campaignContent.key = data name (campaign, themes, areas, submitter
140      * types, submitter types values) - campaignContent.value = data values
141      */
142     private Map<String, Object> putAllStaticContentInCache( )
143     {
144         Map<String, Object> content = new HashMap<String, Object>( );
145 
146         Collection<Campaign> campaigns = CampaignService.getInstance( ).getCampaigns( );
147         for ( Campaign campaign : campaigns )
148         {
149             Map<String, Object> campaignContent = new HashMap<String, Object>( );
150 
151             // Campaign
152             campaignContent.put( MARK_CAMPAIGN, campaign );
153 
154             // Add themes of the campaign
155             campaignContent.put( MARK_THEME_LABEL_LIST, CampaignService.getInstance( ).getThemes( campaign.getCode( ) ) );
156             campaignContent.put( MARK_THEME_FRONT_RGB_LIST, CampaignService.getInstance( ).getThemesFrontRgb( campaign.getCode( ) ) );
157 
158             content.put( campaign.getCode( ), campaignContent );
159         }
160 
161         putInCache( CACHE_KEY, content );
162 
163         return content;
164     }
165 
166 }