TaskEditor.java :  » Workflow-Engines » osbl-1_0 » org » osbl » client » wings » concern » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » org » osbl » client » wings » concern » TaskEditor.java
package org.osbl.client.wings.concern;

import org.concern.*;
import org.osbl.client.action.AbstractObjectAction;
import org.osbl.client.action.ObjectActionEvent;
import org.osbl.client.wings.form.*;
import org.osbl.client.wings.*;
import org.osbl.client.wings.shell.Environment;
import org.osbl.client.wings.shell.Client;
import org.osbl.client.ClientServiceProvider;
import org.osbl.plugin.*;
import org.concern.controller.LoaderFactory;
import org.conform.hibernate.HibernateEnvironment;
import org.wings.session.SessionManager;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.sql.Timestamp;

public class TaskEditor
    extends GenericObjectEditor
{
    protected ForwardAction forwardAction = new ForwardAction(base());
    protected BackwardAction backwardAction = new BackwardAction(base());
    protected CompleteAction completeAction = new CompleteAction(base());
    protected ListAction listAction = new ListAction(base());

    protected void initializeTools() {
        PluginManager pluginManager = (PluginManager)ClientServiceProvider.getInstance().getService("ClientPluginManager");
        final ExtensionPoint extensionPoint = new ExtensionPoint("org.osbl.client.wings.form.taskEditorTools", EditorTool.class);
        final List<Extension> list = pluginManager.getExtensions(extensionPoint);

        for (Extension extension : list) {
            final Class<EditorTool> toolClass = (Class<EditorTool>)extension.getImplementation();
            try {
                EditorTool tool = toolClass.newInstance();
                tool.setEditor(this);
                tools.add(tool);
            }
            catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    protected void initializeEditor() {
        editAction = new TaskEditor.TaskAction(base());
    }

    protected ObjectForm initializeForm() {
        return new TaskForm();
    }

    protected void formControls(Environment environment) {
        environment.addControl(new XButton(listAction));
        //environment.addControl(new XButton(previousAction));
        //environment.addControl(new XButton(nextAction));
        environment.addControl(new XButton(forwardAction));
        environment.addControl(new XButton(backwardAction));
        environment.addControl(new XButton(completeAction));
    }

    protected void listControls(Environment environment) {
    }

    protected ObjectList initializeList() {
        final TaskList taskList = new TaskList();
        /*
        taskList.setLinkAction("subjectLine", new AbstractObjectAction() {
            public void actionPerformed(ObjectActionEvent e) {
                Object object = e.getTargets()[0];
                taskList.setCurrent(object);
                setObject(object);

                if (editAction.isEnabled())
                    editAction.actionPerformed(new ActionEvent(TaskEditor.this, 0, "edit"));
            }
        });
        */
        return taskList;
    }

    protected String linkPropertyName() {
        return "subjectLine";
    }

    public Class getType() {
        return Work.class;
    }

    protected String base() {
        return "org.concern";
    }

    public void showForm() {
        ((TaskForm)getForm()).setTask((Work)getObject());
        super.showForm();
    }

    public void saveObject(Object object) {
        Work task = (Work)object;
        Controller controller = ControllerLookup.getInstance().getController(task.getProcess());
        Loader loader = new LoaderFactory().getLoader(controller);
        if (loader instanceof Persister) {
            Persister persister = (Persister)loader;
            persister.update(task.getSubject());
        }
    }

    public void deleteObject(Object object) {
    }

    public void revertObject(Object object) {
    }

    protected boolean complete() {
        if (!objectForm.hasValidationIssues()) {
            saveObject(objectForm.getObject());
            return true;
        }
        else
            return false;
    }

    protected boolean forward() {
        if (!objectForm.hasValidationIssues()) {
            saveObject(objectForm.getObject());
            return true;
        }
        else
            return false;
    }

    protected boolean backward() {
        if (!objectForm.hasValidationIssues()) {
            saveObject(objectForm.getObject());
            return true;
        }
        else
            return false;
    }

    private Timestamp fiveMinutes() {
        return new Timestamp(System.currentTimeMillis() + 1000 * 60 * 5);
    }

    protected class TaskAction extends EditAction {
        public TaskAction(String base) {
            super(base);
        }

        public void actionPerformed(ActionEvent event) {
            Work task = (Work)getObject();
            Controller controller = ControllerLookup.getInstance().getController(task.getProcess());
            if (controller == null) {
                XOptionPane.showMessageDialog(null, "Controller " + task.getProcess() + " is not deployed", new ActionListener()
                {
                    public void actionPerformed(ActionEvent event) {
                    }
                });
            }
            else {
                try {
                    if (!ControllerLookup.getInstance().getWorklist().lock(task, SessionManager.getSession().getServletRequest().getUserPrincipal().getName(), fiveMinutes())) {
                        environment.setStatusMessage(new Client.Message(Client.Message.TYPE_WARNING, Client.getInstance().getResourceProvider().getMessage("businessobject.messages.locked")));
                        return;
                    }
                    environment.setStatusMessage(null);
                    System.out.println("lock");
                }
                catch (UnknownSubjectException e) {
                    throw new RuntimeException(e);
                }

                showForm();

                // TODO: check level

                boolean forwardable = Boolean.TRUE.equals(controller.getProcess().getActivity(task.getActivity()).getEnvironmentEntry("forwardable"));
                try {
                    forwardAction.setEnabled(forwardable && controller.isForwardPossible(task.getSubjectId(), task.getActivity()));
                    backwardAction.setEnabled(forwardable && controller.isBackwardPossible(task.getSubjectId(), task.getActivity()));
                }
                catch (UnknownSubjectException e) {
                    throw new RuntimeException(e);
                }
            }
            previousAction.setEnabled(objectList.hasPrevious());
            nextAction.setEnabled(objectList.hasNext());
        }
    }

    protected class CompleteAction
        extends AbstractAction
    {
        public CompleteAction(String base) {
            putValue(Action.ACTION_COMMAND_KEY, base + ".buttons.complete");
        }

        public void actionPerformed(ActionEvent event) {
            if (objectForm.hasValidationIssues())
                return;

            Work work = (Work)getObject();
            Controller controller = ControllerLookup.getInstance().getController(work.getProcess());

            try {
                HibernateEnvironment.getInstance().beginTransaction();

                if (complete()) {
                    Map annotations = new HashMap();
                    annotations.put("user", SessionManager.getSession().getServletRequest().getUserPrincipal().getName());
                    controller.complete(work.getSubjectId(), work.getActivity(), annotations);
                }
                controller.announceSubject(work.getSubjectId());

                System.out.println("unlock");
                ControllerLookup.getInstance().getWorklist().unlock(work, SessionManager.getSession().getServletRequest().getUserPrincipal().getName());
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                HibernateEnvironment.getInstance().endTransaction();
            }

            showList();
        }

        public void setEnabled(boolean newValue) {
        }
    }

    protected class ForwardAction
        extends AbstractAction
    {
        public ForwardAction(String base) {
            putValue(Action.ACTION_COMMAND_KEY, base + ".buttons.forward");
        }

        public void actionPerformed(ActionEvent event) {
            if (objectForm.hasValidationIssues())
                return;

            Work work = (Work)getObject();
            Controller controller = ControllerLookup.getInstance().getController(work.getProcess());

            try {
                HibernateEnvironment.getInstance().beginTransaction();

                if (forward()) {
                    Map annotations = new HashMap();
                    annotations.put("user", SessionManager.getSession().getServletRequest().getUserPrincipal().getName());
                    try {
                        controller.forward(work.getSubjectId(), work.getActivity(), annotations);
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }

                    showList();
                }

                System.out.println("unlock");
                ControllerLookup.getInstance().getWorklist().unlock(work, SessionManager.getSession().getServletRequest().getUserPrincipal().getName());
            }
            catch (UnknownSubjectException e) {
                throw new RuntimeException(e);
            }
            finally {
                HibernateEnvironment.getInstance().endTransaction();
            }
        }

        public void setEnabled(boolean newValue) {
        }
    }

    protected class BackwardAction
        extends AbstractAction
    {
        public BackwardAction(String base) {
            putValue(Action.ACTION_COMMAND_KEY, base + ".buttons.backward");
        }

        public void actionPerformed(ActionEvent event) {
            if (objectForm.hasValidationIssues())
                return;

            Work work = (Work)getObject();
            Controller controller = ControllerLookup.getInstance().getController(work.getProcess());
            try {
                HibernateEnvironment.getInstance().beginTransaction();

                if (backward()) {
                    Map annotations = new HashMap();
                    annotations.put("user", SessionManager.getSession().getServletRequest().getUserPrincipal().getName());
                    try {
                        controller.backward(work.getSubjectId(), work.getActivity(), annotations);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    showList();
                }

                System.out.println("unlock");
                ControllerLookup.getInstance().getWorklist().unlock(work, SessionManager.getSession().getServletRequest().getUserPrincipal().getName());
            }
            catch (UnknownSubjectException e) {
                throw new RuntimeException(e);
            }
            finally {
                HibernateEnvironment.getInstance().endTransaction();
            }
        }

        public void setEnabled(boolean newValue) {
        }
    }

    protected class ListAction extends AbstractAction {
        public ListAction(String base) {
            putValue(Action.ACTION_COMMAND_KEY, base + ".buttons.list");
        }

        public void actionPerformed(ActionEvent event) {
            if (objectForm.hasChanges()) {
                String name = Client.getInstance().getResourceProvider().getMessage(getType().getName());
                String message = unsavedChangesMessage(name);
                XOptionPane.showConfirmDialog(null, message, name, new ActionListener() {
                    public void actionPerformed(ActionEvent event) {
                        if (XOptionPane.YES_ACTION == event.getActionCommand())
                            showList();

                        try {
                            System.out.println("unlock");
                            ControllerLookup.getInstance().getWorklist().unlock((Work)getObject(), SessionManager.getSession().getServletRequest().getUserPrincipal().getName());
                        }
                        catch (UnknownSubjectException e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
            }
            else {
                showList();

                try {
                    System.out.println("unlock");
                    ControllerLookup.getInstance().getWorklist().unlock((Work)getObject(), SessionManager.getSession().getServletRequest().getUserPrincipal().getName());
                }
                catch (UnknownSubjectException e) {
                    throw new RuntimeException(e);
                }
            }
        }

        public void setEnabled(boolean newValue) {
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.