View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.genericattributes.service.entrytype;
35  
36  import java.util.List;
37  import java.util.Locale;
38  import java.util.Optional;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.fileupload.FileItem;
43  import org.apache.commons.lang3.StringUtils;
44  
45  import fr.paris.lutece.plugins.genericattributes.business.Entry;
46  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
47  import fr.paris.lutece.plugins.genericattributes.business.MandatoryError;
48  import fr.paris.lutece.plugins.genericattributes.business.Response;
49  import fr.paris.lutece.plugins.genericattributes.service.file.GenericAttributeFileService;
50  import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
51  import fr.paris.lutece.portal.business.file.File;
52  import fr.paris.lutece.portal.service.fileimage.FileImagePublicService;
53  import fr.paris.lutece.portal.service.fileupload.FileUploadService;
54  import fr.paris.lutece.portal.service.i18n.I18nService;
55  import fr.paris.lutece.portal.service.message.AdminMessage;
56  import fr.paris.lutece.portal.service.message.AdminMessageService;
57  
58  /**
59   * 
60   * AbstractEntryTypeGalleryImage
61   *
62   */
63  public abstract class AbstractEntryTypeGalleryImage extends EntryTypeService
64  {
65      // PARAMETERS
66      protected static final String PARAMETER_ID_RESPONSE    = "id_response";
67      protected static final String PARAMETER_CODE_GALLERY   = "code_gallery";
68  
69      // MESSAGES
70      protected static final String ERROR_FIELD_CODE_GALLERY = "genericattributes.createEntry.labelChooseGalleryImage";
71  
72      /**
73       * Check the record field data
74       * 
75       * @param entry
76       *            The entry
77       * @param listFilesSource
78       *            the list of source files to upload
79       * @param locale
80       *            the locale
81       * @param request
82       *            the HTTP request
83       * @return The error if there is any
84       */
85      protected GenericAttributeError checkResponseData( Entry entry, List<FileItem> listFilesSource, Locale locale, HttpServletRequest request )
86      {
87          for ( FileItem fileSource : listFilesSource )
88          {
89              // Check mandatory attribute
90              String strFilename = Optional.ofNullable( fileSource ).map( FileUploadService::getFileNameOnly ).orElse( StringUtils.EMPTY );
91  
92              if ( entry.isMandatory( ) && StringUtils.isBlank( strFilename ) )
93              {
94                  return new MandatoryError( entry, locale );
95              }
96          }
97  
98          return null;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
106     {
107         String strAttributeName = getAttributeName( entry, request );
108 
109         // Upload file from gallery
110         String strFileGallery = request.getParameter( strAttributeName );
111         
112         if ( StringUtils.isNotEmpty( strFileGallery ) )
113         {
114             FileImagePublicService.init( );
115 
116             File file = GenericAttributeFileService.getInstance().load( strFileGallery );
117 
118             Response/genericattributes/business/Response.html#Response">Response response = new Response( );
119             response.setEntry( entry );
120             response.setFile( file );
121             response.setIterationNumber( getResponseIterationValue( request ) );
122 
123             listResponse.add( response );
124 
125             return null;
126         } else if ( StringUtils.isEmpty( strFileGallery ) && entry.isMandatory( ) )
127         {
128             GenericAttributeError genAttError = new MandatoryError( entry, locale );
129 
130             Response/genericattributes/business/Response.html#Response">Response response = new Response( );
131             response.setEntry( entry );
132             listResponse.add( response );
133             return genAttError;
134         } else
135         {
136             Response/genericattributes/business/Response.html#Response">Response response = new Response( );
137             response.setEntry( entry );
138             listResponse.add( response );
139 
140             return null;
141         }
142 
143     }
144 
145     /**
146      * Gives the attribute name
147      * 
148      * @param entry
149      *            the entry
150      * @param request
151      *            the request
152      * @return the attribute name
153      */
154     protected String getAttributeName( Entry entry, HttpServletRequest request )
155     {
156         return IEntryTypeService.PREFIX_ATTRIBUTE + entry.getIdEntry( );
157     }
158 
159     /**
160      * {@inheritDoc}
161      */
162     @Override
163     public String getRequestData( Entry entry, HttpServletRequest request, Locale locale )
164     {
165         initCommonRequestData( entry, request );
166         String strTitle = request.getParameter( PARAMETER_TITLE );
167         String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
168         String strComment = request.getParameter( PARAMETER_COMMENT );
169         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
170         String strCSSClass = request.getParameter( PARAMETER_CSS_CLASS );
171         String strCode = request.getParameter( PARAMETER_ENTRY_CODE );
172         String strOnlyDisplayInBack = request.getParameter( PARAMETER_ONLY_DISPLAY_IN_BACK );
173         String strIndexed = request.getParameter( PARAMETER_INDEXED );
174 
175         String strError = checkEntryData( request, locale );
176 
177         if ( StringUtils.isNotBlank( strError ) )
178         {
179             return strError;
180         }
181 
182         entry.setTitle( strTitle );
183         entry.setCode( strCode );
184         entry.setHelpMessage( strHelpMessage );
185         entry.setComment( strComment );
186         entry.setCSSClass( strCSSClass );
187         entry.setIndexed( strIndexed != null );
188 
189         String strCodeGallery = request.getParameter( PARAMETER_CODE_GALLERY );
190         GenericAttributesUtils.createOrUpdateField( entry, PARAMETER_CODE_GALLERY, null, strCodeGallery );
191 
192         String strExportBinary = request.getParameter( IEntryTypeService.PARAMETER_EXPORT_BINARY );
193         GenericAttributesUtils.createOrUpdateField( entry, IEntryTypeService.FIELD_FILE_BINARY, null, Boolean.toString( StringUtils.isNotBlank( strExportBinary ) ) );
194 
195         entry.setMandatory( strMandatory != null );
196         entry.setOnlyDisplayInBack( strOnlyDisplayInBack != null );
197         return null;
198     }
199 
200     /**
201      * {@inheritDoc}
202      */
203     @Override
204     public String getResponseValueForRecap( Entry entry, HttpServletRequest request, Response response, Locale locale )
205     {
206         if ( ( response.getFile( ) != null ) && StringUtils.isNotBlank( response.getFile( ).getTitle( ) ) )
207         {
208             return response.getFile( ).getTitle( );
209         }
210 
211         return StringUtils.EMPTY;
212     }
213 
214     /**
215      * Check the entry data
216      * 
217      * @param request
218      *            the HTTP request
219      * @param locale
220      *            the locale
221      * @return the error message url if there is an error, an empty string otherwise
222      */
223     private static String checkEntryData( HttpServletRequest request, Locale locale )
224     {
225         String strTitle = request.getParameter( IEntryTypeService.PARAMETER_TITLE );
226         String strFieldError = StringUtils.EMPTY;
227         String strCodeGalleryImage = request.getParameter( PARAMETER_CODE_GALLERY );
228 
229         if ( StringUtils.isBlank( strTitle ) )
230         {
231             strFieldError = IEntryTypeService.ERROR_FIELD_TITLE;
232         }
233 
234         if ( StringUtils.isEmpty( strCodeGalleryImage ) )
235         {
236             strFieldError = ERROR_FIELD_CODE_GALLERY;
237         }
238 
239         if ( StringUtils.isNotBlank( strFieldError ) )
240         {
241             Object[] tabRequiredFields = { I18nService.getLocalizedString( strFieldError, locale ) };
242 
243             return AdminMessageService.getMessageUrl( request, IEntryTypeService.MESSAGE_MANDATORY_FIELD, tabRequiredFields, AdminMessage.TYPE_STOP );
244         }
245 
246         return StringUtils.EMPTY;
247     }
248 }