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