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.reporting;
16  
17  import org.figure8.join.core.EntityObject;
18  import org.figure8.join.core.InvalidParameterException;
19  import org.figure8.join.services.security.PermissionResourceResolver;
20  import org.figure8.join.util.LogUtil;
21  
22  import org.apache.commons.logging.Log;
23  /**
24   * This is an entity object that helps defining events into Join workflow.
25   * An event is thrown at defined moments of the process, it can be used to
26   * create information messages using the templating facilities it provides.
27   * @author  <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
28   * @version $Revision: 1.2 $
29   *
30   * @hibernate.class table="join_events"
31   * @hibernate.cache usage="read-write"
32   *
33   * @hibernate.query name="join.event_findByKey" query="from Event evt where evt.key = :evtKey"
34   */
35  public class Event extends EntityObject{
36  
37     // Static -------------------------------------------------------------------
38  
39     /** Get a commons logger. */
40     private static final Log log = LogUtil.getLog(Event.class);
41  
42     /** Constant denoting the key of built-in delivery creation event */
43     public static final String DELIVERY_EVENT_KEY = "delivery";
44     /** Constant denoting the key of built-in build creation event */
45     public static final String BUILD_EVENT_KEY = "buildCreation";
46     /** Constant denoting the key of built-in assembly creationg event */
47     public static final String ASSEMBLY_EVENT_KEY = "assemblyCreation";
48     /** Constant denoting the key of built-in deployment demand event */
49     public static final String DEPLOYMENT_DEMAND_KEY = "deploymentDemand";
50     /** Constant denoting the key of built-in deployment preparation event */
51     public static final String DEPLOYMENT_PREPARATION_KEY = "deploymentPreparation";
52     /** Constant denoting the key of built-in deployment realization event */
53     public static final String DEPLOYMENT_REALIZATION_KEY = "deploymentRealization";
54     /** Constant denoting the key of built-il deployment end event */
55     public static final String DEPLOYMENT_END_KEY = "deploymentEnd";
56  
57  
58     // Attributes ---------------------------------------------------------------
59  
60     /** The unique key for this event */
61     private String key;
62     /** The label to display for this event */
63     private String label;
64     /** A string representing template for generating messages title */
65     private String msgTitleTemplate;
66     /** A string representing template for generating messages content */
67     private String msgContentTemplate;
68     /** A string representing template for generating messages link */
69     private String msgLinkTemplate;
70     /** Java class used to resolve resource subject to event publication */
71     private String resourceResolverClass = null;
72  
73     /** The resolver instance of type <b>resourceResolverClass</b> */
74     private PermissionResourceResolver resourceResolver = null;
75  
76  
77     // Constructors -------------------------------------------------------------
78     
79     /** Creates a new instance of Event */
80     public Event(){
81     }
82  
83     /**
84      * Creates a new instance of Event with mandatory attributes
85      * @param key The unique of this event
86      * @param label The label to display for this event
87      */
88     public Event(String key, String label){
89        this.key = key;
90        this.label = label;
91     }
92  
93  
94     // Public -------------------------------------------------------------------
95  
96     /**
97      * @hibernate.property column="s_key"
98      *    not-null="true" unique="true" length="25"
99      * @return The unique key for this event
100     */
101    public String getKey(){
102       return key;
103    }
104    /** @param key The unique key for this event */
105    public void setKey(String key){
106       this.key = key;
107    }
108 
109    /**
110     * @hibernate.property column="s_label"
111     *    not-null="true" length="60"
112     * @return The label to display for this event
113     */
114    public String getLabel(){
115       return label;
116    }
117    /** @param label The label to display for this event */
118    public void setLabel(String label){
119       this.label = label;
120    }
121 
122    /**
123     * @hibernate.property column="s_msgtitle" length="255"
124     * @return A string representing template for generating messages title
125     */
126    public String getMsgTitleTemplate(){
127       return msgTitleTemplate;
128    }
129    /** @param msgTitleTemplate String representing template for title generation */
130    public void setMsgTitleTemplate(String msgTitleTemplate){
131       this.msgTitleTemplate = msgTitleTemplate;
132    }
133 
134    /**
135     * @hibernate.property column="s_msgcontent" type="text"
136     * @return A string representing template for generating messages content
137     */
138    public String getMsgContentTemplate(){
139       return msgContentTemplate;
140    }
141    /** @param msgContentTemplate String representing template for generating messages content */
142    public void setMsgContentTemplate(String msgContentTemplate){
143       this.msgContentTemplate = msgContentTemplate;
144    }
145 
146    /**
147     * @hibernate.property column="s_msglink" length="255"
148     * @return A string representing template for generating messages link
149     */
150    public String getMsgLinkTemplate(){
151       return msgLinkTemplate;
152    }
153    /** @param msgLinkTemplate String representing template for generating messages link */
154    public void setMsgLinkTemplate(String msgLinkTemplate){
155       this.msgLinkTemplate = msgLinkTemplate;
156    }
157 
158    /**
159     * @hibernate.property column="s_restypeclass" length="200"
160     * @return The FQN of the Java class used for managing permissions within this role
161     */
162    public String getResourceResolverClass(){
163       return resourceResolverClass;
164    }
165    /**
166     * Give a resource resolver to this event. ie : an helper object that allows to retrieve resources
167     * to whom should apply event publication. This parameter must be the name of a
168     * Java class that implements <code>org.figure8.join.services.security.PermissionResourceResolver</code>
169     * @param resourceResolverClass FQN of Java class
170     * @throws org.figure8.join.core.InvalidParameterException if the <b>permissionResourceResolverClass</b> is not valid
171     */
172    public void setResourceResolverClass(String resourceResolverClass) throws InvalidParameterException{
173       if (resourceResolverClass != null && !resourceResolverClass.equals("")){
174          this.resourceResolverClass = resourceResolverClass;
175          // Try instantiating resource resolver now.
176          instantiateResourceResolver();
177       }
178    }
179 
180    /**
181     * Get the <code>ResourceResolver</code> asssociated to this event (if any).
182     * @throws InvalidParameterException if the <b>permissionResourceResolverClass</b> inner field is not valid
183     * @return The resolver instance or null if no resolver is associated to this role
184     */
185    public PermissionResourceResolver getResourceResolver() throws InvalidParameterException{
186       // Only available if there's a resource resolver.
187       if (resourceResolverClass != null){
188          // Instantiate it if not already done.
189          if (resourceResolver == null)
190             instantiateResourceResolver();
191          return resourceResolver;
192       }
193       return null;
194    }
195 
196 
197    // Protected ----------------------------------------------------------------
198 
199    /**
200     * Try to instantiate the <code>PermissionResourceResolver</code>
201     * associated with this role (if any ...)
202     * @throws InvalidParameterException if the <b>permissionResourceResolverClass</b> inner field is not valid
203     */
204    protected void instantiateResourceResolver() throws InvalidParameterException{
205       if (resourceResolverClass != null && resourceResolver == null){
206          try{
207             // Load class from context class loader and retrieve new instance.
208             Class clazz = Thread.currentThread().getContextClassLoader().loadClass(resourceResolverClass);
209             resourceResolver = (PermissionResourceResolver)clazz.newInstance();
210          }
211          catch (Exception e){
212             // Log and wrap within an InvalidParameterException.
213             log.error("Exception while trying to instantiate resourceResolverClass for event '" + key + "'");
214             log.error("Here's the exception message : " + e.getMessage());
215             throw new InvalidParameterException("'" + resourceResolverClass + "' cannot be found or " +
216                     "does not extends PermissionResourceResolver", e);
217          }
218       }
219    }
220 }