View Javadoc

1   /**
2    * Copyright 2005-2007 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 org.figure8.join.core.EntityObject;
18  
19  import java.util.Date;
20  import java.util.List;
21  import java.util.ArrayList;
22  /**
23   * This is an entity representing a mapping between a logical environment
24   * (attached to a specific process step and release) and a physical environment
25   * (attached to infrastructure resources and machines).
26   * <br/>
27   * EnvironmentMappings are by definition volatile : they have a <code>startDate</code>
28   * and an <code>endDate</code> when mapping ends. Active mappings are those having
29   * endDate not set.
30   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
31   * @version $Revision: 1.4 $
32   *
33   * @see org.figure8.join.businessobjects.environment.LogicalEnvironment
34   * @see org.figure8.join.businessobjects.environment.PhysicalEnvironment
35   *
36   * @hibernate.class table="join_envmappings" lazy="true"
37   * @hibernate.cache usage="read-write"
38   *
39   * @hibernate.query name="join.envmapping_findByLogicalEnvironment"
40   *    query="from EnvironmentMapping mapping left join fetch mapping.physicalEnvironment
41   *                where mapping.logicalEnvironment = :logicalEnv order by mapping.startDate desc"
42   * @hibernate.query name="join.envmapping_findByDateAndLogicalEnvironment"
43   *    query="from EnvironmentMapping mapping left join fetch mapping.physicalEnvironment
44   *                where mapping.startDate <= :date and (mapping.endDate >= :date or mapping.endDate is null)
45   *                   and mapping.logicalEnvironment = :logicalEnv order by mapping.startDate desc"
46   * @hibernate.query name="join.envmapping_findByPhysicalEnvironment"
47   *    query="from EnvironmentMapping mapping left join fetch mapping.logicalEnvironment
48   *                where mapping.physicalEnvironment = :physicalEnv order by mapping.startDate desc"
49   */
50  public class EnvironmentMapping extends EntityObject{
51  
52     // Attributes ---------------------------------------------------------------
53  
54     /** The date this mapping has started being active */
55     private Date startDate;
56     /** The date this mapping has stopped being active */
57     private Date endDate;
58     /** The deployments done during this mapping activity */
59     private List deployments = new ArrayList();
60  
61     /** The logical environment this mapping is for */
62     private LogicalEnvironment logicalEnvironment;
63     /** The physical environment this mapping is for */
64     private PhysicalEnvironment physicalEnvironment;
65  
66  
67     // Constructors -------------------------------------------------------------
68  
69     /** Creates a new EnvironmentMapping. */
70     public EnvironmentMapping(){
71     }
72  
73     /**
74      * Creates a new EnvironmentMapping with mandatory parameters.
75      * @param logicalEnv The logical environment this mapping is for
76      * @param physicalEnv The physical environment this mapping is for
77      */
78     public EnvironmentMapping(LogicalEnvironment logicalEnv, PhysicalEnvironment physicalEnv){
79        this.logicalEnvironment = logicalEnv;
80        this.physicalEnvironment = physicalEnv;
81        // Set the start date as current date.
82        startDate = new Date();
83     }
84  
85  
86     // Public -------------------------------------------------------------------
87  
88     /**
89      * @hibernate.property column="d_start"
90      *    not-null="true" type="timestamp"
91      *    update="false"
92      * @return The date this mapping has started being active
93      */
94     public Date getStartDate(){
95        return startDate;
96     }
97     /** @param startDate The date this mapping has started being active */
98     public void setStartDate(Date startDate){
99        this.startDate = startDate;
100    }
101 
102    /**
103     * @hibernate.property column="d_end" type="timestamp"
104     * @return The date this mapping has stopped being active
105     */
106    public Date getEndDate(){
107       return endDate;
108    }
109    /** @param endDate The date this mapping has stopped being active */
110    public void setEndDate(Date endDate){
111       this.endDate = endDate;
112    }
113 
114    /**
115     * @hibernate.bag cascade="save-update" lazy="true" order-by="d_creation desc" inverse="true"
116     * @hibernate.collection-key column="n_envmapping_fk"
117     * @hibernate.collection-one-to-many class="org.figure8.join.businessobjects.environment.Deployment"
118     * @return A set of {@code Deployments}s done during mapping activity
119     */
120    public List getDeployments(){
121       return deployments;
122    }
123    /** @param deployments The set of {@code Deployment}s done during mapping activity*/
124    public void setDeployments(List deployments){
125       this.deployments = deployments;
126    }
127 
128    /**
129     * Convenient method for adding a deployment to this mapping
130     * and managing the 2 sides of the bidirectionnal relationship.
131     * @param deployment The deployment to add on this
132     */
133    public void addDeployment(Deployment deployment){
134       deployments.add(0, deployment);
135       deployment.setEnvironmentMapping(this);
136    }
137 
138    /**
139     * Convenient method for getting the last deployment done with this mapping.
140     * @return The last deployment that has been completed for this mapping.
141     */
142    public Deployment getLastDeployment(){
143       for (int i=0; i<deployments.size(); i++){
144          Deployment deployment = (Deployment)deployments.get(i);
145          if (deployment.completed())
146             return deployment;
147       }
148       return null;
149    }
150 
151    /**
152     * @hibernate.many-to-one column="n_logicalenv_fk" not-null="true"
153     * @return The logical environment this mapping is for
154     */
155    public LogicalEnvironment getLogicalEnvironment(){
156       return logicalEnvironment;
157    }
158    /** @param logicalEnvironment The logical environment this mapping is for */
159    public void setLogicalEnvironment(LogicalEnvironment logicalEnvironment){
160       this.logicalEnvironment = logicalEnvironment;
161    }
162 
163    /**
164     * @hibernate.many-to-one column="n_physicalenv_fk" not-null="true"
165     * @return The physical environment this mapping is for
166     */
167    public PhysicalEnvironment getPhysicalEnvironment(){
168       return physicalEnvironment;
169    }
170    /** @param physicalEnvironment The physical environment this mapping is for */
171    public void setPhysicalEnvironment(PhysicalEnvironment physicalEnvironment){
172       this.physicalEnvironment = physicalEnvironment;
173    }
174 
175    /** @return Flag telling if this mapping is currently active */
176    public boolean isActive(){
177       return (endDate == null);
178    }
179 
180    /** Close this mapping (ie. make it inactive) */
181    public void close(){
182       // Remove current mapping from active ones.
183       logicalEnvironment.getActiveEnvironmentMappings().remove(this);
184       physicalEnvironment.getActiveEnvironmentMappings().remove(this);
185       endDate = new Date();
186    }
187 }