1 package org.kuali.ole.krad;
2
3 import java.text.DateFormat;
4 import java.text.Format;
5 import java.text.NumberFormat;
6 import java.util.ArrayList;
7 import java.util.Date;
8 import java.util.List;
9
10 import org.apache.commons.lang3.StringUtils;
11 import org.apache.log4j.Logger;
12 import org.kuali.rice.krad.uif.UifConstants;
13 import org.kuali.rice.krad.uif.component.Component;
14 import org.kuali.rice.krad.uif.container.Group;
15 import org.kuali.rice.krad.uif.container.PageGroup;
16 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
17 import org.kuali.rice.krad.web.form.UifFormBase;
18 import org.springframework.web.context.request.RequestAttributes;
19 import org.springframework.web.context.request.RequestContextHolder;
20
21
22
23
24 public final class OleComponentUtils {
25
26 private static final Logger LOG = Logger.getLogger(OleComponentUtils.class);
27
28 private static ThreadLocal<Format> CURRENCY_FORMAT = new ThreadLocal<Format>() {
29 @Override
30 protected Format initialValue() {
31 return NumberFormat.getCurrencyInstance();
32 }
33 };
34
35 private static ThreadLocal<Format> DATE_FORMAT = new ThreadLocal<Format>() {
36 @Override
37 protected Format initialValue() {
38 return DateFormat.getDateInstance();
39 }
40 };
41
42
43
44
45
46
47 public static Object getModel() {
48 RequestAttributes attr = RequestContextHolder.getRequestAttributes();
49 if (attr == null) {
50 return null;
51 } else {
52 return attr.getAttribute(UifConstants.REQUEST_FORM,
53 RequestAttributes.SCOPE_REQUEST);
54 }
55 }
56
57
58
59
60 public static List<? extends Component> filterItems(
61 List<? extends Component> srcitems) {
62 if (srcitems == null) {
63 return null;
64 }
65
66 Object model = getModel();
67 if (model == null) {
68 return srcitems;
69 } else {
70 LOG.debug("Filtering based on " + model);
71 }
72
73 List<Component> items = new ArrayList<Component>();
74 filterloop: for (Component itemComponent : srcitems) {
75
76
77 if (itemComponent instanceof OleComponent) {
78 OleComponent oleComp = (OleComponent) itemComponent;
79 String filterProp = oleComp.getFilterModelProperty();
80 Object filterVal = filterProp == null ? null
81 : ObjectPropertyUtils.getPropertyValue(model,
82 filterProp);
83 if (filterVal instanceof String)
84 filterVal = Boolean.parseBoolean((String) filterVal);
85 if (filterVal == null && filterProp != null)
86 filterVal = Boolean.TRUE;
87 if (Boolean.TRUE.equals(filterVal)) {
88 LOG.debug("Omitting " + itemComponent.getClass() + " "
89 + itemComponent.getId() + ", " + filterProp
90 + " is true");
91 continue filterloop;
92 } else {
93 LOG.debug("Keeping " + itemComponent.getClass() + " "
94 + itemComponent.getId() + ", " + filterProp
95 + " is true");
96 }
97 }
98
99 items.add((Component) itemComponent.copy());
100 }
101
102 return items;
103 }
104
105
106
107
108 public static List<? extends Group> filterCurrentPage(String currentPageId,
109 List<? extends Group> srcitems) {
110 if (srcitems == null) {
111 return null;
112 }
113
114 Object model = getModel();
115 if (model == null || !(model instanceof UifFormBase))
116 return srcitems;
117
118 if (StringUtils.isEmpty(currentPageId))
119 return srcitems;
120
121 LOG.debug("Filtering view pages based on " + model + ", pageId = "
122 + currentPageId);
123
124 List<Group> items = new ArrayList<Group>();
125 for (Group group : srcitems) {
126 String compId = group.getId();
127
128
129 if ((group instanceof PageGroup) && !currentPageId.equals(compId)) {
130 LOG.debug("Omitting " + compId + ", not current page");
131 continue;
132 } else
133 LOG.debug("Keeping " + compId
134 + ", current page or not a PageGroup");
135
136 items.add((Group) group.copy());
137 }
138
139 return items;
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158 public static String formatAsCurrency(Number amount) {
159 return amount == null ? "" : CURRENCY_FORMAT.get().format(amount);
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 public static String formatDate(Date date) {
178 return date == null ? "" : DATE_FORMAT.get().format(date);
179 }
180
181 private OleComponentUtils() {
182 }
183
184 }
185