1 package org.kuali.ole.select.document.krad;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.apache.log4j.Logger;
8 import org.kuali.ole.OLEConstants;
9 import org.kuali.ole.module.purap.businessobject.options.DiscountTypeValuesFinder;
10 import org.kuali.ole.select.businessobject.OleInvoiceItem;
11 import org.kuali.ole.select.document.OleInvoiceDocument;
12 import org.kuali.ole.select.form.OLEInvoiceForm;
13 import org.kuali.rice.core.api.util.KeyValue;
14 import org.kuali.rice.kew.api.WorkflowDocument;
15 import org.kuali.rice.krad.uif.UifConstants;
16 import org.kuali.rice.krad.uif.UifPropertyPaths;
17 import org.kuali.rice.krad.uif.component.BindingInfo;
18 import org.kuali.rice.krad.uif.component.Component;
19 import org.kuali.rice.krad.uif.container.CollectionGroup;
20 import org.kuali.rice.krad.uif.container.Container;
21 import org.kuali.rice.krad.uif.container.Group;
22 import org.kuali.rice.krad.uif.element.Action;
23 import org.kuali.rice.krad.uif.field.Field;
24 import org.kuali.rice.krad.uif.field.FieldGroup;
25 import org.kuali.rice.krad.uif.layout.CollectionLayoutManager;
26 import org.kuali.rice.krad.uif.layout.LayoutManagerBase;
27 import org.kuali.rice.krad.uif.util.ComponentUtils;
28 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
29 import org.kuali.rice.krad.uif.view.View;
30 import org.kuali.rice.krad.uif.widget.Pager;
31 import org.kuali.rice.krad.uif.widget.QuickFinder;
32
33 public class OleInvoiceItemsLayout extends LayoutManagerBase implements
34 CollectionLayoutManager {
35
36 private static final long serialVersionUID = 5289870490845303915L;
37 private static final Logger LOG = Logger
38 .getLogger(OleInvoiceItemsLayout.class);
39
40 @SuppressWarnings("unchecked")
41 private static final List<KeyValue> DISCOUNT_KEY_VALUES = new DiscountTypeValuesFinder()
42 .getKeyValues();
43
44 private List<OleInvoiceItemsLine> itemLines = new ArrayList<OleInvoiceItemsLine>();
45 private int filteredLines;
46 private int displayedLines;
47 private int totalLines;
48 private boolean foreignCurrency;
49
50 private int pageSize;
51 private int currentPage;
52 private Pager pager;
53 private QuickFinder relinkQuickfinder;
54 private Group rowDetailsGroup;
55 private String actionRefreshId;
56
57 private List<Component> components = new ArrayList<Component>();
58
59 public OleInvoiceItemsLayout(){
60 super();
61 pageSize = OLEConstants.DEFAULT_INV_ITM_LMT;
62 String pageSizeFromParameter = OLEConstants.getParameterForSelectModule("INVOICE_ITEM_LIMIT");
63 try{
64 pageSize = Integer.parseInt(pageSizeFromParameter);
65 }catch(Exception e){
66 LOG.info("System parameter is having invalid size for invoice current item section page size. So taking the default value for page size : " + pageSize);
67 }
68 }
69
70 private void setUpPager(OLEInvoiceForm form, CollectionGroup collection) {
71 if (pageSize == 0 || pager == null)
72 return;
73
74 int total = 0;
75 for (OleInvoiceItem item : ObjectPropertyUtils
76 .<List<OleInvoiceItem>> getPropertyValue(form, collection
77 .getBindingInfo().getBindingPath()))
78 if ("Qty".equals(item.getItemType().getItemTypeDescription()))
79 total++;
80 totalLines = total;
81
82 String extensionKey = collection.getId() + ".currentPage";
83 Map<String, Object> extension = form.getExtensionData();
84 int lastPage = total / pageSize + (total % pageSize == 0 ? 0 : 1);
85 Integer currentPage = (Integer) extension.get(extensionKey);
86 if (currentPage == null)
87 currentPage = 1;
88
89 String pageNumber = form.getPageNumber();
90 if (pageNumber != null)
91 switch (pageNumber) {
92 case UifConstants.PageRequest.FIRST:
93 currentPage = 1;
94 break;
95 case UifConstants.PageRequest.LAST:
96 currentPage = lastPage;
97 break;
98 case UifConstants.PageRequest.NEXT:
99 currentPage = Math.min(currentPage + 1, lastPage);
100 break;
101 case UifConstants.PageRequest.PREV:
102 currentPage = Math.max(currentPage - 1, 1);
103 break;
104 default:
105 try {
106 currentPage = Math.max(1,
107 Math.min(lastPage, Integer.parseInt(pageNumber)));
108 } catch (NumberFormatException e) {
109 LOG.warn("Invalid page number " + form.getPageNumber(), e);
110 }
111 break;
112 }
113 form.setPageNumber(null);
114
115 pager.setCurrentPage(currentPage);
116 pager.setNumberOfPages(lastPage);
117 this.currentPage = currentPage;
118
119 synchronized (extension) {
120 extension.put(extensionKey, currentPage);
121 }
122 }
123
124 private Group getRowDetails(OLEInvoiceForm form, String idSuffix,
125 int lineIndex, CollectionGroup collection, View view,
126 String bindingPath, OleInvoiceItem item) {
127 String lineId = collection.getBaseId() + idSuffix;
128 String selectedRowDetails = form.getSelectRowDetails();
129 String extensionKey = lineId + ".rowDetails";
130 Map<String, Object> extension = form.getExtensionData();
131 if (lineId.equals(selectedRowDetails))
132 synchronized (extension) {
133 if (form.isShowSelectedRowDetails())
134 extension.put(extensionKey, true);
135 else
136 extension.remove(extensionKey);
137 }
138
139 if (!Boolean.TRUE.equals(extension.get(extensionKey))) {
140 if (LOG.isDebugEnabled())
141 LOG.debug("Omitting row details " + extensionKey + ", "
142 + form.getSelectRowDetails() + " "
143 + form.isShowSelectedRowDetails());
144 return null;
145 }
146
147 if (LOG.isDebugEnabled())
148 LOG.debug("Including row details " + extensionKey + ", "
149 + form.getSelectRowDetails() + " "
150 + form.isShowSelectedRowDetails());
151
152 Group rowDetails = ComponentUtils.copy(rowDetailsGroup, idSuffix);
153 rowDetails.setFieldBindByNamePrefix(bindingPath);
154 ComponentUtils.updateContextForLine(rowDetails, item, lineIndex,
155 idSuffix);
156 view.getViewHelperService().spawnSubLifecyle(view, form, rowDetails,
157 collection, null, UifConstants.ViewPhases.INITIALIZE);
158
159 synchronized (components) {
160 components.add(rowDetails);
161 }
162
163 return rowDetails;
164 }
165
166 @Override
167 public void performApplyModel(View view, Object model, Container container) {
168 OLEInvoiceForm form = (OLEInvoiceForm) model;
169 CollectionGroup collection = (CollectionGroup) container;
170
171 setUpPager(form, collection);
172
173 if (LOG.isDebugEnabled())
174 LOG.debug(collection
175 + " ro is "
176 + collection.isReadOnly()
177 + " "
178 + ((CollectionGroup) collection).isRenderAddLine()
179 + " "
180 + ((CollectionGroup) collection)
181 .isRenderAddBlankLineButton());
182
183 foreignCurrency = ((OleInvoiceDocument) ((OLEInvoiceForm) model)
184 .getDocument()).isForeignCurrencyFlag();
185
186 super.performApplyModel(view, model, container);
187 }
188
189 @Override
190 public void buildLine(View view, Object model,
191 CollectionGroup collectionGroup, List<Field> lineFields,
192 List<FieldGroup> subCollectionFields, String bindingPath,
193 List<Action> actions, String idSuffix, Object currentLine,
194 int lineIndex) {
195
196 OleInvoiceItem item = (OleInvoiceItem) currentLine;
197 if (!"Qty".equals(item.getItemType().getItemTypeDescription())) {
198 filteredLines++;
199 return;
200 }
201
202 if (currentPage > 0
203 && (lineIndex < (currentPage - 1) * pageSize || lineIndex >= currentPage
204 * pageSize))
205 return;
206
207 OLEInvoiceForm form = (OLEInvoiceForm) model;
208
209 BindingInfo bi = new BindingInfo();
210 bi.setBindByNamePrefix(bindingPath
211 .startsWith(UifPropertyPaths.NEW_COLLECTION_LINES) ? bindingPath
212 : view.getDefaultBindingObjectPath() + '.' + bindingPath);
213
214 OleInvoiceItemsLine line = new OleInvoiceItemsLine();
215 line.setLineNumber(lineIndex);
216 line.setLineId(collectionGroup.getBaseId() + idSuffix);
217 line.setBindPath(bi.getBindByNamePrefix());
218 line.setItem(item);
219 line.setRowDetails(getRowDetails(form, idSuffix, lineIndex,
220 collectionGroup, view, bindingPath, item));
221
222 line.setActions(actions);
223
224 WorkflowDocument workflowDocument = form.getDocument()
225 .getDocumentHeader().getWorkflowDocument();
226 if ((workflowDocument.isInitiated() || workflowDocument.isSaved() || workflowDocument
227 .isEnroute())
228 && (item.getItemTitleId() == null || item
229 .getTempPurchaseOrderIdentifier() == null)) {
230 QuickFinder relinkQuickfinder = ComponentUtils.copy(
231 this.relinkQuickfinder, idSuffix);
232 relinkQuickfinder.updateFieldConversions(bi);
233 relinkQuickfinder.updateLookupParameters(bi);
234 relinkQuickfinder.updateReferencesToRefresh(bi);
235 ComponentUtils.updateContextForLine(relinkQuickfinder, currentLine,
236 lineIndex, idSuffix);
237 view.getViewHelperService().spawnSubLifecyle(view, model,
238 relinkQuickfinder, collectionGroup, null,
239 UifConstants.ViewPhases.INITIALIZE);
240 synchronized (components) {
241 components.add(relinkQuickfinder);
242 }
243 line.setRelinkQuickfinder(relinkQuickfinder);
244 }
245
246 if (actionRefreshId != null)
247 for (Action action : actions) {
248 action.setRefreshId(actionRefreshId);
249 if (LOG.isDebugEnabled())
250 LOG.debug("Action " + action.getId() + " refresh ID = "
251 + action.getRefreshId() + ", jump to "
252 + action.getJumpToIdAfterSubmit());
253 }
254
255 synchronized (components) {
256 components.addAll(actions);
257 }
258
259 synchronized (itemLines) {
260 itemLines.add(line);
261 }
262
263 displayedLines++;
264 }
265
266 public List<KeyValue> getDiscountKeyValues() {
267 return DISCOUNT_KEY_VALUES;
268 }
269
270 @Override
271 public FieldGroup getSubCollectionFieldGroupPrototype() {
272 return null;
273 }
274
275 public int getPageSize() {
276 return pageSize;
277 }
278
279 public void setPageSize(int pageSize) {
280 this.pageSize = pageSize;
281 }
282
283 public int getFilteredLines() {
284 return filteredLines;
285 }
286
287 public int getDisplayedLines() {
288 return displayedLines;
289 }
290
291 public int getTotalLines() {
292 return totalLines;
293 }
294
295 public List<OleInvoiceItemsLine> getItemLines() {
296 return itemLines;
297 }
298
299 public boolean isForeignCurrency() {
300 return foreignCurrency;
301 }
302
303 public void setForeignCurrency(boolean foreignCurrency) {
304 this.foreignCurrency = foreignCurrency;
305 }
306
307 public QuickFinder getRelinkQuickfinder() {
308 return relinkQuickfinder;
309 }
310
311 public void setRelinkQuickfinder(QuickFinder relinkQuickfinder) {
312 this.relinkQuickfinder = relinkQuickfinder;
313 }
314
315 public Group getRowDetailsGroup() {
316 return rowDetailsGroup;
317 }
318
319 public void setRowDetailsGroup(Group rowDetailsGroup) {
320 this.rowDetailsGroup = rowDetailsGroup;
321 }
322
323 public Pager getPager() {
324 return pager;
325 }
326
327 public void setPager(Pager pager) {
328 this.pager = pager;
329 }
330
331 public String getActionRefreshId() {
332 return actionRefreshId;
333 }
334
335 public void setActionRefreshId(String actionRefreshId) {
336 this.actionRefreshId = actionRefreshId;
337 }
338
339 @Override
340 public List<Component> getComponentPrototypes() {
341 List<Component> rv = super.getComponentPrototypes();
342 rv.add(rowDetailsGroup);
343 rv.add(relinkQuickfinder);
344 return rv;
345 }
346
347 @Override
348 public List<Component> getComponentsForLifecycle() {
349 List<Component> rv = super.getComponentsForLifecycle();
350 if (components != null)
351 rv.addAll(components);
352 if (pager != null)
353 rv.add(pager);
354 return rv;
355 }
356
357 @Override
358 protected <T> void copyProperties(T copy) {
359 super.copyProperties(copy);
360
361 OleInvoiceItemsLayout c = (OleInvoiceItemsLayout) copy;
362 c.pageSize = pageSize;
363 c.actionRefreshId = actionRefreshId;
364 if (rowDetailsGroup != null)
365 c.setRowDetailsGroup(ComponentUtils.copy(rowDetailsGroup));
366 if (relinkQuickfinder != null)
367 c.setRelinkQuickfinder(ComponentUtils.copy(relinkQuickfinder));
368 if (pager != null)
369 c.setPager(ComponentUtils.copy(pager));
370 }
371
372 }
373