1   package com.trendmicro.grid.acl.ds.jpa.entities;
2   
3   import com.trendmicro.grid.acl.ds.datatypes.SharedNamedFileIdentifier;
4   import com.trendmicro.grid.acl.l0.datatypes.FileIdentifier;
5   
6   import javax.persistence.*;
7   import java.io.Serializable;
8   
9   import static net.sf.tinyjee.util.Assert.assertNotNull;
10  
11  /**
12   * Implements the JPA driven database bindings for the table "PACKAGE_FILES_HISTORY".
13   *
14   * @author juergen_kellerer, 2010-06-11
15   * @version 1.0
16   */
17  @NamedQueries({
18  		@NamedQuery(name = "PackageFilesHistory.SelectMaxRevisionForPackage",
19  				query = "SELECT MAX(h.id.revision) " +
20  						"FROM PACKAGE_FILES_HISTORY h WHERE id.packageDetails = :packageDetails"),
21  		@NamedQuery(name = "PackageFilesHistory.SelectNamedIdentifierByFileDetails",
22  						query = "SELECT h " +
23  								"FROM PACKAGE_FILES_HISTORY h WHERE id.fileDetails = :fileDetails")
24  })
25  @Entity(name = "PACKAGE_FILES_HISTORY")
26  public class JpaNamedFileIdentifierHistory implements Serializable {
27  
28  	private static final long serialVersionUID = -6605212849235170699L;
29  
30  	public static enum HistoryType {
31  		added, removed, renamed
32  	}
33  
34  	@EmbeddedId
35  	private JpaNamedFileIdentifierHistoryId reference;
36  
37  	@Enumerated(value = EnumType.ORDINAL)
38  	@Column(name = "TYPE", nullable = false)
39  	private HistoryType type;
40  
41  	public JpaNamedFileIdentifierHistory() {
42  	}
43  
44  	public JpaNamedFileIdentifierHistory(JpaFileDetails fileDetails,
45  										 JpaPackageDetails packageDetails,
46  										 String fileName,
47  										 HistoryType type,
48  										 int revision) {
49  		assertNotNull("fileDetails", fileDetails);
50  		assertNotNull("packageDetails", packageDetails);
51  		assertNotNull("fileName", fileName);
52  		assertNotNull("type", type);
53  
54  		reference = new JpaNamedFileIdentifierHistoryId(fileDetails, packageDetails, fileName, revision);
55  		this.type = type;
56  	}
57  
58  	public JpaNamedFileIdentifierHistoryId getReference() {
59  		return reference;
60  	}
61  
62  	public HistoryType getType() {
63  		return type;
64  	}
65  
66  	/**
67  	 * Exports this element for external use, without fetching the identifier values.
68  	 *
69  	 * @param identifier the identifier to set.
70  	 * @return a SharedNamedFileIdentifier instance.
71  	 */
72  	public SharedNamedFileIdentifier export(FileIdentifier identifier) {
73  		return new SharedNamedFileIdentifier(identifier, reference.getFileName());
74  	}
75  
76  	/**
77  	 * Exports this element for external use.
78  	 *
79  	 * @return a SharedNamedFileIdentifier instance.
80  	 */
81  	public SharedNamedFileIdentifier export() {
82  		return new SharedNamedFileIdentifier(reference.getFileDetails().getIdentifier(), reference.getFileName());
83  	}
84  
85  	@Override
86  	public boolean equals(Object o) {
87  		if (this == o) return true;
88  		if (!(o instanceof JpaNamedFileIdentifierHistory)) return false;
89  		JpaNamedFileIdentifierHistory that = (JpaNamedFileIdentifierHistory) o;
90  		return type == that.type && reference.equals(that.reference);
91  	}
92  
93  	@Override
94  	public int hashCode() {
95  		int result = reference.hashCode();
96  		result = 31 * result + type.hashCode();
97  		return result;
98  	}
99  
100 	@Override
101 	public String toString() {
102 		return "JpaNamedFileIdentifierHistory{" +
103 				"reference=" + reference +
104 				", type=" + type +
105 				'}';
106 	}
107 }