001: package org.osbl.client.wings.concern;
002:
003: import org.concern.*;
004: import org.osbl.client.action.*;
005: import org.osbl.client.wings.form.*;
006: import org.osbl.client.wings.*;
007: import org.osbl.client.wings.shell.*;
008: import org.osbl.client.ClientServiceProvider;
009: import org.osbl.plugin.*;
010: import org.concern.controller.LoaderFactory;
011: import org.conform.hibernate.HibernateEnvironment;
012: import org.wings.session.SessionManager;
013: import org.wings.SComponent;
014:
015: import javax.swing.*;
016: import java.awt.event.ActionEvent;
017: import java.awt.event.ActionListener;
018: import java.util.*;
019: import java.sql.Timestamp;
020:
021: public class SubjectEditor extends GenericObjectEditor {
022: OptionForm optionForm = new OptionForm();
023: SubjectForm subjectForm = new SubjectForm();
024:
025: ExerciseOptionAction exerciseOptionAction = new ExerciseOptionAction(
026: base());
027: CancelOptionAction cancelOptionAction = new CancelOptionAction(
028: base());
029:
030: protected void initializeTools() {
031: PluginManager pluginManager = (PluginManager) ClientServiceProvider
032: .getInstance().getService("ClientPluginManager");
033: final ExtensionPoint extensionPoint = new ExtensionPoint(
034: "org.osbl.client.wings.form.subjectEditorTools",
035: EditorTool.class);
036: final List<Extension> list = pluginManager
037: .getExtensions(extensionPoint);
038:
039: for (Extension extension : list) {
040: final Class<EditorTool> toolClass = (Class<EditorTool>) extension
041: .getImplementation();
042: try {
043: EditorTool tool = toolClass.newInstance();
044: tool.setEditor(this );
045: tools.add(tool);
046: } catch (Exception e) {
047: throw new RuntimeException(e);
048: }
049: }
050: }
051:
052: protected void initializeEditor() {
053: editAction = new SubjectAction(base());
054: }
055:
056: protected ObjectForm initializeForm() {
057: NestedObjectForm form = getObject() instanceof Work ? optionForm
058: : subjectForm;
059: form.getEnvironment().setTools(Collections.EMPTY_LIST);
060: form.getEnvironment().setControls(Collections.EMPTY_LIST);
061: return form;
062: }
063:
064: protected void listControls(Environment environment) {
065: }
066:
067: protected ObjectList initializeList() {
068: final SubjectList subjectList = new SubjectList();
069: /*
070: subjectList.setLinkAction("subjectLine", new AbstractObjectAction() {
071: public void actionPerformed(ObjectActionEvent event) {
072: Object object = event.getTargets()[0];
073: try {
074: revertObject(object);
075: subjectList.setCurrent(object);
076: setObject(object);
077:
078: if (editAction.isEnabled())
079: editAction.actionPerformed(new ActionEvent(SubjectEditor.this, 0, "edit"));
080: }
081: catch (Exception e) {
082: String name = Client.getInstance().getResourceProvider().getMessage(getType().getName());
083: String message = Client.getInstance().getResourceProvider().getMessage("businessobject.messages.doesNotExist", name);
084: XOptionPane.showMessageDialog(null, message, name, XOptionPane.ERROR_MESSAGE, new ActionListener() {
085: public void actionPerformed(ActionEvent e) {
086: objectList.refresh();
087: }
088: });
089: }
090: }
091: });
092: */
093: return subjectList;
094: }
095:
096: protected String linkPropertyName() {
097: return "subjectLine";
098: }
099:
100: public Class getType() {
101: return Subject.class;
102: }
103:
104: public void showForm() {
105: if (getForm() instanceof SubjectForm) {
106: SubjectForm form = (SubjectForm) getForm();
107: form.setSubject((Subject) getObject());
108: optionForm.getEnvironment().setActive(false);
109: objectForm.getEnvironment().setActive(true);
110: } else if (getForm() instanceof OptionForm) {
111: OptionForm form = (OptionForm) getForm();
112: form.setOption((Work) getObject());
113: objectForm.getEnvironment().setActive(false);
114: optionForm.getEnvironment().setActive(true);
115: }
116: super .showForm();
117:
118: if (getObject() instanceof Subject) {
119: List<SComponent> controls = new ArrayList<SComponent>();
120: controls.add(new XButton(listAction));
121: controls.add(new XButton(previousAction));
122: controls.add(new XButton(nextAction));
123: optionActions(controls, (Subject) getObject());
124: objectForm.getEnvironment().setControls(controls);
125: } else if (getObject() instanceof Work) {
126: List<SComponent> controls = new ArrayList<SComponent>();
127: controls.add(new XButton(cancelOptionAction));
128: controls.add(new XButton(exerciseOptionAction));
129: optionForm.getEnvironment().setControls(controls);
130: }
131: }
132:
133: private void optionActions(List<SComponent> controls,
134: Subject subject) {
135: Set options = subject.getOptions();
136: System.out.println("options = " + options);
137: int optionCount = options.size();
138: if (optionCount > 0) {
139: Worklist worklist = ControllerLookup.getInstance()
140: .getWorklist();
141: String locale = Client.getInstance().getLocale().toString();
142:
143: for (Iterator iterator = options.iterator(); iterator
144: .hasNext();) {
145: String option = (String) iterator.next();
146: Work work = worklist.getWork(subject.getProcess(),
147: subject.getSubjectId(), option, locale);
148: work.setSubject(subject.getSubject());
149: OptionAction action = new OptionAction(work);
150: controls.add(new XButton(action));
151: }
152: }
153: environment.setStatusMessage(null);
154: }
155:
156: public void setObject(Object object) {
157: super .setObject(object);
158: if ((object instanceof Subject && objectForm instanceof OptionForm)
159: || (object instanceof Work && objectForm instanceof SubjectForm))
160: objectForm = null;
161: }
162:
163: public void saveObject(Object object) {
164: if (object instanceof Work) {
165: Work option = (Work) object;
166: Controller controller = ControllerLookup.getInstance()
167: .getController(option.getProcess());
168: Loader loader = new LoaderFactory().getLoader(controller);
169: if (loader instanceof Persister) {
170: Persister persister = (Persister) loader;
171: persister.update(option.getSubject());
172: }
173: } else {
174: Subject subject = (Subject) object;
175: Controller controller = ControllerLookup.getInstance()
176: .getController(subject.getProcess());
177: Loader loader = new LoaderFactory().getLoader(controller);
178: if (loader instanceof Persister) {
179: Persister persister = (Persister) loader;
180: persister.update(subject.getSubject());
181: }
182: }
183: }
184:
185: public void deleteObject(Object object) {
186: }
187:
188: public void revertObject(Object object) {
189: Subject subject = (Subject) object;
190: Controller controller = ControllerLookup.getInstance()
191: .getController(subject.getProcess());
192: Loader loader = new LoaderFactory().getLoader(controller);
193: try {
194: loader.load(subject.getSubjectId());
195: } catch (SubjectNotFoundException e) {
196: throw new RuntimeException(e);
197: }
198: }
199:
200: protected boolean exercise() {
201: if (!objectForm.hasValidationIssues()) {
202: saveObject(objectForm.getObject());
203: return true;
204: } else
205: return false;
206: }
207:
208: protected class SubjectAction extends EditAction {
209: public SubjectAction(String base) {
210: super (base);
211: }
212:
213: public void actionPerformed(ActionEvent e) {
214: showForm();
215: previousAction.setEnabled(objectList.hasPrevious());
216: nextAction.setEnabled(objectList.hasNext());
217: }
218: }
219:
220: private class OptionAction extends AbstractAction {
221: private Work work;
222:
223: public OptionAction(Work work) {
224: this .work = work;
225: putValue(Action.ACTION_COMMAND_KEY, work.getActivity());
226: putValue(Action.NAME, formatOption(work));
227:
228: String user = SessionManager.getSession()
229: .getServletRequest().getUserPrincipal().getName();
230: boolean locked = work.getLockedBy() != null
231: && !user.equals(work.getLockedBy())
232: && work.getLockedUntil().after(new Date());
233: super .setEnabled(!locked);
234: }
235:
236: private String formatOption(Work work) {
237: return Client.getInstance().getResourceProvider()
238: .getMessage(
239: "process." + work.getProcess() + "."
240: + work.getActivity());
241: }
242:
243: public void actionPerformed(ActionEvent event) {
244: try {
245: if (!ControllerLookup.getInstance().getWorklist().lock(
246: work,
247: SessionManager.getSession().getServletRequest()
248: .getUserPrincipal().getName(),
249: fiveMinutes())) {
250: environment
251: .setStatusMessage(new Client.Message(
252: Client.Message.TYPE_WARNING,
253: Client
254: .getInstance()
255: .getResourceProvider()
256: .getMessage(
257: "businessobject.messages.locked")));
258: return;
259: }
260: environment.setStatusMessage(null);
261: System.out.println("lock");
262:
263: setObject(work);
264: objectForm = null;
265: showForm();
266: } catch (Exception e) {
267: e.printStackTrace();
268: }
269: }
270:
271: public void setEnabled(boolean newValue) {
272: }
273: }
274:
275: protected class ExerciseOptionAction extends AbstractAction {
276: public ExerciseOptionAction(String base) {
277: putValue(Action.ACTION_COMMAND_KEY, base
278: + ".buttons.exerciseOption");
279: }
280:
281: public void actionPerformed(ActionEvent event) {
282: Work work = (Work) getObject();
283: Controller controller = ControllerLookup.getInstance()
284: .getController(work.getProcess());
285: try {
286: HibernateEnvironment.getInstance().beginTransaction();
287:
288: if (exercise()) {
289: Map annotations = new HashMap();
290: annotations.put("user", SessionManager.getSession()
291: .getServletRequest().getUserPrincipal()
292: .getName());
293: controller.complete(work.getSubjectId(), work
294: .getActivity(), annotations);
295: controller.announceSubject(work.getSubjectId());
296:
297: System.out.println("unlock");
298: ControllerLookup.getInstance().getWorklist()
299: .unlock(
300: work,
301: SessionManager.getSession()
302: .getServletRequest()
303: .getUserPrincipal()
304: .getName());
305: }
306: } catch (Exception e) {
307: e.printStackTrace();
308: } finally {
309: HibernateEnvironment.getInstance().endTransaction();
310: }
311:
312: /*
313: try {
314: if (completed)
315: controller.process(concern.getSubjectId());
316: }
317: catch (Exception e) {
318: e.printStackTrace();
319: }
320: */
321:
322: setObject(subjectForm.getObject());
323: try {
324: Set options = new TreeSet(controller.getOptions(work
325: .getSubjectId()));
326: ((Subject) getObject()).setOptions(options);
327: } catch (UnknownSubjectException e) {
328: e.printStackTrace();
329: }
330:
331: showForm();
332: }
333:
334: public void setEnabled(boolean newValue) {
335: }
336: }
337:
338: protected class CancelOptionAction extends AbstractAction {
339: public CancelOptionAction(String base) {
340: putValue(Action.ACTION_COMMAND_KEY, base
341: + ".buttons.cancelOption");
342: }
343:
344: public void actionPerformed(ActionEvent event) {
345: try {
346: HibernateEnvironment.getInstance().beginTransaction();
347:
348: Work work = (Work) getObject();
349:
350: setObject(subjectForm.getObject());
351: showForm();
352:
353: System.out.println("unlock");
354: ControllerLookup.getInstance().getWorklist().unlock(
355: work,
356: SessionManager.getSession().getServletRequest()
357: .getUserPrincipal().getName());
358: } catch (Exception e) {
359: e.printStackTrace();
360: } finally {
361: HibernateEnvironment.getInstance().endTransaction();
362: }
363: }
364:
365: public void setEnabled(boolean newValue) {
366: }
367: }
368:
369: private Timestamp fiveMinutes() {
370: return new Timestamp(System.currentTimeMillis() + 1000 * 60 * 5);
371: }
372: }
|