Project

General

Profile

« Previous | Next » 

Revision 4672

Added by Jing Tao about 16 years ago

Add code to go through eml201 document list to transform them to eml210.

View differences:

src/edu/ucsb/nceas/metacat/util/EMLVersionsTransformer.java
25 25
package edu.ucsb.nceas.metacat.util;
26 26

  
27 27
import java.io.*;
28
import java.sql.PreparedStatement;
29
import java.sql.ResultSet;
30
import java.sql.SQLException;
28 31
import java.util.Enumeration;
29 32
import java.util.Hashtable;
33
import java.util.Vector;
30 34

  
31 35
import org.apache.log4j.Logger;
32 36

  
37
import edu.ucsb.nceas.metacat.DBConnection;
38
import edu.ucsb.nceas.metacat.DBConnectionPool;
39
import edu.ucsb.nceas.metacat.DocumentImpl;
40
import edu.ucsb.nceas.metacat.DocumentImplWrapper;
41
import edu.ucsb.nceas.metacat.McdbException;
42
import edu.ucsb.nceas.metacat.service.PropertyService;
43

  
33 44
import javax.xml.transform.TransformerFactory;
34 45
import javax.xml.transform.Transformer;
35 46
import javax.xml.transform.stream.StreamSource;
......
37 48
import javax.xml.transform.TransformerException;
38 49
import javax.xml.transform.TransformerConfigurationException;
39 50
import javax.xml.transform.URIResolver;
51
import org.ecoinformatics.eml.EMLParser;
40 52

  
41 53

  
42

  
43 54
/**
44 55
 * A Class that transforms older eml version to newer eml version utitlizing XSL style sheets. 
45 56
 */
46 57
public class EMLVersionsTransformer {
47
	private static Logger logMetacat = Logger.getLogger(GeoserverUtil.class);
58
	
59
	private static Logger logMetacat = Logger.getLogger(EMLVersionsTransformer.class);
60
	private static String eml210StyleFile = null;
61
	static{
62
		try
63
		{
64
			eml210StyleFile =PropertyService.getProperty("application.deployDir")+"/"+PropertyService
65
			.getProperty("application.context")+ "/style/common/eml201to210.xsl"; //eml201to210.xsl place
66
		}
67
		catch(Exception e)
68
		{
69
			logMetacat.warn("Couldn't get eml201to210.xsl stylesheet");
70
		}
71
	}
72
	private static String DOT = ".";
48 73

  
49 74
    /**
50 75
     * Public constructor because all methods are static and do not need 
51 76
     * an instance.
52 77
     */
53
    private EMLVersionsTransformer() 
78
    public EMLVersionsTransformer() 
54 79
    {
80
    	
55 81
    }
56

  
82
    
83
    /**
84
     * Method to upgrade old versions of eml to new version
85
     */
86
    public void upgrade()
87
    {
88
    	upgradeEML200ToEML210();
89
    }
90
    
57 91
    /*
58
     * Transform an XML document using an XSLT stylesheet to another format,
59
     * probably HTML or another XML document format.
60
     *
61
     * @param docString the document to be transformed
62
     * @param xslSystemId the system location of the stylesheet
63
     * @param pw the PrintWriter to which output is printed
64
     * @param params some parameters for inclusion to the transformation
92
     * Upgrade every eml200 or eml210 documents into eml210
65 93
     */
66
    private static void transform(String docString, String xslSystemId,
67
        Writer pw, Hashtable param) throws Exception
94
    private  void upgradeEML200ToEML210()
68 95
    {
69
        transform(new StringReader(docString), xslSystemId, pw, param);
96
    	Vector list = getEML2DocList();
97
    	if(list != null)
98
    	{
99
    		for(int i=0; i<list.size(); i++)
100
    		{
101
    			OwnerAndDocid pair = (OwnerAndDocid)list.elementAt(i);
102
    			String docid = pair.getDocid();
103
    			String owner = pair.getOwner();
104
    			try
105
    			{
106
    				handleSingleEML200Document(docid, owner);
107
    			}
108
    			catch(Exception e)
109
    			{
110
    				logMetacat.warn("The docid "+docid+" with owner "+owner+" couldn't be transformed to eml-2.1.0 since "+e.getMessage());
111
    			}
112
    		}
113
    	}
70 114
    }
115
    
116
    /*
117
     * Handle single eml201 or eml 200 document: read the document, transform it to eml210 document 
118
     * then save it to 210 document into metacat
119
     */
120
    private  void handleSingleEML200Document(String docidWithRev, String owner) throws Exception
121
    {
122
    	DocumentImpl docImpl = new DocumentImpl(docidWithRev);
123
    	String eml200Content = docImpl.toString();
124
    	StringReader eml200Source= new StringReader(eml200Content);
125
    	//PipedWriter eml210OutputAfterTransform = new PipedWriter();
126
    	//PipedReader eml210SourceForNewDoc = new PipedReader();
127
    	//eml210SourceForNewDoc.connect(eml210OutputAfterTransform);
128
    	StringWriter strWriter = new StringWriter();
129
    	String newId = increaseRevisionNumber(docidWithRev);
130
    	if(newId != null)
131
    	{
132
    	     transformEML200ToEML210(eml200Source, eml210StyleFile,  strWriter, newId);
133
    	     String eml210Content = "";
134
    	     strWriter.write(eml210Content);
135
    	     String rule = DocumentImpl.EML210;
136
             // using emlparser to check id validation
137
             EMLParser parser = new EMLParser(eml210Content);
138
             DocumentImplWrapper documentWrapper = new DocumentImplWrapper(rule, true);
139
             StringReader xml = new StringReader(eml210Content);
140
             String  doAction = "UPDATE";
141
             String pub = null;
142
             String []groups = null;
143
             DBConnection dbconn = null;
144
             StringReader dtd = null;
145
             int serialNumber = -1;
146
             try
147
             {
148
            	 dbconn = DBConnectionPool
149
                 .getDBConnection("EMLVersionsTransformer.handleSingleEML200Document");
150
                  serialNumber = dbconn.getCheckOutSerialNumber();
151
                  documentWrapper.write(dbconn, xml, pub, dtd,
152
                          doAction, newId, owner, groups);
153
                  logMetacat.warn("Doc "+docidWithRev+"was transformed to eml210 with new id "+newId);
154
             }
155
             catch(Exception e)
156
             {
157
            	 throw e;
158
             }
159
             finally
160
             {
161
            	 // Return db connection
162
                 DBConnectionPool.returnDBConnection(dbconn, serialNumber);
163
             }
164
    	}
165
    	else
166
    	{
167
    		logMetacat.warn("Couldn't increase docid "+docidWithRev+"'s revision");
168
    	}
169
    }
71 170

  
72 171
    /*
172
     * Transform single eml201 (Reader) to eml 210 (Writer)
173
     */
174
    private static void transformEML200ToEML210(Reader reader, String xslfile, Writer writer, String packageid) throws Exception{    
175
        	Hashtable param = null;
176
            if (packageid != null)
177
            {
178
            	param = new Hashtable();
179
            	param.put("package-id", packageid);
180
            }
181
            EMLVersionsTransformer.transform(reader, xslfile, writer, param);
182
         
183
    }
184

  
185

  
186
    /*
73 187
     * Transform an XML document using an XSLT stylesheet to another format,
74 188
     * probably HTML or another XML document format.
75 189
     *
......
105 219
            transformer.transform(ss, sr);
106 220
        
107 221
    }
222
    
223
    /*
224
     * Get list of document (docid and owner) which type is eml200 or eml201. 
225
     * The docid in the list will have revision number too.
226
     */
227
    private Vector getEML2DocList()
228
    {
229
    	Vector list = new Vector();
230
    	 DBConnection dbconn = null;
231
         int serialNumber = -1;
232
         String sql = "select docid, rev, user_owner from xml_documents where doctype like 'eml://ecoinformatics.org/eml-2.0.1' or doctype like 'eml://ecoinformatics.org/eml-2.0.0'";
233
         PreparedStatement pstmt = null;
234
         try {
235
             dbconn = DBConnectionPool
236
                     .getDBConnection("EMLVersionsTransformer.getEML2DocList");
237
             serialNumber = dbconn.getCheckOutSerialNumber();
238
             pstmt = dbconn.prepareStatement(sql.toString());
239
             pstmt.execute();
240
             ResultSet rs = pstmt.getResultSet();
241
             boolean tableHasRows = rs.next();
242
             if (tableHasRows) {
243
                 String docidWithoutRev = rs.getString(1);
244
                 int rev = rs.getInt(2);
245
                 String owner = rs.getString(3);
246
                 String docidWithRev = docidWithoutRev+DOT+rev;
247
                 logMetacat.info("The docid "+docidWithRev+" with owner "+owner+" will be added into list which will be transformed to eml-2.1.0");
248
                 OwnerAndDocid pair = new OwnerAndDocid(owner, docidWithRev);;
249
                 list.add(pair);
250
                 tableHasRows = rs.next();
251
             }
252
             pstmt.close();
108 253

  
254
        
255
        } catch (SQLException e) {
256
             logMetacat.error("error in DocumentImpl.getDocumentInfo: "
257
                + e.getMessage());
258
             e.printStackTrace(System.out);
259
        } finally {
260
            try {
261
                 pstmt.close();
262
            } catch (SQLException ee) {
263
              logMetacat.error(
264
                    "error in DocumentImple.getDocumentInfo: "
265
                            + ee.getMessage());
266
           } finally {
267
              DBConnectionPool.returnDBConnection(dbconn, serialNumber);
268
        }
269
      }
270
      return list;
271
    }
272
    
109 273
    /*
110
     * Transform single eml201 doc to eml 210 doc
274
     * Increase revision number for the given docid. tao.1.1 will be tao.1.2. null will be returned
275
     * if couldn't increase it.
111 276
     */
112
    private static void transformEML201ToEML210(Reader reader, String xslfile, Writer writer, String packageid) throws Exception{    
113
        	Hashtable param = null;
114
            if (packageid != null)
115
            {
116
            	param = new Hashtable();
117
            	param.put("package-id", packageid);
118
            }
119
             EMLVersionsTransformer.transform(reader, xslfile, writer, param);
120
         
121
        }
277
    private static String increaseRevisionNumber(String docidWithRev)
278
    {
279
    	String newid = null;
280
    	try
281
    	{
282
    	  if (docidWithRev != null)
283
    	  {
284
    		int index = docidWithRev.lastIndexOf(DOT);
285
    		if (index != -1)
286
    		{
287
    			String firstTwoParts = docidWithRev.substring(0,index);
288
    			String revStr = docidWithRev.substring(index+1);
289
    			Integer revObj = new Integer(revStr);
290
    			int rev = revObj.intValue();
291
    			rev= rev+1;
292
    			newid = firstTwoParts+DOT+rev;
293
    		}
294
    	  }
295
    	}
296
    	catch(Exception e)
297
    	{
298
    		logMetacat.warn("Couldn't increase revision number since "+e.getMessage());
299
    	}
300
    	return newid;
301
    }
302
    
303
    /*
304
     * Class reprents a document's docid and its owner 
305
     * @author tao
306
     *
307
     */
308
    class OwnerAndDocid{
309
    	private String owner = null;
310
    	private String docidWithRev = null;
311
    	
312
    	public OwnerAndDocid(String owner, String docidWithRev)
313
    	{
314
    		this.owner = owner;
315
    		this.docidWithRev = docidWithRev;
316
    	}
317
    	
318
    	public String getOwner()
319
    	{
320
    		return owner;
321
    	}
322
    	
323
    	public String getDocid()
324
    	{
325
    		return docidWithRev;
326
    	}
327
    }
328
    
329

  
122 330
  
123 331
}
124 332

  

Also available in: Unified diff