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.OlePurchaseOrderItem;
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.krad.uif.UifConstants;
14  import org.kuali.rice.krad.uif.UifPropertyPaths;
15  import org.kuali.rice.krad.uif.component.Component;
16  import org.kuali.rice.krad.uif.container.CollectionGroup;
17  import org.kuali.rice.krad.uif.container.Container;
18  import org.kuali.rice.krad.uif.container.Group;
19  import org.kuali.rice.krad.uif.element.Action;
20  import org.kuali.rice.krad.uif.field.Field;
21  import org.kuali.rice.krad.uif.field.FieldGroup;
22  import org.kuali.rice.krad.uif.layout.CollectionLayoutManager;
23  import org.kuali.rice.krad.uif.layout.LayoutManagerBase;
24  import org.kuali.rice.krad.uif.util.ComponentUtils;
25  import org.kuali.rice.krad.uif.view.View;
26  
27  public class OleInvoicePOCurrentItemsLayout extends LayoutManagerBase implements
28  		CollectionLayoutManager {
29  
30  	private static final long serialVersionUID = 5289870490845303915L;
31  	private static final Logger LOG = Logger
32  			.getLogger(OleInvoicePOCurrentItemsLayout.class);
33  
34  	@SuppressWarnings("unchecked")
35  	private static final List<KeyValue> DISCOUNT_KEY_VALUES = new DiscountTypeValuesFinder()
36  			.getKeyValues();
37  
38  	private List<OleInvoicePOCurrentItemsLine> currentItemLines = new ArrayList<OleInvoicePOCurrentItemsLine>();
39  	private Group rowDetailsGroup;
40  	private boolean foreignCurrency;
41  
42  	private List<Component> components = new ArrayList<Component>();
43  
44  	@Override
45  	public void performApplyModel(View view, Object model, Container container) {
46  		if (LOG.isDebugEnabled())
47  			LOG.debug(container
48  					+ " ro is "
49  					+ container.isReadOnly()
50  					+ " "
51  					+ ((CollectionGroup) container).isRenderAddLine()
52  					+ " "
53  					+ ((CollectionGroup) container)
54  							.isRenderAddBlankLineButton());
55  
56  		foreignCurrency = ((OleInvoiceDocument) ((OLEInvoiceForm) model)
57  				.getDocument()).isForeignCurrencyFlag();
58  
59  		super.performApplyModel(view, model, container);
60  	}
61  
62  	private Group getRowDetails(OLEInvoiceForm form, String idSuffix,
63  			int lineIndex, CollectionGroup collection, View view,
64  			String bindingPath, OlePurchaseOrderItem item) {
65  		String lineId = collection.getBaseId() + idSuffix;
66  		String selectedRowDetails = form.getSelectRowDetails();
67  		String extensionKey = lineId + ".rowDetails";
68  		Map<String, Object> extension = form.getExtensionData();
69  		if (lineId.equals(selectedRowDetails))
70  			synchronized (extension) {
71  				if (form.isShowSelectedRowDetails())
72  					extension.put(extensionKey, true);
73  				else
74  					extension.remove(extensionKey);
75  			}
76  
77  		if (!Boolean.TRUE.equals(extension.get(extensionKey))) {
78  			if (LOG.isDebugEnabled())
79  				LOG.debug("Omitting row details " + extensionKey + ", "
80  						+ form.getSelectRowDetails() + " "
81  						+ form.isShowSelectedRowDetails());
82  			return null;
83  		}
84  
85  		if (LOG.isDebugEnabled())
86  			LOG.debug("Including row details " + extensionKey + ", "
87  					+ form.getSelectRowDetails() + " "
88  					+ form.isShowSelectedRowDetails());
89  
90  		Group rowDetails = ComponentUtils.copy(rowDetailsGroup, idSuffix);
91  		rowDetails.setFieldBindByNamePrefix(bindingPath);
92  		ComponentUtils.updateContextForLine(rowDetails, item, lineIndex,
93  				idSuffix);
94  		view.getViewHelperService().spawnSubLifecyle(view, form, rowDetails,
95  				collection, null, UifConstants.ViewPhases.INITIALIZE);
96  
97  		synchronized (components) {
98  			components.add(rowDetails);
99  		}
100 
101 		return rowDetails;
102 	}
103 
104 	@Override
105 	public void buildLine(View view, Object model,
106 			CollectionGroup collectionGroup, List<Field> lineFields,
107 			List<FieldGroup> subCollectionFields, String bindingPath,
108 			List<Action> actions, String idSuffix, Object currentLine,
109 			int lineIndex) {
110 		OlePurchaseOrderItem poItem = (OlePurchaseOrderItem) currentLine;
111 		OleInvoicePOCurrentItemsLine line = new OleInvoicePOCurrentItemsLine();
112 		line.setLineId(collectionGroup.getBaseId() + idSuffix);
113 		line.setBindPath(bindingPath
114 				.startsWith(UifPropertyPaths.NEW_COLLECTION_LINES) ? bindingPath
115 				: view.getDefaultBindingObjectPath() + '.' + bindingPath);
116 		line.setPoItem(poItem);
117 		line.setRowDetails(getRowDetails((OLEInvoiceForm) model, idSuffix,
118 				lineIndex, collectionGroup, view, bindingPath, poItem));
119 
120 		synchronized (currentItemLines) {
121 			currentItemLines.add(line);
122 		}
123 	}
124 
125 	@Override
126 	public FieldGroup getSubCollectionFieldGroupPrototype() {
127 		return null;
128 	}
129 
130 	public List<KeyValue> getDiscountKeyValues() {
131 		return DISCOUNT_KEY_VALUES;
132 	}
133 
134 	public List<OleInvoicePOCurrentItemsLine> getCurrentItemLines() {
135 		return currentItemLines;
136 	}
137 
138 	public void setCurrentItemLines(
139 			List<OleInvoicePOCurrentItemsLine> currentItemLines) {
140 		this.currentItemLines = currentItemLines;
141 	}
142 
143 	public boolean isForeignCurrency() {
144 		return foreignCurrency;
145 	}
146 
147 	public void setForeignCurrency(boolean foreignCurrency) {
148 		this.foreignCurrency = foreignCurrency;
149 	}
150 
151 	public Group getRowDetailsGroup() {
152 		return rowDetailsGroup;
153 	}
154 
155 	public void setRowDetailsGroup(Group rowDetailsGroup) {
156 		this.rowDetailsGroup = rowDetailsGroup;
157 	}
158 
159 	@Override
160 	public List<Component> getComponentPrototypes() {
161 		List<Component> rv = super.getComponentPrototypes();
162 		rv.add(rowDetailsGroup);
163 		return rv;
164 	}
165 
166 	@Override
167 	public List<Component> getComponentsForLifecycle() {
168 		List<Component> rv = super.getComponentsForLifecycle();
169 		if (components != null)
170 			rv.addAll(components);
171 		return rv;
172 	}
173 
174 	@Override
175 	protected <T> void copyProperties(T copy) {
176 		super.copyProperties(copy);
177 
178 		OleInvoicePOCurrentItemsLayout c = (OleInvoicePOCurrentItemsLayout) copy;
179 		if (rowDetailsGroup != null)
180 			c.setRowDetailsGroup(ComponentUtils.copy(rowDetailsGroup));
181 
182 	}
183 
184 }
185