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.business.portlet;
35  
36  import java.text.SimpleDateFormat;
37  import java.util.ArrayList;
38  import java.util.Calendar;
39  import java.util.Date;
40  import java.util.GregorianCalendar;
41  import java.util.List;
42  import java.util.Locale;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  import org.apache.commons.lang.StringUtils;
47  
48  import fr.paris.lutece.plugins.calendar.business.Event;
49  import fr.paris.lutece.plugins.calendar.service.AgendaResource;
50  import fr.paris.lutece.plugins.calendar.service.EventImageResourceService;
51  import fr.paris.lutece.plugins.calendar.service.Utils;
52  import fr.paris.lutece.plugins.calendar.service.XMLUtils;
53  import fr.paris.lutece.plugins.calendar.service.search.CalendarSearchService;
54  import fr.paris.lutece.plugins.calendar.web.Constants;
55  import fr.paris.lutece.portal.business.portlet.Portlet;
56  import fr.paris.lutece.portal.service.plugin.Plugin;
57  import fr.paris.lutece.portal.service.plugin.PluginService;
58  import fr.paris.lutece.portal.service.security.SecurityService;
59  import fr.paris.lutece.util.date.DateUtil;
60  import fr.paris.lutece.util.xml.XmlUtil;
61  
62  
63  /**
64   * This class represents the business object CalendarPortlet.
65   */
66  public class CalendarPortlet extends Portlet
67  {
68      // The names of the XML tags
69      private static final String TAG_CALENDAR_FILTERED_LIST = "calendar-filtered-list";
70      private static final String TAG_EVENTS = "events";
71      private static final String TAG_AGENDA_ID = "agenda-id";
72      private static final String TAG_EVENT_ID = "event-id";
73      private static final String TAG_AGENDA_NAME = "agenda-name";
74      private static final String TAG_AGENDA_EVENT = "event";
75      private static final String TAG_EVENT_DATE_LOCAL = "date-local";
76      private static final String TAG_AGENDA_EVENT_TITLE = "event-title";
77      private static final String TAG_AGENDA_EVENT_DATE = "event-date";
78      private static final String TAG_AGENDA_EVENT_TOP_EVENT = "event-top_event";
79      private static final String TAG_EVENT_IMAGE = "event-image";
80      private static final String TAG_EVENT_DESCRIPTION = "event-description";
81      private static final String TAG_EVENT_MONTH = "event-month";
82  
83      /**
84       * Sets the name of the plugin associated with this portlet.
85       * 
86       * @param strPluginName The plugin name.
87       */
88      public void setPluginName( String strPluginName )
89      {
90          super.setPluginName( strPluginName );
91      }
92  
93      /**
94       * Returns the Xml code of the Archive portlet with XML heading
95       * 
96       * @param request The HTTP servlet request
97       * @return the Xml code of the Archive portlet
98       */
99      public String getXmlDocument( HttpServletRequest request )
100     {
101         return XmlUtil.getXmlHeader( ) + getXml( request );
102     }
103 
104     /**
105      * Returns the Xml code of the Calendar portlet without XML heading
106      * 
107      * @param request The HTTP servlet request
108      * @return the Xml code of the Archive portlet content
109      */
110     public String getXml( HttpServletRequest request )
111     {
112         StringBuffer strXml = new StringBuffer( );
113         Locale local;
114         Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
115 
116         if ( request != null )
117         {
118             local = request.getLocale( );
119         }
120         else
121         {
122             local = Locale.getDefault( );
123         }
124 
125         XmlUtil.beginElement( strXml, TAG_CALENDAR_FILTERED_LIST );
126 
127         // fetch all the calendars related to the specified portlet
128         List<AgendaResource> listAgendasInPortlet = CalendarPortletHome.findAgendasInPortlet( this.getId( ) );
129         List<AgendaResource> listAuthorizedAgenda = new ArrayList<AgendaResource>( );
130 
131         // Filter to find whether user is identified and authorized to view the agenda on the front office
132         for ( AgendaResource agendaResource : listAgendasInPortlet )
133         {
134             if ( agendaResource != null )
135             {
136                 // Check security access
137                 String strRole = agendaResource.getRole( );
138 
139                 if ( StringUtils.isNotBlank( strRole ) && ( request != null )
140                         && ( !Constants.PROPERTY_ROLE_NONE.equals( strRole ) ) )
141                 {
142                     if ( SecurityService.isAuthenticationEnable( ) )
143                     {
144                         if ( SecurityService.getInstance( ).isUserInRole( request, strRole ) )
145                         {
146                             listAuthorizedAgenda.add( agendaResource );
147                         }
148                     }
149                 }
150                 else
151                 {
152                     listAuthorizedAgenda.add( agendaResource );
153                 }
154             }
155         }
156 
157         //Add the events of the authorized agendas
158         for ( AgendaResource agendaResource : listAuthorizedAgenda )
159         {
160             // Generate the XML code for the agendas :
161             XmlUtil.beginElement( strXml, TAG_EVENTS );
162 
163             // ;
164             String strAgendaId = agendaResource.getAgenda( ).getKeyName( );
165             String strAgendaDesc = agendaResource.getAgenda( ).getName( );
166 
167             // Retrieve the indexed events
168             Date dateBegin = CalendarPortletHome.getBeginDate( this.getId( ) );
169             Date dateEnd = CalendarPortletHome.getEndDate( this.getId( ) );
170             String[] arrayAgendaIds = { strAgendaId };
171             List<Event> listIndexedEvents = CalendarSearchService.getInstance( ).getSearchResults( arrayAgendaIds,
172                     null, StringUtils.EMPTY, dateBegin, dateEnd, plugin );
173 
174             for ( Event event : listIndexedEvents )
175             {
176                 if ( ( event.getTitle( ) != null ) && !event.getTitle( ).equals( "" ) )
177                 {
178                     XmlUtil.beginElement( strXml, TAG_AGENDA_EVENT );
179                     XmlUtil.addElement( strXml, TAG_AGENDA_ID, strAgendaId );
180                     XmlUtil.addElement( strXml, TAG_EVENT_ID, event.getId( ) );
181                     XmlUtil.addElement( strXml, TAG_AGENDA_NAME, strAgendaDesc );
182                     XmlUtil.addElement( strXml, TAG_AGENDA_EVENT_TITLE, event.getTitle( ) );
183                     XmlUtil.addElement( strXml, TAG_AGENDA_EVENT_TOP_EVENT, event.getTopEvent( ) );
184                     XmlUtil.addElement( strXml, TAG_AGENDA_EVENT_DATE, DateUtil.getDateString( event.getDate( ), local ) );
185                     XmlUtil.addElement( strXml, TAG_EVENT_MONTH,
186                             new SimpleDateFormat( "MMMM" ).format( event.getDate( ) ) );
187                     XmlUtil.addElement( strXml, TAG_EVENT_IMAGE, ( EventImageResourceService.getInstance( )
188                             .getResourceImageEvent( event.getId( ) ) ).replaceAll( "&", "&amp;" ) );
189                     XmlUtil.addElementHtml( strXml, TAG_EVENT_DESCRIPTION, event.getDescription( ) );
190 
191                     Calendar calendar = new GregorianCalendar( );
192                     calendar.setTime( event.getDate( ) );
193 
194                     //String strFormat = AppPropertiesService.getProperty( Constants.PROPERTY_LABEL_FORMAT_DATE_OF_DAY );
195 
196                     //DateFormat formatDate = new SimpleDateFormat( strFormat,
197                     //        ( request == null ) ? Locale.getDefault(  ) : request.getLocale(  ) );	                    
198                     String strLocalizedDate = DateUtil.getDateString( event.getDate( ), local );
199 
200                     if ( !Utils.getDate( event.getDate( ) ).equals( Utils.getDate( event.getDateEnd( ) ) ) )
201                     {
202                         strLocalizedDate += ( " - " + DateUtil.getDateString( event.getDateEnd( ), local ) );
203                     }
204 
205                     XmlUtil.addElement( strXml, TAG_EVENT_DATE_LOCAL, strLocalizedDate );
206                     XmlUtil.endElement( strXml, TAG_AGENDA_EVENT );
207                 }
208             }
209 
210             XmlUtil.endElement( strXml, TAG_EVENTS );
211         }
212 
213         XmlUtil.endElement( strXml, TAG_CALENDAR_FILTERED_LIST );
214 
215         //Load the xml calendar	    	
216         strXml.append( XMLUtils.getXMLPortletCalendar( local, new GregorianCalendar( ), request ) );
217 
218         String str = addPortletTags( strXml );
219 
220         return str;
221     }
222 
223     /**
224      * Updates the current instance of the CalendarPortlet object
225      */
226     public void update( )
227     {
228         CalendarPortletHome.getInstance( ).update( this );
229     }
230 
231     /**
232      * Removes the current instance of the CalendarPortlet object
233      */
234     public void remove( )
235     {
236         CalendarPortletHome.getInstance( ).remove( this );
237     }
238 
239     @Override
240     public boolean canBeCachedForAnonymousUsers( )
241     {
242         return false;
243     }
244 
245     @Override
246     public boolean canBeCachedForConnectedUsers( )
247     {
248         return false;
249     }
250 }