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.plugins.dila.web;
35  
36  import fr.paris.lutece.plugins.dila.business.enums.AudienceCategoryEnum;
37  import fr.paris.lutece.plugins.dila.service.DilaPlugin;
38  import fr.paris.lutece.plugins.dila.service.IDilaCacheService;
39  import fr.paris.lutece.plugins.dila.utils.CacheKeyUtils;
40  import fr.paris.lutece.plugins.dila.utils.constants.DilaConstants;
41  import fr.paris.lutece.portal.service.message.SiteMessageException;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.portal.service.security.UserNotSignedException;
44  import fr.paris.lutece.portal.service.spring.SpringContextService;
45  import fr.paris.lutece.portal.service.template.AppTemplateService;
46  import fr.paris.lutece.portal.service.util.AppPropertiesService;
47  import fr.paris.lutece.portal.web.xpages.XPage;
48  import fr.paris.lutece.portal.web.xpages.XPageApplication;
49  import fr.paris.lutece.util.html.HtmlTemplate;
50  
51  import java.util.HashMap;
52  
53  import javax.servlet.http.HttpServletRequest;
54  
55  import org.apache.commons.lang.StringUtils;
56  import org.apache.commons.lang.WordUtils;
57  
58  
59  /**
60   * Dila rendering page
61   */
62  public class DilaApp implements XPageApplication
63  {
64      // Templates
65      private static final String TEMPLATE_XPAGE_DILA = "skin/plugins/dila/page_dila.html";
66  
67      // Markers
68      private static final String MARKER_CATEGORY = "categorie";
69      private static final String MARKER_INSEE = "insee";
70      private static final String MARKER_DATA = "dila_data";
71      private static final String MARK_NO_DATA = "noData";
72      private static final String MARK_URL_INDIVIDUALS = "urlParticuliers";
73      private static final String MARK_URL_PROFESSIONALS = "urlPME";
74      private static final String MARK_URL_ASSOCIATIONS = "urlAssociations";
75      private static final String PARAMETER_XML_FILE = "xmlFile";
76      private IDilaCacheService _dilaCacheService = SpringContextService.getBean( "dilaCacheService" );
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public XPage getPage( HttpServletRequest request, int nMode, Plugin plugin ) throws UserNotSignedException,
83              SiteMessageException
84      {
85          HashMap<String, Object> model = new HashMap<String, Object>( );
86          String xmlName = request.getParameter( PARAMETER_XML_FILE );
87          String strCategory = request.getParameter( MARKER_CATEGORY );
88          strCategory = WordUtils.capitalize( strCategory );
89  
90          String dilaData = "";
91  
92          if ( StringUtils.isNotBlank( strCategory ) && ( AudienceCategoryEnum.fromLabel( strCategory ) != null ) )
93          {
94              AudienceCategoryEnum enumCategory = AudienceCategoryEnum.fromLabel( strCategory );
95  
96              if ( StringUtils.isBlank( xmlName ) )
97              {
98                  String prefix = null;
99                  switch ( enumCategory )
100                 {
101                 case ASSOCIATIONS:
102                     prefix = DilaConstants.ASSOCIATION_PREFIX;
103                     break;
104                 case PROFESSIONNALS:
105                     prefix = DilaConstants.PROFESSIONAL_PREFIX;
106                     break;
107                 case INDIVIDUALS:
108                 default:
109                     prefix = DilaConstants.INDIVIDUAL_PREFIX;
110                     break;
111                 }
112                 xmlName = AppPropertiesService.getProperty( prefix + DilaConstants.PROPERTY_HOME_CARD );
113             }
114 
115             String cacheKey = CacheKeyUtils.generateCacheKey( enumCategory.getId( ), xmlName );
116             dilaData = _dilaCacheService.getRessource( cacheKey, request.getLocale( ) );
117             model.put( MARK_NO_DATA, false );
118         }
119         else
120         {
121             model.put( MARK_NO_DATA, true );
122         }
123 
124         model.put( MARK_URL_ASSOCIATIONS, DilaConstants.XPAGE_ASSO );
125         model.put( MARK_URL_INDIVIDUALS, DilaConstants.XPAGE_PARTICULIERS );
126         model.put( MARK_URL_PROFESSIONALS, DilaConstants.XPAGE_PME );
127         model.put( MARKER_CATEGORY, strCategory );
128         model.put( MARKER_DATA, dilaData );
129         model.put( MARKER_INSEE, AppPropertiesService.getProperty( DilaConstants.PROPERTY_INSEE ) );
130 
131         XPage page = new XPage( );
132         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_XPAGE_DILA, request.getLocale( ), model );
133         page.setContent( template.getHtml( ) );
134         page.setTitle( DilaPlugin.PLUGIN_NAME );
135         page.setPathLabel( DilaPlugin.PLUGIN_NAME );
136 
137         return page;
138     }
139 }