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.plugins.crm.service.user;
35
36 import fr.paris.lutece.plugins.crm.business.user.CRMUser;
37 import fr.paris.lutece.plugins.crm.business.user.CRMUserFilter;
38 import fr.paris.lutece.plugins.crm.business.user.CRMUserHome;
39 import fr.paris.lutece.portal.service.spring.SpringContextService;
40
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.Map.Entry;
44
45 /**
46 *
47 * CRMUserService
48 *
49 */
50 public class CRMUserService
51 {
52 private static final String BEAN_CRM_CRMUSERSERVICE = "crm.crmUserService";
53 private CRMUserAttributesService _crmUserAttributesService;
54
55 /**
56 * Constructor
57 */
58 protected CRMUserService( )
59 {
60 }
61
62 /**
63 * Get the instance of CRMUserService
64 *
65 * @return the instance of CRMUserService
66 */
67 public static CRMUserService getService( )
68 {
69 return SpringContextService.getBean( BEAN_CRM_CRMUSERSERVICE );
70 }
71
72 /**
73 * Set the crm user attributes service
74 *
75 * @param crmUserAttributesService
76 * the crm user attributes service
77 */
78 public void setCRMUserAttributesService( CRMUserAttributesService crmUserAttributesService )
79 {
80 _crmUserAttributesService = crmUserAttributesService;
81 }
82
83 /**
84 * Find by primary key
85 *
86 * @param nIdCRMUser
87 * the id crm user
88 * @return a {@link CRMUser}
89 */
90 public CRMUser findByPrimaryKey( int nIdCRMUser )
91 {
92 CRMUser user = CRMUserHome.findByPrimaryKey( nIdCRMUser );
93 user.setUserAttributes( _crmUserAttributesService.getAttributes( nIdCRMUser ) );
94
95 return user;
96 }
97
98 /**
99 * Find from a given user guid
100 *
101 * @param strUserGuid
102 * the user guid
103 * @return a {@link CRMUser}
104 */
105 public CRMUser findByUserGuid( String strUserGuid )
106 {
107 CRMUser user = CRMUserHome.findByUserGuid( strUserGuid );
108
109 if ( user != null )
110 {
111 user.setUserAttributes( _crmUserAttributesService.getAttributes( user.getIdCRMUser( ) ) );
112 }
113
114 return user;
115 }
116
117 /**
118 * Find by primary key.
119 *
120 * @return a {@link CRMUser}
121 */
122 public List<CRMUser> findAll( )
123 {
124 List<CRMUser> listUsers = CRMUserHome.findAll( );
125
126 for ( CRMUser user : listUsers )
127 {
128 user.setUserAttributes( _crmUserAttributesService.getAttributes( user.getIdCRMUser( ) ) );
129 }
130
131 return listUsers;
132 }
133
134 /**
135 * Find list ids by filter.
136 *
137 * @param filter
138 * the filter
139 * @return the list
140 */
141 public List<Integer> findListIdsByFilter( CRMUserFilter filter )
142 {
143 return CRMUserHome.findListIdsCRMUserByFilter( filter );
144 }
145
146 /**
147 * Find by list ids.
148 *
149 * @param listIdsCRMUser
150 * the list ids crm user
151 * @return the list
152 */
153 public List<CRMUser> findByListIds( List<Integer> listIdsCRMUser )
154 {
155 List<CRMUser> listUsers = new ArrayList<CRMUser>( );
156
157 if ( ( listIdsCRMUser != null ) && !listIdsCRMUser.isEmpty( ) )
158 {
159 listUsers = CRMUserHome.findByListIds( listIdsCRMUser );
160
161 for ( CRMUser user : listUsers )
162 {
163 user.setUserAttributes( _crmUserAttributesService.getAttributes( user.getIdCRMUser( ) ) );
164 }
165 }
166
167 return listUsers;
168 }
169
170 /**
171 * Create a new {@link CRMUser}
172 *
173 * @param user
174 * the {@link CRMUser}
175 * @return the new primary key
176 */
177 public int create( CRMUser user )
178 {
179 int nIdCRMUser = -1;
180
181 if ( user != null )
182 {
183 nIdCRMUser = CRMUserHome.create( user );
184
185 if ( user.getUserAttributes( ) != null )
186 {
187 for ( Entry<String, String> userAttribute : user.getUserAttributes( ).entrySet( ) )
188 {
189 _crmUserAttributesService.create( nIdCRMUser, userAttribute.getKey( ), userAttribute.getValue( ) );
190 }
191 }
192 }
193
194 return nIdCRMUser;
195 }
196
197 /**
198 * Update a {@link CRMUser}
199 *
200 * @param user
201 * the {@link CRMUser}
202 */
203 public void update( CRMUser user )
204 {
205 if ( user != null )
206 {
207 CRMUserHome.update( user );
208 // Remove its attributes first
209 _crmUserAttributesService.remove( user.getIdCRMUser( ) );
210
211 if ( user.getUserAttributes( ) != null )
212 {
213 // Recreate its attributes
214 for ( Entry<String, String> userAttribute : user.getUserAttributes( ).entrySet( ) )
215 {
216 _crmUserAttributesService.create( user.getIdCRMUser( ), userAttribute.getKey( ), userAttribute.getValue( ) );
217 }
218 }
219 }
220 }
221
222 /**
223 * Remove a CRMUser
224 *
225 * @param nIdCRMUser
226 * the id crm user
227 */
228 public void remove( int nIdCRMUser )
229 {
230 CRMUserHome.remove( nIdCRMUser );
231 // Remove its attributes
232 _crmUserAttributesService.remove( nIdCRMUser );
233 }
234 }