1 package org.kuali.ole.describe.krad;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.kuali.ole.deliver.bo.OleProxyPatronDocument;
6 import org.kuali.ole.deliver.form.CircForm;
7 import org.kuali.ole.docstore.common.document.config.DocumentSearchConfig;
8 import org.kuali.rice.core.api.util.ConcreteKeyValue;
9 import org.kuali.rice.core.api.util.KeyValue;
10 import org.kuali.rice.krad.uif.UifConstants;
11 import org.kuali.rice.krad.uif.component.BindingInfo;
12 import org.kuali.rice.krad.uif.component.Component;
13 import org.kuali.rice.krad.uif.container.CollectionGroup;
14 import org.kuali.rice.krad.uif.container.Container;
15 import org.kuali.rice.krad.uif.element.Action;
16 import org.kuali.rice.krad.uif.field.Field;
17 import org.kuali.rice.krad.uif.field.FieldGroup;
18 import org.kuali.rice.krad.uif.layout.CollectionLayoutManager;
19 import org.kuali.rice.krad.uif.layout.LayoutManagerBase;
20 import org.kuali.rice.krad.uif.util.ComponentUtils;
21 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
22 import org.kuali.rice.krad.uif.view.View;
23 import org.kuali.rice.krad.uif.widget.Pager;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28
29
30
31
32 public class ProxyPatrenSearchLayout extends LayoutManagerBase implements
33 CollectionLayoutManager {
34
35 private static final long serialVersionUID = 5289870490845303915L;
36 private static final Logger LOG = Logger.getLogger(OleSearchLayout.class);
37
38 private List<ProxySearchLine> searchLines = new ArrayList<ProxySearchLine>();
39 private int totalLines;
40 private int displayedLines;
41
42
43 private int currentPage;
44 private int pageSize;
45 private List<KeyValue> pageSizeOptions;
46
47
48 private Pager pager;
49
50 private void setUpPager(CircForm form, CollectionGroup collection) {
51 if (pageSize == 0 || pager == null)
52 return;
53
54 int total = 0;
55 List<OleProxyPatronDocument> oleProxyPatronDocuments = ObjectPropertyUtils.<List<OleProxyPatronDocument>>getPropertyValue(form, collection.getBindingInfo().getBindingPath());
56 if (oleProxyPatronDocuments == null)
57 return;
58
59 total = oleProxyPatronDocuments.size();
60
61
62 totalLines = total;
63
64 String extensionKey = collection.getId() + ".currentPage";
65 Map<String, Object> extension = form.getExtensionData();
66 int lastPage = total / pageSize + (total % pageSize == 0 ? 0 : 1);
67 Integer currentPage = (Integer) extension.get(extensionKey);
68 if (currentPage == null)
69 currentPage = 1;
70
71 String pageNumber = form.getPageNumber();
72 if (pageNumber != null)
73 switch (pageNumber) {
74 case UifConstants.PageRequest.FIRST:
75 currentPage = 1;
76 break;
77 case UifConstants.PageRequest.LAST:
78 currentPage = lastPage;
79 break;
80 case UifConstants.PageRequest.NEXT:
81 currentPage = Math.min(currentPage + 1, lastPage);
82 break;
83 case UifConstants.PageRequest.PREV:
84 currentPage = Math.max(currentPage - 1, 1);
85 break;
86 default:
87 try {
88 currentPage = Math.max(1,
89 Math.min(lastPage, Integer.parseInt(pageNumber)));
90 } catch (NumberFormatException e) {
91 LOG.warn("Invalid page number " + form.getPageNumber(), e);
92 }
93 break;
94 }
95 form.setPageNumber(null);
96
97 pager.setCurrentPage(currentPage);
98 pager.setNumberOfPages(lastPage);
99 this.currentPage = currentPage;
100
101 synchronized (extension) {
102 extension.put(extensionKey, currentPage);
103 }
104 }
105
106 @Override
107 public void performApplyModel(View view, Object model, Container container) {
108 CircForm form = (CircForm) model;
109 CollectionGroup collection = (CollectionGroup) container;
110
111 List<Integer> pageSizes = DocumentSearchConfig.getPageSizes();
112 List<KeyValue> pso = new ArrayList<>(pageSizes.size() + 1);
113 pso.add(new ConcreteKeyValue("10", "10"));
114 pso.add(new ConcreteKeyValue("25", "25"));
115 pso.add(new ConcreteKeyValue("50", "50"));
116 pso.add(new ConcreteKeyValue("100", "100"));
117
118 pageSizeOptions = pso;
119 if (StringUtils.isNotEmpty(form.getPageSize()))
120 pageSize = Integer.parseInt(form.getPageSize());
121
122 setUpPager(form, collection);
123 super.performApplyModel(view, model, container);
124 }
125
126 @Override
127 public void buildLine(View view, Object model,
128 CollectionGroup collectionGroup, List<Field> lineFields,
129 List<FieldGroup> subCollectionFields, String bindingPath,
130 List<Action> actions, String idSuffix, Object currentLine,
131 int lineIndex) {
132
133 OleProxyPatronDocument row = (OleProxyPatronDocument) currentLine;
134
135
136 if (currentPage > 0
137 && (lineIndex < (currentPage - 1) * pageSize || lineIndex >= currentPage
138 * pageSize))
139 return;
140
141 BindingInfo bi = new BindingInfo();
142 bi.setBindByNamePrefix(bindingPath);
143
144 ProxySearchLine line = new ProxySearchLine();
145 line.setLineNumber(lineIndex);
146 line.setLineId(collectionGroup.getBaseId() + idSuffix);
147 line.setBindPath(bi.getBindByNamePrefix());
148 line.setRow(row);
149
150 synchronized (searchLines) {
151 searchLines.add(line);
152 }
153
154 if (LOG.isDebugEnabled())
155 LOG.debug("SEARCH LINE: " + line);
156
157 displayedLines++;
158 }
159
160 @Override
161 public FieldGroup getSubCollectionFieldGroupPrototype() {
162 return null;
163 }
164
165
166 public int getPageSize() {
167 return pageSize;
168 }
169
170 public List<KeyValue> getPageSizeOptions() {
171 return pageSizeOptions;
172 }
173
174 public void setPageSizeOptions(List<KeyValue> pageSizeOptions) {
175 this.pageSizeOptions = pageSizeOptions;
176 }
177
178 public int getTotalLines() {
179 return totalLines;
180 }
181
182 public int getDisplayedLines() {
183 return displayedLines;
184 }
185
186 public List<ProxySearchLine> getSearchLines() {
187 return searchLines;
188 }
189
190 public Pager getPager() {
191 return pager;
192 }
193
194 public void setPager(Pager pager) {
195 this.pager = pager;
196 }
197
198 @Override
199 public List<Component> getComponentsForLifecycle() {
200 List<Component> rv = super.getComponentsForLifecycle();
201 if (pager != null)
202 rv.add(pager);
203 return rv;
204 }
205
206 @Override
207 protected <T> void copyProperties(T copy) {
208 super.copyProperties(copy);
209 ProxyPatrenSearchLayout c = (ProxyPatrenSearchLayout) copy;
210 c.pageSize = pageSize;
211 if (pager != null)
212 c.setPager(ComponentUtils.copy(pager));
213 }
214
215
216 }
217