View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.gwe.persistence.model;
18  
19  import java.io.Serializable;
20  import java.sql.Timestamp;
21  
22  import javax.persistence.Column;
23  import javax.persistence.MappedSuperclass;
24  
25  import org.hibernate.HibernateException;
26  import org.hibernate.engine.SessionImplementor;
27  import org.hibernate.id.IdentifierGenerator;
28  
29  /**
30   * @author Marco Ruiz
31   * @since Nov 14, 2007
32   */
33  abstract class BaseModelInfoIdGenerator<INFO_TYPE extends BaseModelInfo<KEY_TYPE>, KEY_TYPE extends Serializable> implements IdentifierGenerator {
34  
35  	public final Serializable generate(SessionImplementor session, Object object) throws HibernateException {
36  		return generateId((INFO_TYPE) object);
37  	}
38  
39  	public abstract KEY_TYPE generateId(INFO_TYPE infoObj);
40  }
41  
42  /**
43   * @author Marco Ruiz
44   * @since Oct 4, 2007
45   */
46  @MappedSuperclass
47  public abstract class BaseModelInfo<KEY_TYPE> implements Serializable {
48  	
49  	private static IEventLogger eventLogger;
50  	
51  	public static void setEventLogger(IEventLogger eventLogger) {
52  		BaseModelInfo.eventLogger = eventLogger;
53  	}
54  	
55  	
56  	@Column
57  	private Timestamp whenCreated;
58  	
59  	public abstract KEY_TYPE getId();
60  
61  	public Timestamp getWhenCreated() {
62  		return whenCreated;
63  	}
64  	
65  	public void stampWhenCreated() {
66  		whenCreated = new Timestamp(System.currentTimeMillis());
67  	}
68  
69  	public void logCreateEvent() {
70  		logEvent(EventType.CREATED, getWhenCreated());
71  	}
72  
73  	public Timestamp logEvent(EventType evType, BaseModelInfo... relatedModels) {
74  		return logEvent(evType, new Timestamp(System.currentTimeMillis()), relatedModels);
75  	}
76  	
77  	public Timestamp logEvent(EventType evType, Timestamp when, BaseModelInfo... relatedModels) {
78  		return (eventLogger == null) ? new Timestamp(System.currentTimeMillis()) : eventLogger.logEvent(this, evType, when, relatedModels);
79  	}
80  	
81  	public ModelSummary<KEY_TYPE> createModelSummaryFor(EventType ev) {
82  		return new ModelSummary<KEY_TYPE>(this);
83  	}
84  }
85