summaryrefslogtreecommitdiff
path: root/src/main/java/com/c2kernel/gui/lifecycle/desc/ElemActDefOutcomeHandler.java
blob: 4a4347289014b85054fe722b475187f8d0511f82 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.c2kernel.gui.lifecycle.desc;

import java.io.BufferedWriter;
import java.io.File;

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

import com.c2kernel.common.ObjectNotFoundException;
import com.c2kernel.entity.agent.Job;
import com.c2kernel.entity.proxy.ItemProxy;
import com.c2kernel.graph.model.GraphableVertex;
import com.c2kernel.gui.MainFrame;
import com.c2kernel.gui.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.persistency.ClusterStorage;
import com.c2kernel.persistency.outcome.Viewpoint;
import com.c2kernel.process.Gateway;
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);
    }

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

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

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

    }

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

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

    /**
     *
     */
    @Override
	public void run() {
        validate();
    }

    @Override
	public boolean isUnsaved() {
        return unsaved;
    }

    @Override
	public void saved() {
        unsaved = false;
    }

	@Override
	public void export(File targetFile) throws Exception {
		exportAct(targetFile.getParentFile(), null, act);
	}

	public static void exportAct(File dir, BufferedWriter imports, ActivityDef actDef) throws Exception {
		
		// Export associated schema
		exportSchema((String)actDef.getProperties().get("SchemaType"), actDef.getProperties().get("SchemaVersion"), imports, new File(dir, "OD"));
		// Export associated script
		exportScript((String)actDef.getProperties().get("ScriptName"), actDef.getProperties().get("ScriptVersion"), imports, new File(dir, "SC"));

		//Export child act if composite
		if (actDef instanceof CompositeActivityDef) {
			CompositeActivityDef compActDef = (CompositeActivityDef)actDef;
			for (int i=0; i<compActDef.getChildren().length; i++) { // export slot defined scripts and schemas
				GraphableVertex vert = compActDef.getChildren()[i];
				exportScript((String)vert.getProperties().get("ScriptName"), actDef.getProperties().get("ScriptVersion"), imports, new File(dir, "SC"));
				exportScript((String)vert.getProperties().get("RoutingScriptName"), actDef.getProperties().get("RoutingScriptVersion"), imports, new File(dir, "SC"));
				exportSchema((String)vert.getProperties().get("SchemaType"), actDef.getProperties().get("SchemaVersion"), imports, new File(dir, "OD"));
			}
			GraphableVertex[] childDefs = compActDef.getLayoutableChildren();
			for (GraphableVertex childDef : childDefs) {
				if (childDef instanceof ActivitySlotDef) {
					if ("last".equals(childDef.getProperties().get("Version"))) {
						resolveRealVersion((ActivitySlotDef)childDef);
					}
					exportAct(dir, imports, ((ActivitySlotDef)childDef).getTheActivityDef());
				}
			}
			// export marshalled compAct
			FileStringUtility.string2File(new File(new File(dir, "CA"), compActDef.getActName()+".xml"), Gateway.getMarshaller().marshall(compActDef));
			if (imports!=null) {
				imports.write("<Resource name=\""+compActDef.getActName()+"\" "+(compActDef.getVersion()==-1?"":"version=\""+compActDef.getVersion()+"\" ")+"type=\"CA\">boot/CA/"+compActDef.getActName()+".xml</Resource>\n");
			}
		}
		else {
			FileStringUtility.string2File(new File(new File(dir, "EA"), actDef.getActName()+".xml"), Gateway.getMarshaller().marshall(actDef));
			if (imports!=null) imports.write("<Resource name=\""+actDef.getActName()+"\" "+(actDef.getVersion()==-1?"":"version=\""+actDef.getVersion()+"\" ")+"type=\"EA\">boot/EA/"+actDef.getActName()+".xml</Resource>\n");
		}
	}

	private static void resolveRealVersion(ActivitySlotDef childDef) throws ObjectNotFoundException {
		String defName = childDef.getActName();
		ItemProxy actDefItem = LocalObjectLoader.loadLocalObjectDef("/desc/ActivityDesc/", defName);
		String actType = actDefItem.getProperty("Complexity");
		Viewpoint last = actDefItem.getViewpoint( actType + "ActivityDef", "last");
		String[] viewpoints = actDefItem.getContents(ClusterStorage.VIEWPOINT+"/"+actType + "ActivityDef");
		for (String viewName : viewpoints) {
			try {
				Integer.parseInt(viewName);
			} catch (NumberFormatException e) {
				continue; // only count integer viewnames
			}
			Viewpoint thisView = actDefItem.getViewpoint(actType + "ActivityDef", viewName);
			if (thisView.getEventId() == last.getEventId()) {
				JOptionPane.showMessageDialog(null, defName+" defined as 'last'. Will use view "+viewName+" instead");
				childDef.getProperties().put("Version", viewName);
				return;
			}
		}
		JOptionPane.showMessageDialog(null, "Last view for "+defName+" has not been materialized. Executing CreateFromLast");
		try {
			Job createJob = actDefItem.getJobByName("AssignNewActivityVersionFromLast", MainFrame.userAgent);
			MainFrame.userAgent.execute(createJob);
		} catch (Exception e) {
			Logger.error(e);
			throw new ObjectNotFoundException("Could not create new version of "+defName+" from last", "");
		}
		resolveRealVersion(childDef);
	}

	public static void exportScript(String name, Object version, BufferedWriter imports, File dir) {
		if (name == null || name.length()==0 || name.contains(":")) return;
		try {
			int intVersion;
			if (version instanceof String) intVersion = Integer.parseInt((String)version);
			else if (version instanceof Integer) intVersion = ((Integer)version).intValue();
			else return;

			FileStringUtility.string2File(new File(dir, name+".xml"),
			LocalObjectLoader.getScript(name, intVersion));
			if (imports!=null) imports.write("<Resource name=\""+name+"\" "+(version==null?"":"version=\""+version+"\" ")+"type=\"SC\">boot/SC/"+name+".xml</Resource>\n");
		} catch (NumberFormatException ex) {
			JOptionPane.showMessageDialog(null, "Invalid version number in script version:"+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, Object version, BufferedWriter imports, File dir) {
		if (name == null || name.length()==0) return;
		try {
			int intVersion;
			if (version instanceof String) intVersion = Integer.parseInt((String)version);
			else if (version instanceof Integer) intVersion = ((Integer)version).intValue();
			else return;
			FileStringUtility.string2File(new File(dir, name+".xsd"),
			LocalObjectLoader.getSchema(name, intVersion).schema);
			if (imports!=null) imports.write("<Resource name=\""+name+"\" "+(version==null?"":"version=\""+version+"\" ")+"type=\"OD\">boot/OD/"+name+".xsd</Resource>\n");
		} catch (NumberFormatException ex) {
			JOptionPane.showMessageDialog(null, "Invalid version number in script version:"+version);
		} catch (Exception ex) {
			Logger.error(ex);
			JOptionPane.showMessageDialog(null, "Could not export schema "+name+"_"+version, "Error", JOptionPane.ERROR_MESSAGE);
		}
	}
}