001: package org.osbl.inventory.gui;
002:
003: import org.osbl.client.wings.form.*;
004: import org.osbl.client.wings.form.editor.ManyToOneAssociationEditor;
005: import org.osbl.client.wings.form.editor.OneToManyCompositionEditor;
006: import org.osbl.client.wings.form.layouter.*;
007: import org.osbl.client.wings.shell.Client;
008: import org.osbl.inventory.model.*;
009: import org.osbl.identity.gui.IdentityConcreteDomainProvider;
010: import org.osbl.identity.gui.IdentityEditor;
011: import org.osbl.identity.model.Identity;
012: import org.osbl.persistence.*;
013: import org.osbl.persistence.model.TreeNodeEntityFormat;
014: import org.osbl.ServiceProvider;
015: import org.conform.*;
016: import org.conform.modifier.InlineModifier;
017:
018: import javax.swing.event.ChangeListener;
019: import javax.swing.event.ChangeEvent;
020: import java.util.*;
021: import java.text.DateFormat;
022: import java.text.SimpleDateFormat;
023: import java.sql.Timestamp;
024: import java.math.BigDecimal;
025:
026: public class InventoryForm extends GenericObjectForm {
027: private IdentityConcreteDomainProvider<Employee> employeeDomain = new IdentityConcreteDomainProvider<Employee>(
028: Employee.class);
029: private IdentityConcreteDomainProvider<Supplier> supplierDomain = new IdentityConcreteDomainProvider<Supplier>(
030: Supplier.class);
031:
032: private AbstractDynamicDomainProvider costTypeDomain = new AbstractDynamicDomainProvider(
033: "costType") {
034: protected List createValues() {
035: Persistence persistence = (Persistence) ServiceProvider
036: .getInstance().getService("CostTypePersistence");
037: SimpleQueryCommand queryCommand = (SimpleQueryCommand) persistence
038: .createCommand("list");
039: queryCommand.setType(CostType.class);
040: queryCommand.addSort("path", Sort.ASCENDING);
041: queryCommand.addSort("name", Sort.ASCENDING);
042: List<CostType> list = queryCommand.execute();
043: list.add(0, null);
044: return list;
045: }
046:
047: protected boolean isSorted() {
048: return false;
049: }
050: };
051:
052: private AbstractDynamicDomainProvider locationDomain = new AbstractDynamicDomainProvider(
053: "location") {
054: protected List createValues() {
055: Persistence persistence = (Persistence) ServiceProvider
056: .getInstance().getService("LocationPersistence");
057: SimpleQueryCommand queryCommand = (SimpleQueryCommand) persistence
058: .createCommand("list");
059: queryCommand.setType(Location.class);
060: List<Location> list = queryCommand.execute();
061: list.add(0, null);
062: return list;
063: }
064: };
065:
066: private AllocationEditor allocationEditor = new AllocationEditor();
067:
068: protected Instruction createLayoutInstruction() {
069: return new PageSet(
070: new Page(
071: Inventory.class.getName(),
072: new Division(
073: Inventory.class.getName(),
074: new Column(
075: new LabelAndEditor(
076: ComponentProvider.ROOT,
077: "name"),
078: new LabelAndEditor(
079: ComponentProvider.ROOT,
080: "costType"),
081: new LabelAndEditor(
082: ComponentProvider.ROOT,
083: "employee"),
084: new LabelAndEditor(
085: ComponentProvider.ROOT,
086: "location"),
087: new LabelAndEditor(
088: ComponentProvider.ROOT,
089: "supplier"),
090: new LabelAndEditor(
091: ComponentProvider.ROOT,
092: "guarantee"),
093: new LabelAndEditor(
094: ComponentProvider.ROOT,
095: "costs"),
096: new LabelAndEditor(
097: ComponentProvider.ROOT,
098: "recurringCosts")),
099: new Column(
100: new LabelAndEditor(
101: ComponentProvider.ROOT,
102: "key"),
103: new LabelAndEditors(
104: "businessobject.created",
105: new Editor(
106: ComponentProvider.ROOT,
107: "created"),
108: new Editor(
109: ComponentProvider.ROOT,
110: "createdBy")),
111: new LabelAndEditor(
112: ComponentProvider.ROOT,
113: "state"),
114: new EmptyLine(),
115: new LabelAndEditor(
116: ComponentProvider.ROOT,
117: "barcode"))),
118: new Division(Inventory.class.getName()
119: + ".allocations", new Column(
120: new LabelAndEditor(
121: ComponentProvider.ROOT,
122: "allocations")))), new Page(
123: "businessobject.description", new Division(
124: "businessobject.description",
125: new Column(new LabelAndEditor(
126: ComponentProvider.ROOT,
127: "description")))));
128: }
129:
130: protected Object getObject(String path) {
131: return object;
132: }
133:
134: protected void setObject(String path, Object object) {
135: this .object = object;
136: }
137:
138: public void setObject(Object object) {
139: super .setObject(object);
140: ObjectContext context = getContext();
141: context.putObject("inventory", this .object);
142: allocationEditor.setContext(context);
143: }
144:
145: protected BeanMeta createBeanMeta(String path) {
146: VariationBeanMetaProvider provider = new VariationBeanMetaProvider(
147: Client.getInstance().getBeanMetaProvider());
148: provider.addModifier(new InlineModifier() {
149: protected void configure() {
150: property("employee").setDomainProvider(employeeDomain);
151: property("employee").setFormat(
152: Client.getInstance().getBeanMetaProvider()
153: .getBeanMeta(Identity.class)
154: .getFormat());
155: property("employee").setAttribute(
156: org.conform.wings.Editor.CUSTOM_EDITOR,
157: new ManyToOneAssociationEditor());
158: property("employee").setAttribute("objectEditor",
159: new IdentityEditor());
160: property("supplier").setDomainProvider(supplierDomain);
161: property("supplier").setFormat(
162: Client.getInstance().getBeanMetaProvider()
163: .getBeanMeta(Identity.class)
164: .getFormat());
165: property("supplier").setAttribute(
166: org.conform.wings.Editor.CUSTOM_EDITOR,
167: new ManyToOneAssociationEditor());
168: property("supplier").setAttribute("objectEditor",
169: new IdentityEditor());
170: property("costType").setDomainProvider(costTypeDomain);
171: property("costType").setFormat(
172: new TreeNodeEntityFormat(Client.getInstance()
173: .getBeanMetaProvider().getBeanMeta(
174: CostType.class).getFormat()));
175: property("costType").setAttribute(
176: org.conform.wings.Editor.CUSTOM_EDITOR,
177: new ManyToOneAssociationEditor());
178: property("costType").setAttribute("objectEditor",
179: new CostTypeEditor());
180: property("costType").setAttribute(
181: PropertyMeta.ATTRIBUTE_CHANGE_LISTENER,
182: new PropertyChangeHandler<CostType>() {
183: final DateFormat format = new SimpleDateFormat(
184: "yyMMdd");
185:
186: protected void propertyChange(
187: CostType oldValue, CostType costType) {
188: PropertyData allocationsData = propertyData("allocations");
189: List<Allocation> allocations = (List<Allocation>) allocationsData
190: .getValue();
191: if (allocations == null)
192: return;
193:
194: for (Allocation allocation : allocations) {
195: String key = AllocationForm
196: .synthesizeKey(
197: costType,
198: allocation
199: .getCostCenter());
200: allocation.setKey(key);
201: }
202: allocationsData.revalidate();
203: allocationEditor.getTableModel()
204: .refresh();
205:
206: if (get("barcode") != null
207: && get("key") != null)
208: return;
209:
210: Timestamp created = (Timestamp) get("created");
211: if (created != null && costType != null) {
212: String key = num(costType.getKey()
213: .toUpperCase()
214: + ""
215: + format.format(created));
216: set("key", key);
217: set("barcode", key);
218: } else {
219: set("key", null);
220: set("barcode", null);
221: }
222: }
223:
224: private String num(String string) {
225: Persistence persistence = (Persistence) ServiceProvider
226: .getInstance().getService(
227: "InventoryPersistence");
228: SimpleQueryCommand command = (SimpleQueryCommand) persistence
229: .createCommand("list");
230: command.setType(Inventory.class);
231: command.addFilter("barcode",
232: Operator.LIKE, string + "%");
233: List<Inventory> list = command
234: .execute();
235: return string + (list.size() + 1);
236: }
237: });
238: property("location").setDomainProvider(locationDomain);
239: property("location").setFormat(
240: Client.getInstance().getBeanMetaProvider()
241: .getBeanMeta(Location.class)
242: .getFormat());
243: property("location").setAttribute(
244: org.conform.wings.Editor.CUSTOM_EDITOR,
245: new ManyToOneAssociationEditor());
246: property("location").setAttribute("objectEditor",
247: new LocationEditor());
248: property("state").setInitializer(
249: new StaticInitializer(State.IN_USE));
250:
251: property("barcode").setAttribute(
252: org.conform.wings.Editor.CUSTOM_EDITOR,
253: new BarcodeEditor());
254: property("allocations").setAttribute(
255: org.conform.wings.Editor.CUSTOM_EDITOR,
256: new OneToManyCompositionEditor());
257: property("allocations").setAttribute("objectEditor",
258: allocationEditor);
259: allocationEditor
260: .addChangeListener(new ChangeListener() {
261: public void stateChanged(ChangeEvent e) {
262: BeanMeta beanMeta = getBeanMeta(ComponentProvider.ROOT);
263: PropertyMeta propertyMeta = beanMeta
264: .getProperty("allocations");
265: getBeanData(ComponentProvider.ROOT)
266: .getPropertyData(propertyMeta)
267: .revalidate();
268: }
269: });
270: property("allocations").addValidator(new Validator() {
271: public Object validate(Object value)
272: throws ValidationException {
273: List<Allocation> allocations = (List<Allocation>) value;
274: if (allocations == null)
275: return allocations;
276:
277: BigDecimal sumPercentage = new BigDecimal("0");
278: for (Iterator<Allocation> iterator = allocations
279: .iterator(); iterator.hasNext();) {
280: Allocation allocation = iterator.next();
281: sumPercentage = sumPercentage
282: .add(allocation.getPercentage());
283: }
284: if (!new BigDecimal("100")
285: .equals(sumPercentage))
286: throw new ValidationException(
287: new ValidationException.Message(
288: Inventory.class.getName()
289: + ".validation.allocations"));
290:
291: return allocations;
292: }
293: });
294:
295: property("costs").setAttribute(
296: PropertyMeta.ATTRIBUTE_CHANGE_LISTENER,
297: new PropertyChangeHandler<BigDecimal>() {
298: protected void propertyChange(
299: BigDecimal oldValue,
300: BigDecimal costs) {
301: if (costs == null)
302: return;
303:
304: PropertyData allocationsData = propertyData("allocations");
305: List<Allocation> allocations = (List<Allocation>) allocationsData
306: .getValue();
307: if (allocations == null)
308: return;
309:
310: for (Allocation allocation : allocations) {
311: BigDecimal amount = AllocationForm
312: .percentualCosts(allocation
313: .getPercentage(),
314: costs);
315: allocation.setAmount(amount);
316: }
317:
318: allocationsData.revalidate();
319: allocationEditor.getTableModel()
320: .refresh();
321: }
322: });
323: }
324: });
325: return provider.getBeanMeta(Inventory.class);
326: }
327: }
|