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.businessobjects.environment;
16  
17  import java.util.List;
18  import java.util.ArrayList;
19  /**
20   * A Resource implementation that is updatable an in consequence may
21   * have different versions.
22   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
23   * @version $Revision: 1.3 $
24   *
25   * @see org.figure8.join.businessobjects.environment.ResourceVersion
26   *
27   * @hibernate.subclass table="join_resources" discriminator-value="VERSIONED"
28   */
29  public class VersionedResource extends AbstractResource{
30  
31     // Static -------------------------------------------------------------------
32  
33     /** The category information of the VersionedResource implementation of AbstractResource */
34     public static final String CATEGORY = "VersionedResource";
35  
36  
37     // Attributes ---------------------------------------------------------------
38  
39     /** The list of active version updates (should be only one) */
40     private List activeVersionUpdates = new ArrayList();
41  
42  
43     // Constructors -------------------------------------------------------------
44  
45     /** Creates a new VersionedResource instance */
46     public VersionedResource(){
47        super();
48     }
49  
50     /**
51      * Creates a new VersionedResource with mandatory attributes.
52      * @param name The name of this new Resource
53      * @param type The type of this new versioned resource.
54      */
55     public VersionedResource(String name, VersionedResourceType type){
56        super(name, type);
57     }
58     
59     
60     // Public -------------------------------------------------------------------
61  
62     /**
63      * @hibernate.bag cascade="save-update" lazy="true" inverse="true"
64      *    where="d_end is null" order-by="d_start desc"
65      * @hibernate.collection-key column="n_resource_fk"
66      * @hibernate.collection-one-to-many class="org.figure8.join.businessobjects.environment.VersionedResourceUpdate"
67      * @return A set of {@code VersionedResourceUpdate}s that are active now
68      */
69     public List getActiveVersionUpdates(){
70        return activeVersionUpdates;
71     }
72     /** @param mappings The set of {@code EnvironmentMapping}s that are active now */
73     public void setActiveVersionUpdates(List mappings){
74        this.activeVersionUpdates = mappings;
75     }
76  
77     /**
78      * Get the most recent active VersionedResourceUpdate for this resource
79      * @return The last update that has been created
80      */
81     public VersionedResourceUpdate getActiveVersionUpdate(){
82        // Peek the first mapping into list (hope there's only one).
83        List actives = getActiveVersionUpdates();
84        if (actives.isEmpty()) return null;
85        // Get the first element...
86        return (VersionedResourceUpdate)actives.get(0);
87     }
88     /** @param update The VersionResourceUpdate to set as the active one */
89     public void setActiveVersionUpdate(VersionedResourceUpdate update){
90        closeActiveResourceUpdates();
91        activeVersionUpdates.add(update);
92     }
93  
94  
95     // Implementation of AbstractResource ---------------------------------------
96  
97     /**
98      * Return the category information on this resource.
99      * @return The category information as character string
100     */
101    public String getCategory(){
102       return CATEGORY;
103    }
104 
105 
106    // Private ------------------------------------------------------------------
107 
108    /** Close the currently active resource updates */
109    private void closeActiveResourceUpdates(){
110       // Remove all current active updates.
111       while (!activeVersionUpdates.isEmpty() && activeVersionUpdates.get(0) != null){
112          VersionedResourceUpdate update = (VersionedResourceUpdate)activeVersionUpdates.remove(0);
113          update.close();
114       }
115    }
116 }
117