1 /**
2 * Copyright 2005-2016 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.datadictionary;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
20
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24
25 /**
26 * The defaultSort element specifies the sequence in which the lookup search results should be displayed
27 *
28 * <p>It contains an ascending/descending indicator and a list of attribute names.
29 * JSTL: defaultSort is a Map with the following keys:
30 * sortAscending (boolean String) and sortAttributes (Map).
31 * By the time JSTL export occurs, the optional attributeName from the defaultSort
32 * tag will have been converted into the first contained sortAttribute.
33 * </p>
34 *
35 * @author Kuali Rice Team (rice.collab@kuali.org)
36 */
37 public class SortDefinition extends DataDictionaryDefinitionBase {
38 private static final long serialVersionUID = -1092811342186612461L;
39
40 protected boolean sortAscending = true;
41 protected List<String> attributeNames = new ArrayList<String>();
42
43 public SortDefinition() {
44
45 }
46
47 /**
48 * The sortAttribute element defines one part of the sort key.
49 * The full sort key is comprised of the sortAttribute's in the
50 * order in which they have been defined.
51 *
52 * DD: See SortAttributesDefinition.java.
53 *
54 * JSTL: sortAttribute is a Map which is accessed using a
55 * key of the attributeName of the sortAttribute.
56 * It contains a single entry with the following key:
57 * "attributeName"
58 *
59 * The associated value is the attributeName of the sortAttribute.
60 * See LookupMapBuilder.java
61 *
62 * @throws IllegalArgumentException if the given attributeName is blank
63 */
64 public void setAttributeName(String attributeName) {
65 if (StringUtils.isBlank(attributeName)) {
66 throw new IllegalArgumentException("invalid (blank) attributeName");
67 }
68 if (!attributeNames.isEmpty()) {
69 throw new IllegalStateException(
70 "unable to set sort attributeName when sortAttributes have already been added");
71 }
72
73 attributeNames.add(attributeName);
74 }
75
76 /**
77 * @return the List of associated attribute names as Strings
78 */
79 public List<String> getAttributeNames() {
80 return this.attributeNames;
81 }
82
83 /**
84 * Indicates that the items must be sorted in ascending order
85 *
86 * @return true if items should sort in ascending order
87 */
88 public boolean getSortAscending() {
89 return sortAscending;
90 }
91
92 /**
93 * Setter for the flag to indicate ascending sorting of items
94 *
95 * @param sortAscending
96 */
97 public void setSortAscending(boolean sortAscending) {
98 this.sortAscending = sortAscending;
99 }
100
101 /**
102 * Directly validate simple fields
103 *
104 * @see org.kuali.rice.krad.datadictionary.DataDictionaryDefinition#completeValidation(Class, Class)
105 */
106 @Override
107 public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
108 for (String attributeName : attributeNames) {
109 if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, attributeName)) {
110 throw new AttributeValidationException("unable to find sort attribute '"
111 + attributeName
112 + "' in rootBusinessObjectClass '"
113 + rootBusinessObjectClass.getName()
114 + "' ("
115 + ")");
116 }
117 }
118 }
119
120 /**
121 * @see java.lang.Object#toString()
122 */
123 public String toString() {
124 StringBuilder attrList = new StringBuilder("[");
125 for (Iterator<String> i = attributeNames.iterator(); i.hasNext(); ) {
126 attrList.append(i.next());
127 if (i.hasNext()) {
128 attrList.append(",");
129 }
130 }
131 attrList.append("]");
132
133 return "SortDefinition : " + attrList;
134 }
135
136 /**
137 * The sortAttributes element allows a multiple-part sort key to be defined
138 *
139 * JSTL: sortAttributes is a Map which is accessed using a key of "sortAttributes". This map contains an entry for
140 * sort attribute. The key is: attributeName of a sort field. The associated value is a sortAttribute ExportMap.
141 */
142 public void setAttributeNames(List<String> attributeNames) {
143 this.attributeNames = attributeNames;
144 }
145
146 }