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 EntryTypeUrl extends Entry
58  {
59      private final String _template_create = "admin/plugins/digglike/create_entry_type_url.html";
60      private final String _template_modify = "admin/plugins/digglike/modify_entry_type_url.html";
61      private final String _template_html_code_form = "admin/plugins/digglike/html_code_form_entry_type_url.html";
62      private final String _template_html_code_response = "admin/plugins/digglike/html_code_response_entry_type_url.html";
63  
64      /**
65       * Get the HtmlCode  of   the entry
66       * @return the 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 template create url of the entry
174      * @return template create url of the entry
175      */
176     public String getTemplateCreate(  )
177     {
178         return _template_create;
179     }
180 
181     /**
182      * Get the template modify url  of the entry
183      * @return template modify url  of the entry
184      */
185     public String getTemplateModify(  )
186     {
187         return _template_modify;
188     }
189 
190     /**
191      * The paginator who is use in the template modify of the entry
192      * @param nItemPerPage Number of items to display per page
193      * @param strBaseUrl The base Url for build links on each page link
194      * @param strPageIndexParameterName The parameter name for the page index
195      * @param strPageIndex The current page index
196      * @return the paginator who is use in the template modify of the entry
197      */
198     public Paginator getPaginator( int nItemPerPage, String strBaseUrl, String strPageIndexParameterName,
199         String strPageIndex )
200     {
201         return new Paginator( this.getRegularExpressionList(  ), nItemPerPage, strBaseUrl, strPageIndexParameterName,
202             strPageIndex );
203     }
204 
205     /**
206      * return the list of regular expression whose not associate to the entry
207      * @param entry the entry
208      * @param plugin the plugin
209      * @return the list of regular expression whose not associate to the entry
210      */
211     public ReferenceList getReferenceListRegularExpression( IEntry entry, Plugin plugin )
212     {
213         ReferenceList refListRegularExpression = new ReferenceList(  );
214 
215         if ( RegularExpressionService.getInstance(  ).isAvailable(  ) )
216         {
217             List<RegularExpression> listRegularExpression = RegularExpressionService.getInstance(  )
218                                                                                     .getAllRegularExpression(  );
219 
220             for ( RegularExpression regularExpression : listRegularExpression )
221             {
222                 if ( !entry.getRegularExpressionList(  ).contains( regularExpression ) )
223                 {
224                     refListRegularExpression.addItem( regularExpression.getIdExpression(  ),
225                         regularExpression.getTitle(  ) );
226                 }
227             }
228         }
229 
230         return refListRegularExpression;
231     }
232 
233     /**
234      * save in the list of response the response associate to the entry in the form submit
235      * @param request HttpRequest
236      * @param listResponse the list of response associate to the entry in the form submit
237      * @param locale the locale
238      * @return a Form error object if there is an error in the response
239      */
240     public FormError getResponseData( HttpServletRequest request, List<Response> listResponse, Locale locale )
241     {
242         String strValueEntry = request.getParameter( DiggUtils.EMPTY_STRING + this.getIdEntry(  ) ).trim(  );
243         List<RegularExpression> listRegularExpression = this.getRegularExpressionList(  );
244         Response response = new Response(  );
245         response.setEntry( this );
246 
247         if ( strValueEntry != null )
248         {
249             if ( this.isMandatory(  ) )
250             {
251                 if ( strValueEntry.equals( DiggUtils.EMPTY_STRING ) )
252                 {
253                     FormError formError = new FormError(  );
254                     formError.setMandatoryError( true );
255                     formError.setTitleQuestion( this.getTitle(  ) );
256 
257                     return formError;
258                 }
259             }
260 
261             if ( ( listRegularExpression != null ) && ( listRegularExpression.size(  ) != 0 ) &&
262                     RegularExpressionService.getInstance(  ).isAvailable(  ) )
263             {
264                 for ( RegularExpression regularExpression : listRegularExpression )
265                 {
266                     if ( !RegularExpressionService.getInstance(  ).isMatches( strValueEntry, regularExpression ) )
267                     {
268                         FormError formError = new FormError(  );
269                         formError.setMandatoryError( false );
270                         formError.setTitleQuestion( this.getTitle(  ) );
271                         formError.setErrorMessage( regularExpression.getErrorMessage(  ) );
272 
273                         return formError;
274                     }
275                 }
276             }
277 
278             response.setValueResponse( strValueEntry );
279         }
280 
281         listResponse.add( response );
282 
283         return null;
284     }
285 
286     /**
287      * save in the list of response the response associate to the entry in the form submit
288      * @param nIdDiggSubmit The if of the DiggSubmit
289      * @param request HttpRequest
290      * @param listResponse the list of response associate to the entry in the form submit
291      * @param locale the locale
292      * @param plugin the plugin
293      * @return a Form error object if there is an error in the response
294      */
295     public FormError getResponseData( int nIdDiggSubmit, HttpServletRequest request, List<Response> listResponse,
296         Locale locale, Plugin plugin )
297     {
298         return getResponseData( request, listResponse, locale );
299     }
300 
301     /**
302      * Get the template of the html code of the response value  associate to the entry
303      * @return the template of the html code of the response value  associate to the entry
304      */
305     public String getTemplateHtmlCodeResponse(  )
306     {
307         return _template_html_code_response;
308     }
309 }