1 /**
2 * Copyright 2005-2013 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 public class DataObjectRelationship implements Serializable {
33 private Class<?> relatedClass;
34 private Class<?> parentClass;
35 private String parentAttributeName;
36 private String userVisibleIdentifierKey = null;
37
38 private Map<String, String> parentToChildReferences = new HashMap<String, String>(4);
39
40 public DataObjectRelationship() {
41 }
42
43 public DataObjectRelationship(Class<?> parentClass, String parentAttributeName, Class<?> relatedClass) {
44 super();
45
46 this.relatedClass = relatedClass;
47 this.parentClass = parentClass;
48 this.parentAttributeName = parentAttributeName;
49 }
50
51 /**
52 * Returns the Class that contains the relationship (the parent)
53 *
54 * @return Class<?> parent class
55 */
56 public Class<?> getParentClass() {
57 return parentClass;
58 }
59
60 /**
61 * Returns the class the attribute within the parent class has a relationship to
62 *
63 * @return Class<?> related class
64 */
65 public Class<?> getRelatedClass() {
66 return this.relatedClass;
67 }
68
69 /**
70 * Returns the name of the attribute within the parent class that holds the related class object
71 *
72 * <p>
73 * Note this attribute should be of type given by #getRelatedClass
74 * </p>
75 *
76 * @return String attribute name within parent class
77 */
78 public String getParentAttributeName() {
79 return parentAttributeName;
80 }
81
82 /**
83 * Provides a Map of attribute pairs that make up the relationship, where the map key
84 * is the attribute name on the parent class and the map value is the attribute name on
85 * the related class
86 *
87 * @return Map<String, String> related attribute pairs
88 */
89 public Map<String, String> getParentToChildReferences() {
90 return parentToChildReferences;
91 }
92
93 /**
94 * Setter for the Map of attributes that participate in the relationship
95 *
96 * @param referenceAttributes
97 */
98 public void setParentToChildReferences(Map<String, String> referenceAttributes) {
99 this.parentToChildReferences = referenceAttributes;
100 }
101
102 /**
103 * Retrieves the attribute within the parent class that is related to the given attribute of
104 * the related class by the relationship represented by this object
105 *
106 * @param childAttributeName - name of attribute within the related class to find parent attribute for
107 * @return String attribute name within parent class
108 */
109 public String getParentAttributeForChildAttribute(String childAttributeName) {
110 for (Map.Entry<String, String> entry : parentToChildReferences.entrySet()) {
111 if (entry.getValue().equals(childAttributeName)) {
112 return entry.getKey();
113 }
114 }
115 return null;
116 }
117
118 /**
119 * Retrieves the attribute within the related class that is related to the given attribute of the
120 * parent class by the relationship represented by this object
121 *
122 * @param parentAttributeName - name of attribute within the parent class to find related (child) attribute for
123 * @return String attribute name within the related class
124 */
125 public String getChildAttributeForParentAttribute(String parentAttributeName) {
126 return parentToChildReferences.get(parentAttributeName);
127 }
128
129 public String getUserVisibleIdentifierKey() {
130 return userVisibleIdentifierKey;
131 }
132
133 public void setUserVisibleIdentifierKey(String userVisibleIdentifierKey) {
134 this.userVisibleIdentifierKey = userVisibleIdentifierKey;
135 }
136
137 public String toString() {
138 StringBuffer sb = new StringBuffer();
139 sb.append("Relationship: ").append(parentClass.getName()).append(" -> ").append(relatedClass.getName());
140 for (Map.Entry<String, String> refs : parentToChildReferences.entrySet()) {
141 sb.append("\n ").append(refs.getKey()).append(" -> ").append(refs.getValue());
142 }
143 return sb.toString();
144 }
145 }