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