001/**
002 * Copyright 2005-2015 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krad.bo;
017
018import java.io.Serializable;
019import java.util.HashMap;
020import java.util.Map;
021
022/**
023 * Represents a relationship to another class that exists within a given parent class
024 *
025 * <p>
026 * In terms of relational db, this can be thought of as a foreign key relationship. That is one of the
027 * properties (fields) of the parent class (parent table) has a relationship to another class (table)
028 * </p>
029 *
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 *
032 * @deprecated use {@link org.kuali.rice.krad.data.metadata.DataObjectRelationship} instead
033 */
034@Deprecated
035public class DataObjectRelationship implements Serializable {
036    private Class<?> relatedClass;
037    private Class<?> parentClass;
038    private String parentAttributeName;
039    private String userVisibleIdentifierKey = null;
040
041    private Map<String, String> parentToChildReferences = new HashMap<String, String>(4);
042
043    public DataObjectRelationship() {
044    }
045
046    public DataObjectRelationship(Class<?> parentClass, String parentAttributeName, Class<?> relatedClass) {
047        super();
048
049        this.relatedClass = relatedClass;
050        this.parentClass = parentClass;
051        this.parentAttributeName = parentAttributeName;
052    }
053
054    /**
055     * Returns the Class that contains the relationship (the parent)
056     *
057     * @return Class<?> parent class
058     */
059    public Class<?> getParentClass() {
060        return parentClass;
061    }
062
063    /**
064     * Returns the class the attribute within the parent class has a relationship to
065     *
066     * @return Class<?> related class
067     */
068    public Class<?> getRelatedClass() {
069        return this.relatedClass;
070    }
071
072    /**
073     * Returns the name of the attribute within the parent class that holds the related class object
074     *
075     * <p>
076     * Note this attribute should be of type given by #getRelatedClass
077     * </p>
078     *
079     * @return String attribute name within parent class
080     */
081    public String getParentAttributeName() {
082        return parentAttributeName;
083    }
084
085    /**
086     * Provides a Map of attribute pairs that make up the relationship, where the map key
087     * is the attribute name on the parent class and the map value is the attribute name on
088     * the related class
089     *
090     * @return Map<String, String> related attribute pairs
091     */
092    public Map<String, String> getParentToChildReferences() {
093        return parentToChildReferences;
094    }
095
096    /**
097     * Setter for the Map of attributes that participate in the relationship
098     *
099     * @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}