1 /**
2 * Copyright 2005-2015 The Kuali Foundation
3 *
4 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 package org.kuali.rice.krad.bo;
17
18 import java.io.Serializable;
19 import java.util.HashMap;
20 import java.util.Map;
21
22 /**
23 * Represents a relationship to another class that exists within a given parent class
24 *
25 * <p>
26 * In terms of relational db, this can be thought of as a foreign key relationship. That is one of the
27 * properties (fields) of the parent class (parent table) has a relationship to another class (table)
28 * </p>
29 *
30 * @author Kuali Rice Team (rice.collab@kuali.org)
31 *
32 * @deprecated use {@link org.kuali.rice.krad.data.metadata.DataObjectRelationship} instead
33 */
34 @Deprecated
35 public class DataObjectRelationship implements Serializable {
36 private Class<?> relatedClass;
37 private Class<?> parentClass;
38 private String parentAttributeName;
39 private String userVisibleIdentifierKey = null;
40
41 private Map<String, String> parentToChildReferences = new HashMap<String, String>(4);
42
43 public DataObjectRelationship() {
44 }
45
46 public DataObjectRelationship(Class<?> parentClass, String parentAttributeName, Class<?> relatedClass) {
47 super();
48
49 this.relatedClass = relatedClass;
50 this.parentClass = parentClass;
51 this.parentAttributeName = parentAttributeName;
52 }
53
54 /**
55 * Returns the Class that contains the relationship (the parent)
56 *
57 * @return Class<?> parent class
58 */
59 public Class<?> getParentClass() {
60 return parentClass;
61 }
62
63 /**
64 * Returns the class the attribute within the parent class has a relationship to
65 *
66 * @return Class<?> related class
67 */
68 public Class<?> getRelatedClass() {
69 return this.relatedClass;
70 }
71
72 /**
73 * Returns the name of the attribute within the parent class that holds the related class object
74 *
75 * <p>
76 * Note this attribute should be of type given by #getRelatedClass
77 * </p>
78 *
79 * @return String attribute name within parent class
80 */
81 public String getParentAttributeName() {
82 return parentAttributeName;
83 }
84
85 /**
86 * Provides a Map of attribute pairs that make up the relationship, where the map key
87 * is the attribute name on the parent class and the map value is the attribute name on
88 * the related class
89 *
90 * @return Map<String, String> related attribute pairs
91 */
92 public Map<String, String> getParentToChildReferences() {
93 return parentToChildReferences;
94 }
95
96 /**
97 * Setter for the Map of attributes that participate in the relationship
98 *
99 * @param referenceAttributes
100 */
101 public void setParentToChildReferences(Map<String, String> referenceAttributes) {
102 this.parentToChildReferences = referenceAttributes;
103 }
104
105 /**
106 * Retrieves the attribute within the parent class that is related to the given attribute of
107 * the related class by the relationship represented by this object
108 *
109 * @param childAttributeName - name of attribute within the related class to find parent attribute for
110 * @return String attribute name within parent class
111 */
112 public String getParentAttributeForChildAttribute(String childAttributeName) {
113 for (Map.Entry<String, String> entry : parentToChildReferences.entrySet()) {
114 if (entry.getValue().equals(childAttributeName)) {
115 return entry.getKey();
116 }
117 }
118 return null;
119 }
120
121 /**
122 * Retrieves the attribute within the related class that is related to the given attribute of the
123 * parent class by the relationship represented by this object
124 *
125 * @param parentAttributeName - name of attribute within the parent class to find related (child) attribute for
126 * @return String attribute name within the related class
127 */
128 public String getChildAttributeForParentAttribute(String parentAttributeName) {
129 return parentToChildReferences.get(parentAttributeName);
130 }
131
132 public String getUserVisibleIdentifierKey() {
133 return userVisibleIdentifierKey;
134 }
135
136 public void setUserVisibleIdentifierKey(String userVisibleIdentifierKey) {
137 this.userVisibleIdentifierKey = userVisibleIdentifierKey;
138 }
139
140 @Override
141 public String toString() {
142 StringBuffer sb = new StringBuffer();
143 sb.append("Relationship: ").append(parentClass.getName()).append(" -> ").append(relatedClass.getName());
144 for (Map.Entry<String, String> refs : parentToChildReferences.entrySet()) {
145 sb.append("\n ").append(refs.getKey()).append(" -> ").append(refs.getValue());
146 }
147 return sb.toString();
148 }
149
150 public void setRelatedClass(Class<?> relatedClass) {
151 this.relatedClass = relatedClass;
152 }
153
154 public void setParentClass(Class<?> parentClass) {
155 this.parentClass = parentClass;
156 }
157
158 public void setParentAttributeName(String parentAttributeName) {
159 this.parentAttributeName = parentAttributeName;
160 }
161 }