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.service.DiggSubmitService;
37  import fr.paris.lutece.plugins.digglike.utils.DiggUtils;
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.web.upload.MultipartHttpServletRequest;
43  
44  import java.util.ArrayList;
45  import java.util.List;
46  import java.util.Locale;
47  
48  import javax.servlet.http.HttpServletRequest;
49  
50  import org.apache.commons.fileupload.FileItem;
51  
52  
53  /**
54  *
55  * class EntryTypeVideo
56  *
57  */
58  public class EntryTypeVideo extends Entry
59  {
60      private static final String PARAMETER_CREDITS = "credits";
61      private static final String PARAMETER_AUTOSTART = "autostart";
62      private static final String PARAMETER_LOOP = "loop";
63      private static final String PARAMETER_QUALITY = "quality";
64      private static final String PARAMETER_ALIGNMENT = "alignment";
65      private static final String PARAMETER_MENU = "menu";
66      private static final String MESSAGE_FILE_TOO_HEAVY = "digglike.message.fileTooHeavy";
67      private static final String PROPERTY_CREDITS = "digglike.createEntry.labelCredits";
68      private final String _template_create = "admin/plugins/digglike/create_entry_type_video.html";
69      private final String _template_modify = "admin/plugins/digglike/modify_entry_type_video.html";
70      private final String _template_html_code_form = "admin/plugins/digglike/html_code_form_entry_type_video.html";
71      private final String _template_html_code_response = "admin/plugins/digglike/html_code_response_entry_type_video.html";
72  
73      /**
74       * Get the HtmlCode  of   the entry
75       * @return the HtmlCode  of   the entry
76       *
77       * */
78      public String getTemplateHtmlCodeForm(  )
79      {
80          return _template_html_code_form;
81      }
82  
83      /**
84       * Get the request data
85       * @param request HttpRequest
86       * @param locale the local
87       * @return null if all data required are in the request else the url of jsp error
88       */
89      public String getRequestData( HttpServletRequest request, Locale locale )
90      {
91          String strTitle = request.getParameter( PARAMETER_TITLE );
92          String strHelpMessage = request.getParameter( PARAMETER_HELP_MESSAGE );
93          String strComment = request.getParameter( PARAMETER_COMMENT );
94          String strValue = request.getParameter( PARAMETER_VALUE );
95          String strMandatory = request.getParameter( PARAMETER_MANDATORY );
96          String strWidth = request.getParameter( PARAMETER_WIDTH );
97          String strHeight = request.getParameter( PARAMETER_HEIGHT );
98          String strShowInDiggSubmitList = request.getParameter( PARAMETER_SHOW_IN_DIGG_SUBMIT_LIST );
99          String strAutostart = request.getParameter( PARAMETER_AUTOSTART );
100         String strLoop = request.getParameter( PARAMETER_LOOP );
101         String strQuality = request.getParameter( PARAMETER_QUALITY );
102         String strAlignment = request.getParameter( PARAMETER_ALIGNMENT );
103         String strMenu = request.getParameter( PARAMETER_MENU );
104 
105         int nWidth = -1;
106         int nHeight = -1;
107 
108         String strFieldError = EMPTY_STRING;
109 
110         if ( ( strTitle == null ) || strTitle.trim(  ).equals( EMPTY_STRING ) )
111         {
112             strFieldError = FIELD_TITLE;
113         }
114 
115         if ( !strFieldError.equals( EMPTY_STRING ) )
116         {
117             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, locale ) };
118 
119             return AdminMessageService.getMessageUrl( request, MESSAGE_MANDATORY_FIELD, tabRequiredFields,
120                 AdminMessage.TYPE_STOP );
121         }
122 
123         try
124         {
125             if ( !( ( strHeight == null ) || strHeight.trim(  ).equals( EMPTY_STRING ) ) )
126             {
127                 nHeight = Integer.parseInt( strHeight );
128             }
129         }
130         catch ( NumberFormatException ne )
131         {
132             strFieldError = FIELD_HEIGHT;
133         }
134 
135         try
136         {
137             if ( !( ( strWidth == null ) || strWidth.trim(  ).equals( EMPTY_STRING ) ) )
138             {
139                 nWidth = Integer.parseInt( strWidth );
140             }
141         }
142         catch ( NumberFormatException ne )
143         {
144             strFieldError = FIELD_WIDTH;
145         }
146 
147         if ( !strFieldError.equals( EMPTY_STRING ) )
148         {
149             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, locale ) };
150 
151             return AdminMessageService.getMessageUrl( request, MESSAGE_NUMERIC_FIELD, tabRequiredFields,
152                 AdminMessage.TYPE_STOP );
153         }
154 
155         List<EntryAdditionalAttribute> entryAdditionalAttributeList = new ArrayList<EntryAdditionalAttribute>(  );
156         EntryAdditionalAttribute entryAdditionalAttributeAutostart = new EntryAdditionalAttribute(  );
157         entryAdditionalAttributeAutostart.setName( PARAMETER_AUTOSTART );
158         entryAdditionalAttributeAutostart.setValue( strAutostart );
159         entryAdditionalAttributeList.add( entryAdditionalAttributeAutostart );
160 
161         EntryAdditionalAttribute entryAdditionalAttributeLoop = new EntryAdditionalAttribute(  );
162         entryAdditionalAttributeLoop.setName( PARAMETER_LOOP );
163         entryAdditionalAttributeLoop.setValue( strLoop );
164         entryAdditionalAttributeList.add( entryAdditionalAttributeLoop );
165 
166         EntryAdditionalAttribute entryAdditionalAttributeAlignment = new EntryAdditionalAttribute(  );
167         entryAdditionalAttributeAlignment.setName( PARAMETER_ALIGNMENT );
168         entryAdditionalAttributeAlignment.setValue( strAlignment );
169         entryAdditionalAttributeList.add( entryAdditionalAttributeAlignment );
170 
171         EntryAdditionalAttribute entryAdditionalAttributeQuality = new EntryAdditionalAttribute(  );
172         entryAdditionalAttributeQuality.setName( PARAMETER_QUALITY );
173         entryAdditionalAttributeQuality.setValue( strQuality );
174         entryAdditionalAttributeList.add( entryAdditionalAttributeQuality );
175 
176         EntryAdditionalAttribute entryAdditionalAttributeMenu = new EntryAdditionalAttribute(  );
177         entryAdditionalAttributeMenu.setName( PARAMETER_MENU );
178         entryAdditionalAttributeMenu.setValue( strMenu );
179         entryAdditionalAttributeList.add( entryAdditionalAttributeMenu );
180 
181         this.setTitle( strTitle );
182         this.setHelpMessage( DiggUtils.trim( strHelpMessage ) );
183         this.setComment( strComment );
184 
185         this.setDefaultValue( strValue );
186         this.setWidth( nWidth );
187         this.setHeight( nHeight );
188 
189         this.setEntryAdditionalAttributeList( entryAdditionalAttributeList );
190 
191         if ( strMandatory != null )
192         {
193             this.setMandatory( true );
194         }
195         else
196         {
197             this.setMandatory( false );
198         }
199 
200         if ( strShowInDiggSubmitList != null )
201         {
202             this.setShowInDiggSubmitList( true );
203         }
204         else
205         {
206             this.setShowInDiggSubmitList( false );
207         }
208 
209         return null;
210     }
211 
212     /**
213      * Get template create url of the entry
214      * @return template create url of the entry
215      */
216     public String getTemplateCreate(  )
217     {
218         return _template_create;
219     }
220 
221     /**
222      * Get the template modify url  of the entry
223      * @return template modify url  of the entry
224      */
225     public String getTemplateModify(  )
226     {
227         return _template_modify;
228     }
229 
230     /**
231      * save in the list of response the response associate to the entry in the form submit
232      * @param nIdDiggSubmit the id of the DiggSubmit
233      * @param request HttpRequest
234      * @param listResponse the list of response associate to the entry in the form submit
235      * @param locale the locale
236      * @param plugin the plugin
237      * @return a Form error object if there is an error in the response
238      */
239     public FormError getResponseData( int nIdDiggSubmit, HttpServletRequest request, List<Response> listResponse,
240         Locale locale, Plugin plugin )
241     {
242         String strCredits = request.getParameter( PARAMETER_CREDITS + this.getIdEntry(  ) );
243 
244         MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
245         FileItem item = mRequest.getFile( DiggUtils.EMPTY_STRING + this.getIdEntry(  ) );
246 
247         byte[] bytes = item.get(  );
248 
249         Response response = new Response(  );
250         response.setEntry( this );
251 
252         if ( bytes != null )
253         {
254             VideoType video = new VideoType(  );
255             video.setVideo( bytes );
256             video.setMimeType( item.getContentType(  ) );
257             video.setCredits( strCredits );
258             video.setIdDiggSubmit( nIdDiggSubmit );
259 
260             try
261             {
262                 VideoTypeHome.create( video, plugin );
263 
264                 String strMenu = "false";
265                 String strLoop = "false";
266                 String strAutostart = "false";
267                 //                String strAlignment = "bottom";
268                 String strQuality = "low";
269 
270                 for ( EntryAdditionalAttribute attr : this.getEntryAdditionalAttributeList(  ) )
271                 {
272                     if ( attr.getName(  ).equals( PARAMETER_AUTOSTART ) )
273                     {
274                         strAutostart = attr.getValue(  );
275                     }
276                     //                    else if ( attr.getName(  ).equals( PARAMETER_ALIGNMENT ) )
277                     //                    {
278                     //                        strAlignment = attr.getValue(  );
279                     //                    }
280                     else if ( attr.getName(  ).equals( PARAMETER_LOOP ) )
281                     {
282                         strLoop = attr.getValue(  );
283                     }
284                     else if ( attr.getName(  ).equals( PARAMETER_QUALITY ) )
285                     {
286                         strQuality = attr.getValue(  );
287                     }
288                     else if ( attr.getName(  ).equals( PARAMETER_MENU ) )
289                     {
290                         strMenu = attr.getValue(  );
291                     }
292                 }
293 
294                 String strResponse = "<div id='mediaspace" + nIdDiggSubmit + "'></div>" +
295                     "<script type='text/javascript' src='js/player/swfobject.js'></script>" +
296                     "<script type='text/javascript'>" + " var so = new SWFObject('js/player/player.swf','mpl','" +
297                     this.getWidth(  ) + "','" + this.getHeight(  ) + "','9');" +
298                     "  so.addParam('allowfullscreen','true');" + "  so.addParam('allowscriptaccess','always');" +
299                     "  so.addParam('wmode','opaque');" + "  so.addParam('quality','" + strQuality + "');" +
300                     "  so.addParam('menu','" + strMenu + "');" +
301                     "  so.addVariable('file','../../jsp/site/plugins/digglike/getVideo.jsp?video_id=" + nIdDiggSubmit +
302                     "');" + "  so.addVariable('provider','video');" + "  so.addVariable('autostart','" + strAutostart +
303                     "');" + "  so.addVariable('icons','false');" + "  so.addVariable('repeat','" + strLoop + "');" +
304                     "  so.write('mediaspace" + nIdDiggSubmit + "');" + "</script>";
305 
306                 strResponse += ( "<br /><b>" + I18nService.getLocalizedString( PROPERTY_CREDITS, locale ) +
307                 "&nbsp;:&nbsp;</b>" + strCredits );
308 
309                 response.setValueResponse( strResponse );
310             }
311             catch ( com.mysql.jdbc.PacketTooBigException e )
312             {
313                 //Remove the digg submit potentially created
314                 DiggSubmitService.getService(  ).remove( nIdDiggSubmit, plugin );
315                 //Remove the video potentially created
316                 VideoTypeHome.remove( nIdDiggSubmit, plugin );
317 
318                 FormError formError = new FormError(  );
319                 formError.setTitleQuestion( this.getTitle(  ) );
320                 formError.setErrorMessage( I18nService.getLocalizedString( MESSAGE_FILE_TOO_HEAVY, locale ) );
321 
322                 return formError;
323             }
324         }
325         else
326         {
327             if ( this.isMandatory(  ) )
328             {
329                 FormError formError = new FormError(  );
330                 formError.setMandatoryError( true );
331                 formError.setTitleQuestion( this.getTitle(  ) );
332 
333                 return formError;
334             }
335         }
336 
337         listResponse.add( response );
338 
339         return null;
340     }
341 
342     /**
343      * Get the template of the html code of the response value  associate to the entry
344     * @return the template of the html code of the response value  associate to the entry
345      */
346     public String getTemplateHtmlCodeResponse(  )
347     {
348         return _template_html_code_response;
349     }
350 }