001/**
002 * Copyright 2005-2016 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.datadictionary;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
020
021import java.util.ArrayList;
022import java.util.Iterator;
023import java.util.List;
024
025/**
026                The defaultSort element specifies the sequence in which the
027                lookup search results should be displayed.  It contains an
028                ascending/descending indicator and a list of attribute names.
029
030                DD: See SortDefinition.java
031
032                JSTL: defaultSort is a Map with the following keys:
033                * sortAscending (boolean String)
034                * sortAttributes (Map)
035
036                By the time JSTL export occurs, the optional attributeName from the defaultSort
037                tag will have been converted into the first contained sortAttribute
038 */
039public class SortDefinition extends DataDictionaryDefinitionBase {
040    private static final long serialVersionUID = -1092811342186612461L;
041    
042        protected boolean sortAscending = true;
043    protected List<String> attributeNames = new ArrayList<String>();
044
045    public SortDefinition() {}
046
047
048    /**
049                       The sortAttribute element defines one part of the sort key.
050                        The full sort key is comprised of the sortAttribute's in the
051                        order in which they have been defined.
052
053                        DD: See SortAttributesDefinition.java.
054
055                        JSTL: sortAttribute is a Map which is accessed using a
056                        key of the attributeName of the sortAttribute.
057                        It contains a single entry with the following key:
058                            * "attributeName"
059
060                        The associated value is the attributeName of the sortAttribute.
061                        See LookupMapBuilder.java
062     * @throws IllegalArgumentException if the given attributeName is blank
063     */
064    public void setAttributeName(String attributeName) {
065        if (StringUtils.isBlank(attributeName)) {
066            throw new IllegalArgumentException("invalid (blank) attributeName");
067        }
068        if (attributeNames.size() != 0) {
069            throw new IllegalStateException("unable to set sort attributeName when sortAttributes have already been added");
070        }
071
072        attributeNames.add(attributeName);
073    }
074
075    /**
076     * @return the List of associated attribute names as Strings
077     */
078    public List<String> getAttributeNames() {
079        return this.attributeNames;
080    }
081
082
083    /**
084     * @return true if items should sort in ascending order
085     */
086    public boolean getSortAscending() {
087        return sortAscending;
088    }
089
090    public void setSortAscending(boolean sortAscending) {
091        this.sortAscending = sortAscending;
092    }
093
094
095    /**
096     * Directly validate simple fields.
097     * 
098     * @see org.kuali.rice.krad.datadictionary.DataDictionaryDefinition#completeValidation(java.lang.Class, java.lang.Object)
099     */
100    public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
101        for ( String attributeName : attributeNames ) {
102            if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, attributeName)) {
103                throw new AttributeValidationException("unable to find sort attribute '" + attributeName + "' in rootBusinessObjectClass '" + rootBusinessObjectClass.getName() + "' (" + "" + ")");
104            }            
105        }
106    }
107
108
109    /**
110     * @see java.lang.Object#toString()
111     */
112    public String toString() {
113        StringBuffer attrList = new StringBuffer("[");
114        for (Iterator<String> i = attributeNames.iterator(); i.hasNext();) {
115            attrList.append(i.next());
116            if (i.hasNext()) {
117                attrList.append(",");
118            }
119        }
120        attrList.append("]");
121
122        return "SortDefinition :  " + attrList.toString();
123    }
124
125
126    /**
127                     The sortAttributes element allows a multiple-part sort key
128                      to be defined
129
130                      JSTL: sortAttributes is a Map which is accessed using a
131                      key of "sortAttributes". This map contains an entry for
132                      sort attribute.  The key is:
133                      * attributeName of a sort field.
134                      The associated value is a sortAttribute ExportMap.
135     */
136    public void setAttributeNames(List<String> attributeNames) {
137        this.attributeNames = attributeNames;
138    }
139    
140}