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.digglike.web;
35  
36  import java.util.HashMap;
37  import java.util.Locale;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import org.apache.commons.lang.StringUtils;
42  
43  import fr.paris.lutece.plugins.digglike.business.DefaultMessage;
44  import fr.paris.lutece.plugins.digglike.business.DefaultMessageHome;
45  import fr.paris.lutece.plugins.digglike.service.DefaultMessageResourceIdService;
46  import fr.paris.lutece.plugins.digglike.utils.DiggUtils;
47  import fr.paris.lutece.portal.business.rbac.RBAC;
48  import fr.paris.lutece.portal.service.admin.AdminUserService;
49  import fr.paris.lutece.portal.service.i18n.I18nService;
50  import fr.paris.lutece.portal.service.message.AdminMessage;
51  import fr.paris.lutece.portal.service.message.AdminMessageService;
52  import fr.paris.lutece.portal.service.rbac.RBACService;
53  import fr.paris.lutece.portal.service.template.AppTemplateService;
54  import fr.paris.lutece.portal.service.util.AppPathService;
55  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
56  import fr.paris.lutece.util.html.HtmlTemplate;
57  
58  
59  /**
60   *
61   *  class DefaultMessageJspBean
62   *
63   */
64  public class DefaultMessageJspBean extends PluginAdminPageJspBean
65  {
66      
67  	
68  
69  	/**
70  	 * 
71  	 */
72  	private static final long serialVersionUID = -8248033294625285115L;
73  
74  	//	templates
75      private static final String TEMPLATE_MANAGE_DEFAULT_MESSAGE = "admin/plugins/digglike/manage_default_message.html";
76  
77      //	Markers
78      private static final String MARK_WEBAPP_URL = "webapp_url";
79      private static final String MARK_LOCALE = "locale";
80      private static final String MARK_DEFAULT_MESSAGE = "default_message";
81  
82      //	parameters form
83      private static final String PARAMETER_UNAVAILABILITY_MESSAGE = "unavailability_message";
84      private static final String PARAMETER_LIBELLE_VALIDATE_BUTTON = "libelle_validate_button";
85      private static final String PARAMETER_LIBELLE_CONTRIBUTION = "libelle_contribution";
86      private static final String PARAMETER_NUMBER_DIGG_SUBMIT_IN_TOP_SCORE = "number_digg_submit_in_top_score";
87      private static final String PARAMETER_NUMBER_DIGG_SUBMIT_IN_TOP_COMMENT = "number_digg_submit_in_top_comment";
88      private static final String PARAMETER_NUMBER_DIGG_SUBMIT_CARACTERS_SHOWN = "number_digg_submit_caracters_shown";
89      private static final String PARAMETER_NOTIFICATION_NEW_COMMENT_TITLE="notification_new_comment_title";
90      private static final String PARAMETER_NOTIFICATION_NEW_COMMENT_BODY="notification_new_comment_body";
91      private static final String PARAMETER_NOTIFICATION_NEW_DIGG_SUBMIT_TITLE="notification_new_digg_submit_title";
92      private static final String PARAMETER_NOTIFICATION_NEW_DIGG_SUBMIT_BODY="notification_new_digg_submit_body";
93  
94  
95      //	 other constants
96      private static final String EMPTY_STRING = "";
97  
98      //	message
99      private static final String MESSAGE_MANDATORY_FIELD = "digglike.message.mandatory.field";
100     private static final String FIELD_UNAVAILABILITY_MESSAGE = "digglike.manageDefaultMessage.labelUnavailabilityMessage";
101     private static final String FIELD_LIBELLE_VALIDATE_BUTTON = "digglike.manageDefaultMessage.labelLibelleValidateButton";
102     private static final String FIELD_LIBELLE_CONTRIBUTION = "digglike.manageDefaultMessage.labelLibelleContribution";
103     private static final String FIELD_NUMBER_DIGG_SUBMIT_IN_TOP_SCORE = "digglike.createDigg.labelNumberDiggSumitInTopScore";
104     private static final String FIELD_NUMBER_DIGG_SUBMIT_IN_TOP_COMMENT = "digglike.createDigg.labelNumberDiggSumitInTopComment";
105     private static final String FIELD_NUMBER_DIGG_SUBMIT_CARACTERS_SHOWN = "digglike.createDigg.labelNumberDiggSubmitCaractersShown";
106     
107     private static final String FIELD_NOTIFICATION_NEW_COMMENT_TITLE="digglike.manageDefaultMessage.labelNotificationNewCommentTitle";
108     private static final String FIELD_NOTIFICATION_NEW_COMMENT_BODY="digglike.manageDefaultMessage.labelNotificationNewCommentBody";
109     private static final String FIELD_NOTIFICATION_NEW_DIGG_DUBMIT_TITLE="digglike.manageDefaultMessage.labelNotificationNewDiggSubmitTitle";
110     private static final String FIELD_NOTIFICATION_NEW_DIGG_DUBMIT_BODY="digglike.manageDefaultMessage.labelNotificationNewDiggSubmitBody";
111   
112     
113     private static final String MESSAGE_ILLOGICAL_NUMBER_DIGG_SUBMIT_IN_TOP_SCORE = "digglike.message.illogicalNumberDiggSumitInTopScore";
114     private static final String MESSAGE_ILLOGICAL_NUMBER_DIGG_SUBMIT_IN_TOP_COMMENT = "digglike.message.illogicalNumberDiggSumitInTopComment";
115     private static final String MESSAGE_ILLOGICAL_NUMBER_DIGG_SUBMIT_CARACTERS_SHOWN = "digglike.message.illogicalNumberDiggSubmitCaractersShown";
116     	
117     //properties
118     private static final String PROPERTY_MANAGE_DEFAULT_MESSAGE_TITLE = "form.manageDefaultMessage.title";
119 
120     /**
121      * gets the manage default message page
122      * @param request the Http request
123      * @return the manage default message page
124      */
125     public String getManageDefaultMessage( HttpServletRequest request )
126     {
127         Locale locale = getLocale(  );
128         HashMap model = new HashMap(  );
129         DefaultMessage defaultMessage = DefaultMessageHome.find( getPlugin(  ) );
130         model.put( MARK_DEFAULT_MESSAGE, defaultMessage );
131         model.put( MARK_WEBAPP_URL, AppPathService.getBaseUrl( request ) );
132         model.put( MARK_LOCALE, AdminUserService.getLocale( request ).getLanguage(  ) );
133         setPageTitleProperty( PROPERTY_MANAGE_DEFAULT_MESSAGE_TITLE );
134 
135         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_MANAGE_DEFAULT_MESSAGE, locale, model );
136 
137         return getAdminPage( templateList.getHtml(  ) );
138     }
139 
140     /**
141      * perform the default message modification
142      * @param request the Http request
143      * @return  The URL to go after performing the action
144      */
145     public String doModifyDefaultMessage( HttpServletRequest request )
146     {
147         if ( RBACService.isAuthorized( DefaultMessage.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID,
148                     DefaultMessageResourceIdService.PERMISSION_MANAGE, getUser(  ) ) )
149         {
150             DefaultMessage defaultMessage = new DefaultMessage(  );
151             String strError = getDefaultMessageData( request, defaultMessage );
152 
153             if ( strError != null )
154             {
155                 return strError;
156             }
157 
158             DefaultMessageHome.update( defaultMessage, getPlugin(  ) );
159         }
160 
161         return getHomeUrl( request );
162     }
163 
164     /**
165      * Get the request data and if there is no error insert the data in the default Message object specified in parameter.
166      * return null if there is no error or else return the error page url
167      * @param request the request
168      * @param defaultMessage the default message
169      * @return null if there is no error or else return the error page url
170      */
171     private String getDefaultMessageData( HttpServletRequest request, DefaultMessage defaultMessage )
172     {
173         String strLibelleContribution = request.getParameter( PARAMETER_LIBELLE_CONTRIBUTION );
174         String strUnavailabilityMessage = request.getParameter( PARAMETER_UNAVAILABILITY_MESSAGE );
175         String strLibelleValidateButton = request.getParameter( PARAMETER_LIBELLE_VALIDATE_BUTTON );
176         String strNumberDiggSubmitInTopScore = request.getParameter( PARAMETER_NUMBER_DIGG_SUBMIT_IN_TOP_SCORE );
177         String strNumberDiggSubmitInTopComment = request.getParameter( PARAMETER_NUMBER_DIGG_SUBMIT_IN_TOP_COMMENT );
178         String strNumberDiggSubmitCaractersShown = request.getParameter( PARAMETER_NUMBER_DIGG_SUBMIT_CARACTERS_SHOWN );
179         String strNotificationNewCommentTitle=request.getParameter(PARAMETER_NOTIFICATION_NEW_COMMENT_TITLE);
180         String strNotificationNewCommentBody=request.getParameter(PARAMETER_NOTIFICATION_NEW_COMMENT_BODY);
181         String strNotificationNewDiggSubmitTitle=request.getParameter(PARAMETER_NOTIFICATION_NEW_DIGG_SUBMIT_TITLE);
182         String strNotificationNewDiggSubmitBody=request.getParameter(PARAMETER_NOTIFICATION_NEW_DIGG_SUBMIT_BODY);
183       
184         int nNumberDiggSubmitInTopScore = DiggUtils.getIntegerParameter( strNumberDiggSubmitInTopScore );
185         int nNumberDiggSubmitInTopComment = DiggUtils.getIntegerParameter( strNumberDiggSubmitInTopComment );
186         int nNumberDiggSubmitCaractersShown = DiggUtils.getIntegerParameter( strNumberDiggSubmitCaractersShown );
187 
188         String strFieldError = EMPTY_STRING;
189 
190         if ( StringUtils.isEmpty( strLibelleContribution )  )
191         {
192             strFieldError = FIELD_LIBELLE_CONTRIBUTION;
193         }
194         else if ( StringUtils.isEmpty(strUnavailabilityMessage) )
195         {
196             strFieldError = FIELD_UNAVAILABILITY_MESSAGE;
197         }
198 
199         else if ( StringUtils.isEmpty(strLibelleValidateButton) )
200         {
201             strFieldError = FIELD_LIBELLE_VALIDATE_BUTTON;
202         }
203         else if ( StringUtils.isEmpty(strNumberDiggSubmitInTopScore ) )
204         {
205             strFieldError = FIELD_NUMBER_DIGG_SUBMIT_IN_TOP_SCORE;
206         }
207 
208         else if ( StringUtils.isEmpty(strNumberDiggSubmitInTopComment ) )
209         {
210             strFieldError = FIELD_NUMBER_DIGG_SUBMIT_IN_TOP_COMMENT;
211         }
212         else if (StringUtils.isEmpty(strNumberDiggSubmitCaractersShown) )
213         {
214             strFieldError = FIELD_NUMBER_DIGG_SUBMIT_CARACTERS_SHOWN;
215         }
216       
217         else if(StringUtils.isEmpty(strNotificationNewCommentTitle))
218         {
219         	strFieldError = FIELD_NOTIFICATION_NEW_COMMENT_TITLE;
220         	
221         }
222         else if(StringUtils.isEmpty(strNotificationNewCommentBody))
223         {
224         	strFieldError = FIELD_NOTIFICATION_NEW_COMMENT_BODY;
225         	
226         }
227    
228         else if(StringUtils.isEmpty(strNotificationNewDiggSubmitTitle))
229         {
230         	
231         	strFieldError = FIELD_NOTIFICATION_NEW_DIGG_DUBMIT_TITLE;
232         }
233         else if(StringUtils.isEmpty(strNotificationNewDiggSubmitBody))
234         {
235         	
236         	strFieldError = FIELD_NOTIFICATION_NEW_DIGG_DUBMIT_BODY;
237         }
238 
239         if ( !strFieldError.equals( EMPTY_STRING ) )
240         {
241             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, getLocale(  ) ) };
242 
243             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
244                 AdminMessage.TYPE_STOP );
245         }
246 
247         if ( nNumberDiggSubmitInTopScore < 0 )
248         {
249             return AdminMessageService.getMessageUrl( request, MESSAGE_ILLOGICAL_NUMBER_DIGG_SUBMIT_IN_TOP_SCORE,
250                 AdminMessage.TYPE_STOP );
251         }
252 
253         if ( nNumberDiggSubmitInTopComment < 0 )
254         {
255             return AdminMessageService.getMessageUrl( request, MESSAGE_ILLOGICAL_NUMBER_DIGG_SUBMIT_IN_TOP_COMMENT,
256                 AdminMessage.TYPE_STOP );
257         }
258 
259         if ( nNumberDiggSubmitCaractersShown < 0 )
260         {
261             return AdminMessageService.getMessageUrl( request, MESSAGE_ILLOGICAL_NUMBER_DIGG_SUBMIT_CARACTERS_SHOWN,
262                 AdminMessage.TYPE_STOP );
263         }
264 
265         defaultMessage.setLibelleContribution( strLibelleContribution );
266         defaultMessage.setUnavailabilityMessage( strUnavailabilityMessage );
267         defaultMessage.setLibelleValidateButton( strLibelleValidateButton );
268         defaultMessage.setNumberDiggSubmitInTopScore( nNumberDiggSubmitInTopScore );
269         defaultMessage.setNumberDiggSubmitInTopComment( nNumberDiggSubmitInTopComment );
270         defaultMessage.setNumberDiggSubmitCaractersShown( nNumberDiggSubmitCaractersShown );
271         
272         defaultMessage.setNotificationNewCommentTitle(strNotificationNewCommentTitle);
273         defaultMessage.setNotificationNewCommentBody(strNotificationNewCommentBody);
274         defaultMessage.setNotificationNewDiggSubmitTitle(strNotificationNewDiggSubmitTitle);
275         defaultMessage.setNotificationNewDiggSubmitBody(strNotificationNewDiggSubmitBody);
276 
277         return null;
278     }
279 }