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.service.fileupload.FileUploadService;
38  import fr.paris.lutece.portal.service.i18n.I18nService;
39  import fr.paris.lutece.portal.service.image.ImageResource;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.plugin.Plugin;
43  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
44  
45  import org.apache.commons.fileupload.FileItem;
46  
47  import java.util.List;
48  import java.util.Locale;
49  
50  import javax.servlet.http.HttpServletRequest;
51  
52  
53  /**
54   *
55   * class EntryTypeImage
56   *
57   */
58  public class EntryTypeImage extends Entry
59  {
60      private static final String PARAMETER_WIDTH = "width";
61      private static final String PARAMETER_HEIGHT = "height";
62      private static final String MESSAGE_IMAGE_TOO_HEAVY = "digglike.message.imageTooHeavy";
63      private final String _template_create = "admin/plugins/digglike/create_entry_type_image.html";
64      private final String _template_modify = "admin/plugins/digglike/modify_entry_type_image.html";
65      private final String _template_html_code_form = "admin/plugins/digglike/html_code_form_entry_type_image.html";
66      private final String _template_html_code_response = "admin/plugins/digglike/html_code_response_entry_type_image.html";
67  
68      /**
69       * Get the HtmlCode  of   the entry
70       * @return the HtmlCode  of   the entry
71       *
72       * */
73      public String getTemplateHtmlCodeForm(  )
74      {
75          return _template_html_code_form;
76      }
77  
78      /**
79       * Get the request data
80       * @param request HttpRequest
81       * @param locale the locale
82       * @return null if all data requiered are in the request else the url of jsp error
83       */
84      public String getRequestData( HttpServletRequest request, Locale locale )
85      {
86          String strTitle = request.getParameter( PARAMETER_TITLE );
87          String strHelpMessage = request.getParameter( PARAMETER_HELP_MESSAGE );
88          String strComment = request.getParameter( PARAMETER_COMMENT );
89          String strValue = request.getParameter( PARAMETER_VALUE );
90          String strMandatory = request.getParameter( PARAMETER_MANDATORY );
91          String strWidth = request.getParameter( PARAMETER_WIDTH );
92          String strHeight = request.getParameter( PARAMETER_HEIGHT );
93          String strShowInDiggSubmitList = request.getParameter( PARAMETER_SHOW_IN_DIGG_SUBMIT_LIST );
94  
95          int nWidth = -1;
96          int nHeight = -1;
97  
98          String strFieldError = EMPTY_STRING;
99  
100         if ( ( strTitle == null ) || strTitle.trim(  ).equals( EMPTY_STRING ) )
101         {
102             strFieldError = FIELD_TITLE;
103         }
104 
105         if ( !strFieldError.equals( EMPTY_STRING ) )
106         {
107             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, locale ) };
108 
109             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
110                 AdminMessage.TYPE_STOP );
111         }
112 
113         try
114         {
115             if ( !( ( strHeight == null ) || strHeight.trim(  ).equals( EMPTY_STRING ) ) )
116             {
117                 nHeight = Integer.parseInt( strHeight );
118             }
119         }
120         catch ( NumberFormatException ne )
121         {
122             strFieldError = FIELD_HEIGHT;
123         }
124 
125         try
126         {
127             if ( !( ( strWidth == null ) || strWidth.trim(  ).equals( EMPTY_STRING ) ) )
128             {
129                 nWidth = Integer.parseInt( strWidth );
130             }
131         }
132         catch ( NumberFormatException ne )
133         {
134             strFieldError = FIELD_WIDTH;
135         }
136 
137         if ( !strFieldError.equals( EMPTY_STRING ) )
138         {
139             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, locale ) };
140 
141             return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields,
142                 AdminMessage.TYPE_STOP );
143         }
144 
145         this.setTitle( strTitle );
146         this.setHelpMessage( DiggUtils.trim( strHelpMessage ) );
147         this.setComment( strComment );
148 
149         this.setDefaultValue( strValue );
150         this.setWidth( nWidth );
151         this.setHeight( nHeight );
152 
153         if ( strMandatory != null )
154         {
155             this.setMandatory( true );
156         }
157         else
158         {
159             this.setMandatory( false );
160         }
161 
162         if ( strShowInDiggSubmitList != null )
163         {
164             this.setShowInDiggSubmitList( true );
165         }
166         else
167         {
168             this.setShowInDiggSubmitList( false );
169         }
170 
171         return null;
172     }
173 
174     /**
175      * Get template create url of the entry
176      * @return template create url of the entry
177      */
178     public String getTemplateCreate(  )
179     {
180         return _template_create;
181     }
182 
183     /**
184      * Get the template modify url  of the entry
185      * @return template modify url  of the entry
186      */
187     public String getTemplateModify(  )
188     {
189         return _template_modify;
190     }
191 
192     /**
193      * save in the list of response the response associate to the entry in the form submit
194      * @param nIdDiggSubmit the id of the DiggSubmit
195      * @param request HttpRequest
196      * @param listResponse the list of response associate to the entry in the form submit
197      * @param locale the locale
198      * @param plugin the plugin
199      * @return a Form error object if there is an error in the response
200      */
201     public FormError getResponseData( int nIdDiggSubmit, HttpServletRequest request, List<Response> listResponse,
202         Locale locale, Plugin plugin )
203     {
204         MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
205         FileItem item = mRequest.getFile( DiggUtils.EMPTY_STRING + this.getIdEntry(  ) );
206 
207         byte[] bytes = item.get(  );
208 
209         Response response = new Response(  );
210         response.setEntry( this );
211 
212         if ( bytes != null )
213         {
214             ImageResource image = new ImageResource(  );
215             image.setImage( bytes );
216             image.setMimeType( item.getContentType(  ) );
217             response.setImage( image );
218             response.setValueResponse( FileUploadService.getFileNameOnly( item ) );
219         }
220         else
221         {
222             if ( this.isMandatory(  ) )
223             {
224                 FormError formError = new FormError(  );
225                 formError.setMandatoryError( true );
226                 formError.setTitleQuestion( this.getTitle(  ) );
227 
228                 return formError;
229             }
230         }
231 
232         listResponse.add( response );
233 
234         return null;
235     }
236 
237     /**
238      * Get the template of the html code of the response value  associate to the entry
239     * @return the template of the html code of the response value  associate to the entry
240      */
241     public String getTemplateHtmlCodeResponse(  )
242     {
243         return _template_html_code_response;
244     }
245 }