View Javadoc
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.data.metadata.impl;
17  
18  import java.util.Collections;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.rice.krad.data.metadata.DataObjectAttributeRelationship;
23  import org.kuali.rice.krad.data.metadata.MetadataChild;
24  
25  /**
26   * {@inheritDoc}
27   */
28  public abstract class MetadataChildBase extends MetadataCommonBase implements MetadataChild {
29  	private static final long serialVersionUID = 2244738385101424133L;
30  
31  	protected MetadataChild embeddedMetadataChild;
32  
33  	protected Class<?> relatedType;
34  	protected List<DataObjectAttributeRelationship> attributeRelationships;
35      protected MetadataChild inverseRelationship;
36  
37  	protected Boolean savedWithParent;
38  	protected Boolean deletedWithParent;
39  	protected Boolean loadedAtParentLoadTime;
40  	protected Boolean loadedDynamicallyUponUse;
41  
42      /**
43      * {@inheritDoc}
44      */
45  	@Override
46  	public boolean isSavedWithParent() {
47  		if (savedWithParent != null) {
48  			return savedWithParent;
49  		}
50  		if (embeddedMetadataChild != null) {
51  			return embeddedMetadataChild.isSavedWithParent();
52  		}
53  		return false;
54  	}
55  
56  	public void setSavedWithParent(boolean savedWithParent) {
57  		this.savedWithParent = savedWithParent;
58  	}
59  
60      /**
61      * {@inheritDoc}
62      */
63  	@Override
64  	public boolean isDeletedWithParent() {
65  		if (deletedWithParent != null) {
66  			return deletedWithParent;
67  		}
68  		if (embeddedMetadataChild != null) {
69  			return embeddedMetadataChild.isDeletedWithParent();
70  		}
71  		return false;
72  	}
73  
74  	public void setDeletedWithParent(boolean deletedWithParent) {
75  		this.deletedWithParent = deletedWithParent;
76  	}
77  
78      /**
79      * {@inheritDoc}
80      */
81  	@Override
82  	public boolean isLoadedAtParentLoadTime() {
83  		if (loadedAtParentLoadTime != null) {
84  			return loadedAtParentLoadTime;
85  		}
86  		if (embeddedMetadataChild != null) {
87  			return embeddedMetadataChild.isLoadedAtParentLoadTime();
88  		}
89  		return false;
90  	}
91  
92  	public void setLoadedAtParentLoadTime(boolean loadedAtParentLoadTime) {
93  		this.loadedAtParentLoadTime = loadedAtParentLoadTime;
94  	}
95  
96      /**
97      * {@inheritDoc}
98      */
99  	@Override
100 	public boolean isLoadedDynamicallyUponUse() {
101 		if (loadedDynamicallyUponUse != null) {
102 			return loadedDynamicallyUponUse;
103 		}
104 		if (embeddedMetadataChild != null) {
105 			return embeddedMetadataChild.isLoadedDynamicallyUponUse();
106 		}
107 		return false;
108 	}
109 
110 	public void setLoadedDynamicallyUponUse(boolean loadedDynamicallyUponUse) {
111 		this.loadedDynamicallyUponUse = loadedDynamicallyUponUse;
112 	}
113 
114     /**
115     * {@inheritDoc}
116     */
117 	@Override
118 	public List<DataObjectAttributeRelationship> getAttributeRelationships() {
119 		if (attributeRelationships != null) {
120 			return Collections.unmodifiableList(attributeRelationships);
121 		}
122 		if (embeddedMetadataChild != null) {
123 			return embeddedMetadataChild.getAttributeRelationships();
124 		}
125 		return Collections.emptyList();
126 	}
127 
128 	public void setAttributeRelationships(List<DataObjectAttributeRelationship> attributeRelationships) {
129 		this.attributeRelationships = attributeRelationships;
130 	}
131 
132     /**
133     * {@inheritDoc}
134     */
135     @Override
136     public MetadataChild getInverseRelationship() {
137         if (inverseRelationship != null) {
138             return inverseRelationship;
139         }
140         if (embeddedMetadataChild != null) {
141             return embeddedMetadataChild.getInverseRelationship();
142         }
143         return null;
144     }
145 
146     public void setInverseRelationship(MetadataChild inverseRelationship) {
147         this.inverseRelationship = inverseRelationship;
148     }
149 
150     /**
151     * {@inheritDoc}
152     */
153     @Override
154 	public String getParentAttributeNameRelatedToChildAttributeName(String childAttribute) {
155         for(DataObjectAttributeRelationship dataObjectAttributeRelationship : attributeRelationships){
156             if(StringUtils.equals(dataObjectAttributeRelationship.getChildAttributeName(),childAttribute)){
157                 return dataObjectAttributeRelationship.getParentAttributeName();
158             }
159         }
160         return null;
161     }
162 
163     /**
164     * {@inheritDoc}
165     */
166 	@Override
167 	public String toString() {
168 		StringBuilder builder = new StringBuilder();
169 		builder.append(this.getClass().getSimpleName()).append(" [");
170 		builder.append("name=").append(getName()).append(", ");
171 		builder.append("relatedType=").append(getRelatedType()).append(", ");
172 		builder.append("attributes=").append(getAttributeRelationships()).append(", ");
173 		builder.append("backingObjectName=").append(getBackingObjectName()).append(", ");
174 		builder.append("readOnly=").append(isReadOnly());
175 		builder.append(", savedWithParent=");
176 		builder.append(savedWithParent);
177 		builder.append(", deletedWithParent=");
178 		builder.append(deletedWithParent);
179 		builder.append(", loadedAtParentLoadTime=");
180 		builder.append(loadedAtParentLoadTime);
181 		builder.append(", loadedDynamicallyUponUse=");
182 		builder.append(loadedDynamicallyUponUse);
183 		if (embeddedMetadataChild != null) {
184 			builder.append(", embedded=").append(embeddedMetadataChild);
185 		}
186 		builder.append("]");
187 		return builder.toString();
188 	}
189 
190     /**
191     * {@inheritDoc}
192     */
193 	@Override
194 	public Class<?> getRelatedType() {
195 		return relatedType;
196 	}
197 
198 	public void setRelatedType(Class<?> relatedType) {
199 		this.relatedType = relatedType;
200 	}
201 
202     /**
203     * Gets the metadata from the child
204     *
205     * @return embedded metadata
206     */
207 	public MetadataChild getEmbeddedMetadataChild() {
208 		return embeddedMetadataChild;
209 	}
210 
211 	public void setEmbeddedMetadataChild(MetadataChild embeddedMetadataChild) {
212 		this.embeddedMetadataChild = embeddedMetadataChild;
213 		setEmbeddedCommonMetadata(embeddedMetadataChild);
214 	}
215 
216 }