summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/lifecycle/gui/view/ElemActDefOutcomeHandler.java
blob: a30491ebfd74a7f3da8cd112a5e36e0bc37e3691 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.c2kernel.lifecycle.gui.view;

import java.io.File;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.c2kernel.graph.model.GraphableVertex;
import com.c2kernel.graph.view.VertexPropertyPanel;
import com.c2kernel.gui.tabs.outcome.InvalidOutcomeException;
import com.c2kernel.gui.tabs.outcome.InvalidSchemaException;
import com.c2kernel.gui.tabs.outcome.OutcomeException;
import com.c2kernel.gui.tabs.outcome.OutcomeHandler;
import com.c2kernel.gui.tabs.outcome.OutcomeNotInitialisedException;
import com.c2kernel.lifecycle.ActivityDef;
import com.c2kernel.lifecycle.ActivitySlotDef;
import com.c2kernel.lifecycle.CompositeActivityDef;
import com.c2kernel.utils.CastorXMLUtility;
import com.c2kernel.utils.FileStringUtility;
import com.c2kernel.utils.LocalObjectLoader;
import com.c2kernel.utils.Logger;

/**************************************************************************
 *
 * $Revision: 1.5 $
 * $Date: 2005/10/05 07:39:37 $
 *
 * Copyright (C) 2003 CERN - European Organization for Nuclear Research
 * All rights reserved.
 **************************************************************************/

public class ElemActDefOutcomeHandler extends VertexPropertyPanel implements OutcomeHandler {

    ActivityDef act;
    boolean unsaved;
    public ElemActDefOutcomeHandler() {
        super();
        createLayout(null);
    }

    /**
     *
     */
    public void setOutcome(String outcome) throws InvalidOutcomeException {
        try {
            act = (ActivityDef)CastorXMLUtility.unmarshall(outcome);
            setVertex(act);
        } catch (Exception ex) {
            Logger.error(ex);
            throw new InvalidOutcomeException();
        }
    }

    /**
     *
     */
    public void setDescription(String description)
            throws InvalidSchemaException {
        // ignore
    }

    /**
     *
     */
    public void setReadOnly(boolean readOnly) {
        setEditable(!readOnly);

    }

    /**
     *
     */
    public JPanel getPanel() throws OutcomeNotInitialisedException {
        return this;
    }

    /**
     *
     */
    public String getOutcome() throws OutcomeException {
        try {
            return CastorXMLUtility.marshall(act);
        } catch (Exception ex) {
            Logger.error(ex);
            throw new OutcomeException();
        }           
    }

    /**
     *
     */
    public void run() {
        validate();
    }
    
    public boolean isUnsaved() {
        return unsaved;
    }
    
    public void saved() {
        unsaved = false;
    }
    
	public void export(File targetFile) throws Exception {
		exportAct(targetFile.getParentFile(), act);
	}
	
	public static void exportAct(File dir, ActivityDef actDef) throws Exception {
		FileStringUtility.string2File(new File(dir, actDef.getActName()+".xml"), CastorXMLUtility.marshall(actDef));
		// Export associated schema
		exportSchema((String)actDef.getProperties().get("SchemaType"), (String)actDef.getProperties().get("SchemaVersion"), dir);
		// Export associated script
		exportScript((String)actDef.getProperties().get("ScriptName"), (String)actDef.getProperties().get("ScriptVersion"), dir);
		
		//Export child act if composite
		if (actDef instanceof CompositeActivityDef) {
			CompositeActivityDef compActDef = (CompositeActivityDef)actDef;
			for (int i=0; i<compActDef.getChildren().length; i++) {
				GraphableVertex vert = compActDef.getChildren()[i];
				exportScript((String)vert.getProperties().get("ScriptName"), (String)vert.getProperties().get("ScriptVersion"), dir);
				exportScript((String)vert.getProperties().get("RoutingScriptName"), (String)vert.getProperties().get("RoutingScriptVersion"), dir);
			}
			GraphableVertex[] childDefs = compActDef.getLayoutableChildren();
			for (int i=0; i<childDefs.length; i++) {
				if (childDefs[i] instanceof ActivitySlotDef)
				exportAct(dir, ((ActivitySlotDef)childDefs[i]).getTheActivityDef());
			}
		}
	}
	
	public static void exportScript(String name, String version, File dir) {
		if (name == null || name.length()==0) return;
		try {
			FileStringUtility.string2File(new File(dir, name+"_"+version+".xml"), 
				LocalObjectLoader.getScript(name, version));
		} catch (Exception ex) {
			Logger.error(ex);
			JOptionPane.showMessageDialog(null, "Could not export script "+name+"_"+version, "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public static void exportSchema(String name, String version, File dir) {
		if (name == null || name.length()==0) return;
		try {
			FileStringUtility.string2File(new File(dir, name+"_"+version+".xsd"), 
					LocalObjectLoader.getSchema(name, Integer.parseInt(version)).schema);
		} catch (Exception ex) {
			Logger.error(ex);
			JOptionPane.showMessageDialog(null, "Could not export schema "+name+"_"+version, "Error", JOptionPane.ERROR_MESSAGE);
		}
	}	
}