1 /*
2 * Copyright (c) 2002-2025, City of 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.service.user.attribute;
35
36 import fr.paris.lutece.portal.business.user.attribute.AttributeField;
37 import fr.paris.lutece.portal.business.user.attribute.AttributeFieldHome;
38 import fr.paris.lutece.portal.business.user.attribute.AttributeHome;
39 import fr.paris.lutece.portal.business.user.attribute.IAttribute;
40
41 import java.util.List;
42 import java.util.Locale;
43
44 /**
45 *
46 * AttributeService
47 *
48 */
49 public final class AttributeService
50 {
51 private static AttributeService _singleton;
52
53 /**
54 * Private constructor
55 */
56 private AttributeService( )
57 {
58 }
59
60 /**
61 * Get the instance of {@link AttributeService}
62 *
63 * @return an instance of {@link AttributeService}
64 */
65 public static synchronized AttributeService getInstance( )
66 {
67 if ( _singleton == null )
68 {
69 _singleton = new AttributeService( );
70 }
71
72 return _singleton;
73 }
74
75 /**
76 * Get an attribute without its attribute fields
77 *
78 * @param nIdAttribute
79 * the id attribute
80 * @param locale
81 * {@link Locale}
82 * @return a {@link IAttribute}
83 */
84 public IAttribute getAttributeWithoutFields( int nIdAttribute, Locale locale )
85 {
86 return AttributeHome.findByPrimaryKey( nIdAttribute, locale );
87 }
88
89 /**
90 * Get the attribute with its attribute fields
91 *
92 * @param nIdAttribute
93 * the id attribute
94 * @param locale
95 * the {@link Locale}
96 * @return a {@link IAttribute}
97 */
98 public IAttribute getAttributeWithFields( int nIdAttribute, Locale locale )
99 {
100 IAttribute attribute = getAttributeWithoutFields( nIdAttribute, locale );
101 setAttributeField( attribute );
102
103 return attribute;
104 }
105
106 /**
107 * Get all user attribute without its attribute fields.
108 *
109 * @param locale
110 * the {@link Locale}
111 * @return a list of {@link IAttribute}
112 */
113 public List<IAttribute> getAllAttributesWithoutFields( Locale locale )
114 {
115 return AttributeHome.findAll( locale );
116 }
117
118 /**
119 * Get core user attribute without its attribute fields
120 *
121 * @param locale
122 * the {@link Locale}
123 * @return a list of {@link IAttribute}
124 */
125 public List<IAttribute> getCoreAttributesWithoutFields( Locale locale )
126 {
127 return AttributeHome.findCoreAttributes( locale );
128 }
129
130 /**
131 * Get plugin user attribute without its attribute fields
132 *
133 * @param strPluginName
134 * the plugin name
135 * @param locale
136 * the {@link Locale}
137 * @return a list of {@link IAttribute}
138 */
139 public List<IAttribute> getPluginAttributesWithoutFields( String strPluginName, Locale locale )
140 {
141 return AttributeHome.findPluginAttributes( strPluginName, locale );
142 }
143
144 /**
145 * Get all user attributes with its attribute fields
146 *
147 * @param locale
148 * the {@link Locale}
149 * @return a list of {@link IAttribute}
150 */
151 public List<IAttribute> getAllAttributesWithFields( Locale locale )
152 {
153 List<IAttribute> listAttributes = getAllAttributesWithoutFields( locale );
154 setAttributeFields( listAttributes );
155
156 return listAttributes;
157 }
158
159 /**
160 * Get core user attributes with its attribute fields
161 *
162 * @param locale
163 * the {@link Locale}
164 * @return a list of {@link IAttribute}
165 */
166 public List<IAttribute> getCoreAttributesWithFields( Locale locale )
167 {
168 List<IAttribute> listAttributes = getCoreAttributesWithoutFields( locale );
169 setAttributeFields( listAttributes );
170
171 return listAttributes;
172 }
173
174 /**
175 * Get plugin user attributes with its attribute fields
176 *
177 * @param strPluginName
178 * the plugin name
179 * @param locale
180 * the {@link Locale}
181 * @return a list of {@link IAttribute}
182 */
183 public List<IAttribute> getPluginAttributesWithFields( String strPluginName, Locale locale )
184 {
185 List<IAttribute> listAttributes = getPluginAttributesWithoutFields( strPluginName, locale );
186 setAttributeFields( listAttributes );
187
188 return listAttributes;
189 }
190
191 /**
192 * Set the attribute fields from a given list of {@link IAttribute}
193 *
194 * @param listAttributes
195 * the list of {@link IAttribute}
196 */
197 public void setAttributeFields( List<IAttribute> listAttributes )
198 {
199 for ( IAttribute attribute : listAttributes )
200 {
201 setAttributeField( attribute );
202 }
203 }
204
205 /**
206 * Set the attribute field from a given {@link IAttribute}
207 *
208 * @param attribute
209 * the {@link IAttribute}
210 */
211 public void setAttributeField( IAttribute attribute )
212 {
213 if ( attribute != null )
214 {
215 List<AttributeField> listAttributeFields = AttributeFieldHome.selectAttributeFieldsByIdAttribute( attribute.getIdAttribute( ) );
216 attribute.setListAttributeFields( listAttributeFields );
217 }
218 }
219
220 /**
221 * Create a new attribute and its attribute field.
222 *
223 * @param attribute
224 * the {@link IAttribute} to create
225 */
226 public void createAttribute( IAttribute attribute )
227 {
228 if ( attribute != null )
229 {
230 int nIdAttribute = AttributeHome.create( attribute );
231 attribute.setIdAttribute( nIdAttribute );
232
233 if ( attribute.getListAttributeFields( ) != null )
234 {
235 for ( AttributeField attributeField : attribute.getListAttributeFields( ) )
236 {
237 attributeField.setAttribute( attribute );
238 AttributeFieldService.getInstance( ).createAttributeField( attributeField );
239 }
240 }
241 }
242 }
243
244 /**
245 * Update the attribute
246 *
247 * @param attribute
248 * the {@link IAttribute} to update
249 */
250 public void updateAttribute( IAttribute attribute )
251 {
252 if ( attribute != null )
253 {
254 AttributeHome.update( attribute );
255
256 if ( attribute.getListAttributeFields( ) != null )
257 {
258 for ( AttributeField attributeField : attribute.getListAttributeFields( ) )
259 {
260 attributeField.setAttribute( attribute );
261 AttributeFieldService.getInstance( ).updateAttributeField( attributeField );
262 }
263 }
264 }
265 }
266
267 /**
268 * Remove the attribute from a given attribute ID
269 *
270 * @param nIdAttribute
271 * the ID attribute
272 */
273 public void removeAttribute( int nIdAttribute )
274 {
275 // Remove the AdminUserField associated to the attribute
276 AdminUserFieldService.doRemoveUserFieldsByIdAttribute( nIdAttribute );
277 // Remove the AttributeField associated to the attribute
278 AttributeFieldService.getInstance( ).removeAttributeFieldsFromIdAttribute( nIdAttribute );
279 // Remove the Attribute
280 AttributeHome.remove( nIdAttribute );
281 }
282
283 /**
284 * Update the anonymization status of the attribute.
285 *
286 * @param nIdAttribute
287 * Id of the attribute
288 * @param bAnonymize
289 * New value of the anonymization status. True means the attribute should be anonymize, false means it doesn't.
290 */
291 public void updateAnonymizationStatusUserField( int nIdAttribute, boolean bAnonymize )
292 {
293 AttributeHome.updateAttributeAnonymization( nIdAttribute, bAnonymize );
294 }
295 }