001: package org.osbl.inventory.gui;
002:
003: import org.conform.hibernate.HibernateEnvironment;
004: import org.osbl.ServiceProvider;
005: import org.osbl.client.wings.form.*;
006: import org.osbl.inventory.logic.InventoryLogic;
007: import org.osbl.inventory.model.CostType;
008: import org.osbl.inventory.model.CostTypeHierarchy;
009: import org.osbl.persistence.*;
010:
011: import java.util.*;
012:
013: public class CostTypeEditor extends GenericObjectTreeEditor {
014: Persistence persistence = (Persistence) ServiceProvider
015: .getInstance().getService("CostTypePersistence");
016: InventoryLogic inventoryLogic = (InventoryLogic) ServiceProvider
017: .getInstance().getService("InventoryLogic");
018:
019: protected NodeForm initializeForm() {
020: NodeForm nodeForm = new NodeForm();
021: nodeForm.addFormClass(String.class,
022: CostTypeHierarchiesForm.class);
023: nodeForm.addFormClass(CostTypeHierarchy.class,
024: CostTypeHierarchyForm.class);
025: nodeForm.addFormClass(CostType.class, CostTypeForm.class);
026:
027: return nodeForm;
028: }
029:
030: protected GenericObjectTreeList initializeList() {
031: return new CostTypeTreeList();
032: }
033:
034: protected List loadParentPath(Object object) {
035: if (object instanceof CostType) {
036: CostType costType = (CostType) object;
037: return inventoryLogic.ancestors(costType.getTree().getId(),
038: costType.getId());
039: }
040: if (object instanceof CostTypeHierarchy) {
041: List list = new ArrayList(1);
042: list.add(object);
043: return list;
044: }
045: return Collections.EMPTY_LIST;
046: }
047:
048: public Class getType() {
049: return CostType.class;
050: }
051:
052: public void saveObject(Object object) {
053: GenericObjectTreeNode node = (GenericObjectTreeNode) object;
054: object = node.getObject();
055:
056: SaveOrUpdateCommand command = (SaveOrUpdateCommand) persistence
057: .createCommand("save");
058: try {
059: HibernateEnvironment.getInstance().beginTransaction();
060: command.setObject(object);
061: command.execute();
062:
063: node.connect();
064: } finally {
065: HibernateEnvironment.getInstance().endTransaction();
066: }
067: }
068:
069: public void deleteObject(Object object) {
070: GenericObjectTreeNode node = (GenericObjectTreeNode) object;
071: try {
072: HibernateEnvironment.getInstance().beginTransaction();
073:
074: SaveOrUpdateCommand updateCommand = (SaveOrUpdateCommand) persistence
075: .createCommand("save");
076: updateCommand.setObject(node.getObject());
077: updateCommand.execute();
078:
079: if (CostType.class.isAssignableFrom(node.getObjectType())) {
080: CostType costType = (CostType) node.getObject();
081: List<CostType> descendants = ((CostTypeTreeModel) node
082: .getModel()).getTreeLogic().descendants(
083: costType.getTree().getId(), costType.getId());
084: Collections.sort(descendants,
085: CostTypeTreeModel.PATH_LENGTH_COMPARATOR);
086: System.out.println("descendants = " + descendants);
087: Collections.reverse(descendants);
088: System.out.println("descendants = " + descendants);
089:
090: descendants.add(costType);
091: deleteAllObjects(descendants);
092: } else if (CostTypeHierarchy.class.isAssignableFrom(node
093: .getObjectType())) {
094: CostTypeHierarchy costTypeHierarchy = (CostTypeHierarchy) node
095: .getObject();
096: List<CostType> descendants = ((CostTypeTreeModel) node
097: .getModel()).getTreeLogic().all(
098: costTypeHierarchy.getId());
099: Collections.sort(descendants,
100: CostTypeTreeModel.PATH_LENGTH_COMPARATOR);
101: System.out.println("descendants = " + descendants);
102: Collections.reverse(descendants);
103: System.out.println("descendants = " + descendants);
104:
105: List objects = new ArrayList(descendants);
106: objects.add(costTypeHierarchy);
107: deleteAllObjects(objects);
108: }
109: } finally {
110: HibernateEnvironment.getInstance().endTransaction();
111: }
112: }
113:
114: public void revertObject(Object object) {
115: }
116:
117: private void deleteAllObjects(List objects) {
118: DeleteCommand command = (DeleteCommand) persistence
119: .createCommand("delete");
120:
121: for (Object object : objects) {
122: command.setObject(object);
123: command.execute();
124: }
125: }
126: }
|