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.participatoryideation.service;
35  
36  import java.util.ArrayList;
37  import java.util.Arrays;
38  import java.util.Collection;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Locale;
42  import java.util.Map;
43  
44  import fr.paris.lutece.plugins.participatoryideation.business.proposal.Proposal;
45  import fr.paris.lutece.plugins.participatoryideation.business.submitter.SubmitterType;
46  import fr.paris.lutece.plugins.participatoryideation.business.submitter.SubmitterTypeHome;
47  import fr.paris.lutece.plugins.participatoryideation.service.campaign.IdeationCampaignDataProvider;
48  import fr.paris.lutece.portal.service.cache.AbstractCacheableService;
49  import fr.paris.lutece.portal.service.i18n.I18nService;
50  import fr.paris.lutece.portal.service.spring.SpringContextService;
51  import fr.paris.lutece.portal.service.util.AppLogService;
52  import fr.paris.lutece.portal.service.workflow.WorkflowService;
53  import fr.paris.lutece.util.ReferenceItem;
54  import fr.paris.lutece.util.ReferenceList;
55  
56  public class IdeationStaticService extends AbstractCacheableService implements IIdeationStaticService
57  {
58  
59      private static IIdeationStaticService _singleton;
60      private static final String BEAN_IDEATION_STATIC_SERVICE = "participatoryideation.ideationStaticService";
61  
62      private static final String SERVICE_NAME = "Ideation Static Cache";
63  
64      private static final String MARK_GLOBAL_STATIC = "global_static";
65      private static final String MARK_CAMPAIGN_STATIC = "campaign_static";
66      private static final String MARK_LIST_CAMPAIGN = "list_campaign";
67      private static final String MARK_LIST_STATUS_STATIC = "status_static_list";
68  
69      private static final String MARK_CAMPAIGN = "campaign";
70  
71      private static final String MARK_AREA_LIST = "area_list";
72  
73      private static final String MARK_THEME_LABEL_LIST = "theme_label_list";
74      private static final String MARK_THEME_FRONT_RGB_LIST = "theme_front_rgb_list";
75  
76      private static final String MARK_QPVQVA_LIST = "qpvqva_list";
77      private static final String MARK_QPVQVA_MAP = "qpvqva_map";
78  
79      private static final String MARK_LOCATION_TYPE_LIST = "type_location_list";
80      private static final String MARK_LOCATION_TYPE_MAP = "type_location_map";
81  
82      private static final String MARK_FIELD4_CODES_LIST = "field4_list";
83      private static final String MARK_FIELD4_CODES_MAP = "field4_map";
84  
85      private static final String MARK_SUBMITTERS_TYPES_LIST = "submitter_types_list";
86      private static final String MARK_SUBMITTERS_TYPES_MAP = "submitters_types_map";
87      private static final String MARK_SUBMITTERS_TYPES_LIST_VALUES_MAP = "submitters_types_list_values_map";
88  
89      private static final String MARK_FIELD_MAP = "type_field_map";
90  
91      public static final String CACHE_KEY = "[ideationStatic]";
92  
93      // *********************************************************************************************
94      // * SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON *
95      // * SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON SINGLETON *
96      // *********************************************************************************************
97  
98      public static IIdeationStaticService getInstance( )
99      {
100         if ( _singleton == null )
101         {
102             _singleton = SpringContextService.getBean( BEAN_IDEATION_STATIC_SERVICE );
103         }
104         return _singleton;
105     }
106 
107     public IdeationStaticService( )
108     {
109         initCache( );
110     }
111 
112     public String getName( )
113     {
114         return SERVICE_NAME;
115     }
116 
117     // *********************************************************************************************
118     // * BY_CAMPAIGN BY_CAMPAIGN BY_CAMPAIGN BY_CAMPAIGN BY_CAMPAIGN BY_CAMPAIGN BY_CAMPAIGN BY_CA *
119     // * BY_CAMPAIGN BY_CAMPAIGN BY_CAMPAIGN BY_CAMPAIGN BY_CAMPAIGN BY_CAMPAIGN BY_CAMPAIGN BY_CA *
120     // *********************************************************************************************
121 
122     /**
123      * Returns static content for a specific campaign.
124      */
125     public void fillCampaignStaticContent( Map<String, Object> model, String strCampaignCode )
126     {
127         // Add global static data
128         model.put( MARK_QPVQVA_LIST, ProposalService.getInstance( ).getQpvQvaCodesList( ) );
129         model.put( MARK_QPVQVA_MAP, ProposalService.getInstance( ).getQpvQvaCodesMap( ) );
130         
131         model.put( MARK_LOCATION_TYPE_LIST, ProposalService.getInstance( ).getTypeLocationList( ) );
132         model.put( MARK_LOCATION_TYPE_MAP, ProposalService.getInstance( ).getTypeLocationMap( ) );
133 
134         model.put( MARK_FIELD4_CODES_LIST, ProposalService.getInstance( ).getField4CodesList( ) );
135         model.put( MARK_FIELD4_CODES_MAP, ProposalService.getInstance( ).getField4CodesMap( ) );
136 
137         // Add list of campaigns
138         model.put( MARK_LIST_CAMPAIGN, IdeationCampaignDataProvider.getInstance( ).getCampaigns( ) );
139 
140         // Add list of proposal status
141         if ( WorkflowService.getInstance( ).isAvailable( ) )
142         {
143             List<Proposal.Status> enumList = Arrays.asList( Proposal.Status.values( ) );
144             ReferenceList WorkflowStatesReferenceList = new ReferenceList( );
145             for ( Proposal.Status status : enumList )
146             {
147                 if ( status.isPublished( ) )
148                 {
149                     WorkflowStatesReferenceList.addItem( status.getValeur( ),
150                             I18nService.getLocalizedString( status.getLibelle( ), new Locale( "fr", "FR" ) ) );
151                 }
152             }
153             model.put( MARK_LIST_STATUS_STATIC, WorkflowStatesReferenceList );
154         }
155 
156         // Add static data of the specified campaign
157         @SuppressWarnings( "unchecked" )
158         Map<String, Object> cached = (Map<String, Object>) getFromCache( CACHE_KEY );
159         if ( cached == null )
160         {
161             cached = putAllStaticContentInCache( );
162         }
163         model.put( MARK_CAMPAIGN_STATIC, cached.get( strCampaignCode ) );
164     }
165 
166     // *********************************************************************************************
167     // * BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_AL *
168     // * BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_ALL BY_AL *
169     // *********************************************************************************************
170 
171     /**
172      * Returns static content for all campaigns
173      */
174     public void fillAllStaticContent( Map<String, Object> model )
175     {
176         // Add global static data
177         model.put( MARK_QPVQVA_LIST, ProposalService.getInstance( ).getQpvQvaCodesList( ) );
178         model.put( MARK_QPVQVA_MAP, ProposalService.getInstance( ).getQpvQvaCodesMap( ) );
179 
180         model.put( MARK_LOCATION_TYPE_LIST, ProposalService.getInstance( ).getTypeLocationList( ) );
181         model.put( MARK_LOCATION_TYPE_MAP, ProposalService.getInstance( ).getTypeLocationMap( ) );
182         
183         model.put( MARK_FIELD4_CODES_LIST, ProposalService.getInstance( ).getField4CodesList( ) );
184         model.put( MARK_FIELD4_CODES_MAP, ProposalService.getInstance( ).getField4CodesMap( ) );
185 
186         // Add static data of all campaigns
187         @SuppressWarnings( "unchecked" )
188         Map<String, Object> cached = (Map<String, Object>) getFromCache( CACHE_KEY );
189         if ( cached == null )
190         {
191             cached = putAllStaticContentInCache( );
192         }
193         model.put( MARK_GLOBAL_STATIC, cached );
194 
195     }
196 
197     /**
198      * Returns a map with data of all campaigns :
199      * 
200      * - content.Key = campaign code
201      * 
202      * - content.Value = a map with data of the campaign : - campaignContent.key = data name (campaign, themes, areas, submitter types, submitter types values)
203      * - campaignContent.value = data values
204      */
205     private Map<String, Object> putAllStaticContentInCache( )
206     {
207         Map<String, Object> content = new HashMap<String, Object>( );
208 
209         // For each campaign, add data about submitters
210         Map<String, List<SubmitterType>> mapSubmittersTypes = SubmitterTypeHome.getSubmitterTypesMapByCampaign( );
211         ReferenceList listCampaign = IdeationCampaignDataProvider.getInstance( ).getCampaigns( );
212         for ( ReferenceItem campaign : listCampaign )
213         {
214             Map<String, Object> campaignContent = new HashMap<String, Object>( );
215 
216             // Data about campaign
217             campaignContent.put( MARK_CAMPAIGN, campaign );
218 
219             // Add themes of the campaign
220             campaignContent.put( MARK_THEME_LABEL_LIST, IdeationCampaignDataProvider.getInstance( ).getCampaignThemes( campaign.getCode( ) ) );
221             campaignContent.put( MARK_THEME_FRONT_RGB_LIST, IdeationCampaignDataProvider.getInstance( ).getCampaignThemesFrontRgb( campaign.getCode( ) ) );
222 
223             // Add areas of the campaign
224             campaignContent.put( MARK_AREA_LIST, IdeationCampaignDataProvider.getInstance( ).getCampaignAllAreaLabels( campaign.getCode( ) ) );
225 
226             // Types of submitter of the campaign
227             Collection<SubmitterType> submitterTypes = mapSubmittersTypes.get( campaign.getCode( ) );
228             if ( submitterTypes == null )
229             {
230                 AppLogService.error( "No submitter type found for campaign '" + campaign.getCode( ) + "'." );
231                 submitterTypes = new ArrayList<>( );
232             }
233 
234             campaignContent.put( MARK_SUBMITTERS_TYPES_LIST, submitterTypes );
235             Map<String, SubmitterType> mapSubmittersTypesByCode = new HashMap<String, SubmitterType>( );
236             for ( SubmitterType submitterType : submitterTypes )
237             {
238                 mapSubmittersTypesByCode.put( submitterType.getCode( ), submitterType );
239             }
240             campaignContent.put( MARK_SUBMITTERS_TYPES_MAP, mapSubmittersTypesByCode );
241 
242             // Values of list-typed submitters of the campaign
243             Map<String, String> mapSubmittersTypesListValuesByCode = new HashMap<String, String>( );
244             for ( SubmitterType submitterType : submitterTypes )
245             {
246                 if ( SubmitterType.CODE_COMPLEMENT_TYPE_LIST.equals( submitterType.getCodeComplementType( ) ) )
247                 {
248                     for ( ReferenceItem referenceItem : submitterType.getValues( ) )
249                     {
250                         mapSubmittersTypesListValuesByCode.put( submitterType.getCode( ) + "-" + referenceItem.getCode( ), referenceItem.getName( ) );
251                     }
252                 }
253             }
254             campaignContent.put( MARK_SUBMITTERS_TYPES_LIST_VALUES_MAP, mapSubmittersTypesListValuesByCode );
255 
256             // Optional fields
257             campaignContent.put( MARK_FIELD_MAP, IdeationCampaignDataProvider.getInstance( ).getCampaignFieldsData( campaign.getCode( ) ) );
258 
259             content.put( campaign.getCode( ), campaignContent );
260         }
261 
262         putInCache( CACHE_KEY, content );
263 
264         return content;
265     }
266 
267 }