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