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