1 /**
2 * Copyright 2005-2014 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 java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
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 @Override
102 public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass, ValidationTrace tracer) {
103
104 if ( attributeNames == null || attributeNames.isEmpty() ) {
105 String currentValues[] = {"attributeNames = " + attributeNames, "rootBusinessObjectClass = " + rootBusinessObjectClass};
106 tracer.createError("SortDefinition may not have an empty attribute list", currentValues);
107 }
108
109 for (String attributeName : attributeNames) {
110 if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, attributeName)) {
111 String currentValues[] = {"attributeName = " + attributeName, "rootBusinessObjectClass = " + rootBusinessObjectClass};
112 tracer.createError("attribute in SortDefinition not found on business object", currentValues);
113 }
114 }
115 }
116
117 @Override
118 public String toString() {
119 StringBuilder attrList = new StringBuilder("[");
120 for (Iterator<String> i = attributeNames.iterator(); i.hasNext(); ) {
121 attrList.append(i.next());
122 if (i.hasNext()) {
123 attrList.append(",");
124 }
125 }
126 attrList.append("]");
127
128 return "SortDefinition : " + attrList;
129 }
130
131 /**
132 * The sortAttributes element allows a multiple-part sort key to be defined
133 *
134 * JSTL: sortAttributes is a Map which is accessed using a key of "sortAttributes". This map contains an entry for
135 * sort attribute. The key is: attributeName of a sort field. The associated value is a sortAttribute ExportMap.
136 */
137 public void setAttributeNames(List<String> attributeNames) {
138 this.attributeNames = attributeNames;
139 }
140
141 }