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