1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: SecondaryKeyMetadata.java,v 1.1 2008/02/07 17:12:28 mark Exp $
7 */
8
9package com.sleepycat.persist.model;
10
11
12/**
13 * The metadata for a secondary key field.  A secondary key may be specified
14 * with the {@link SecondaryKey} annotation.
15 *
16 * <p>{@code SecondaryKeyMetadata} objects are thread-safe.  Multiple threads
17 * may safely call the methods of a shared {@code SecondaryKeyMetadata}
18 * object.</p>
19 *
20 * @author Mark Hayes
21 */
22public class SecondaryKeyMetadata extends FieldMetadata {
23
24    private static final long serialVersionUID = 8118924993396722502L;
25
26    private String keyName;
27    private Relationship relationship;
28    private String elementClassName;
29    private String relatedEntity;
30    private DeleteAction deleteAction;
31
32    /**
33     * Used by an {@code EntityModel} to construct secondary key metadata.
34     */
35    public SecondaryKeyMetadata(String name,
36                                String className,
37                                String declaringClassName,
38                                String elementClassName,
39                                String keyName,
40                                Relationship relationship,
41                                String relatedEntity,
42                                DeleteAction deleteAction) {
43        super(name, className, declaringClassName);
44        this.elementClassName = elementClassName;
45        this.keyName = keyName;
46        this.relationship = relationship;
47        this.relatedEntity = relatedEntity;
48        this.deleteAction = deleteAction;
49    }
50
51    /**
52     * Returns the class name of the array or collection element for a {@link
53     * Relationship#ONE_TO_MANY ONE_TO_MANY} or {@link
54     * Relationship#MANY_TO_MANY MANY_TO_MANY} relationship, or null for a
55     * Relationship#ONE_TO_ONE ONE_TO_ONE} or {@link Relationship#MANY_TO_ONE
56     * MANY_TO_ONE} relationship.
57     */
58    public String getElementClassName() {
59        return elementClassName;
60    }
61
62    /**
63     * Returns the key name, which may be different from the field name.
64     */
65    public String getKeyName() {
66        return keyName;
67    }
68
69    /**
70     * Returns the relationship between instances of the entity class and the
71     * secondary keys.  This may be specified using the {@link
72     * SecondaryKey#relate} annotation.
73     */
74    public Relationship getRelationship() {
75        return relationship;
76    }
77
78    /**
79     * Returns the class name of the related (foreign) entity, for which
80     * foreign key constraints are specified using the {@link
81     * SecondaryKey#relatedEntity} annotation.
82     */
83    public String getRelatedEntity() {
84        return relatedEntity;
85    }
86
87    /**
88     * Returns the action to take when a related entity is deleted having a
89     * primary key value that exists as a secondary key value for this entity.
90     * This may be specified using the {@link
91     * SecondaryKey#onRelatedEntityDelete} annotation.
92     */
93    public DeleteAction getDeleteAction() {
94        return deleteAction;
95    }
96
97    @Override
98    public boolean equals(Object other) {
99        if (other instanceof SecondaryKeyMetadata) {
100            SecondaryKeyMetadata o = (SecondaryKeyMetadata) other;
101            return super.equals(o) &&
102                   relationship == o.relationship &&
103                   deleteAction == o.deleteAction &&
104                   ClassMetadata.nullOrEqual(keyName, o.keyName) &&
105                   ClassMetadata.nullOrEqual(elementClassName,
106                                             o.elementClassName) &&
107                   ClassMetadata.nullOrEqual(relatedEntity, o.relatedEntity);
108        } else {
109            return false;
110        }
111    }
112
113    @Override
114    public int hashCode() {
115        return super.hashCode() +
116               relationship.hashCode() +
117               deleteAction.hashCode() +
118               ClassMetadata.hashCode(keyName) +
119               ClassMetadata.hashCode(elementClassName) +
120               ClassMetadata.hashCode(relatedEntity);
121    }
122}
123