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.calendar.web;
35  
36  import fr.paris.lutece.plugins.calendar.business.MultiAgendaEvent;
37  import fr.paris.lutece.plugins.calendar.service.AgendaResource;
38  import fr.paris.lutece.plugins.calendar.service.CalendarService;
39  import fr.paris.lutece.portal.service.spring.SpringContextService;
40  import fr.paris.lutece.portal.service.util.AppPropertiesService;
41  
42  import java.util.Map;
43  
44  import org.apache.commons.lang.StringUtils;
45  
46  
47  /**
48   * An Utility class to manipulate html code.
49   */
50  public final class HtmlUtils
51  {
52      private static final String HTML_START = "<";
53      private static final String HTML_BR = "<br />";
54      private static final String LINEFEED = "\\n";
55  
56      /**
57       * Default constructor
58       */
59      private HtmlUtils( )
60      {
61      }
62  
63      /**
64       * Remove the part of the string that contains HTML
65       * @param strSource The source string
66       * @return the cleaned string
67       */
68      public static String removeHtml( String strSource )
69      {
70          String strReturn = strSource;
71          int nPos = strSource.indexOf( HTML_START );
72  
73          if ( nPos >= 0 )
74          {
75              strReturn = strSource.substring( 0, nPos );
76          }
77  
78          return strReturn;
79      }
80  
81      /**
82       * Convert CR into HTML <br />
83       * @param strSource The string to convert
84       * @return The converted string
85       */
86      public static String convertCR( String strSource )
87      {
88          String strReturn = StringUtils.EMPTY;
89  
90          if ( StringUtils.isNotBlank( strSource ) )
91          {
92              strReturn = strSource.replaceAll( LINEFEED, HTML_BR );
93          }
94  
95          return strReturn;
96      }
97  
98      /**
99       * Fill a template with events info
100      * @param model The model used to fill events
101      * @param event The event
102      * @param strDate The current date
103      */
104     public static void fillEventTemplate( Map<String, Object> model, MultiAgendaEvent event, String strDate )
105     {
106         CalendarService calendarService = SpringContextService.getBean( Constants.BEAN_CALENDAR_CALENDARSERVICE );
107         int nShortTitleLength = AppPropertiesService.getPropertyInt( Constants.PROPERTY_EVENT_SHORT_TITLE_LENGTH, 18 );
108         StringBuilder sbTitle = new StringBuilder( );
109         String strImage = StringUtils.EMPTY;
110 
111         AgendaResource agenda = calendarService.getAgendaResource( event.getAgenda( ) );
112         if ( agenda != null )
113         {
114             sbTitle.append( agenda.getEventPrefix( ) );
115             sbTitle.append( Constants.SPACE );
116             strImage = agenda.getEventImage( );
117         }
118 
119         if ( StringUtils.isNotBlank( event.getDateTimeStart( ) ) )
120         {
121             sbTitle.append( event.getDateTimeStart( ) );
122         }
123         sbTitle.append( Constants.INDENT );
124         if ( StringUtils.isNotBlank( event.getDateTimeEnd( ) ) )
125         {
126             sbTitle.append( event.getDateTimeEnd( ) );
127         }
128         sbTitle.append( Constants.SPACE );
129 
130         sbTitle.append( removeHtml( event.getTitle( ) ) );
131 
132         String strShortTitle = sbTitle.toString( );
133 
134         if ( strShortTitle.length( ) > nShortTitleLength )
135         {
136             StringBuilder sbShortTitle = new StringBuilder( );
137             sbShortTitle.append( strShortTitle.substring( 0, nShortTitleLength ) );
138             sbShortTitle.append( AppPropertiesService.getProperty( Constants.PROPERTY_EVENT_SHORT_TITLE_END ) );
139             strShortTitle = sbShortTitle.toString( );
140         }
141 
142         String strDescription = ( event.getDescription( ) != null ) ? event.getDescription( ) : StringUtils.EMPTY;
143         String strLocation = ( event.getLocation( ) != null ) ? event.getLocation( ) : StringUtils.EMPTY;
144         String strUrl = ( event.getUrl( ) != null ) ? event.getUrl( ) : StringUtils.EMPTY;
145 
146         model.put( Constants.MARK_AGENDA, event.getAgenda( ) );
147         model.put( Constants.MARK_EVENT_TITLE, sbTitle.toString( ) );
148         model.put( Constants.MARK_EVENT_SHORT_TITLE, strShortTitle );
149         model.put( Constants.MARK_EVENT_DESCRIPTION, strDescription );
150         model.put( Constants.MARK_EVENT_LOCATION, strLocation );
151         model.put( Constants.MARK_EVENT_URL, strUrl );
152         model.put( Constants.MARK_EVENT_IMAGE, strImage );
153         model.put( Constants.MARK_DATE, strDate );
154     }
155 }