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.business;
35  
36  import fr.paris.lutece.plugins.digglike.utils.DiggUtils;
37  import fr.paris.lutece.portal.business.regularexpression.RegularExpression;
38  import fr.paris.lutece.portal.service.i18n.I18nService;
39  import fr.paris.lutece.portal.service.message.AdminMessage;
40  import fr.paris.lutece.portal.service.message.AdminMessageService;
41  import fr.paris.lutece.portal.service.plugin.Plugin;
42  import fr.paris.lutece.portal.service.regularexpression.RegularExpressionService;
43  import fr.paris.lutece.util.ReferenceList;
44  import fr.paris.lutece.util.html.Paginator;
45  
46  import java.util.List;
47  import java.util.Locale;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  
52  /**
53   *
54   * class EntryTypeText
55   *
56   */
57  public class EntryTypeText extends Entry
58  {
59      private final String _template_create = "admin/plugins/digglike/create_entry_type_text.html";
60      private final String _template_modify = "admin/plugins/digglike/modify_entry_type_text.html";
61      private final String _template_html_code_form = "admin/plugins/digglike/html_code_form_entry_type_text.html";
62      private final String _template_html_code_response = "admin/plugins/digglike/html_code_response_entry_type_text.html";
63  
64      /**
65       * Get the the template of  HtmlCode  of   the entry
66       * @return the template HtmlCode  of   the entry
67       *
68       * */
69      public String getTemplateHtmlCodeForm(  )
70      {
71          return _template_html_code_form;
72      }
73  
74      /**
75       * Get the request data
76       * @param request HttpRequest
77       * @param locale the locale
78       * @return null if all data requiered are in the request else the url of jsp error
79       */
80      public String getRequestData( HttpServletRequest request, Locale locale )
81      {
82          String strTitle = request.getParameter( PARAMETER_TITLE );
83          String strHelpMessage = request.getParameter( PARAMETER_HELP_MESSAGE );
84          String strComment = request.getParameter( PARAMETER_COMMENT );
85          String strValue = request.getParameter( PARAMETER_VALUE );
86          String strMandatory = request.getParameter( PARAMETER_MANDATORY );
87          String strWidth = request.getParameter( PARAMETER_WIDTH );
88          String strMaxSizeEnter = request.getParameter( PARAMETER_MAX_SIZE_ENTER );
89          String strShowInDiggSubmitList = request.getParameter( PARAMETER_SHOW_IN_DIGG_SUBMIT_LIST );
90  
91          int nWidth = -1;
92          int nMaxSizeEnter = -1;
93  
94          String strFieldError = EMPTY_STRING;
95  
96          if ( ( strTitle == null ) || strTitle.trim(  ).equals( EMPTY_STRING ) )
97          {
98              strFieldError = FIELD_TITLE;
99          }
100 
101         else if ( ( strWidth == null ) || strWidth.trim(  ).equals( EMPTY_STRING ) )
102         {
103             strFieldError = FIELD_WIDTH;
104         }
105 
106         if ( !strFieldError.equals( EMPTY_STRING ) )
107         {
108             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, locale ) };
109 
110             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
111                 AdminMessage.TYPE_STOP );
112         }
113 
114         try
115         {
116             nWidth = Integer.parseInt( strWidth );
117         }
118         catch ( NumberFormatException ne )
119         {
120             strFieldError = FIELD_WIDTH;
121         }
122 
123         try
124         {
125             if ( ( strMaxSizeEnter != null ) && !strMaxSizeEnter.trim(  ).equals( EMPTY_STRING ) )
126             {
127                 nMaxSizeEnter = Integer.parseInt( strMaxSizeEnter );
128             }
129         }
130         catch ( NumberFormatException ne )
131         {
132             strFieldError = FIELD_MAX_SIZE_ENTER;
133         }
134 
135         if ( !strFieldError.equals( EMPTY_STRING ) )
136         {
137             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, locale ) };
138 
139             return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields,
140                 AdminMessage.TYPE_STOP );
141         }
142 
143         this.setTitle( strTitle );
144         this.setHelpMessage( DiggUtils.trim( strHelpMessage ) );
145         this.setComment( strComment );
146 
147         this.setDefaultValue( strValue );
148         this.setWidth( nWidth );
149         this.setMaxSizeEnter( nMaxSizeEnter );
150 
151         if ( strMandatory != null )
152         {
153             this.setMandatory( true );
154         }
155         else
156         {
157             this.setMandatory( false );
158         }
159 
160         if ( strShowInDiggSubmitList != null )
161         {
162             this.setShowInDiggSubmitList( true );
163         }
164         else
165         {
166             this.setShowInDiggSubmitList( false );
167         }
168 
169         return null;
170     }
171 
172     /**
173      * Get the request data
174      * @param request HttpRequest
175      * @param locale the locale
176      * @param plugin the plugin
177      * @return null if all data requiered are in the request else the url of jsp error
178      */
179     public String getRequestData( HttpServletRequest request, Locale locale, Plugin plugin )
180     {
181         return getRequestData( request, locale );
182     }
183 
184     /**
185      * Get template create url of the entry
186      * @return template create url of the entry
187      */
188     public String getTemplateCreate(  )
189     {
190         return _template_create;
191     }
192 
193     /**
194      * Get the template modify url  of the entry
195      * @return template modify url  of the entry
196      */
197     public String getTemplateModify(  )
198     {
199         return _template_modify;
200     }
201 
202     /**
203      * The paginator who is use in the template modify of the entry
204      * @param nItemPerPage Number of items to display per page
205      * @param strBaseUrl The base Url for build links on each page link
206      * @param strPageIndexParameterName The parameter name for the page index
207      * @param strPageIndex The current page index
208      * @return the paginator who is use in the template modify of the entry
209      */
210     public Paginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName,
211         String strPageIndex )
212     {
213         return new Paginator( this.getRegularExpressionList(  ), nItemPerPage, strBaseUrl, strPageIndexParameterName,
214             strPageIndex );
215     }
216 
217     /**
218      * return the list of regular expression whose not associate to the entry
219      * @param entry the entry
220      * @param plugin the plugin
221      * @return the list of regular expression whose not associate to the entry
222      */
223     public ReferenceList getReferenceListRegularExpression( IEntry entry, Plugin plugin )
224     {
225         ReferenceList refListRegularExpression = new ReferenceList(  );
226 
227         if ( RegularExpressionService.getInstance(  ).isAvailable(  ) )
228         {
229             List<RegularExpression> listRegularExpression = RegularExpressionService.getInstance(  )
230                                                                                     .getAllRegularExpression(  );
231 
232             for ( RegularExpression regularExpression : listRegularExpression )
233             {
234                 if ( !entry.getRegularExpressionList(  ).contains( regularExpression ) )
235                 {
236                     refListRegularExpression.addItem( regularExpression.getIdExpression(  ),
237                         regularExpression.getTitle(  ) );
238                 }
239             }
240         }
241 
242         return refListRegularExpression;
243     }
244 
245     /**
246      * save in the list of response the response associate to the entry in the form submit
247      * @param request HttpRequest
248      * @param listResponse the list of response associate to the entry in the form submit
249      * @param locale the locale
250      * @return a Form error object if there is an error in the response
251      */
252     public FormError getResponseData( HttpServletRequest request, List<Response> listResponse, Locale locale )
253     {
254         String strValueEntry = request.getParameter( DiggUtils.EMPTY_STRING + this.getIdEntry(  ) ).trim(  );
255         List<RegularExpression> listRegularExpression = this.getRegularExpressionList(  );
256         Response response = new Response(  );
257         response.setEntry( this );
258 
259         if ( strValueEntry != null )
260         {
261             if ( this.isMandatory(  ) )
262             {
263                 if ( strValueEntry.equals( DiggUtils.EMPTY_STRING ) )
264                 {
265                     FormError formError = new FormError(  );
266                     formError.setMandatoryError( true );
267                     formError.setTitleQuestion( this.getTitle(  ) );
268 
269                     return formError;
270                 }
271             }
272 
273             if ( ( listRegularExpression != null ) && ( listRegularExpression.size(  ) != 0 ) &&
274                     RegularExpressionService.getInstance(  ).isAvailable(  ) )
275             {
276                 for ( RegularExpression regularExpression : listRegularExpression )
277                 {
278                     if ( !RegularExpressionService.getInstance(  ).isMatches( strValueEntry, regularExpression ) )
279                     {
280                         FormError formError = new FormError(  );
281                         formError.setMandatoryError( false );
282                         formError.setTitleQuestion( this.getTitle(  ) );
283                         formError.setErrorMessage( regularExpression.getErrorMessage(  ) );
284 
285                         return formError;
286                     }
287                 }
288             }
289 
290             response.setValueResponse( strValueEntry );
291         }
292 
293         listResponse.add( response );
294 
295         return null;
296     }
297 
298     /**
299      * save in the list of response the response associate to the entry in the form submit
300      * @param nIdDiggSubmit The if of the DiggSubmit
301      * @param request HttpRequest
302      * @param listResponse the list of response associate to the entry in the form submit
303      * @param locale the locale
304      * @param plugin the plugin
305      * @return a Form error object if there is an error in the response
306      */
307     public FormError getResponseData( int nIdDiggSubmit, HttpServletRequest request, List<Response> listResponse,
308         Locale locale, Plugin plugin )
309     {
310         return getResponseData( request, listResponse, locale );
311     }
312 
313     /**
314      * Get the template of the html code of the response value  associate to the entry
315      * @param response the response associate to the entry
316      * @return the template of the html code of the response value  associate to the entry
317      */
318     public String getTemplateHtmlCodeResponse(  )
319     {
320         return _template_html_code_response;
321     }
322 }