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.plugins.genericattributes.util;
35
36 import fr.paris.lutece.plugins.genericattributes.business.Entry;
37 import fr.paris.lutece.plugins.genericattributes.business.Field;
38 import fr.paris.lutece.plugins.genericattributes.service.GenericAttributesPlugin;
39 import fr.paris.lutece.plugins.genericattributes.service.entrytype.IEntryTypeService;
40 import fr.paris.lutece.plugins.referencelist.business.ReferenceItem;
41 import fr.paris.lutece.portal.service.plugin.Plugin;
42 import fr.paris.lutece.portal.service.plugin.PluginService;
43 import fr.paris.lutece.portal.service.util.AppLogService;
44
45 import org.apache.commons.lang3.StringUtils;
46
47 import java.util.List;
48
49
50
51
52 public final class GenericAttributesUtils
53 {
54
55
56
57 public static final String CONSTANT_EQUAL = "=";
58
59
60
61
62 public static final int CONSTANT_ID_NULL = -1;
63
64
65
66
67 public static final String CONSTANT_RESPONSE_VALUE_ANONYMIZED = "anonymized";
68 private static final String REGEX_ID = "^[\\d]+$";
69
70
71
72
73 private GenericAttributesUtils( )
74 {
75
76 }
77
78
79
80
81
82
83
84
85
86
87 public static Field findFieldByTitleInTheList( String strTitle, List<Field> listFields )
88 {
89 if ( ( listFields == null ) || listFields.isEmpty( ) )
90 {
91 return null;
92 }
93
94 for ( Field field : listFields )
95 {
96 if ( StringUtils.isNotBlank( strTitle ) )
97 {
98 if ( StringUtils.equals( StringUtils.trim( strTitle ), StringUtils.trim( field.getTitle( ) ) ) )
99 {
100 return field;
101 }
102 }
103 else
104 if ( StringUtils.isBlank( field.getTitle( ) ) )
105 {
106 return field;
107 }
108 }
109
110 return null;
111 }
112
113
114
115
116
117
118
119
120
121
122 public static Field findFieldByIdInTheList( int nIdField, List<Field> listField )
123 {
124 for ( Field field : listField )
125 {
126 if ( field.getIdField( ) == nIdField )
127 {
128 return field;
129 }
130 }
131
132 return null;
133 }
134
135
136
137
138
139
140 public static Plugin getPlugin( )
141 {
142 return PluginService.getPlugin( GenericAttributesPlugin.PLUGIN_NAME );
143 }
144
145
146
147
148
149
150
151
152 public static int convertStringToInt( String strParameter )
153 {
154 int nIdParameter = -1;
155
156 try
157 {
158 if ( ( strParameter != null ) && strParameter.matches( REGEX_ID ) )
159 {
160 nIdParameter = Integer.parseInt( strParameter );
161 }
162 }
163 catch( NumberFormatException ne )
164 {
165 AppLogService.error( ne );
166 }
167
168 return nIdParameter;
169 }
170
171
172
173
174
175
176
177
178
179
180 public static Field createOrUpdateField( Entry entry, String strCode, String strTitle, String strValue )
181 {
182 Field field = entry.getFieldByCode( strCode );
183 if ( field == null )
184 {
185 field = new Field( );
186 field.setCode( strCode );
187 field.setParentEntry( entry );
188 entry.getFields( ).add( field );
189 }
190 field.setTitle( strTitle );
191 field.setValue( strValue );
192 return field;
193 }
194
195
196
197
198
199
200
201
202 public static Field createFieldFromReferenceItem( Entry entry, ReferenceItem item )
203 {
204 Fieldins/genericattributes/business/Field.html#Field">Field field = new Field( );
205 field.setCode( IEntryTypeService.FIELD_ANSWER_CHOICE );
206 field.setValue( item.getCode( ) );
207 field.setTitle( item.getName( ) );
208 field.setDefaultValue( false );
209 field.setParentEntry( entry );
210 field.setLinkedItem( item );
211
212 return field;
213 }
214 }