1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
69
70
71 public class AttributeImage extends AbstractAttribute
72 {
73
74 private static final String EMPTY_STRING = "";
75 private static final String CONSTANT_UNDERSCORE = "_";
76
77
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
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
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
103
104 public AttributeImage( )
105 {
106 setAttributeImage( true );
107 }
108
109
110
111
112
113 @Override
114 public String getTemplateCreateAttribute( )
115 {
116 return TEMPLATE_CREATE_ATTRIBUTE;
117 }
118
119
120
121
122
123 @Override
124 public String getTemplateModifyAttribute( )
125 {
126 return TEMPLATE_MODIFY_ATTRIBUTE;
127 }
128
129
130
131
132
133 @Override
134 public String getTemplateHtmlFormAttribute( )
135 {
136 return TEMPLATE_HTML_FORM_ATTRIBUTE;
137 }
138
139
140
141
142
143 @Override
144 public String getTemplateHtmlFormSearchAttribute( )
145 {
146 return EMPTY_STRING;
147 }
148
149
150
151
152
153 @Override
154 public String getTemplateHtmlValue( )
155 {
156 return TEMPLATE_HTML_VALUE;
157 }
158
159
160
161
162
163 @Override
164 public String getPropertyCreatePageTitle( )
165 {
166 return PROPERTY_CREATE_IMAGE_PAGETITLE;
167 }
168
169
170
171
172
173 @Override
174 public String getPropertyModifyPageTitle( )
175 {
176 return PROPERTY_MODIFY_IMAGE_PAGETITLE;
177 }
178
179
180
181
182
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
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
255
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
269
270
271
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
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
355
356
357 @Override
358 public boolean isAnonymizable( )
359 {
360 return false;
361 }
362 }