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.persistence;
16  
17  import org.figure8.join.core.persistence.HibernateObjectDao;
18  import org.figure8.join.core.InfrastructureException;
19  import org.figure8.join.businessobjects.commons.Status;
20  import org.figure8.join.businessobjects.environment.Deployment;
21  import org.figure8.join.businessobjects.environment.LogicalEnvironment;
22  import org.figure8.join.businessobjects.environment.PhysicalEnvironment;
23  import org.figure8.join.businessobjects.environment.EnvironmentMapping;
24  import org.figure8.join.util.LogUtil;
25  
26  import org.apache.commons.logging.Log;
27  
28  import java.util.List;
29  /**
30   * Implementation of DeploymentDao using Hibernate ORM system.
31   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
32   * @version $Revision: 1.2 $
33   */
34  public class HibernateDeploymentDao extends HibernateObjectDao implements DeploymentDao{
35  
36     // Static -------------------------------------------------------------------
37  
38     /** Get a commons logger */
39     private static final Log log = LogUtil.getLog(HibernateDeploymentDao.class);
40  
41  
42     // Constructors -------------------------------------------------------------
43  
44     /** Creates a new instance of HibernateDeploymentDao */
45     public HibernateDeploymentDao(){
46     }
47  
48  
49     // Implementation of DeploymentDao ------------------------------------------
50  
51     /**
52      * Retrieve a deployment specified by its identifier
53      * @param id The unique identifier of deployment to retrieve
54      * @return The deployment corresponding to id or null if no deployment has this is
55      */
56     public Deployment getDeployment(long id){
57        return (Deployment)getById(id);
58     }
59  
60     /**
61      * Retrieve the currently active deployment done using an EnvironmentMapping
62      * @param mapping The mapping to get last active deployment for
63      * @return The active deployment corresponding to mapping, or null if none.
64      */
65     public Deployment getActiveDeploymentForMapping(EnvironmentMapping mapping){
66        Deployment lastActive = (Deployment)findSingleObject(findNamedQueryStringParam(
67                "join.deployment_findActivesForEnvironmentMapping", "envMapping", mapping, false, 1));
68        if (log.isDebugEnabled())
69           log.debug("Found an active deployment for EnvironmentMapping " + mapping.getId() + " ? " + (lastActive != null ? "true" : "false"));
70        return lastActive;
71     }
72  
73     /**
74      * Get all the deployments being realized at time of invocation
75      * @return A list of {@link org.figure8.join.businessobjects.environment.Deployment}s
76      */
77     public List getRealizingDeployments(){
78        List result = findNamedQuery("join.deployment_findRealizing");
79        if (log.isDebugEnabled())
80           log.debug("Found " + result.size() + " deployment(s) being realized now");
81        return result;
82     }
83  
84     /**
85      * Get all the deployments being prepared at time of invocation
86      * @return A list of {@link org.figure8.join.businessobjects.environment.Deployment}s
87      */
88     public List getPreparingDeployments(){
89        List result = findNamedQuery("join.deployment_findPreparing");
90        if (log.isDebugEnabled())
91           log.debug("Found " + result.size() + " deployment(s) being prepared now");
92        return result;
93     }
94  
95     /**
96      * Get all the deployments having one of the specified status
97      * @param status A list of specified status
98      * @param includeNull Flag telling if deployment with no status should be included
99      * @return A list of {@link org.figure8.join.businessobjects.environment.Deployment}s
100     */
101    public List getDeploymentsHavingStatus(List status, boolean includeNull){
102       List result = null;
103       try{
104          StringBuffer whereclause = new StringBuffer();
105          // Build a In clause if we have status !
106          if (status != null && !status.isEmpty()){
107             StringBuffer inclause = new StringBuffer("(");
108             for (int i=0; i<status.size(); i++){
109                inclause.append(((Status)status.get(i)).getId());
110                if (i<status.size() - 1) inclause.append(", ");
111             }
112             inclause.append(")");
113             // Use Hibernate template to create a custom SQL query with in clause.
114             whereclause.append("dep.status.id in ").append(inclause);
115          }
116          // Eventually complete where clause.
117          if (includeNull){
118             if (whereclause.length() == 0) whereclause.append("dep.status is null");
119             if (whereclause.length() > 0) whereclause.append(" or dep.status is null");
120          }
121          result = getHibernateTemplate().find("from Deployment dep where " + whereclause);
122       }
123       catch (Exception e){
124          // Log and wrap into runtime infrastructure exception.
125          log.error("Exception into getDeploymentsHavingStatus: " + e.getMessage());
126          throw new InfrastructureException("Exception while retrieving deployments with one of specified status", e);
127       }
128       if (log.isDebugEnabled())
129          log.debug("Found " + result.size() + " deployment(s) for specified status: " + status);
130       return result;
131    }
132 
133    /**
134     * Retrieve all the deployments done for a specified logical environment
135     * @param logicalEnv The logical environment to get deployments for
136     * @return A list of {@link org.figure8.join.businessobjects.environment.Deployment}s done for logical environment
137     */
138    public List getDeploymentsByLogicalEnvironment(LogicalEnvironment logicalEnv){
139       List result = findNamedQueryStringParam("join.deployment_findByLogicalEnvironment", "logicalEnv", logicalEnv);
140       if (log.isDebugEnabled())
141          log.debug("Found " + result.size() + " deployments for logical env: " + logicalEnv.getKey());
142       return result;
143    }
144 
145    /**
146     * Retrieve all the deployments done onto a specified physical environment
147     * @param physicalEnv The physical environment to get deployments for
148     * @return A list of {@link org.figure8.join.businessobjects.environment.Deployment}s done onto this physical environemnt
149     */
150    public List getDeploymentsByPhysicalEnvironment(PhysicalEnvironment physicalEnv){
151       List result = findNamedQueryStringParam("join.deployment_findByPhysicalEnvironment", "physicalEnv", physicalEnv);
152       if (log.isDebugEnabled())
153          log.debug("Found " + result.size() + " deployments for physical env: " + physicalEnv.getKey());
154       return result;
155    }
156 
157 
158    // Implementation of ObjectDao ----------------------------------------------
159 
160    /** @return The org.figure8.join.businessobjects.environment.Deployment class */
161    public Class getPersistentClass(){
162       return org.figure8.join.businessobjects.environment.Deployment.class;
163    }
164 }