001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: */
016:
017: package org.kuali.core.datadictionary;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.LinkedHashMap;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.commons.lang.StringUtils;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.kuali.core.datadictionary.exception.DuplicateEntryException;
030:
031: /**
032: * Contains inquiry-related information relating to the parent BusinessObject.
033: * Note: the setters do copious amounts of validation, to facilitate generating errors during the parsing process.
034: */
035: public class InquiryDefinition extends DataDictionaryDefinitionBase {
036: private static Log LOG = LogFactory.getLog(InquiryDefinition.class);
037:
038: private String title;
039: private Map inquirySections;
040: private Class inquirableClass;
041:
042: public InquiryDefinition() {
043: LOG.debug("creating new InquiryDefinition");
044: this .inquirySections = new LinkedHashMap();
045: }
046:
047: /**
048: * @return title
049: */
050: public String getTitle() {
051: return title;
052: }
053:
054: /**
055: * Sets title to the given value.
056: *
057: * @param title
058: * @throws IllegalArgumentException if the given title is blank
059: */
060: public void setTitle(String title) {
061: if (StringUtils.isBlank(title)) {
062: throw new IllegalArgumentException("invalid (blank) title");
063: }
064: LOG.debug("calling setTitle '" + title + "'");
065:
066: this .title = title;
067: }
068:
069: /**
070: * @param inquirySection
071: * @throws IllegalArgumentException if the given inquirySection is null
072: */
073: public void addInquirySection(
074: InquirySectionDefinition inquirySection) {
075: if (inquirySection == null) {
076: throw new IllegalArgumentException(
077: "invalid (null) inquirySection");
078: }
079: LOG.debug("calling addInquirySection for section '"
080: + inquirySection.getTitle() + "'");
081:
082: String sectionTitle = inquirySection.getTitle();
083: if (this .inquirySections.containsKey(sectionTitle)) {
084: throw new DuplicateEntryException(
085: "duplicate inquirySection entry for attribute '"
086: + sectionTitle + "'");
087: }
088:
089: this .inquirySections.put(sectionTitle, inquirySection);
090: }
091:
092: /**
093: * @return Collection of all inquiryField FieldDefinitions associated with this InquiryDefinition, in the order in which they
094: * were added
095: */
096: public List getInquirySections() {
097: List sectionList = new ArrayList();
098:
099: sectionList.addAll(this .inquirySections.values());
100:
101: return Collections.unmodifiableList(sectionList);
102: }
103:
104: /**
105: * @return InquirySectionDefinition for the inquiry section associated with the given section title, or null if there is none
106: */
107: public InquirySectionDefinition getInquirySection(
108: String sectionTitle) {
109: return (InquirySectionDefinition) inquirySections
110: .get(sectionTitle);
111: }
112:
113: /**
114: * Returns the FieldDefinition associated with the field attribute name
115: * @param fieldName
116: * @return
117: */
118: public FieldDefinition getFieldDefinition(String fieldName) {
119: for (Iterator iter = inquirySections.values().iterator(); iter
120: .hasNext();) {
121: InquirySectionDefinition section = (InquirySectionDefinition) iter
122: .next();
123: for (Iterator iterator = section.getInquiryFields()
124: .iterator(); iterator.hasNext();) {
125: FieldDefinition field = (FieldDefinition) iterator
126: .next();
127: if (field.getAttributeName().equals(fieldName)) {
128: return field;
129: }
130: }
131: }
132:
133: return null;
134: }
135:
136: /**
137: * Directly validate simple fields, call completeValidation on Definition fields.
138: *
139: * @see org.kuali.core.datadictionary.DataDictionaryDefinition#completeValidation(java.lang.Class, java.lang.Object)
140: */
141: public void completeValidation(Class rootBusinessObjectClass,
142: Class otherBusinessObjectClass,
143: ValidationCompletionUtils validationCompletionUtils) {
144: for (Iterator i = inquirySections.entrySet().iterator(); i
145: .hasNext();) {
146: Map.Entry e = (Map.Entry) i.next();
147:
148: InquirySectionDefinition inquirySection = (InquirySectionDefinition) e
149: .getValue();
150: inquirySection.completeValidation(rootBusinessObjectClass,
151: null, validationCompletionUtils);
152: }
153: }
154:
155: /**
156: * @see java.lang.Object#toString()
157: */
158: public String toString() {
159: return "InquiryDefinition '" + getTitle() + "'";
160: }
161:
162: public Class getInquirableClass() {
163: return inquirableClass;
164: }
165:
166: public void setInquirableClass(Class inquirableClass) {
167: this.inquirableClass = inquirableClass;
168: }
169: }
|