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