{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPblDeliveryOutput.java
More file actions
348 lines (305 loc) · 11.3 KB
/
Copy pathPblDeliveryOutput.java
File metadata and controls
348 lines (305 loc) · 11.3 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package program;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import mdio.CConnector;
import mdio.CConnectorException;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import data.Mapping;
import data.RegionType;
/**
* This is the main class. It has a responsibility to set up the logger, process
* the command line and initiate the processing.
*
* @author Vortech
*
*/
public class PblDeliveryOutput
{
int xoffset = 0;
// column of the cell with 'VARIABLE' in it
int yoffset = 0;
// row of the cell with 'VARIBLE' in it
static Logger logger = Logger.getLogger(PblDeliveryOutput.class.getName());
Mapping _mapping;
public PblDeliveryOutput()
{
_mapping = new Mapping();
}
public static final String kMAPPINGSHEETNAME = "data";
protected String _scenarioBasePath = "";
protected String _templateFile = "";
protected String _mappingFile = "";
protected String _outputName = "";
protected String _outputFormat = "excel";
protected String _numberFormat = "decimal";
protected ArrayList<String> _scenario = new ArrayList<String>();
protected ArrayList<RegionType.RegionName> _reportRegions = new ArrayList<RegionType.RegionName>(); ;
private void ProcessRegios(String iRegio) throws IllegalArgumentException
{
_reportRegions.add(RegionType.RegionName.valueOf(iRegio));
}
private void processDelivery() throws Exception
{
try
{
readMapping();
}
catch (IOException e)
{
e.printStackTrace();
logger.error("Unable to process mapping file (is the filename correct?) ");
}
catch (Exception e)
{
e.printStackTrace();
logger.error("Unable to process mapping file (the document probably contains an error)");
}
Engine aReportCreator = new Engine(_templateFile, _scenario, _scenarioBasePath, _outputName, _mapping,
_outputFormat, _numberFormat, _reportRegions);
aReportCreator.doWork();
}
private void readMapping() throws FileNotFoundException, IOException, Exception
{
FileInputStream fis = new FileInputStream(_mappingFile);
Workbook aWorkBook = WorkbookFactory.create(fis);
Sheet aSheet=null;
aSheet = aWorkBook.getSheet(kMAPPINGSHEETNAME);
logger.info("looking for VARIABLE keyword in mapping file");
findVariableKeywordCell(aSheet);
logger.info("got VARIABLE keyword in mapping file");
int aRowVariable = 1;
while (aRowVariable + yoffset != aSheet.getLastRowNum())
{
try
{
aSheet.getRow(aRowVariable + yoffset).getCell(xoffset).getStringCellValue();
}
catch (NullPointerException e)
{
break;
}
logger.info("Adding row (Excel) " + (aRowVariable + yoffset + 1) + " from mapping sheet");
_mapping.addMappingFromRowSheet(aSheet.getRow(aRowVariable + yoffset), xoffset);
aRowVariable++;
}
logger.debug(_mapping.toString());
}
/*
*
*/
private void findVariableKeywordCell(Sheet iSheet) throws Exception
{
for (int i = 0; i < 15; i++)
{
Row aRow = iSheet.getRow(i);
for (int j = 0; j < 15 && j < aRow.getLastCellNum(); j++)
{
if (aRow.getCell(j).getStringCellValue().equals("VARIABLE"))
{
xoffset = j;
yoffset = i;
return;
}
}
}
throw new Exception("Could not find cell 'VARIABLE' in the mapping file: " + _mappingFile);
}
public static void readManifest() throws IOException
{
Enumeration<URL> resources = PblDeliveryOutput.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
while (resources.hasMoreElements())
{
try
{
Manifest manifest = new Manifest(resources.nextElement().openStream());
// check that this is your manifest and do what you need or get the
// next one
Attributes b = manifest.getMainAttributes();
if (b.getValue("Build-time") != null)
{
String aMsg = "Your build is: " + b.getValue("Build-time");
logger.info(aMsg);
System.out.println(aMsg);
}
}
catch (Exception e)
{
// handle
}
}
}
/**
*
* @param args
* -t template.xls -m mapping.xls -b scenarioBasedir -s scenario1
* -s scenario2 -r Regio1 -r Regio2
*/
public static void main(String[] args)
{
java.util.Date aDate = new java.util.Date();
long aStart = aDate.getTime();
PblLogger.setupLogging(Level.ALL);
try
{
readManifest();
}
catch (IOException e)
{
}
PblDeliveryOutput aDelivery = new PblDeliveryOutput();
boolean aCommandLineOk = true;
// test if the native MDIO dll will function properly
CConnector a = new CConnector();
try
{
a.connect();
}
catch (CConnectorException e)
{
String aMsg = "There is no connection with the native dll (for M-file reading).";
System.err.println(aMsg);
logger.fatal(aMsg);
}
if (args.length >= 8)
{
aCommandLineOk = aDelivery.processArguments(args);
if (aCommandLineOk)
{
try
{
aDelivery.processDelivery();
logger.info("Reached the end of the program.");
}
catch (Exception e)
{
e.printStackTrace();
logger.fatal(e.getMessage());
logger.fatal("An exception prevented the program from finishing normally.");
}
}
}
else
{
aCommandLineOk = false;
}
if (!aCommandLineOk)
{
String aUsage = "PblDeliveryTool creates an Excel report from MyM data and Excel formatting sheets\r\n"
+ "\r\n"
+ "Usage: PblDeliveryTool <outputfile> <loglevel> <template> <mapping> <scenario base path> <scenarios> [regions]\r\n"
+ "with:\t<outputfile>\t '-f myfile.xls' Name and path where the generated Excel output file should be created\r\n"
+ "\t<loglevel>\t '-l WARN' One of ALL, FATAL, ERROR, WARN, INFO. All messages that have less importance than the specified level are nog logged.\r\n"
+ "\t<template>\t '-t myTemplate.xls' The template file contains the subset of the mapping to report and the years to report on (Excel file).\r\n"
+ "\t<mapping>\t '-m myMapping.xls' The mapping file defines a variable that can be reported and provides information its dimensions (Excel file).\r\n"
+ "\t<scenario base path> '-b C:\\path\\to\\scenario_' The path that is put in front of the scenario's, note that no additional slashes are added when appending the scenario to it.\r\n"
+ "\t<scenarios>\t'-s scenario [-s scenario2]' The scenario's (collection of MyM files) that should be part of the report, multiple scenario's can be provided.\r\n"
+ "\t[regions]\t '-r World -r CAN' Filter to put only the selected regions in the report, provide no regions to include all regions.\r\n"
+ "\t <outputformat>\t '-of csv|excel Format of outputfile. Excel is the default.\r\n"
+ "\t <numberformat>\t '-nf decimal|e numberformat of data. decimal is the default.\r\n" + "\r\n"
+ "\tThe base path with a scenario appended should be a valid path that is the root dir of the MyM collection, i.e. should have subdirs F2RT, I2RT, T2RT\r\n"
+ "\r\nExample: java -jar PblDeliveryTool.jar -l INFO -t test.xls -t templateTest.xls -m mapping.xls -b C:\\data\\scenario_ -s R2G23 -r World -r CAN\r\n\r\n"
+ "ERROR: check your commandline arguments";
logger.fatal(aUsage);
System.out.flush();
System.err.print("\r\n\r\n");
System.err.print(aUsage);
// System.err.print(aExample);
System.exit(1);
}
aDate = new java.util.Date();
long aStop = aDate.getTime();
long aRuntime = aStop - aStart;
logger.info("Run took " + aRuntime + " milliseconds");
}
private boolean processArguments(String[] args)
{
boolean aCommandLineOk = true;
for (int i = 0; i < args.length; i += 2)
{
if (args[i].equals("-f")) // output file
{
_outputName = args[i + 1];
}
else if (args[i].equals("-t")) // template file
{
_templateFile = args[i + 1];
}
else if (args[i].equals("-m")) // mappping file
{
_mappingFile = args[i + 1];
}
else if (args[i].equals("-b")) // scenario common path
{
_scenarioBasePath = args[i + 1];
}
else if (args[i].equals("-s")) // scenarios
{
_scenario.add(args[i + 1]);
}
else if (args[i].equals("-l")) // loglevel
{
PblLogger.changeLogLevel(Level.toLevel(args[i + 1]));
}
else if (args[i].equals("-of")) // outputformat
{
if ((args[i + 1].equals("csv"))||(args[i + 1].equals("excel")))
{
this._outputFormat= args[i + 1];
}
else
{
logger.fatal("outputformat should be: excel or csv, supplied was: "
+ args[i + 1]);
aCommandLineOk = false;
}
}
else if (args[i].equals("-nf")) // numberformat
{
if ((args[i + 1].equals("e"))||(args[i + 1].equals("decimal")))
{
this._numberFormat= args[i + 1];
}
else
{
logger.fatal("numberformat should be: decimal or e, supplied was: "
+ args[i + 1]);
aCommandLineOk = false;
}
}
else if (args[i].equals("-r")) // regions
{
try
{
ProcessRegios(args[i + 1]);
}
catch (IllegalArgumentException e)
{
logger.fatal("The regions should be reported separated: -r USA -r WEU is valid, supplied was: "
+ args[i + 1]);
for (RegionType.RegionName aName : RegionType.RegionName.values())
{
logger.info(aName + " is a valid region");
}
aCommandLineOk = false;
}
}
else
{
logger.error("Command switch not recognized. |" + args[i] + "|");
aCommandLineOk = false;
}
}
return aCommandLineOk;
}
}
You can’t perform that action at this time.
