001: package org.osbl.orga.gui;
002:
003: import org.conform.hibernate.HibernateEnvironment;
004: import org.osbl.ServiceProvider;
005: import org.osbl.client.wings.form.*;
006: import org.osbl.orga.logic.OrgaStructureLogic;
007: import org.osbl.orga.model.*;
008: import org.osbl.persistence.*;
009:
010: import java.util.*;
011:
012: public class OrgaEditor extends GenericObjectTreeEditor {
013: Persistence persistence = (Persistence) ServiceProvider
014: .getInstance().getService("OrgaPersistence");
015: OrgaStructureLogic orgaStructureLogic = (OrgaStructureLogic) ServiceProvider
016: .getInstance().getService("OrgaStructureLogic");
017:
018: protected NodeForm initializeForm() {
019: NodeForm nodeForm = new NodeForm();
020: nodeForm.addFormClass(String.class, OrganisationsForm.class);
021: nodeForm.addFormClass(Organisation.class,
022: OrganisationForm.class);
023: nodeForm.addFormClass(Group.class, GroupForm.class);
024: nodeForm.addFormClass(Membership.class, MemberShipForm.class);
025:
026: return nodeForm;
027: }
028:
029: protected GenericObjectTreeList initializeList() {
030: return new OrgaTreeList();
031: }
032:
033: protected List loadParentPath(Object object) {
034: if (object instanceof Group) {
035: Group group = (Group) object;
036: return orgaStructureLogic.ancestors(
037: group.getTree().getId(), group.getId());
038: }
039: if (object instanceof Organisation) {
040: List list = new ArrayList(1);
041: list.add(object);
042: return list;
043: }
044: if (object instanceof Membership) {
045: Group group = ((Membership) object).getGroup();
046: List ancestors = orgaStructureLogic.ancestors(group
047: .getTree().getId(), group.getId());
048: ancestors.add(object);
049: return ancestors;
050:
051: }
052: return Collections.EMPTY_LIST;
053: }
054:
055: public Class getType() {
056: return Organisation.class;
057: }
058:
059: public void saveObject(Object object) {
060: GenericObjectTreeNode node = (GenericObjectTreeNode) object;
061: object = node.getObject();
062:
063: SaveOrUpdateCommand command = (SaveOrUpdateCommand) persistence
064: .createCommand("save");
065: try {
066: HibernateEnvironment.getInstance().beginTransaction();
067: command.setObject(object);
068: command.execute();
069:
070: node.connect();
071:
072: if (node instanceof MembershipTreeNode) {
073: command.setObject(node.getParent().getObject());
074: command.execute();
075: }
076: } finally {
077: HibernateEnvironment.getInstance().endTransaction();
078: }
079: }
080:
081: public void deleteObject(Object object) {
082: GenericObjectTreeNode node = (GenericObjectTreeNode) object;
083: try {
084: HibernateEnvironment.getInstance().beginTransaction();
085:
086: SaveOrUpdateCommand updateCommand = (SaveOrUpdateCommand) persistence
087: .createCommand("save");
088: updateCommand.setObject(node.getObject());
089: updateCommand.execute();
090:
091: if (Organisation.class.isAssignableFrom(node
092: .getObjectType())) {
093: Organisation organisation = (Organisation) node
094: .getObject();
095: List<Group> groups = ((OrgaTreeModel) node.getModel())
096: .getTreeLogic().all(organisation.getId());
097: Collections.sort(groups,
098: OrgaTreeModel.PATH_LENGTH_COMPARATOR);
099: Collections.reverse(groups);
100:
101: List list = groups;
102:
103: // TODO: bulk delete
104: SimpleQueryCommand<Member> listCommand = (SimpleQueryCommand<Member>) persistence
105: .createCommand("list");
106: listCommand.setType(Member.class);
107: listCommand.addFilter("organisation", Operator.EQUAL,
108: organisation);
109: list.addAll(listCommand.execute());
110:
111: list.add(organisation);
112: deleteAllObjects(groups);
113: } else if (Group.class.isAssignableFrom(node
114: .getObjectType())) {
115: Group group = (Group) node.getObject();
116: List<Group> descendants = ((OrgaTreeModel) node
117: .getModel()).getTreeLogic().descendants(
118: group.getTree().getId(), group.getId());
119: Collections.sort(descendants,
120: OrgaTreeModel.PATH_LENGTH_COMPARATOR);
121: Collections.reverse(descendants);
122:
123: descendants.add(group);
124: deleteAllObjects(descendants);
125: } else if (Membership.class.isAssignableFrom(node
126: .getObjectType())) {
127: Membership membership = (Membership) node.getObject();
128: Group group = membership.getGroup();
129: RevertCommand revertCommand = (RevertCommand) persistence
130: .createCommand("revert");
131: revertCommand.setObject(group);
132: revertCommand.execute();
133:
134: group.getMemberships().remove(membership);
135: deleteAllObjects(Collections.singletonList(membership));
136:
137: node.getParent().setObject(
138: node.getParent().loadObject());
139: }
140: } finally {
141: HibernateEnvironment.getInstance().endTransaction();
142: }
143: }
144:
145: public void revertObject(Object object) {
146: }
147:
148: private void deleteAllObjects(List objects) {
149: DeleteCommand command = (DeleteCommand) persistence
150: .createCommand("delete");
151:
152: for (Object object : objects) {
153: command.setObject(object);
154: command.execute();
155: }
156: }
157: }
|