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