Project

General

Profile

« Previous | Next » 

Revision 3662

Added by Jing Tao almost 17 years ago

Add a file which will upload ipcc data to metacat.

View differences:

test/edu/ucsb/nceas/metacattest/UploadIPCCDataTest.java
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$'
9
 *     '$Date$'
10
 * '$Revision$'
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.utilities.IOUtil;
33
import edu.ucsb.nceas.utilities.Options;
34
import edu.ucsb.nceas.utilities.XMLUtilities;
35
//import edu.ucsb.nceas.morpho.framework.*;
36
import junit.framework.Test;
37
import junit.framework.TestCase;
38
import junit.framework.TestResult;
39
import junit.framework.TestSuite;
40
import org.apache.commons.logging.Log;
41
import org.apache.commons.logging.LogFactory;
42
import org.w3c.dom.Document;
43
import org.w3c.dom.Node;
44
import org.w3c.dom.NodeList;
45

  
46
import java.io.*;
47
import java.net.*;
48
import java.util.*;
49

  
50

  
51
/**
52
 * This class is used to change the data file location for IPCC eml documents.
53
 * Currently IPCC eml documents point data file ti SRB server. However, the srb 
54
 * earthgrid is not very stable. We decided to change the online URL from srb to knb.
55
 * So this class will handle this case.
56
 * Before running this program, it needs:
57
 * 1. Downloaded data files from SRB
58
 * 2. A list of IPCC docid(with revision number) text file. If the text file is not available, it need
59
 * a metacat query file to search metacat to get the doicd list.
60
 * What the class will do:
61
 * 1. After getting a eml docid form the docid list, it will read the eml from Metacat.
62
 * 2. DOM parser will get online URL information from eml document.
63
 * 3. It will generate docid for data file and modify the URL in eml base on the generated docid.
64
 * 4. Base on old URL information, this program will find the data file in the direcotry which contains
65
 * the srb data file, then upload the download srb data file to Metacat with assigned docid.
66
 *  5. Update eml document with the new URL information (pointing to knb).
67
 * 
68
 */ 
69
public class UploadIPCCDataTest extends TestCase
70
{
71
  
72
	  private static final Log log = LogFactory.getLog("edu.ucsb.nceas.metacattest.UploadIPCCDataTest");
73
	  /* Initialize Options*/
74
	  static
75
	  {
76
		  try
77
		  {
78
			  Options.initialize(new File("build/tests/metacat.properties"));
79
			  MetaCatUtil.pathsForIndexing 
80
			         = MetaCatUtil.getOptionList(MetaCatUtil.getOption("indexPaths"));
81
		  }
82
		  catch(Exception e)
83
		  {
84
			  System.err.println("Exception in initialize option in MetacatServletNetTest "+e.getMessage());
85
		  }
86
	  }
87
	  
88
	  /**Constants*/
89
	  private static String SRBDATAFILEDIR = "/home/tao/data-file"; // Dir for storing srb data file
90
	  private static String DOCLISTFILE       = "docidList"; // File name which stores IPCC document id
91
	  private static String METACATURL      = "http://chico.dyndns.org/knb/metacat";
92
	  private static String USERNAME          = "uid=tao,o=NCEAS,dc=ecoinformatics,dc=org";
93
	  private static String PASSWORD           = "password";
94
	  private static String TABLEONLINEURL= "/eml/dataset/dataTable/physical/distribution/online/url";
95
	  private static String SPATIALONLINEURL = "/eml/dataset/spatialRaster/physical/distribution/online/url";
96
	  private static String SRB                           = "srb://";
97
	  
98
	  /**
99
	   * Constructor to build the test
100
	   *
101
	   * @param name the name of the test method
102
	   */
103
	  public UploadIPCCDataTest(String name)
104
	  {
105
	    super(name);
106
	  }
107

  
108

  
109
	  /**
110
	   * Create a suite of tests to be run together
111
	   */
112
	  public static Test suite()
113
	  {
114
		   TestSuite suite = new TestSuite();
115
		    suite.addTest(new UploadIPCCDataTest("upload"));
116
		    return suite;
117
	 }
118
	  
119
	  /**
120
	   * Upload the data file to Metacat and modify the eml documents
121
	   * @return
122
	   * @throws Exception
123
	   */
124
	  public void upload()
125
	  {
126
		  
127
		      // Get eml document first
128
			  Vector list = getDocumentList();
129
			  //If list is not empty, goes through every document by handleSingleEML method -
130
			  //1. It will read the eml from Metacat.
131
			  // 2. Get online URL information from eml document by DOM parser.
132
			  // 3. Base on the URL information, this program will find the data file in
133
			  // the direcotry which contains the srb data file.
134
			  // 4. It will generate docid for the data file
135
			  // 5. Modify the eml document with the new URL information (pointing to
136
			  // knb) and new version number in eml,  then update it to a new version in Metacat.
137
			  //6. At last upload the download srb data file to Metacat with assigned docid.
138
              if (list != null && !list.isEmpty())
139
              {
140
            	   int size = list.size();
141
            	   for (int i=0; i<size; i++)
142
            	   {
143
            		   String docid = null;
144
            		   try
145
            		   {
146
            			   docid = (String)list.elementAt(i);
147
            			   handleSingleEML(docid);
148
            		   }
149
            		   catch(Exception e)
150
            		   {
151
            			   System.err.println("Failed to handle eml document "+docid + " since "+
152
            					   e.getMessage());
153
            		   }
154
            	   }
155
              }
156
              else
157
              {
158
            	  System.err.println("There is no EML document to handle");
159
              }
160
		
161
	  }
162
	  /*
163
	   * Does actually job to upload data file and modify eml document for a given id.
164
	   * Here are its tasts:
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. Modify the eml document with the new URL information (pointing to
171
	   *     knb) and new version number in eml,  then update it to a new version in Metacat.
172
	   * 6.  At last upload the download srb data file to Metacat with assigned docid.
173
	   */	 
174
	  private void handleSingleEML(String docid) throws Exception
175
	  {
176
		  Metacat metacat = MetacatFactory.createMetacatConnection(METACATURL);
177
		  // login metacat 
178
		  String loginResponse = metacat.login(USERNAME, PASSWORD);
179
		  if (loginResponse.indexOf("<login>") == -1)
180
		  {
181
			  throw new Exception("login failed "+loginResponse);
182
		  }
183
		  // Reads eml document from metacat
184
		  Reader r = metacat.read(docid);
185
          Document DOMdoc = XMLUtilities.getXMLReaderAsDOMDocument(r);
186
          // Gets online url information. If onlineUrl is not SRB, through an exception
187
          String onlineUrl = getOnLineURL(DOMdoc);
188
          // Find the srb data file name
189
          
190
          // Generate docid for data file
191
          //String dataId = generateId();
192
          // Updates eml online url and package id
193
          //updateEMLDoc();
194
          // update EML document in metacat
195
          
196
          // upload data file to Metacat
197
          
198
          metacat.logout();
199
	  }
200
	  
201
	  /*
202
	   * Gets onlineUrl value from a given eml DOM document.
203
	   * The online url xpath can be "/eml/dataset/dataTable/physical/distribution/online/url"
204
	   * or "/eml/dataset/spatialRaster/physical/distribution/online/url"
205
	   */
206
	  private String getOnLineURL(Document doc) throws Exception
207
	  {
208
		  String url = null;
209
		  if (doc == null)
210
		  {
211
			  throw new Exception("DOM document for this EML is null and couldn't get online url from it");
212
		  }
213
		  Node root = (Node)doc.getDocumentElement();
214
		  if (root == null)
215
		  {
216
			  throw new Exception("root node for this EML is null and couldn't get online url from it");
217
		  }
218
		  Node urlNode = XMLUtilities.getTextNodeWithXPath(root, TABLEONLINEURL);
219
		  // in table online url does exist, we will try to use another xpath - SPATIALONLEURL
220
		  if (urlNode == null)
221
		  {
222
			  urlNode = XMLUtilities.getTextNodeWithXPath(root, SPATIALONLINEURL);
223
		  }
224
		  // Couldn't find any matche element, throw exception
225
		  if(urlNode == null)
226
		  {
227
			  throw new Exception("Couldn't find any onlie url information in eml document");
228
		  }
229
		  //Gets text node value and if the url doesn't contain "srb;//", it will throw a exception
230
		 url = urlNode.getNodeValue();
231
		 if (url == null || url.indexOf(SRB)== -1)
232
		 {
233
			 throw new Exception("The online url doesn't have srb protocol and we don't need to handle");
234
		 }
235
		  return url;
236
	  }
237
     
238
      /*
239
       * Gets eml document list from text file. The text file format should be:
240
       * tao.1.1
241
       * tao.2.1
242
       */
243
	  private Vector getDocumentListFromFile() throws Exception
244
	  {
245
		  Vector docList = new Vector();
246
		  File docListFile = new File(SRBDATAFILEDIR,DOCLISTFILE);
247
		  FileReader docListFileReader= new FileReader(docListFile);
248
		  BufferedReader readDocList = new BufferedReader(docListFileReader);
249
		  // Read every line from the text file and put it into a vector
250
		  String docid = readDocList.readLine();
251
		  while (docid != null)
252
		  {
253
			  // If the docid string is not empty, put it into vector
254
			  if (!docid.trim().equals(""))
255
			  {
256
			     docList.add(docid.trim());
257
			  }
258
			  docid = readDocList.readLine();
259
		  }
260
		  return docList;
261
	  }
262
	  
263
	  /*
264
	   * Gets eml document list from searching Metacat
265
	   * TO-DO: This method need to be implemented
266
	   */
267
	  private Vector getDocumentListFromMetacat()
268
	  {
269
		  Vector docList = new Vector();
270
		  return docList;
271
	  }
272
	  
273
	  /*
274
	   * Get eml document list. First this method will try
275
	   * to get the eml document list form text file. If the result is empty or
276
	   * it caught an exception it will try to get eml document list from metacat.
277
	   */
278
	  private Vector getDocumentList()
279
	  {
280
		  Vector list = null;
281
		  try
282
		  {
283
			  //First, try to get eml doc list from text file
284
			  list = getDocumentListFromFile();
285
			  if (list == null || list.isEmpty())
286
			  {
287
				  throw new Exception("The eml doclist is empty in text file");
288
			  }
289
		  }
290
		  catch(Exception e)
291
		  {
292
			  System.err.println("Couldn't get eml document list from text file: "+e.getMessage());
293
			  // If an exception happened, try to get eml doc list from metacat
294
			  list = getDocumentListFromMetacat();
295
		  }
296
		  if (list != null)
297
		  {
298
			  System.out.println("the list is "+list);
299
		  }
300
		  return list;
301
	  }
302

  
303
}
0 304

  

Also available in: Unified diff