1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2000 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
* Purpose: To test the ReplicationServerList class by JUnit
|
6
|
* Authors: Jing Tao
|
7
|
*
|
8
|
* '$Author: leinfelder $'
|
9
|
* '$Date: 2013-09-23 15:54:55 -0700 (Mon, 23 Sep 2013) $'
|
10
|
* '$Revision: 8265 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
|
27
|
package edu.ucsb.nceas.metacattest;
|
28
|
|
29
|
import edu.ucsb.nceas.metacat.*;
|
30
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
31
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
32
|
import edu.ucsb.nceas.metacat.replication.*;
|
33
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
34
|
import edu.ucsb.nceas.metacat.util.MetacatUtil;
|
35
|
import edu.ucsb.nceas.utilities.IOUtil;
|
36
|
import edu.ucsb.nceas.utilities.XMLUtilities;
|
37
|
//import edu.ucsb.nceas.morpho.framework.*;
|
38
|
import junit.framework.Test;
|
39
|
import junit.framework.TestCase;
|
40
|
import junit.framework.TestResult;
|
41
|
import junit.framework.TestSuite;
|
42
|
import org.apache.commons.logging.Log;
|
43
|
import org.apache.commons.logging.LogFactory;
|
44
|
import org.w3c.dom.Document;
|
45
|
import org.w3c.dom.Node;
|
46
|
import org.w3c.dom.NodeList;
|
47
|
|
48
|
import java.io.*;
|
49
|
import java.net.*;
|
50
|
import java.text.SimpleDateFormat;
|
51
|
import java.util.*;
|
52
|
|
53
|
|
54
|
/**
|
55
|
* This class is used to change the data file location for IPCC eml documents.
|
56
|
* Currently IPCC eml documents point data file ti SRB server. However, the srb
|
57
|
* earthgrid is not very stable. We decided to change the online URL from srb to knb.
|
58
|
* So this class will handle this case.
|
59
|
* Before running this program, it needs:
|
60
|
* 1. Downloaded data files from SRB
|
61
|
* 2. A list of IPCC docid(with revision number) text file. If the text file is not available, it need
|
62
|
* a metacat query file to search metacat to get the doicd list.
|
63
|
* What the class will do:
|
64
|
* 1. It will read the eml from Metacat.
|
65
|
* 2. Get online URL information from eml document by DOM parser.
|
66
|
* 3. Base on the URL information, this program will find the data file in
|
67
|
* the direcotry which contains the srb data file.
|
68
|
* 4. It will generate docid for the data file.
|
69
|
* 5. Upload the download srb data file to Metacat with assigned docid.
|
70
|
* 6. Modify the eml document with the new URL information (pointing to
|
71
|
* knb) and new version number in eml
|
72
|
* 7. Update it to a new version in Metacat.
|
73
|
* 8 . Go through above 7 steps for every eml document in the list.
|
74
|
*
|
75
|
*/
|
76
|
public class UploadIPCCDataTest extends TestCase
|
77
|
{
|
78
|
|
79
|
|
80
|
/* Initialize properties*/
|
81
|
static
|
82
|
{
|
83
|
try
|
84
|
{
|
85
|
PropertyService.getInstance();
|
86
|
}
|
87
|
catch(Exception e)
|
88
|
{
|
89
|
System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
|
90
|
}
|
91
|
}
|
92
|
|
93
|
/**Constants*/
|
94
|
private static String SRBDATAFILEDIR = "/home/tao/data-file/IPCC"; // Dir for storing srb data file
|
95
|
private static String DOCLISTFILE = "docidList"; // File name which stores IPCC document id
|
96
|
private static String METACATURL = "http://chico.dyndns.org/metacat/metacat";
|
97
|
private static String USERNAME = "uid=dpennington,o=LTER,dc=ecoinformatics,dc=org";
|
98
|
private static String PASSWORD = "password";
|
99
|
private static String TABLEONLINEURL= "/eml:eml/dataset/dataTable/physical/distribution/online/url";
|
100
|
private static String SPATIALONLINEURL = "/eml:eml/dataset/spatialRaster/physical/distribution/online/url";
|
101
|
private static String PACKAGEID ="/eml:eml/@packageId";
|
102
|
private static String SRB = "srb://";
|
103
|
private static String KNB = "ecogrid://knb/";
|
104
|
private static String DATAIDPREFIX = "IPCC";
|
105
|
private static String DOT = ".";
|
106
|
private static String SUCCESSLOG = "update.log";
|
107
|
private static String ERRORLOG = "error.log";
|
108
|
private static String CURRENT_CORRECTFILENAME = "correct_filename.csv";
|
109
|
private File log = new File(SUCCESSLOG);
|
110
|
private File error = new File (ERRORLOG);
|
111
|
|
112
|
|
113
|
/**
|
114
|
* Constructor to build the test
|
115
|
*
|
116
|
* @param name the name of the test method
|
117
|
*/
|
118
|
public UploadIPCCDataTest(String name)
|
119
|
{
|
120
|
super(name);
|
121
|
|
122
|
}
|
123
|
|
124
|
|
125
|
/**
|
126
|
* Create a suite of tests to be run together
|
127
|
*/
|
128
|
public static Test suite()
|
129
|
{
|
130
|
TestSuite suite = new TestSuite();
|
131
|
//suite.addTest(new UploadIPCCDataTest("modifyEMLDocsWithCorrectDataFileName"));
|
132
|
//suite.addTest(new UploadIPCCDataTest("modifyEMLDocsWithIncorrectDataFileName"));
|
133
|
return suite;
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Modify EML Docs' data url online from SRB to ecogrid.
|
138
|
*Those eml docs pointe valide srb file names.
|
139
|
*/
|
140
|
public void modifyEMLDocsWithCorrectDataFileName()
|
141
|
{
|
142
|
boolean originalDataFileIncorrect = false;
|
143
|
updateEML(originalDataFileIncorrect);
|
144
|
}
|
145
|
/**
|
146
|
* Modify EML Docs' data url online from SRB to ecogrid.
|
147
|
*Those eml docs pointe invalide srb file names.
|
148
|
*/
|
149
|
public void modifyEMLDocsWithIncorrectDataFileName()
|
150
|
{
|
151
|
boolean originalDataFileIncorrect = true;
|
152
|
updateEML(originalDataFileIncorrect);
|
153
|
}
|
154
|
/*
|
155
|
* Upload the data file to Metacat and modify the eml documents
|
156
|
* @return
|
157
|
* @throws Exception
|
158
|
*/
|
159
|
private void updateEML(boolean originalDataFileIncorrect)
|
160
|
{
|
161
|
|
162
|
// Get eml document first
|
163
|
Vector list = getDocumentList();
|
164
|
//If list is not empty, goes through every document by handleSingleEML method -
|
165
|
//1. It will read the eml from Metacat.
|
166
|
// 2. Get online URL information from eml document by DOM parser.
|
167
|
// 3. Base on the URL information, this program will find the data file in
|
168
|
// the direcotry which contains the srb data file.
|
169
|
// 4. It will generate docid for the data file
|
170
|
// 5. At last upload the download srb data file to Metacat with assigned docid.
|
171
|
// 6. Modify the eml document with the new URL information (pointing to
|
172
|
// knb) and new version number in eml.
|
173
|
// 7.Update it to a new version in Metacat.
|
174
|
|
175
|
if (list != null && !list.isEmpty())
|
176
|
{
|
177
|
int size = list.size();
|
178
|
for (int i=0; i<size; i++)
|
179
|
{
|
180
|
String docid = null;
|
181
|
try
|
182
|
{
|
183
|
docid = (String)list.elementAt(i);
|
184
|
String dataId = handleSingleEML(docid, originalDataFileIncorrect);
|
185
|
String message = "Successfully update eml "+docid + " with data id "+dataId;
|
186
|
writeLog(log, message);
|
187
|
}
|
188
|
catch(Exception e)
|
189
|
{
|
190
|
System.err.println("Failed to handle eml document "+docid + " since "+
|
191
|
e.getMessage());
|
192
|
String message = "failed to update eml "+docid + "\n "+e.getMessage();
|
193
|
writeLog(error, message);
|
194
|
}
|
195
|
}
|
196
|
}
|
197
|
else
|
198
|
{
|
199
|
System.err.println("There is no EML document to handle");
|
200
|
}
|
201
|
|
202
|
}
|
203
|
|
204
|
/*
|
205
|
* Does actually job to upload data file and modify eml document for a given id.
|
206
|
* Here are its tasks:
|
207
|
* 1. It will read the eml from Metacat.
|
208
|
* 2. Get online URL information from eml document by DOM parser.
|
209
|
* 3. Base on the URL information, this program will find the data file in
|
210
|
* the direcotry which contains the srb data file.
|
211
|
* 4. It will generate docid for the data file.
|
212
|
* 5. Upload the download srb data file to Metacat with assigned docid.
|
213
|
* 6. Modify the eml document with the new URL information (pointing to
|
214
|
* knb) and new version number in eml
|
215
|
* 7. Update it to a new version in Metacat.
|
216
|
*
|
217
|
*/
|
218
|
private String handleSingleEML(String docid,boolean originalDataFileIncorrect) throws Exception
|
219
|
{
|
220
|
Metacat metacat = MetacatFactory.createMetacatConnection(METACATURL);
|
221
|
// login metacat
|
222
|
String response = metacat.login(USERNAME, PASSWORD);
|
223
|
if (response.indexOf("<login>") == -1)
|
224
|
{
|
225
|
throw new Exception("login failed "+response);
|
226
|
}
|
227
|
// 1. Reads eml document from metacat
|
228
|
Reader r = new InputStreamReader(metacat.read(docid));
|
229
|
Document DOMdoc = XMLUtilities.getXMLReaderAsDOMDocument(r);
|
230
|
Node rootNode = (Node)DOMdoc.getDocumentElement();
|
231
|
|
232
|
//2. Gets online url information. If onlineUrl is not SRB, through an exception
|
233
|
String onlineUrl = getOnLineURL(rootNode);
|
234
|
//System.out.println("=================The url is "+onlineUrl);
|
235
|
|
236
|
//3. Find the srb data file
|
237
|
String dataFileName = getDataFileNameFromURL(onlineUrl);
|
238
|
//System.out.println("the data fileName in eml "+dataFileName);
|
239
|
//If the dataFileName in original eml is wrong, we need to look up the
|
240
|
// the correct name first
|
241
|
if (originalDataFileIncorrect)
|
242
|
{
|
243
|
Hashtable correctName = getCurrent_CorrectFileNamesPair();
|
244
|
dataFileName =(String) correctName.get(dataFileName);
|
245
|
}
|
246
|
//System.out.println("=================The data file is "+dataFileName);
|
247
|
File dataFile = null;
|
248
|
dataFile = new File(SRBDATAFILEDIR,dataFileName);
|
249
|
if (!dataFile.exists())
|
250
|
{
|
251
|
throw new Exception("Couldn't find the data file in srb data directory "+dataFile);
|
252
|
}
|
253
|
|
254
|
//4. Generate docid for data file
|
255
|
String dataId = generateId();
|
256
|
//System.out.println("=======The docid for data file will be "+dataId);
|
257
|
|
258
|
//5. upload data file to Metacat
|
259
|
response = metacat.upload(dataId, dataFile);
|
260
|
if (response.indexOf("<success>") == -1)
|
261
|
{
|
262
|
throw new Exception("Couldn't upload data file "+dataFileName +
|
263
|
" with id "+dataId+ " into Metacat since "+response);
|
264
|
}
|
265
|
|
266
|
//6. Updates eml online url and package id in DOM
|
267
|
String newId = updateEMLDoc(rootNode, docid, dataId);
|
268
|
//System.out.println("The new docid is ========"+newId);
|
269
|
|
270
|
//Put EML DOM with the new packagId and oneline url into a StringWriter and store it to String
|
271
|
StringWriter stringWriter = new StringWriter();
|
272
|
PrintWriter printWriter = new PrintWriter(stringWriter);
|
273
|
XMLUtilities.print(rootNode, printWriter);
|
274
|
String xml = stringWriter.toString();
|
275
|
//System.out.println("the xml is "+xml);
|
276
|
|
277
|
//7.insert new (update) EML document into Metacat
|
278
|
StringReader xmlReader = new StringReader(xml);
|
279
|
response = metacat.update(newId, xmlReader, null);
|
280
|
if (response.indexOf("<success>") == -1)
|
281
|
{
|
282
|
throw new Exception("Upload data file "+dataFileName +
|
283
|
" with id "+dataId+ " successfully but update eml "+newId +" failed since "+ response);
|
284
|
}
|
285
|
metacat.logout();
|
286
|
return dataId;
|
287
|
}
|
288
|
|
289
|
/*
|
290
|
* Gets onlineUrl value from a given eml DOM document.
|
291
|
* The online url xpath can be "/eml/dataset/dataTable/physical/distribution/online/url"
|
292
|
* or "/eml/dataset/spatialRaster/physical/distribution/online/url"
|
293
|
*/
|
294
|
private String getOnLineURL(Node root) throws Exception
|
295
|
{
|
296
|
String url = null;
|
297
|
if (root == null)
|
298
|
{
|
299
|
throw new Exception("root node for this EML is null and couldn't get online url from it");
|
300
|
}
|
301
|
Node urlNode = XMLUtilities.getTextNodeWithXPath(root, TABLEONLINEURL);
|
302
|
// in table online url does exist, we will try to use another xpath - SPATIALONLEURL
|
303
|
if (urlNode == null)
|
304
|
{
|
305
|
urlNode = XMLUtilities.getTextNodeWithXPath(root, SPATIALONLINEURL);
|
306
|
}
|
307
|
// Couldn't find any matche element, throw exception
|
308
|
if(urlNode == null)
|
309
|
{
|
310
|
throw new Exception("Couldn't find any onlie url information in eml document");
|
311
|
}
|
312
|
//Gets text node value and if the url doesn't contain "srb;//", it will throw a exception
|
313
|
url = urlNode.getNodeValue();
|
314
|
if (url == null || url.indexOf(SRB)== -1)
|
315
|
{
|
316
|
throw new Exception("The online url doesn't have srb protocol and we don't need to handle");
|
317
|
}
|
318
|
return url;
|
319
|
}
|
320
|
|
321
|
/*
|
322
|
* Automatically to generate a unique id for ddata file.
|
323
|
* This id will be looked like - DATAIDPREFIX.numberBaseonTime.1, e.g
|
324
|
* IPCC.20072321.1
|
325
|
*/
|
326
|
private String generateId()
|
327
|
{
|
328
|
int version = 1;
|
329
|
StringBuffer docid = new StringBuffer(DATAIDPREFIX);
|
330
|
docid.append(DOT);
|
331
|
|
332
|
// Create a calendar to get the date formatted properly
|
333
|
String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
|
334
|
SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
|
335
|
pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2*60*60*1000);
|
336
|
pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
|
337
|
Calendar calendar = new GregorianCalendar(pdt);
|
338
|
Date trialTime = new Date();
|
339
|
calendar.setTime(trialTime);
|
340
|
|
341
|
int time = 0;
|
342
|
|
343
|
docid.append(calendar.get(Calendar.YEAR));
|
344
|
|
345
|
time = calendar.get(Calendar.DAY_OF_YEAR);
|
346
|
if(time < 10){
|
347
|
docid.append("0");
|
348
|
docid.append("0");
|
349
|
docid.append(time);
|
350
|
} else if(time < 100) {
|
351
|
docid.append("0");
|
352
|
docid.append(time);
|
353
|
} else {
|
354
|
docid.append(time);
|
355
|
}
|
356
|
|
357
|
time = calendar.get(Calendar.HOUR_OF_DAY);
|
358
|
if(time < 10){
|
359
|
docid.append("0");
|
360
|
docid.append(time);
|
361
|
} else {
|
362
|
docid.append(time);
|
363
|
}
|
364
|
|
365
|
time = calendar.get(Calendar.MINUTE);
|
366
|
if(time < 10){
|
367
|
docid.append("0");
|
368
|
docid.append(time);
|
369
|
} else {
|
370
|
docid.append(time);
|
371
|
}
|
372
|
|
373
|
time = calendar.get(Calendar.SECOND);
|
374
|
if(time < 10){
|
375
|
docid.append("0");
|
376
|
docid.append(time);
|
377
|
} else {
|
378
|
docid.append(time);
|
379
|
}
|
380
|
//sometimes this number is not unique, so we append a random number
|
381
|
int random = (new Double(Math.random()*100)).intValue();
|
382
|
docid.append(random);
|
383
|
docid.append(DOT);
|
384
|
docid.append(version);
|
385
|
|
386
|
return docid.toString();
|
387
|
|
388
|
}
|
389
|
/*
|
390
|
* Get data file name from online url. SRB oneline url will looks like -
|
391
|
* srb://seek:/home/beam.seek/IPCC_climate/Present/ccld6190.dat.
|
392
|
* The last part - ccld6190.dat is the file name. This method will get
|
393
|
* the file name from the give url
|
394
|
*/
|
395
|
private String getDataFileNameFromURL(String onlineUrl) throws Exception
|
396
|
{
|
397
|
String dataFile = null;
|
398
|
String slash = "/";
|
399
|
if (onlineUrl != null)
|
400
|
{
|
401
|
int index = onlineUrl.lastIndexOf(slash);
|
402
|
try
|
403
|
{
|
404
|
dataFile = onlineUrl.substring(index+1);
|
405
|
}
|
406
|
catch(Exception e)
|
407
|
{
|
408
|
throw new Exception("Couldn't get data file name from the given url "+onlineUrl+
|
409
|
" since "+e.getMessage());
|
410
|
}
|
411
|
}
|
412
|
return dataFile;
|
413
|
}
|
414
|
|
415
|
|
416
|
|
417
|
/*
|
418
|
* Gets eml document list from text file. The text file format should be:
|
419
|
* tao.1.1
|
420
|
* tao.2.1
|
421
|
*/
|
422
|
private Vector getDocumentListFromFile() throws Exception
|
423
|
{
|
424
|
Vector docList = new Vector();
|
425
|
File docListFile = new File(SRBDATAFILEDIR,DOCLISTFILE);
|
426
|
FileReader docListFileReader= new FileReader(docListFile);
|
427
|
BufferedReader readDocList = new BufferedReader(docListFileReader);
|
428
|
// Read every line from the text file and put it into a vector
|
429
|
String docid = readDocList.readLine();
|
430
|
while (docid != null)
|
431
|
{
|
432
|
// If the docid string is not empty, put it into vector
|
433
|
if (!docid.trim().equals(""))
|
434
|
{
|
435
|
docList.add(docid.trim());
|
436
|
}
|
437
|
docid = readDocList.readLine();
|
438
|
}
|
439
|
return docList;
|
440
|
}
|
441
|
|
442
|
/*
|
443
|
* Update the given eml document (in DOM). There are two places to be updated
|
444
|
* The package id will be increased 1, i.e., from 1 to 2. The distribution online url will
|
445
|
* point to the new ecogrid id, i.e. , ecogrid://knb/IPCC.2007.1
|
446
|
*/
|
447
|
private String updateEMLDoc(Node root, String docid, String dataId) throws Exception
|
448
|
{
|
449
|
// update package id
|
450
|
docid = getIncreasedNewDocid(docid);
|
451
|
XMLUtilities.addAttributeNodeToDOMTree( root, PACKAGEID, docid);
|
452
|
// update online url. oneline url should either in spatialRaster or dataTable.
|
453
|
// First try to see if spatialRaster exist or not. If not try data table
|
454
|
String newUrl = KNB+dataId; //new url looks like ecogrid://knb/IPCC.2007.1
|
455
|
boolean isSpatialRaster = true;
|
456
|
boolean isDataTable = false;
|
457
|
Node urlNode = XMLUtilities.getTextNodeWithXPath(root, SPATIALONLINEURL);
|
458
|
if (urlNode == null)
|
459
|
{
|
460
|
// has no spatialRaster
|
461
|
isSpatialRaster = false;
|
462
|
}
|
463
|
else
|
464
|
{
|
465
|
// has spatialRaster
|
466
|
isSpatialRaster = true;
|
467
|
}
|
468
|
// determin if has datable or not
|
469
|
urlNode = XMLUtilities.getTextNodeWithXPath(root,TABLEONLINEURL);
|
470
|
if (urlNode != null)
|
471
|
{
|
472
|
isDataTable = true;
|
473
|
}
|
474
|
|
475
|
if (isSpatialRaster && !isDataTable)
|
476
|
{
|
477
|
//only has spatialRaster and no dataTable, update spatialRaster online url
|
478
|
XMLUtilities.addTextNodeToDOMTree(root, SPATIALONLINEURL, newUrl);
|
479
|
}
|
480
|
else if (!isSpatialRaster && isDataTable)
|
481
|
{
|
482
|
//only has dataTable and no spatialRaster, update dataTable online url
|
483
|
XMLUtilities.addTextNodeToDOMTree(root, TABLEONLINEURL, newUrl);
|
484
|
}
|
485
|
else
|
486
|
{
|
487
|
//some strange things happen
|
488
|
throw new Exception("The eml either has both dataTable or spatialRaster OR doesn't has any entity");
|
489
|
}
|
490
|
return docid;
|
491
|
}
|
492
|
|
493
|
/*
|
494
|
* Gets new docid with increased version. Docid looks like tao.1.1. The new docid will be
|
495
|
* tao.1.2.
|
496
|
*/
|
497
|
private String getIncreasedNewDocid(String docid) throws Exception
|
498
|
{
|
499
|
int rev = 1;
|
500
|
String revision = null;
|
501
|
String prefix = null;
|
502
|
String newId = null;
|
503
|
if (docid != null)
|
504
|
{
|
505
|
int index = docid.lastIndexOf(DOT);
|
506
|
try
|
507
|
{
|
508
|
// Get revsion part(1)
|
509
|
revision = docid.substring(index+1);
|
510
|
// Get prefix part (tao.1.)
|
511
|
prefix = docid.substring(0, index+1);
|
512
|
// increase version from 1 to 2
|
513
|
rev = (new Integer(revision)).intValue();
|
514
|
rev++;
|
515
|
// combines the prefix tao.1. and new revision2 to get tao.1.2
|
516
|
newId= prefix+rev;
|
517
|
|
518
|
}
|
519
|
catch(Exception e)
|
520
|
{
|
521
|
throw new Exception("Couldn't increase revsion number from the given docid "+docid+
|
522
|
" since "+e.getMessage());
|
523
|
|
524
|
}
|
525
|
}
|
526
|
return newId;
|
527
|
}
|
528
|
|
529
|
/*
|
530
|
* Gets eml document list from searching Metacat
|
531
|
* TO-DO: This method need to be implemented
|
532
|
*/
|
533
|
private Vector getDocumentListFromMetacat()
|
534
|
{
|
535
|
Vector docList = new Vector();
|
536
|
return docList;
|
537
|
}
|
538
|
|
539
|
/*
|
540
|
* Get eml document list. First this method will try
|
541
|
* to get the eml document list form text file. If the result is empty or
|
542
|
* it caught an exception it will try to get eml document list from metacat.
|
543
|
*/
|
544
|
private Vector getDocumentList()
|
545
|
{
|
546
|
Vector list = null;
|
547
|
try
|
548
|
{
|
549
|
//First, try to get eml doc list from text file
|
550
|
list = getDocumentListFromFile();
|
551
|
if (list == null || list.isEmpty())
|
552
|
{
|
553
|
throw new Exception("The eml doclist is empty in text file");
|
554
|
}
|
555
|
}
|
556
|
catch(Exception e)
|
557
|
{
|
558
|
System.err.println("Couldn't get eml document list from text file: "+e.getMessage());
|
559
|
// If an exception happened, try to get eml doc list from metacat
|
560
|
list = getDocumentListFromMetacat();
|
561
|
}
|
562
|
if (list != null)
|
563
|
{
|
564
|
System.out.println("the list is "+list);
|
565
|
}
|
566
|
return list;
|
567
|
}
|
568
|
|
569
|
/*
|
570
|
* Writes error message into log file.
|
571
|
*/
|
572
|
private void writeLog(File file, String message)
|
573
|
{
|
574
|
try
|
575
|
{
|
576
|
FileOutputStream fos = new FileOutputStream(file, true);
|
577
|
PrintWriter pw = new PrintWriter(fos);
|
578
|
SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
|
579
|
java.util.Date localtime = new java.util.Date();
|
580
|
String dateString = formatter.format(localtime);
|
581
|
dateString += " :: " + message;
|
582
|
//time stamp each entry
|
583
|
pw.println(dateString);
|
584
|
pw.flush();
|
585
|
pw.close();
|
586
|
fos.close();
|
587
|
}
|
588
|
catch(Exception e)
|
589
|
{
|
590
|
System.out.println("error writing to replication log from " +
|
591
|
"MetacatReplication.replLog: " + e.getMessage());
|
592
|
//e.printStackTrace(System.out);
|
593
|
}
|
594
|
}
|
595
|
|
596
|
/*
|
597
|
* Read a csv file which contains current data file name and correct data file name.
|
598
|
* The format of csv file is:
|
599
|
* currentname1,correctname1
|
600
|
* currentname2,correctname2
|
601
|
* ........
|
602
|
* The return value is hash table, the current data file name is key and correct file name is
|
603
|
* value.
|
604
|
*/
|
605
|
private Hashtable getCurrent_CorrectFileNamesPair() throws Exception
|
606
|
{
|
607
|
Hashtable fileNamesHash = new Hashtable();
|
608
|
File current_correctFileNames = new File(CURRENT_CORRECTFILENAME);
|
609
|
FileReader fileReader= new FileReader(current_correctFileNames);
|
610
|
BufferedReader readDocList = new BufferedReader(fileReader);
|
611
|
// Read every line from the text file, this line will look like:
|
612
|
// currentname1,correctname1
|
613
|
String lineString = readDocList.readLine();
|
614
|
while (lineString != null)
|
615
|
{
|
616
|
//Get the comma index number
|
617
|
int commaIndex = lineString.indexOf(",");
|
618
|
if (commaIndex != -1)
|
619
|
{
|
620
|
//Get the current file name part
|
621
|
String currentName = lineString.substring(0, commaIndex);
|
622
|
//Get the correct file name part
|
623
|
String correctName = lineString.substring(commaIndex+1, lineString.length());
|
624
|
if (currentName != null && correctName != null)
|
625
|
{
|
626
|
fileNamesHash.put(currentName.trim(), correctName.trim());
|
627
|
}
|
628
|
}
|
629
|
lineString = readDocList.readLine();
|
630
|
}
|
631
|
return fileNamesHash;
|
632
|
}
|
633
|
}
|