001: /*
002: * Copyright 2005-2006 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.Iterator;
021: import java.util.List;
022:
023: import org.apache.commons.lang.StringUtils;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: /**
028: * Contains sorting-related information for DataDictionary entries.
029: *
030: * Note: the setters do copious amounts of validation, to facilitate generating errors during the parsing process.
031: *
032: *
033: */
034: public class SortDefinition extends DataDictionaryDefinitionBase {
035: // logger
036: private static Log LOG = LogFactory.getLog(SortDefinition.class);
037:
038: private boolean sortAscending;
039: private String attributeName;
040: private List attributes;
041: private List attributeNames;
042:
043: public SortDefinition() {
044: LOG.debug("creating new SortDefinition");
045:
046: this .sortAscending = true;
047: this .attributes = new ArrayList();
048: this .attributeNames = new ArrayList();
049: }
050:
051: /**
052: * Sets attributeName to the given value.
053: *
054: * @param attributeName
055: * @throws IllegalArgumentException if the given attributeName is blank
056: */
057: public void setAttributeName(String attributeName) {
058: if (StringUtils.isBlank(attributeName)) {
059: throw new IllegalArgumentException(
060: "invalid (blank) attributeName");
061: }
062: LOG.debug("calling setAttributeName '" + attributeName + "'");
063: if (attributes.size() != 0) {
064: throw new IllegalStateException(
065: "unable to set sort attributeName when sortAttributes have already been added");
066: }
067:
068: this .attributeName = attributeName;
069: }
070:
071: /**
072: * Adds the given attribute to the list of attributes associated with this sortDefinition.
073: *
074: * @param sortAttributeDefinition
075: */
076: public void addSortAttribute(
077: SortAttributeDefinition sortAttributeDefinition) {
078: if (attributeName != null) {
079: throw new IllegalStateException(
080: "unable to add sortAttributes when sort attributeName has already been set");
081: }
082:
083: this .attributes.add(sortAttributeDefinition);
084: }
085:
086: /**
087: * @return the List of associated attribute names as Strings
088: */
089: public List getAttributeNames() {
090: return this .attributeNames;
091: }
092:
093: /**
094: * @return true if items should sort in ascending order
095: */
096: public boolean getSortAscending() {
097: return sortAscending;
098: }
099:
100: /**
101: * Sets sortAscending to the given value
102: *
103: * @param sortAscending
104: */
105: public void setSortAscending(boolean sortAscending) {
106: LOG.debug("calling setSortAscending '" + sortAscending + "'");
107:
108: this .sortAscending = sortAscending;
109: }
110:
111: /**
112: * Directly validate simple fields.
113: *
114: * @see org.kuali.core.datadictionary.DataDictionaryDefinition#completeValidation(java.lang.Class, java.lang.Object)
115: */
116: public void completeValidation(Class rootBusinessObjectClass,
117: Class otherBusinessObjectClass,
118: ValidationCompletionUtils validationCompletionUtils) {
119: if (this .attributeName != null) {
120: boolean oldIsParsingFile = isParsingFile;
121: isParsingFile = false;
122: SortAttributeDefinition syntheticAttribute = new SortAttributeDefinition();
123: isParsingFile = oldIsParsingFile;
124: syntheticAttribute.setAttributeName(this .attributeName);
125: this .attributeName = null;
126:
127: addSortAttribute(syntheticAttribute);
128: }
129:
130: for (Iterator i = attributes.iterator(); i.hasNext();) {
131: SortAttributeDefinition sortAttributeDefinition = (SortAttributeDefinition) i
132: .next();
133: sortAttributeDefinition.completeValidation(
134: rootBusinessObjectClass, otherBusinessObjectClass,
135: validationCompletionUtils);
136:
137: attributeNames.add(sortAttributeDefinition
138: .getAttributeName());
139: }
140: }
141:
142: /**
143: * @see java.lang.Object#toString()
144: */
145: public String toString() {
146: StringBuffer attrList = new StringBuffer("[");
147: for (Iterator i = attributes.iterator(); i.hasNext();) {
148: attrList.append(((SortAttributeDefinition) i.next())
149: .getAttributeName());
150: if (i.hasNext()) {
151: attrList.append(",");
152: }
153: }
154: attrList.append("]");
155:
156: return "SortDefinition for attribute " + attrList.toString();
157: }
158: }
|