1 package org.apache.continuum.profile;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import javax.annotation.Resource;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.continuum.dao.ProfileDao;
29 import org.apache.maven.continuum.installation.InstallationService;
30 import org.apache.maven.continuum.model.system.Installation;
31 import org.apache.maven.continuum.model.system.Profile;
32 import org.apache.maven.continuum.profile.AlreadyExistsProfileException;
33 import org.apache.maven.continuum.profile.ProfileException;
34 import org.apache.maven.continuum.profile.ProfileService;
35 import org.apache.maven.continuum.store.ContinuumObjectNotFoundException;
36 import org.apache.maven.continuum.store.ContinuumStoreException;
37 import org.springframework.stereotype.Service;
38
39
40
41
42
43
44
45 @Service("profileService")
46 public class DefaultProfileService
47 implements ProfileService
48 {
49 @Resource
50 private ProfileDao profileDao;
51
52
53
54
55 public void updateProfile( Profile profile )
56 throws ProfileException, AlreadyExistsProfileException
57 {
58
59
60
61
62
63
64
65
66
67 try
68 {
69 Profile stored = getProfile( profile.getId() );
70 stored.setActive( profile.isActive() );
71 stored.setBuilder( profile.getBuilder() );
72 stored.setBuildWithoutChanges( profile.isBuildWithoutChanges() );
73 stored.setDescription( profile.getDescription() );
74 stored.setJdk( profile.getJdk() );
75 stored.setName( profile.getName() );
76 stored.setEnvironmentVariables( profile.getEnvironmentVariables() );
77 stored.setBuildAgentGroup( profile.getBuildAgentGroup() );
78 profileDao.updateProfile( stored );
79 }
80 catch ( ContinuumStoreException e )
81 {
82 throw new ProfileException( e.getMessage(), e );
83 }
84 }
85
86 public void updateProfileCheckDuplicateName( Profile profile, boolean checkDuplicateName )
87 throws ProfileException, AlreadyExistsProfileException
88 {
89 if ( checkDuplicateName )
90 {
91
92
93 if ( alreadyExistsProfileName( profile ) )
94 {
95 throw new AlreadyExistsProfileException( "profile with name " + profile.getName() + " already exists" );
96 }
97 }
98 try
99 {
100 Profile stored = getProfile( profile.getId() );
101 stored.setActive( profile.isActive() );
102 stored.setBuilder( profile.getBuilder() );
103 stored.setBuildWithoutChanges( profile.isBuildWithoutChanges() );
104 stored.setDescription( profile.getDescription() );
105 stored.setJdk( profile.getJdk() );
106 stored.setName( profile.getName() );
107 stored.setEnvironmentVariables( profile.getEnvironmentVariables() );
108 stored.setBuildAgentGroup( profile.getBuildAgentGroup() );
109 profileDao.updateProfile( stored );
110 }
111 catch ( ContinuumStoreException e )
112 {
113 throw new ProfileException( e.getMessage(), e );
114 }
115 }
116
117
118
119
120 public Profile addProfile( Profile profile )
121 throws ProfileException, AlreadyExistsProfileException
122 {
123
124
125 if ( alreadyExistsProfileName( profile ) )
126 {
127 throw new AlreadyExistsProfileException( "profile with name " + profile.getName() + " already exists" );
128 }
129 profile.setBuilder( null );
130 profile.setJdk( null );
131 profile.setEnvironmentVariables( null );
132 return profileDao.addProfile( profile );
133 }
134
135
136
137
138 public void deleteProfile( int profileId )
139 throws ProfileException
140 {
141 try
142 {
143 profileDao.removeProfile( getProfile( profileId ) );
144 }
145 catch ( Exception e )
146 {
147 throw new ProfileException( "Cannot remove the profile", e );
148 }
149 }
150
151
152
153
154 public List<Profile> getAllProfiles()
155 throws ProfileException
156 {
157 return profileDao.getAllProfilesByName();
158 }
159
160
161
162
163 public Profile getProfile( int profileId )
164 throws ProfileException
165 {
166 try
167 {
168 return profileDao.getProfile( profileId );
169 }
170 catch ( ContinuumObjectNotFoundException e )
171 {
172
173 return null;
174 }
175 catch ( ContinuumStoreException e )
176 {
177 throw new ProfileException( e.getMessage(), e );
178 }
179 }
180
181
182
183
184 public void setBuilderInProfile( Profile profile, Installation builder )
185 throws ProfileException
186 {
187 Profile stored = getProfile( profile.getId() );
188 stored.setBuilder( builder );
189 try
190 {
191 profileDao.updateProfile( stored );
192 }
193 catch ( ContinuumStoreException e )
194 {
195 throw new ProfileException( e.getMessage(), e );
196 }
197 }
198
199
200
201
202 public void setJdkInProfile( Profile profile, Installation jdk )
203 throws ProfileException
204 {
205 Profile stored = getProfile( profile.getId() );
206 stored.setJdk( jdk );
207 try
208 {
209 profileDao.updateProfile( stored );
210 }
211 catch ( ContinuumStoreException e )
212 {
213 throw new ProfileException( e.getMessage(), e );
214 }
215 }
216
217
218
219
220 public void addEnvVarInProfile( Profile profile, Installation envVar )
221 throws ProfileException
222 {
223 Profile stored = getProfile( profile.getId() );
224 stored.addEnvironmentVariable( envVar );
225 try
226 {
227 profileDao.updateProfile( stored );
228 }
229 catch ( ContinuumStoreException e )
230 {
231 throw new ProfileException( e.getMessage(), e );
232 }
233 }
234
235 public void addInstallationInProfile( Profile profile, Installation installation )
236 throws ProfileException
237 {
238 if ( InstallationService.JDK_TYPE.equals( installation.getType() ) )
239 {
240 setJdkInProfile( profile, installation );
241 }
242 else if ( InstallationService.MAVEN1_TYPE.equals( installation.getType() ) ||
243 InstallationService.MAVEN2_TYPE.equals( installation.getType() ) ||
244 InstallationService.ANT_TYPE.equals( installation.getType() ) )
245 {
246 setBuilderInProfile( profile, installation );
247 }
248 else
249 {
250 addEnvVarInProfile( profile, installation );
251 }
252
253 }
254
255 public void removeInstallationFromProfile( Profile profile, Installation installation )
256 throws ProfileException
257 {
258 Profile stored = getProfile( profile.getId() );
259 if ( InstallationService.JDK_TYPE.equals( installation.getType() ) )
260 {
261 stored.setJdk( null );
262 }
263 else if ( InstallationService.MAVEN1_TYPE.equals( installation.getType() ) ||
264 InstallationService.MAVEN2_TYPE.equals( installation.getType() ) ||
265 InstallationService.ANT_TYPE.equals( installation.getType() ) )
266 {
267 stored.setBuilder( null );
268 }
269 else
270 {
271
272 List<Installation> storedEnvVars = stored.getEnvironmentVariables();
273 List<Installation> newEnvVars = new ArrayList<Installation>();
274 for ( Installation storedInstallation : storedEnvVars )
275 {
276 if ( !StringUtils.equals( storedInstallation.getName(), installation.getName() ) )
277 {
278 newEnvVars.add( storedInstallation );
279 }
280 }
281 stored.setEnvironmentVariables( newEnvVars );
282 }
283 try
284 {
285 updateProfileCheckDuplicateName( stored, false );
286 }
287 catch ( AlreadyExistsProfileException e )
288 {
289
290 throw new ProfileException( e.getMessage(), e );
291 }
292 }
293
294
295 public Profile getProfileWithName( String profileName )
296 throws ProfileException
297 {
298 List<Profile> allProfiles = getAllProfiles();
299 for ( Profile profile : allProfiles )
300 {
301 if ( StringUtils.equals( profile.getName(), profileName ) )
302 {
303 return profile;
304 }
305 }
306 return null;
307 }
308
309
310
311
312
313
314 public boolean alreadyExistsProfileName( Profile profile )
315 throws ProfileException
316 {
317 return getProfileWithName( profile.getName() ) != null;
318 }
319
320 }