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.portlet;
35  
36  import fr.paris.lutece.plugins.calendar.business.portlet.MiniCalendarPortlet;
37  import fr.paris.lutece.plugins.calendar.business.portlet.MiniCalendarPortletHome;
38  import fr.paris.lutece.plugins.calendar.web.Constants;
39  import fr.paris.lutece.portal.business.portlet.PortletHome;
40  import fr.paris.lutece.portal.service.i18n.I18nService;
41  import fr.paris.lutece.portal.service.message.AdminMessage;
42  import fr.paris.lutece.portal.service.message.AdminMessageService;
43  import fr.paris.lutece.portal.service.util.AppPathService;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.portal.web.constants.Messages;
46  import fr.paris.lutece.portal.web.constants.Parameters;
47  import fr.paris.lutece.portal.web.portlet.PortletJspBean;
48  import fr.paris.lutece.util.ReferenceList;
49  import fr.paris.lutece.util.html.HtmlTemplate;
50  
51  import java.util.HashMap;
52  import java.util.StringTokenizer;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  
57  /**
58   * This class provides the user interface to manage calendar interval portlets.
59   */
60  public class MiniCalendarPortletJspBean extends PortletJspBean
61  {
62      // Prefix of the properties related to this checkbox
63      public static final String PROPERTY_TIME_INTERVAL_LIST = "calendar.interval.time";
64  
65      private static final long serialVersionUID = 8594742404929705042L;
66  
67      // Bookmarks
68      private static final String BOOKMARK_PAGE_ID = "@page_id@";
69      private static final String BOOKMARK_PORTLET_ID = "@portlet_id@";
70  
71      // Parameters
72      private static final String PARAMETER_PAGE_ID = "page_id";
73      private static final String PARAMETER_PORTLET_ID = "portlet_id";
74      private static final String PARAMETER_PORTLET_TYPE_ID = "portlet_type_id";
75      private static final String PARAMETER_CBX_TOP_EVENT = "cbx_top_event";
76  
77      // Templates
78      private static final String MARK_TOP_EVENT = "top_event";
79      private static final String MARK_BASE_URL = "base_url";
80  
81      /**
82       * Returns the creation form for the portlet
83       * 
84       * @param request the HTML request
85       * @return the HTML code for the page
86       */
87      public String getCreate( HttpServletRequest request )
88      {
89          String strPageId = request.getParameter( PARAMETER_PAGE_ID );
90          String strPortletTypeId = request.getParameter( PARAMETER_PORTLET_TYPE_ID );
91  
92          HtmlTemplate template = getCreateTemplate( strPageId, strPortletTypeId );
93  
94          return template.getHtml( );
95      }
96  
97      /**
98       * Processes the creation of the portlet
99       * 
100      * @param request the HTML request
101      * @return the URL to redirect to
102      */
103     public String doCreate( HttpServletRequest request )
104     {
105         MiniCalendarPortlet portlet = new MiniCalendarPortlet( );
106 
107         // Standard controls on the creation form
108         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
109         int nIdPage = Integer.parseInt( strIdPage );
110 
111         String strStyleId = request.getParameter( Parameters.STYLE );
112 
113         if ( ( strStyleId == null ) || strStyleId.trim( ).equals( "" ) )
114         {
115             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
116         }
117 
118         setPortletCommonData( request, portlet );
119 
120         // mandatory field
121         String strName = portlet.getName( );
122 
123         if ( strName.trim( ).equals( "" ) )
124         {
125             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
126         }
127 
128         portlet.setPageId( nIdPage );
129 
130         // Creating portlet
131         MiniCalendarPortletHome.getInstance( ).create( portlet );
132 
133         // Returns page with new created portlet
134         return getPageUrl( portlet.getPageId( ) );
135     }
136 
137     /**
138      * Returns the modification form for the portlet
139      * 
140      * @param request the HTML request
141      * @return the HTML code for the page
142      */
143     public String getModify( HttpServletRequest request )
144     {
145         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
146         int nPortletId = Integer.parseInt( strPortletId );
147         MiniCalendarPortlet portlet = (MiniCalendarPortlet) PortletHome.findByPrimaryKey( nPortletId );
148 
149         String strIdPage = request.getParameter( PARAMETER_PAGE_ID );
150         String strBaseUrl = AppPathService.getBaseUrl( request );
151 
152         HashMap<String, Object> model = new HashMap<String, Object>( );
153         model.put( BOOKMARK_PORTLET_ID, strPortletId );
154         model.put( BOOKMARK_PAGE_ID, strIdPage );
155 
156         boolean top_event = MiniCalendarPortletHome.showTopEvent( );
157 
158         model.put( MARK_TOP_EVENT, top_event );
159         model.put( MARK_BASE_URL, strBaseUrl );
160         model.put( Constants.MARK_LOCALE, getLocale( ).getLanguage( ) );
161 
162         // Fill the specific part of the modify form
163         HtmlTemplate template = getModifyTemplate( portlet, model );
164 
165         return template.getHtml( );
166     }
167 
168     /**
169      * Processes the modification of the portlet
170      * 
171      * @param request the HTTP request
172      * @return the URL to redirect to
173      */
174     public String doModify( HttpServletRequest request )
175     {
176         // Use the id in the request to load the portlet
177         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
178         int nPortletId = Integer.parseInt( strPortletId );
179         MiniCalendarPortlet portlet = (MiniCalendarPortlet) PortletHome.findByPrimaryKey( nPortletId );
180 
181         // Standard controls on the creation form
182         String strStyleId = request.getParameter( Parameters.STYLE );
183 
184         if ( ( strStyleId == null ) || strStyleId.trim( ).equals( "" ) )
185         {
186             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
187         }
188 
189         setPortletCommonData( request, portlet );
190 
191         // mandatory field
192         String strName = portlet.getName( );
193 
194         if ( strName.trim( ).equals( "" ) )
195         {
196             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
197         }
198 
199         portlet.update( );
200 
201         String strTopEvent = request.getParameter( PARAMETER_CBX_TOP_EVENT );
202         boolean top_event = Boolean.FALSE;
203 
204         if ( strTopEvent != null )
205         {
206             top_event = Boolean.TRUE;
207         }
208 
209         MiniCalendarPortletHome.updateTopEvent( top_event );
210 
211         return getPageUrl( portlet.getPageId( ) );
212     }
213 
214     /**
215      * Return the list of time intervals declared in properties file
216      * @return A ReferenceList of time interval
217      * @param request The HttpRequest
218      */
219     public static ReferenceList getIntervalList( HttpServletRequest request )
220     {
221         StringTokenizer st = new StringTokenizer( AppPropertiesService.getProperty( PROPERTY_TIME_INTERVAL_LIST ), "," );
222         ReferenceList timeIntervalList = new ReferenceList( );
223 
224         while ( st.hasMoreElements( ) )
225         {
226             String strIntervalName = st.nextToken( ).trim( );
227             String strDescription = I18nService.getLocalizedString( "calendar.interval." + strIntervalName
228                     + ".description", request.getLocale( ) );
229             int nDays = AppPropertiesService.getPropertyInt( "calendar.interval." + strIntervalName + ".value", 7 );
230             timeIntervalList.addItem( nDays, strDescription );
231         }
232 
233         return timeIntervalList;
234     }
235 }