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.user.AdminUser;
37 import fr.paris.lutece.portal.service.message.AdminMessage;
38 import fr.paris.lutece.portal.service.message.AdminMessageService;
39 import fr.paris.lutece.portal.service.user.attribute.AttributeService;
40 import fr.paris.lutece.portal.web.constants.Messages;
41
42 import org.apache.commons.collections.CollectionUtils;
43 import org.apache.commons.lang3.StringUtils;
44
45 import java.util.ArrayList;
46 import java.util.List;
47 import java.util.Locale;
48
49 import javax.servlet.http.HttpServletRequest;
50
51
52
53
54
55
56 public class AttributeText extends AbstractAttribute implements ISimpleValuesAttributes
57 {
58
59 private static final String EMPTY_STRING = "";
60 private static final String CONSTANT_UNDERSCORE = "_";
61
62
63 private static final String PARAMETER_TITLE = "title";
64 private static final String PARAMETER_HELP_MESSAGE = "help_message";
65 private static final String PARAMETER_MANDATORY = "mandatory";
66 private static final String PARAMETER_WIDTH = "width";
67 private static final String PARAMETER_MAX_SIZE_ENTER = "max_size_enter";
68 private static final String PARAMETER_VALUE = "value";
69 private static final String PARAMETER_IS_SHOWN_IN_SEARCH = "is_shown_in_search";
70 private static final String PARAMETER_IS_SHOWN_IN_RESULT_LIST = "is_shown_in_result_list";
71 private static final String PARAMETER_ATTRIBUTE = "attribute";
72
73
74 private static final String PROPERTY_TYPE_TEXT = "portal.users.attribute.type.text";
75 private static final String PROPERTY_CREATE_TEXT_PAGETITLE = "portal.users.create_attribute.pageTitleAttributeText";
76 private static final String PROPERTY_MODIFY_TEXT_PAGETITLE = "portal.users.modify_attribute.pageTitleAttributeText";
77 private static final String PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS = "portal.users.message.noArithmeticalCharacters";
78
79
80 private static final String TEMPLATE_CREATE_ATTRIBUTE = "admin/user/attribute/text/create_attribute_text.html";
81 private static final String TEMPLATE_MODIFY_ATTRIBUTE = "admin/user/attribute/text/modify_attribute_text.html";
82 private static final String TEMPLATE_HTML_FORM_ATTRIBUTE = "admin/user/attribute/text/html_code_form_attribute_text.html";
83 private static final String TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE = "admin/user/attribute/text/html_code_form_search_attribute_text.html";
84 private static final String TEMPLATE_HTML_VALUE = "admin/user/attribute/text/html_code_value_attribute_text.html";
85 private static final String REGEX_ID = "-?[0-9]+";
86
87
88
89
90 public AttributeText( )
91 {
92
93 }
94
95
96
97
98
99
100 @Override
101 public String getTemplateCreateAttribute( )
102 {
103 return TEMPLATE_CREATE_ATTRIBUTE;
104 }
105
106
107
108
109
110
111 @Override
112 public String getTemplateModifyAttribute( )
113 {
114 return TEMPLATE_MODIFY_ATTRIBUTE;
115 }
116
117
118
119
120
121
122 @Override
123 public String getTemplateHtmlFormAttribute( )
124 {
125 return TEMPLATE_HTML_FORM_ATTRIBUTE;
126 }
127
128
129
130
131
132
133 @Override
134 public String getTemplateHtmlFormSearchAttribute( )
135 {
136 return TEMPLATE_HTML_FORM_SEARCH_ATTRIBUTE;
137 }
138
139
140
141
142
143
144 @Override
145 public String getTemplateHtmlValue( )
146 {
147 return TEMPLATE_HTML_VALUE;
148 }
149
150
151
152
153
154
155 @Override
156 public String getPropertyCreatePageTitle( )
157 {
158 return PROPERTY_CREATE_TEXT_PAGETITLE;
159 }
160
161
162
163
164
165
166 @Override
167 public String getPropertyModifyPageTitle( )
168 {
169 return PROPERTY_MODIFY_TEXT_PAGETITLE;
170 }
171
172
173
174
175
176
177
178
179 @Override
180 public String setAttributeData( HttpServletRequest request )
181 {
182 String strTitle = request.getParameter( PARAMETER_TITLE );
183 String strHelpMessage = ( request.getParameter( PARAMETER_HELP_MESSAGE ) != null ) ? request.getParameter( PARAMETER_HELP_MESSAGE ).trim( ) : null;
184 String strIsShownInSearch = request.getParameter( PARAMETER_IS_SHOWN_IN_SEARCH );
185 String strIsShownInResultList = request.getParameter( PARAMETER_IS_SHOWN_IN_RESULT_LIST );
186 String strMandatory = request.getParameter( PARAMETER_MANDATORY );
187 String strWidth = request.getParameter( PARAMETER_WIDTH );
188 String strMaxSizeEnter = request.getParameter( PARAMETER_MAX_SIZE_ENTER );
189 String strValue = request.getParameter( PARAMETER_VALUE );
190
191 if ( StringUtils.isBlank( strTitle ) || StringUtils.isBlank( strWidth ) )
192 {
193 return AdminMessageService.getMessageUrl( request, Messages.MANDATORY_FIELDS, AdminMessage.TYPE_STOP );
194 }
195
196 if ( !strWidth.matches( REGEX_ID ) )
197 {
198 return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS, AdminMessage.TYPE_STOP );
199 }
200
201 int nWidth = Integer.parseInt( strWidth );
202
203 int nMaxSizeEnter;
204
205 if ( StringUtils.isNotBlank( strMaxSizeEnter ) && !strMaxSizeEnter.matches( REGEX_ID ) )
206 {
207 return AdminMessageService.getMessageUrl( request, PROPERTY_MESSAGE_NO_ARITHMETICAL_CHARACTERS, AdminMessage.TYPE_STOP );
208 }
209 else
210 if ( ( strMaxSizeEnter == null ) || strMaxSizeEnter.equals( EMPTY_STRING ) )
211 {
212 nMaxSizeEnter = -1;
213 }
214 else
215 {
216 nMaxSizeEnter = Integer.parseInt( strMaxSizeEnter );
217 }
218
219 setTitle( strTitle );
220 setHelpMessage( strHelpMessage );
221 setMandatory( strMandatory != null );
222 setShownInSearch( strIsShownInSearch != null );
223 setShownInResultList( strIsShownInResultList != null );
224
225 if ( getListAttributeFields( ) == null )
226 {
227 List<AttributeField> listAttributeFields = new ArrayList<>( );
228 AttributeFieldr/attribute/AttributeField.html#AttributeField">AttributeField attributeField = new AttributeField( );
229 listAttributeFields.add( attributeField );
230 setListAttributeFields( listAttributeFields );
231 }
232
233 getListAttributeFields( ).get( 0 ).setValue( strValue );
234 getListAttributeFields( ).get( 0 ).setWidth( nWidth );
235 getListAttributeFields( ).get( 0 ).setMaxSizeEnter( nMaxSizeEnter );
236
237 return null;
238 }
239
240
241
242
243
244
245
246 @Override
247 public void setAttributeType( Locale locale )
248 {
249 AttributeTypeser/attribute/AttributeType.html#AttributeType">AttributeType attributeType = new AttributeType( );
250 attributeType.setLocale( locale );
251 attributeType.setClassName( this.getClass( ).getName( ) );
252 attributeType.setLabelType( PROPERTY_TYPE_TEXT );
253 setAttributeType( attributeType );
254 }
255
256
257
258
259
260
261
262
263
264
265 @Override
266 public List<AdminUserField> getUserFieldsData( HttpServletRequest request, AdminUser user )
267 {
268 String [ ] strValues = request.getParameterValues( PARAMETER_ATTRIBUTE + CONSTANT_UNDERSCORE + getIdAttribute( ) );
269
270 return getUserFieldsData( strValues, user );
271 }
272
273
274
275
276
277
278
279
280
281
282 @Override
283 public List<AdminUserField> getUserFieldsData( String [ ] strValues, AdminUser user )
284 {
285 List<AdminUserField> listUserFields = new ArrayList<>( );
286 AdminUserFields/user/attribute/AdminUserField.html#AdminUserField">AdminUserField userField = new AdminUserField( );
287 AttributeService.getInstance( ).setAttributeField( this );
288
289 if ( strValues != null )
290 {
291 for ( String strValue : strValues )
292 {
293 if ( strValue == null )
294 {
295 strValue = EMPTY_STRING;
296 }
297
298 userField.setUser( user );
299 userField.setAttribute( this );
300
301 if ( CollectionUtils.isNotEmpty( getListAttributeFields( ) ) )
302 {
303 userField.setAttributeField( getListAttributeFields( ).get( 0 ) );
304 }
305
306 userField.setValue( strValue );
307
308 listUserFields.add( userField );
309 }
310 }
311
312 return listUserFields;
313 }
314
315
316
317
318
319
320 @Override
321 public boolean isAnonymizable( )
322 {
323 return true;
324 }
325 }