001: package org.osbl.orga.gui;
002:
003: import org.osbl.client.wings.form.*;
004: import org.osbl.client.wings.form.initializer.CreatedInitializer;
005: import org.osbl.client.wings.form.initializer.CreatedByInitializer;
006: import org.osbl.client.wings.form.layouter.*;
007: import org.osbl.client.wings.*;
008: import org.osbl.client.wings.shell.Client;
009: import org.conform.BeanMeta;
010: import org.conform.hibernate.HibernateEnvironment;
011: import org.osbl.orga.model.*;
012: import org.osbl.orga.logic.OrgaStructureLogic;
013: import org.osbl.ServiceProvider;
014: import org.osbl.persistence.Persistence;
015: import org.osbl.persistence.SaveOrUpdateCommand;
016: import org.wings.*;
017:
018: import javax.swing.*;
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.util.*;
022: import java.sql.Timestamp;
023:
024: public class OrganisationsForm extends GenericObjectForm implements
025: HierarchicalContextAware {
026: private DuplicateOrganisationAction duplicateOrganisationAction = new DuplicateOrganisationAction();
027:
028: Object object;
029: private GenericObjectTreeNode node;
030:
031: @Override
032: public Object getObject() {
033: return object;
034: }
035:
036: @Override
037: public void setObject(Object object) {
038: this .object = object;
039: }
040:
041: @Override
042: protected Object getObject(String path) {
043: if (ComponentProvider.ROOT.equals(path))
044: return object;
045:
046: return null;
047: }
048:
049: @Override
050: protected void setObject(String path, Object object) {
051: if (ComponentProvider.ROOT.equals(path))
052: this .object = object;
053: }
054:
055: @Override
056: protected Instruction createLayoutInstruction() {
057: return new Page("Organisations", new Division("blub",
058: new Column(new CustomEditor("duplicate"))));
059: }
060:
061: protected SComponent createCustomComponent(String key) {
062: return new XButton(duplicateOrganisationAction);
063: }
064:
065: @Override
066: protected BeanMeta createBeanMeta(String path) {
067: return null;
068: }
069:
070: public Class getType() {
071: return String.class;
072: }
073:
074: public void setNode(GenericObjectTreeNode node) {
075: this .node = node;
076: }
077:
078: private class DuplicateOrganisationAction extends AbstractAction {
079: private Timestamp created;
080: private String createdBy;
081: private String name;
082: private Organisation newOrganisation;
083: private SaveOrUpdateCommand saveOrUpdateCommand;
084: private Group newGroup;
085: Map<Member, Member> memberMap;
086: Map<Long, Long> groupMap;
087:
088: public DuplicateOrganisationAction() {
089: putValue(Action.NAME, Client.getInstance()
090: .getResourceProvider().getMessage(
091: Organisation.class.getName()
092: + ".buttons.duplicate"));
093: putValue(Action.ACTION_COMMAND_KEY, Organisation.class
094: .getName()
095: + ".buttons.duplicate");
096: }
097:
098: public void actionPerformed(ActionEvent event) {
099: OrgaTreeModel model = (OrgaTreeModel) node.getModel();
100: final SComboBox combo = new SComboBox(model
101: .getOrgaListModel());
102: final STextField textField = new STextField();
103:
104: SPanel panel = new SPanel(new SGridLayout(2, 2, 10, 10));
105: panel.add(new SLabel(Client.getInstance()
106: .getResourceProvider().getMessage(
107: Organisation.class.getName()
108: + ".titles.copyOrganisation")));
109: panel.add(combo);
110: panel.add(new SLabel(Client.getInstance()
111: .getResourceProvider().getMessage(
112: Organisation.class.getName()
113: + ".titles.copyName")));
114: panel.add(textField);
115:
116: XOptionPane.showInputDialog(getComponent(), Client
117: .getInstance().getResourceProvider().getMessage(
118: Organisation.class.getName()
119: + ".titles.copyMessage"), Client
120: .getInstance().getResourceProvider().getMessage(
121: Organisation.class.getName()
122: + ".buttons.duplicate"), panel,
123: new ActionListener() {
124: public void actionPerformed(ActionEvent event) {
125: if (event.getActionCommand() == XOptionPane.CANCEL_ACTION)
126: return;
127:
128: OrganisationTreeNode organisationTreeNode = (OrganisationTreeNode) combo
129: .getSelectedItem();
130: Organisation organisation = organisationTreeNode
131: .getObject();
132:
133: name = textField.getText();
134: if (name == null || organisation == null)
135: return;
136:
137: created = new CreatedInitializer()
138: .getValue();
139: createdBy = new CreatedByInitializer()
140: .getValue();
141:
142: try {
143: HibernateEnvironment.getInstance()
144: .beginTransaction();
145:
146: Persistence orgaPersistence = (Persistence) ServiceProvider
147: .getInstance().getService(
148: "OrgaPersistence");
149: saveOrUpdateCommand = (SaveOrUpdateCommand) orgaPersistence
150: .createCommand("save");
151: saveOrUpdateCommand
152: .setObject(organisation);
153: saveOrUpdateCommand.execute();
154:
155: newOrganisation = new Organisation();
156: newOrganisation.setCreated(created);
157: newOrganisation.setCreatedBy(createdBy);
158: newOrganisation.setKey(name);
159: if (organisation.getRelations() != null)
160: newOrganisation
161: .setRelations(new ArrayList<String>(
162: organisation
163: .getRelations()));
164: if (organisation.getRoles() != null)
165: newOrganisation
166: .setRoles(new ArrayList<String>(
167: organisation
168: .getRoles()));
169: /*
170: TODO: copy members
171: if (organisation.getMembers() != null)
172: newOrganisation.setMembers(copyMembers(organisation.getMembers()));
173: */
174:
175: saveOrUpdateCommand
176: .setObject(newOrganisation);
177: saveOrUpdateCommand.execute();
178:
179: OrgaTreeModel orgaTreeModel = (OrgaTreeModel) node
180: .getModel();
181: OrgaStructureLogic orgaStructureLogic = orgaTreeModel
182: .getTreeLogic();
183: List<Group> groups = orgaStructureLogic
184: .all(organisation.getId());
185: copyGroups(groups);
186:
187: OrganisationTreeNode newOrganisationTreeNode = new OrganisationTreeNode(
188: newOrganisation);
189: OrganisationsTreeNode parent = organisationTreeNode
190: .getParent();
191: parent.insert(newOrganisationTreeNode,
192: -1);
193: } finally {
194: HibernateEnvironment.getInstance()
195: .endTransaction();
196: }
197: }
198: });
199: }
200:
201: private void copyGroups(List<Group> groups) {
202: groupMap = new HashMap<Long, Long>();
203: Collections.sort(groups,
204: OrgaTreeModel.PATH_LENGTH_COMPARATOR);
205: List<Group> copies = new ArrayList<Group>(groups.size());
206:
207: for (Group group : groups) {
208: newGroup = new Group();
209: newGroup.setCreated(created);
210: newGroup.setCreatedBy(createdBy);
211: newGroup.setKey(group.getKey());
212: newGroup.setIdentity(group.getIdentity());
213: newGroup.setTree(newOrganisation);
214:
215: saveOrUpdateCommand.setObject(newGroup);
216: saveOrUpdateCommand.execute();
217:
218: newGroup.setMemberships(copyMemberships(group
219: .getMemberships()));
220:
221: groupMap.put(group.getId(), newGroup.getId());
222: newGroup.setPath(copyPath(group.getPath()));
223: newGroup.setDepth(group.getDepth());
224: copies.add(newGroup);
225: }
226: }
227:
228: private String copyPath(String path) {
229: StringBuilder newPath = new StringBuilder(path.length() * 2);
230: newPath.append('/');
231: for (StringTokenizer tokenizer = new StringTokenizer(path,
232: "/"); tokenizer.hasMoreTokens();) {
233: String part = tokenizer.nextToken();
234: Long newPart = groupMap.get(new Long(part));
235: newPath.append(newPart);
236: newPath.append('/');
237: }
238: return newPath.toString();
239: }
240:
241: private List<Membership> copyMemberships(
242: List<Membership> memberships) {
243: List<Membership> copies = new ArrayList<Membership>(
244: memberships.size());
245: for (Membership membership : memberships) {
246: Membership newMembership = new Membership();
247: newMembership.setGroup(newGroup);
248: newMembership.setRole(membership.getRole());
249: newMembership.setMember(memberMap.get(membership
250: .getMember()));
251:
252: saveOrUpdateCommand.setObject(newMembership);
253: saveOrUpdateCommand.execute();
254:
255: copies.add(newMembership);
256: }
257: return copies;
258: }
259:
260: private List<Member> copyMembers(List<Member> members) {
261: memberMap = new HashMap<Member, Member>();
262: List<Member> copies = new ArrayList<Member>(members.size());
263: for (Member member : members) {
264: Member newMember = new Member();
265: newMember.setCreated(created);
266: newMember.setCreatedBy(createdBy);
267: newMember.setIdentity(member.getIdentity());
268: newMember.setKey(name + "_"
269: + member.getIdentity().getKey());
270: newMember.setOrganisation(newOrganisation);
271: newMember.setRelationships(copyRelationShips(member
272: .getRelationships()));
273:
274: saveOrUpdateCommand.setObject(newMember);
275: saveOrUpdateCommand.execute();
276:
277: memberMap.put(member, newMember);
278: copies.add(newMember);
279: }
280: return copies;
281: }
282:
283: private List<Relationship> copyRelationShips(
284: List<Relationship> member) {
285: return null;
286: }
287: }
288: }
|