View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.businessfacades.artifact;
16  
17  import org.figure8.join.core.DuplicateEntityException;
18  import org.figure8.join.businessobjects.commons.Release;
19  import org.figure8.join.businessobjects.artifact.Build;
20  import org.figure8.join.businessobjects.artifact.Assembly;
21  import org.figure8.join.businessobjects.artifact.Component;
22  import org.figure8.join.businessobjects.artifact.Deliverable;
23  import org.figure8.join.businessobjects.artifact.ComponentType;
24  import org.figure8.join.businessobjects.artifact.persistence.BuildDao;
25  import org.figure8.join.businessobjects.artifact.persistence.AssemblyDao;
26  import org.figure8.join.businessfacades.reporting.EventPublicationManager;
27  import org.figure8.join.util.LogUtil;
28  
29  import org.apache.commons.logging.Log;
30  
31  import java.util.Map;
32  import java.util.List;
33  import java.util.Iterator;
34  /**
35   * This is the default implementation of {@link AssemblyManager}
36   * @author  <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
37   * @version $Revision: 1.3 $
38   */
39  public class DefaultAssemblyManager implements AssemblyManager{
40  
41     // Static -------------------------------------------------------------------
42  
43     /** Get a commons logger. */
44     private static final Log log = LogUtil.getLog(DefaultAssemblyManager.class);
45  
46  
47     // Attributes ---------------------------------------------------------------
48  
49     /** Build data access object */
50     protected BuildDao buildDao = null;
51     /** Assembly data access object */
52     protected AssemblyDao assemblyDao = null;
53     /** The ArtifactManager to use */
54     protected ArtifactManager artifactManager = null;
55     /** The EventPublicationManager to use */
56     protected EventPublicationManager publicationManager = null;
57  
58  
59     // Constructors -------------------------------------------------------------
60  
61     /** Creates a new instance of DefaultAssemblyManager */
62     public DefaultAssemblyManager(){
63     }
64  
65  
66     // Public -------------------------------------------------------------------
67  
68     /** @param buildDao A Build data access object */
69     public void setBuildDao(BuildDao buildDao){
70        this.buildDao = buildDao;
71     }
72     /** @param assemblyDao A Assembly data access object */
73     public void setAssemblyDao(AssemblyDao assemblyDao){
74        this.assemblyDao = assemblyDao;
75     }
76     /** @param artifactManager The ArtifactManager implementation to use */
77     public void setArtifactManager(ArtifactManager artifactManager){
78        this.artifactManager = artifactManager;
79     }
80     /** @param publicationManager The EventPublicationManager implementation to use */
81     public void setEventPublicationManager(EventPublicationManager publicationManager){
82        this.publicationManager = publicationManager;
83     }
84  
85  
86     // Implementation of AssemblyManager ----------------------------------------
87  
88     /**
89      * Save a specified assembly (indeed a save or update method)
90      * @param assembly The assembly to save within datastore
91      * @param deliverables A list of deliverables composing the assembly to create
92      * @throws DuplicateEntityException if another assembly with same deliverables already exists
93      */
94     public void saveAssembly(Assembly assembly, List deliverables) throws DuplicateEntityException{
95        if (log.isInfoEnabled())
96           log.info("Creating a new Assembly with key '" + assembly.getKey() + "'");
97  
98        // Set a flag telling if it's a new assembly.
99        boolean newAssembly = assembly.isTransient();
100 
101       if (newAssembly){
102          // If new one, check that there's no other assembly with this key.
103          Assembly other = assemblyDao.getAssembly(assembly.getKey());
104          if (other != null){
105             log.error("An Assembly with same key already exists: " + other.getKey());
106             throw new DuplicateEntityException("An Assembly with same key already exists", other);
107          }
108          // Ensure no other assembly with same deliverables exists too.
109          List others = assemblyDao.getAssembliesWithDeliverables(deliverables);
110          if (others != null && !others.isEmpty() && !others.contains(assembly)){
111             log.error("An Assembly with same deliverables already exists: " + ((Assembly)others.get(0)).getKey());
112             throw new DuplicateEntityException("An Assembly with same deliverables already exists", (Assembly)others.get(0));
113          }
114          // Add deliverables to assembly and call save on dao.
115          assembly.getDeliverables().clear();
116          for (int i=0; i<deliverables.size(); i++){
117             Deliverable deliverable = (Deliverable)deliverables.get(i);
118             assembly.addDeliverable(deliverable);
119          }
120       }
121       // Call save() on dao.
122       assemblyDao.save(assembly);
123       // Publish a message if it's a new assembly.
124       if (newAssembly)
125          publicationManager.publishAssemblyCreationEvent(assembly);
126    }
127    
128    /**
129     * Retrieve all assemblies created for specified release
130     * @param release The release to get assemblies for
131     * @return A list of {@code org.figure8.join.businessobjects.artifact.Assembly} objects
132     */
133    public List getAssemblies(Release release){
134       return assemblyDao.getAssembliesByRelease(release);
135    }
136    
137    /**
138     * Retrieve an Assembly using its unique key
139     * @param key The unique key of assembly to retrieve
140     * @return The corresponding assembly or null if no assembly has this key
141     */
142    public Assembly getAssembly(String key){
143       return assemblyDao.getAssembly(key);
144    }
145 
146    /**
147     * Save a Build for a specified release (indeed a create or update method).
148     * @param build The build to save within datastore.
149     * @param components A list of Components composing the build to create
150     * @param extractedComponents A map representing components to extract from an assembly.
151     *    Key of map entry is a component type and value is the assembly to extract this component from.
152     * @throws DuplicateEntityException if another build with same component already exists
153     */
154    public void saveBuild(Build build, List components, Map extractedComponents) throws DuplicateEntityException{
155       if (log.isInfoEnabled())
156          log.info("Saving the Build with key '" + build.getKey() + "'");
157 
158       // Set a flag telling if it's a new build.
159       boolean newBuild = build.isTransient();
160 
161       // Only save components for new builds.
162       if (newBuild){
163          // Check that no other build with same key already exist.
164          Build other = buildDao.getBuild(build.getKey());
165          if (other != null){
166             log.error("A Build with same key already exists: " + other.getKey());
167             throw new DuplicateEntityException("A Build with same key already exists", other);
168          }
169 
170          // Deal with components that may be extacted.
171          if (extractedComponents != null){
172             Iterator types = extractedComponents.keySet().iterator();
173             while (types != null && types.hasNext()){
174                ComponentType type = (ComponentType)types.next();
175                Assembly assembly = (Assembly)extractedComponents.get(type);
176                // Sanity check...
177                if (assembly != null){
178                   // Get this component from assembly if existing...
179                   Component component = assembly.getComponent(type);
180                   if (component == null){
181                      // .. or create and bind a new one.
182                      if (log.isDebugEnabled())
183                         log.debug("Creating a new component of type '"
184                                 + type.getLabel() + "', extracted from " + assembly.getKey());
185                      component = new Component(assembly.getKey(), -1, type);
186                      artifactManager.bindComponent(component, assembly);
187                   }
188                   components.add(component);
189                }
190             }
191          }
192          // Ensure no other build with same deliverables exists too.
193          List others = buildDao.getBuildsWithComponents(components);
194          if (others != null && !others.isEmpty() && !others.contains(build)){
195             log.error("A Build with same components already exists: " + ((Build)others.get(0)).getKey());
196             throw new DuplicateEntityException("A Build with same component already exists", (Build)others.get(0));
197          }
198 
199          // Add components to build and call save on dao.
200          build.getComponents().clear();
201          for (int i=0; i<components.size(); i++){
202             Component component = (Component)components.get(i);
203             build.addComponent(component);
204          }
205       }
206       // Call save() on dao and publish a message.
207       buildDao.save(build);
208       // Publish a message if it's a new build.
209       if (newBuild)
210          publicationManager.publishBuildCreationEvent(build);
211    }
212    
213    /**
214     * Retrieve all builds created for specified release
215     * @param release The release to get builds for
216     * @return A list of {@code org.figure8.join.businessobjects.artifact.Build} objects
217     */
218    public List getBuilds(Release release){
219       // Delegate call to dao.
220       return buildDao.getBuildsByRelease(release);
221    }
222    
223    /**
224     * Retrieve a Build using its unique key
225     * @param key The unique key of build to retrieve
226     * @return The corresponding build or null if no build has this key
227     */
228    public Build getBuild(String key){
229       // Delegate call to dao.
230       return buildDao.getBuild(key);
231    }
232 }