001: package org.osbl.client.wings.concern;
002:
003: import org.concern.*;
004: import org.concern.model.*;
005: import org.osbl.client.wings.form.*;
006: import org.concern.controller.LoaderFactory;
007: import org.conform.hibernate.HibernateEnvironment;
008:
009: import java.util.*;
010:
011: public class SubjectForm extends NestedObjectForm {
012: private Map<String, ObjectForm> documentForms = new HashMap<String, ObjectForm>();
013: private Map<String, List> environments = new HashMap<String, List>();
014: private boolean edit;
015:
016: public SubjectForm() {
017: }
018:
019: public SubjectForm(boolean edit) {
020: this .edit = edit;
021: }
022:
023: public boolean isEdit() {
024: return edit;
025: }
026:
027: public void setEdit(boolean edit) {
028: this .edit = edit;
029: }
030:
031: public Subject getSubject() {
032: return (Subject) getObject();
033: }
034:
035: public void setSubject(Subject subject) {
036: if (subject == null)
037: return;
038:
039: String key = subject.getProcess();
040:
041: if (nestedForm != null)
042: nestedForm.getEnvironment().setActive(false);
043:
044: nestedForm = documentForms.get(key);
045:
046: if (nestedForm == null) {
047: Controller controller = ControllerLookup.getInstance()
048: .getController(subject.getProcess());
049: org.concern.model.Process process = controller.getProcess();
050: String formClassName = (String) process
051: .getEnvironmentEntry(edit ? "edit.formClassName"
052: : "view.formClassName");
053: if (formClassName == null)
054: formClassName = (String) process
055: .getEnvironmentEntry("subject.formClassName");
056: if (formClassName == null)
057: throw new UnsupportedOperationException(
058: "There's no SubjectForm defined for this Process: "
059: + key);
060: try {
061: Class formClass = Class.forName(formClassName);
062: nestedForm = (ObjectForm) formClass.newInstance();
063: if (nestedForm instanceof ObjectContextAware) {
064: ObjectContextAware contextAware = (ObjectContextAware) nestedForm;
065: getContext().putObject("environment",
066: process.getEnvironment());
067: getContext().putObject("process.environment",
068: process.getEnvironment());
069: contextAware.setContext(getContext());
070: }
071: nestedForm.getEnvironment().setDelegate(
072: nestedEnvironment);
073: } catch (Exception e) {
074: throw new RuntimeException(e);
075: }
076: documentForms.put(key, nestedForm);
077: environments.put(key, process.getEnvironment());
078: }
079:
080: if (nestedForm instanceof ObjectContextAware) {
081: ObjectContextAware contextAware = (ObjectContextAware) nestedForm;
082: getContext()
083: .putObject("environment", environments.get(key));
084: contextAware.setContext(getContext());
085: }
086: nestedForm.getEnvironment().setActive(true);
087: }
088:
089: public void setObject(Object object) {
090: super .setObject(object);
091:
092: if (object == null)
093: return;
094:
095: Subject subject = (Subject) object;
096:
097: try {
098: Controller controller = ControllerLookup.getInstance()
099: .getController(subject.getProcess());
100: HibernateEnvironment.getInstance().beginTransaction();
101: org.concern.controller.Loader loader = new LoaderFactory()
102: .getLoader(controller);
103: subject.setSubject(loader.load(subject.getSubjectId()));
104: nestedForm.setObject(subject.getSubject());
105: } catch (Exception e) {
106: throw new RuntimeException(e);
107: } finally {
108: HibernateEnvironment.getInstance().endTransaction();
109: }
110: }
111: }
|