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.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
70
71
72 public class AttributeImage extends AbstractAttribute
73 {
74
75 private static final String EMPTY_STRING = "";
76 private static final String CONSTANT_UNDERSCORE = "_";
77
78
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
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
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
104
105 public AttributeImage( )
106 {
107 setAttributeImage( true );
108 }
109
110
111
112
113
114
115 @Override
116 public String getTemplateCreateAttribute( )
117 {
118 return TEMPLATE_CREATE_ATTRIBUTE;
119 }
120
121
122
123
124
125
126 @Override
127 public String getTemplateModifyAttribute( )
128 {
129 return TEMPLATE_MODIFY_ATTRIBUTE;
130 }
131
132
133
134
135
136
137 @Override
138 public String getTemplateHtmlFormAttribute( )
139 {
140 return TEMPLATE_HTML_FORM_ATTRIBUTE;
141 }
142
143
144
145
146
147
148 @Override
149 public String getTemplateHtmlFormSearchAttribute( )
150 {
151 return EMPTY_STRING;
152 }
153
154
155
156
157
158
159 @Override
160 public String getTemplateHtmlValue( )
161 {
162 return TEMPLATE_HTML_VALUE;
163 }
164
165
166
167
168
169
170 @Override
171 public String getPropertyCreatePageTitle( )
172 {
173 return PROPERTY_CREATE_IMAGE_PAGETITLE;
174 }
175
176
177
178
179
180
181 @Override
182 public String getPropertyModifyPageTitle( )
183 {
184 return PROPERTY_MODIFY_IMAGE_PAGETITLE;
185 }
186
187
188
189
190
191
192
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
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
255
256
257
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
271
272
273
274
275
276
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
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
352
353
354
355 @Override
356 public boolean isAnonymizable( )
357 {
358 return false;
359 }
360 }