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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
package com.ibm.ServerWizard2.controller;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.TreeMap;
import java.util.Vector;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.status.StatusLogger;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.TreeItem;
import com.ibm.ServerWizard2.ServerWizard2;
import com.ibm.ServerWizard2.model.Connection;
import com.ibm.ServerWizard2.model.Field;
import com.ibm.ServerWizard2.model.SystemModel;
import com.ibm.ServerWizard2.model.Target;
import com.ibm.ServerWizard2.utility.GithubRepository;
import com.ibm.ServerWizard2.view.LogViewerDialog;
import com.ibm.ServerWizard2.view.MainDialog;
public class TargetWizardController {
private SystemModel model;
private MainDialog view;
private Boolean modelCreationMode = false;
private String PROCESSING_SCRIPT = "scripts"+File.separator+"gen_html.pl";
private final String LIBRARY_NAME = "common-mrw-xml";
public TargetWizardController() {
}
public void setModelCreationMode() {
this.modelCreationMode = true;
}
public Boolean getModelCreationMode() {
return this.modelCreationMode;
}
public TreeMap<String,Boolean> getAttributeFilters() {
return model.getAttributeFilters();
}
public void init() {
try {
String libraryLocation = ServerWizard2.GIT_LOCATION + File.separator + this.LIBRARY_NAME;
File chk = new File(libraryLocation);
if (!chk.exists()) {
ServerWizard2.LOGGER.info("XML library does not exist so cloning: "+libraryLocation);
StatusLogger.getLogger().setLevel(Level.FATAL);
GithubRepository git = new GithubRepository(ServerWizard2.DEFAULT_REMOTE_URL, ServerWizard2.GIT_LOCATION, false);
git.cloneRepository();
}
model.loadLibrary(libraryLocation);
this.initModel();
} catch (Exception e) {
ServerWizard2.LOGGER.severe(e.toString());
MessageDialog.openError(null, "Error", e.toString());
e.printStackTrace();
System.exit(4);
}
}
public void initModel() throws Exception {
this.modelCreationMode = false;
model.deleteAllInstances();
model.addUnitInstances();
}
public void deepCopyAttributes(Target t) {
t.deepCopyAttributes(model.getUnitTargetModel(), model.getTargetLookup());
}
public void setView(MainDialog view) {
this.view = view;
}
public void setModel(SystemModel model) {
this.model = model;
}
public void deleteTarget(Target target) {
model.deleteTarget(target);
}
public Vector<Field> getAttributesAndGlobals(Target targetInstance, String path, String filter) {
return model.getAttributesAndGlobals(targetInstance, path, !this.modelCreationMode, filter);
}
public void addTargetInstance(Target targetModel, Target parentTarget,
TreeItem parentItem, String nameOverride) {
Target targetInstance;
Target instanceCheck = model.getTargetInstance(targetModel.getType());
if (instanceCheck != null) {
// target instance found of this model type
targetInstance = new Target(instanceCheck);
targetInstance.copyChildren(instanceCheck);
} else {
targetInstance = new Target(targetModel);
}
targetInstance.setName(nameOverride);
model.updateTargetPosition(targetInstance, parentTarget, -1, modelCreationMode);
try {
model.addTarget(parentTarget, targetInstance, modelCreationMode);
view.updateInstanceTree(targetInstance, parentItem);
} catch (Exception e) {
MessageDialog.openError(null, "Add Target Error", e.getMessage());
}
}
public Target copyTargetInstance(Target target, Target parentTarget,
Boolean incrementPosition) {
Target newTarget = new Target(target);
if (incrementPosition) {
newTarget.setPosition(newTarget.getPosition() + 1);
newTarget.setSpecialAttributes();
}
try {
model.addTarget(parentTarget, newTarget, false);
newTarget.copyChildren(target);
} catch (Exception e) {
MessageDialog.openError(null, "Add Target Error", e.getMessage());
newTarget = null;
}
return newTarget;
}
public void deleteConnection(Target target, Target busTarget,
Connection conn) {
target.deleteConnection(busTarget, conn);
}
public Vector<Target> getRootTargets() {
return model.rootTargets;
}
public void writeXML(String filename) {
try {
String tmpFilename = filename + ".tmp";
model.writeXML(tmpFilename, this.modelCreationMode);
File from = new File(tmpFilename);
File to = new File(filename);
Files.copy(from.toPath(), to.toPath(),
StandardCopyOption.REPLACE_EXISTING);
Files.delete(from.toPath());
ServerWizard2.LOGGER.info(filename + " Saved");
} catch (Exception exc) {
MessageDialog.openError(null, "Error", exc.getMessage());
exc.printStackTrace();
}
}
public Boolean readXML(String filename) {
Boolean rtn = false;
try {
model.readXML(filename);
this.modelCreationMode = model.partsMode;
rtn = model.errataUpdated;
} catch (Exception e) {
MessageDialog.openError(null, "Error", e.getMessage());
e.printStackTrace();
}
return rtn;
}
public Vector<Target> getConnectionCapableTargets() {
return model.getConnectionCapableTargets();
}
public void setGlobalSetting(String path, String attribute, String value) {
model.setGlobalSetting(path, attribute, value);
}
public Field getGlobalSetting(String path, String attribute) {
return model.getGlobalSetting(path, attribute);
}
public Boolean isGlobalSettings(String path, String attribute) {
return model.isGlobalSetting(path, attribute);
}
public Vector<Target> getChildTargets(Target target) {
if (target == null) {
return model.getTopLevelTargets();
}
if (this.modelCreationMode) {
Boolean override = false;
if (target.getType().startsWith("targetoverride")) { override = true; }
return model.getUnitTargets(override);
}
return model.getChildTargetTypes(target.getType());
}
public void hideBusses(Target target) {
target.hideBusses(model.getTargetLookup());
}
public Vector<Target> getVisibleChildren(Target target, boolean showHidden) {
Vector<Target> children = new Vector<Target>();
for (String c : target.getChildren()) {
Target t = model.getTarget(c);
if (t == null) {
String msg = "Invalid Child target id: " + c;
ServerWizard2.LOGGER.severe(msg);
}
children.add(t);
}
if (showHidden) {
for (String c : target.getHiddenChildren()) {
Target t = model.getTarget(c);
if (t == null) {
String msg = "Invalid Child target id: " + c;
ServerWizard2.LOGGER.severe(msg);
}
children.add(t);
}
}
return children;
}
public Vector<Target> getBusTypes() {
return model.getBusTypes();
}
public void loadLibrary(String path) {
try {
model.loadLibrary(path);
} catch (Exception e) {
ServerWizard2.LOGGER.severe("Unable to load library: "+path);
}
}
public void runChecks(String xmlFile, String htmlFile) {
String workingDir = ServerWizard2.getWorkingDir();
String commandLine[] = { "perl", "-I", workingDir + "/scripts",
workingDir + PROCESSING_SCRIPT, "-x",
xmlFile, "-f", "-o", htmlFile };
String commandLineStr = "";
for (int i = 0; i < commandLine.length; i++) {
commandLineStr = commandLineStr + commandLine[i] + " ";
}
ServerWizard2.LOGGER.info("Running: " + commandLineStr);
try {
final ProcessBuilder builder = new ProcessBuilder(commandLine)
.redirectErrorStream(true);
final Process process = builder.start();
final StringWriter writer = new StringWriter();
new Thread(new Runnable() {
public void run() {
char[] buffer = new char[1024];
int len;
InputStreamReader in = new InputStreamReader(
process.getInputStream());
try {
while ((len = in.read(buffer)) != -1) {
writer.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
final int exitValue = process.waitFor();
final String processOutput = writer.toString();
ServerWizard2.LOGGER.info(processOutput);
ServerWizard2.LOGGER.info("Return Code: " + exitValue);
LogViewerDialog dlg = new LogViewerDialog(null);
dlg.setData(processOutput);
dlg.open();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|