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.portal.business.user.attribute;
35  
36  import fr.paris.lutece.portal.business.file.File;
37  import fr.paris.lutece.portal.business.file.FileHome;
38  import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
39  import fr.paris.lutece.portal.business.physicalfile.PhysicalFileHome;
40  import fr.paris.lutece.portal.business.user.AdminUser;
41  import fr.paris.lutece.portal.service.fileupload.FileUploadService;
42  import fr.paris.lutece.portal.service.message.AdminMessage;
43  import fr.paris.lutece.portal.service.message.AdminMessageService;
44  import fr.paris.lutece.portal.service.user.attribute.AttributeService;
45  import fr.paris.lutece.portal.service.util.AppLogService;
46  import fr.paris.lutece.portal.web.constants.Messages;
47  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
48  import fr.paris.lutece.util.filesystem.FileSystemUtil;
49  import fr.paris.lutece.util.string.StringUtil;
50  
51  import org.apache.commons.collections.CollectionUtils;
52  import org.apache.commons.fileupload.FileItem;
53  import org.apache.commons.lang3.StringUtils;
54  
55  import java.io.ByteArrayInputStream;
56  import java.io.IOException;
57  
58  import java.util.ArrayList;
59  import java.util.List;
60  import java.util.Locale;
61  import java.util.Optional;
62  
63  import javax.imageio.ImageIO;
64  
65  import javax.servlet.http.HttpServletRequest;
66  
67  /**
68   *
69   * AttributeComboBox
70   *
71   */
72  public class AttributeImage extends AbstractAttribute
73  {
74      // Constants
75      private static final String EMPTY_STRING = "";
76      private static final String CONSTANT_UNDERSCORE = "_";
77  
78      // Parameters
79      private static final String PARAMETER_TITLE = "title";
80      private static final String PARAMETER_HELP_MESSAGE = "help_message";
81      private static final String PARAMETER_MANDATORY = "mandatory";
82      private static final String PARAMETER_ATTRIBUTE = "attribute";
83      private static final String PARAMETER_WIDTH = "width";
84      private static final String PARAMETER_HEIGHT = "height";
85      private static final String PARAMETER_UPDATE_ATTRIBUTE = "update_attribute";
86      private static final String PARAMETER_IS_SHOWN_IN_RESULT_LIST = "is_shown_in_result_list";
87      private static final String PARAMETER_ID_USER = "id_user";
88  
89      // Properties
90      private static final String PROPERTY_TYPE_IMAGE = "portal.users.attribute.type.image";
91      private static final String PROPERTY_CREATE_IMAGE_PAGETITLE = "portal.users.create_attribute.pageTitleAttributeImage";
92      private static final String PROPERTY_MODIFY_IMAGE_PAGETITLE = "portal.users.modify_attribute.pageTitleAttributeImage";
93      private static final String PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS = "portal.users.message.noArithmeticalCharacters";
94  
95      // Templates
96      private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/user/attribute/image/create_attribute_image.html";
97      private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/user/attribute/image/modify_attribute_image.html";
98      private static final String TEMPLATE_HTML_FORM_ATTRIBUTE = "admin/user/attribute/image/html_code_form_attribute_image.html";
99      private static final String TEMPLATE_HTML_VALUE = "admin/user/attribute/image/html_code_value_attribute_image.html";
100     private static final String REGEX_ID = "-?[0-9]+";
101 
102     /**
103      * Constructor
104      */
105     public AttributeImage( )
106     {
107         setAttributeImage( true );
108     }
109 
110     /**
111      * Get the template create an attribute
112      * 
113      * @return The URL of the template
114      */
115     @Override
116     public String getTemplateCreateAttribute( )
117     {
118         return TEMPLATE_CREATE_ATTRIBUTE;
119     }
120 
121     /**
122      * Get the template modify an attribute
123      * 
124      * @return The URL of the template
125      */
126     @Override
127     public String getTemplateModifyAttribute( )
128     {
129         return TEMPLATE_MODIFY_ATTRIBUTE;
130     }
131 
132     /**
133      * Get the template html form attribute
134      * 
135      * @return the template
136      */
137     @Override
138     public String getTemplateHtmlFormAttribute( )
139     {
140         return TEMPLATE_HTML_FORM_ATTRIBUTE;
141     }
142 
143     /**
144      * Get the template html form search attribute
145      * 
146      * @return the template
147      */
148     @Override
149     public String getTemplateHtmlFormSearchAttribute( )
150     {
151         return EMPTY_STRING;
152     }
153 
154     /**
155      * Get the template html for the value of the attribute
156      * 
157      * @return the template
158      */
159     @Override
160     public String getTemplateHtmlValue( )
161     {
162         return TEMPLATE_HTML_VALUE;
163     }
164 
165     /**
166      * Get page title for create page
167      * 
168      * @return page title
169      */
170     @Override
171     public String getPropertyCreatePageTitle( )
172     {
173         return PROPERTY_CREATE_IMAGE_PAGETITLE;
174     }
175 
176     /**
177      * Get page title for modify page
178      * 
179      * @return page title
180      */
181     @Override
182     public String getPropertyModifyPageTitle( )
183     {
184         return PROPERTY_MODIFY_IMAGE_PAGETITLE;
185     }
186 
187     /**
188      * Set the data of the attribute
189      * 
190      * @param request
191      *            HttpServletRequest
192      * @return null if there are no errors
193      */
194     @Override
195     public String setAttributeData( HttpServletRequest request )
196     {
197         String strTitle = request.getParameter( PARAMETER_TITLE );
198         String strHelpMessage = Optional.ofNullable( request.getParameter( PARAMETER_HELP_MESSAGE ) ).map( String::trim ).orElse( null );
199         String strMandatory = request.getParameter( PARAMETER_MANDATORY );
200         String strWidth = request.getParameter( PARAMETER_WIDTH );
201         String strHeight = request.getParameter( PARAMETER_HEIGHT );
202         String strShownInResultList = request.getParameter( PARAMETER_IS_SHOWN_IN_RESULT_LIST );
203 
204         if ( StringUtils.isBlank( strTitle ) )
205         {
206             return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
207         }
208 
209         setTitle( strTitle );
210         setHelpMessage( strHelpMessage );
211         setMandatory( strMandatory != null );
212         setShownInResultList( strShownInResultList != null );
213         // Never show an image in the search box
214         setShownInSearch( false );
215 
216         if ( getListAttributeFields( ) == null )
217         {
218             List<AttributeField> listAttributeFields = new ArrayList<>( );
219             AttributeFieldr/attribute/AttributeField.html#AttributeField">AttributeField attributeField = new AttributeField( );
220             listAttributeFields.add( attributeField );
221             setListAttributeFields( listAttributeFields );
222         }
223 
224         if ( ( StringUtils.isNotBlank( strWidth ) && !strWidth.matches( REGEX_ID ) )
225                 || ( StringUtils.isNotBlank( strHeight ) && !strHeight.matches( REGEX_ID ) ) )
226         {
227             return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS, AdminMessage.TYPE_STOP );
228         }
229 
230         if ( StringUtils.isNotBlank( strWidth ) && strWidth.matches( REGEX_ID ) )
231         {
232             int nWidth = Integer.parseInt( strWidth );
233             getListAttributeFields( ).get( 0 ).setWidth( nWidth );
234         }
235         else
236         {
237             getListAttributeFields( ).get( 0 ).setWidth( -1 );
238         }
239 
240         if ( StringUtils.isNotBlank( strHeight ) && strHeight.matches( REGEX_ID ) )
241         {
242             int nHeight = Integer.parseInt( strHeight );
243             getListAttributeFields( ).get( 0 ).setHeight( nHeight );
244         }
245         else
246         {
247             getListAttributeFields( ).get( 0 ).setHeight( -1 );
248         }
249 
250         return null;
251     }
252 
253     /**
254      * Set attribute type
255      * 
256      * @param locale
257      *            locale
258      */
259     @Override
260     public void setAttributeType( Locale locale )
261     {
262         AttributeTypeser/attribute/AttributeType.html#AttributeType">AttributeType attributeType = new AttributeType( );
263         attributeType.setLocale( locale );
264         attributeType.setClassName( this.getClass( ).getName( ) );
265         attributeType.setLabelType( PROPERTY_TYPE_IMAGE );
266         setAttributeType( attributeType );
267     }
268 
269     /**
270      * Get the data of the user fields
271      * 
272      * @param request
273      *            HttpServletRequest
274      * @param user
275      *            user
276      * @return user field data
277      */
278     @Override
279     public List<AdminUserField> getUserFieldsData( HttpServletRequest request, AdminUser user )
280     {
281         String strUpdateAttribute = request.getParameter( PARAMETER_UPDATE_ATTRIBUTE + CONSTANT_UNDERSCORE + getIdAttribute( ) );
282         List<AdminUserField> listUserFields = new ArrayList<>( );
283 
284         try
285         {
286             if ( StringUtils.isNotBlank( strUpdateAttribute ) )
287             {
288                 MultipartHttpServletRequestutece/portal/web/upload/MultipartHttpServletRequest.html#MultipartHttpServletRequest">MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
289                 FileItem fileItem = multipartRequest.getFile( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + getIdAttribute( ) );
290 
291                 if ( ( fileItem != null ) && ( StringUtils.isNotEmpty( fileItem.getName( ) ) ) )
292                 {
293                     File/portal/business/file/File.html#File">File file = new File( );
294                     PhysicalFile/physicalfile/PhysicalFile.html#PhysicalFile">PhysicalFile physicalFile = new PhysicalFile( );
295                     physicalFile.setValue( fileItem.get( ) );
296                     file.setTitle( FileUploadService.getFileNameOnly( fileItem ) );
297                     file.setSize( (int) fileItem.getSize( ) );
298                     file.setPhysicalFile( physicalFile );
299                     file.setMimeType( FileSystemUtil.getMIMEType( FileUploadService.getFileNameOnly( fileItem ) ) );
300 
301                     // verify that the file is an image
302                     ImageIO.read( new ByteArrayInputStream( file.getPhysicalFile( ).getValue( ) ) );
303 
304                     AdminUserFields/user/attribute/AdminUserField.html#AdminUserField">AdminUserField userField = new AdminUserField( );
305                     userField.setUser( user );
306                     userField.setAttribute( this );
307 
308                     AttributeService.getInstance( ).setAttributeField( this );
309 
310                     if ( CollectionUtils.isNotEmpty( getListAttributeFields( ) ) )
311                     {
312                         userField.setAttributeField( getListAttributeFields( ).get( 0 ) );
313                         userField.setFile( file );
314                     }
315 
316                     listUserFields.add( userField );
317                 }
318             }
319             else
320             {
321                 AdminUserFieldFilterribute/AdminUserFieldFilter.html#AdminUserFieldFilter">AdminUserFieldFilter auFieldFilter = new AdminUserFieldFilter( );
322                 auFieldFilter.setIdAttribute( getIdAttribute( ) );
323 
324                 String strIdUser = request.getParameter( PARAMETER_ID_USER );
325 
326                 if ( StringUtils.isNotBlank( strIdUser ) )
327                 {
328                     auFieldFilter.setIdUser( StringUtil.getIntValue( strIdUser, 0 ) );
329                 }
330 
331                 listUserFields = AdminUserFieldHome.findByFilter( auFieldFilter );
332                 listUserFields.stream( ).filter( a -> a.getFile( ) != null ).forEach( ( AdminUserField userField ) -> {
333                     File file = FileHome.findByPrimaryKey( userField.getFile( ).getIdFile( ) );
334                     userField.setFile( file );
335 
336                     int nIdPhysicalFile = file.getPhysicalFile( ).getIdPhysicalFile( );
337                     PhysicalFile physicalFile = PhysicalFileHome.findByPrimaryKey( nIdPhysicalFile );
338                     userField.getFile( ).setPhysicalFile( physicalFile );
339                 } );
340             }
341         }
342         catch( IOException e )
343         {
344             AppLogService.error( e.getMessage( ), e );
345         }
346 
347         return listUserFields;
348     }
349 
350     /**
351      * Get whether the attribute is anonymizable.
352      * 
353      * @return True if the attribute can be anonymized, false otherwise.
354      */
355     @Override
356     public boolean isAnonymizable( )
357     {
358         return false;
359     }
360 }