Project

General

Profile

1
package edu.ucsb.nceas.metacat.annotation;
2

    
3
import java.io.InputStream;
4
import java.io.StringWriter;
5
import java.sql.PreparedStatement;
6
import java.sql.SQLException;
7
import java.util.ArrayList;
8
import java.util.Arrays;
9
import java.util.HashMap;
10
import java.util.Iterator;
11
import java.util.List;
12
import java.util.Map;
13
import java.util.Vector;
14

    
15
import org.apache.commons.io.IOUtils;
16
import org.apache.log4j.Logger;
17
import org.apache.wicket.protocol.http.mock.MockHttpServletRequest;
18
import org.dataone.service.types.v1.Identifier;
19
import org.dataone.service.types.v1.Session;
20
import org.dataone.service.types.v1.Subject;
21
import org.ecoinformatics.datamanager.parser.Attribute;
22
import org.ecoinformatics.datamanager.parser.DataPackage;
23
import org.ecoinformatics.datamanager.parser.Entity;
24
import org.ecoinformatics.datamanager.parser.generic.DataPackageParserInterface;
25
import org.ecoinformatics.datamanager.parser.generic.Eml200DataPackageParser;
26

    
27
import com.hp.hpl.jena.ontology.AllValuesFromRestriction;
28
import com.hp.hpl.jena.ontology.Individual;
29
import com.hp.hpl.jena.ontology.ObjectProperty;
30
import com.hp.hpl.jena.ontology.OntClass;
31
import com.hp.hpl.jena.ontology.OntModel;
32
import com.hp.hpl.jena.ontology.Ontology;
33
import com.hp.hpl.jena.query.Dataset;
34
import com.hp.hpl.jena.query.Query;
35
import com.hp.hpl.jena.query.QueryExecution;
36
import com.hp.hpl.jena.query.QueryExecutionFactory;
37
import com.hp.hpl.jena.query.QueryFactory;
38
import com.hp.hpl.jena.query.QuerySolution;
39
import com.hp.hpl.jena.query.ResultSet;
40
import com.hp.hpl.jena.rdf.model.ModelFactory;
41
import com.hp.hpl.jena.rdf.model.Property;
42
import com.hp.hpl.jena.rdf.model.Resource;
43
import com.hp.hpl.jena.tdb.TDBFactory;
44
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
45

    
46
import edu.ucsb.nceas.metacat.DBUtil;
47
import edu.ucsb.nceas.metacat.DocumentImpl;
48
import edu.ucsb.nceas.metacat.IdentifierManager;
49
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
50
import edu.ucsb.nceas.metacat.database.DBConnection;
51
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
52
import edu.ucsb.nceas.metacat.dataone.MNodeService;
53
import edu.ucsb.nceas.metacat.index.MetacatSolrIndex;
54
import edu.ucsb.nceas.metacat.properties.PropertyService;
55
import edu.ucsb.nceas.metacat.replication.ReplicationService;
56
import edu.ucsb.nceas.metacat.util.DocumentUtil;
57
import edu.ucsb.nceas.utilities.SortedProperties;
58

    
59
public class DatapackageSummarizer {
60

    
61
	private static Logger logMetacat = Logger.getLogger(DatapackageSummarizer.class);
62
	
63
	public static String rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
64
	public static String rdfs = "http://www.w3.org/2000/01/rdf-schema#";
65
	public static String owl = "http://www.w3.org/2002/07/owl#";
66
	public static String oboe = "http://ecoinformatics.org/oboe/oboe.1.0/oboe.owl#";
67
	public static String oboe_core = "http://ecoinformatics.org/oboe/oboe.1.0/oboe-core.owl#";
68
	public static String oboe_characteristics = "http://ecoinformatics.org/oboe/oboe.1.0/oboe-characteristics.owl#";
69
	public static String oboe_sbc = "http://ecoinformatics.org/oboe-ext/sbclter.1.0/oboe-sbclter.owl#";
70
	public static String oa = "http://www.w3.org/ns/oa#";
71
	public static String oa_source = "http://www.w3.org/ns/oa.rdf";
72
	public static String dcterms = "http://purl.org/dc/terms/";
73
	public static String dcterms_source = "http://dublincore.org/2012/06/14/dcterms.rdf";
74
	public static String foaf = "http://xmlns.com/foaf/0.1/";
75
	public static String foaf_source = "http://xmlns.com/foaf/spec/index.rdf";
76
    public static String prov = "http://www.w3.org/ns/prov#";
77
    public static String prov_source = "http://www.w3.org/ns/prov.owl";
78
    public static String cito =  "http://purl.org/spar/cito/";
79
    
80
	public static String OBOE_SBC = "OBOE-SBC";
81
    
82
    // package visibility for testing only
83
    boolean randomize = false;
84
    
85
    public void indexEphemeralAnnotation(Identifier metadataPid) throws Exception {
86

    
87
    	// generate an annotation for the metadata given
88
		String rdfContent = this.generateAnnotation(metadataPid);
89
		
90
		// load to triple store
91
		Dataset dataset = TDBFactory.createDataset("./tbd");
92
		
93
    	// read the annotation into the triplestore
94
		InputStream source = IOUtils.toInputStream(rdfContent, "UTF-8");
95
    	String name = "http://annotation";
96
    	boolean loaded = dataset.containsNamedModel(name);
97
    	if (loaded) {
98
    		dataset.removeNamedModel(name);
99
    		loaded = false;
100
    	}
101
		if (!loaded) {
102
			OntModel ontModel = ModelFactory.createOntologyModel();
103
			ontModel.read(source, name);
104
			dataset.addNamedModel(name, ontModel);
105
		}
106
		
107
		// query for fields to add to index
108
        Map<String, List<Object>> fields = new HashMap<String, List<Object>>();
109
		
110
        // TODO: look up the query to use (support multiple like in the indexing project)
111
        String q = null;
112
        
113
        q = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
114
        	+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
115
        	+ "PREFIX owl: <http://www.w3.org/2002/07/owl#> " 
116
			+ "PREFIX oboe-core: <http://ecoinformatics.org/oboe/oboe.1.0/oboe-core.owl#> "
117
			+ "PREFIX oa: <http://www.w3.org/ns/oa#> "
118
			+ "PREFIX dcterms: <http://purl.org/dc/terms/> "
119
			+ "SELECT ?standard_sm ?id "
120
			+ "FROM <$GRAPH_NAME> "
121
			+ "WHERE { "
122
			+ "		?measurement rdf:type oboe-core:Measurement . "
123
			+ "		?measurement rdf:type ?restriction . "
124
			+ "		?restriction owl:onProperty oboe-core:usesStandard . "
125
			+ "		?restriction owl:allValuesFrom ?standard . "
126
			+ "		?standard rdfs:subClassOf+ ?standard_sm . "
127
			+ "		?standard_sm rdfs:subClassOf oboe-core:Standard . "				
128
			+ "		?annotation oa:hasBody ?measurement . "												
129
			+ "		?annotation oa:hasTarget ?target . "
130
			+ "		?target oa:hasSource ?metadata . "
131
			+ "		?metadata dcterms:identifier ?id . " 
132
			+ "}";
133

    
134
        q = q.replaceAll("\\$GRAPH_NAME", name);
135
		Query query = QueryFactory.create(q);
136
		QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
137
		ResultSet results = qexec.execSelect();
138
		
139
		while (results.hasNext()) {
140
			QuerySolution solution = results.next();
141
			System.out.println(solution.toString());
142
			
143
			// find the index document we are trying to augment with the annotation
144
			if (solution.contains("id")) {
145
				String id = solution.getLiteral("id").getString();
146
				if (!id.equals(metadataPid.getValue())) {
147
					// skip any solution that does not annotate the given pid
148
					continue;
149
				}
150
				
151
			}
152
			// loop through the solution variables, add an index value for each
153
			Iterator<String> varNameIter = solution.varNames();
154
			while (varNameIter.hasNext()) {
155
				String key = varNameIter.next();
156
				if (key.equals("id")) {
157
					// don't include the id
158
					continue;
159
				}
160
				String value = solution.get(key).toString();
161
				List<Object> values = fields.get(key);
162
				if (values  == null) {
163
					values = new ArrayList<Object>();
164
				}
165
				values.add(value);
166
				fields.put(key, values);
167
			}
168
		}
169

    
170
		// clean up the triple store
171
		TDBFactory.release(dataset);
172
        
173
		// add to index
174
		MetacatSolrIndex.getInstance().submit(metadataPid, null, fields, true);
175
		
176
		
177
	}
178

    
179
    /**
180
     * Generate annotation for given metadata identifier
181
     * @param metadataPid
182
     */
183
    public String generateAnnotation(Identifier metadataPid) throws Exception {
184
    	
185
    	DataPackage dataPackage = this.getDataPackage(metadataPid);
186
    	
187
		OntModel m = ModelFactory.createOntologyModel();
188
		Ontology ont = m.createOntology("http://annotation/" + metadataPid.getValue());
189
		
190
		// TODO: import the ontologies we use
191
		ont.addImport(m.createResource(oboe));
192
		m.addSubModel(ModelFactory.createOntologyModel().read(oboe));
193
		
194
		ont.addImport(m.createResource(oboe_sbc));
195
		m.addSubModel(ModelFactory.createOntologyModel().read(oboe_sbc));
196
		
197
		ont.addImport(m.createResource(oa));
198
		m.addSubModel(ModelFactory.createOntologyModel().read(oa_source));
199

    
200
		ont.addImport(m.createResource(dcterms));
201
		m.addSubModel(ModelFactory.createOntologyModel().read(dcterms_source));
202

    
203
		ont.addImport(m.createResource(foaf));
204
		m.addSubModel(ModelFactory.createOntologyModel().read(foaf_source));
205
		
206
		ont.addImport(m.createResource(prov));
207
		//m.addSubModel(ModelFactory.createOntologyModel().read(prov_source));
208

    
209
		ont.addImport(m.createResource(cito));
210
		
211
		// properties
212
		ObjectProperty hasBodyProperty = m.getObjectProperty(oa + "hasBody");
213
		ObjectProperty hasTargetProperty = m.getObjectProperty(oa + "hasTarget");
214
		ObjectProperty hasSourceProperty = m.getObjectProperty(oa + "hasSource");
215
		ObjectProperty hasSelectorProperty = m.getObjectProperty(oa + "hasSelector");
216
		ObjectProperty annotatedByProperty = m.getObjectProperty(oa + "annotatedBy");
217
		Property identifierProperty = m.getProperty(dcterms + "identifier");
218
		Property conformsToProperty = m.getProperty(dcterms + "conformsTo");
219
		Property wasAttributedTo = m.getProperty(prov + "wasAttributedTo");
220
		Property nameProperty = m.getProperty(foaf + "name");
221
		Property rdfValue = m.getProperty(rdf + "value");
222
		
223
		ObjectProperty ofCharacteristic = m.getObjectProperty(oboe_core + "ofCharacteristic");
224
		ObjectProperty usesStandard = m.getObjectProperty(oboe_core + "usesStandard");
225
		ObjectProperty ofEntity = m.getObjectProperty(oboe_core + "ofEntity");
226
		ObjectProperty hasMeasurement = m.getObjectProperty(oboe_core + "hasMeasurement");
227

    
228
		// classes
229
		OntClass entityClass =  m.getOntClass(oboe_core + "Entity");
230
		OntClass observationClass =  m.getOntClass(oboe_core + "Observation");
231
		OntClass measurementClass =  m.getOntClass(oboe_core + "Measurement");
232
		OntClass characteristicClass = m.getOntClass(oboe_core + "Characteristic");
233
		OntClass standardClass =  m.getOntClass(oboe_core + "Standard");
234
		
235
		Resource annotationClass =  m.getOntClass(oa + "Annotation");
236
		Resource specificResourceClass =  m.getOntClass(oa + "SpecificResource");
237
		Resource fragmentSelectorClass =  m.getOntClass(oa + "FragmentSelector");
238
		Resource provEntityClass =  m.getResource(prov + "Entity");
239
		Resource personClass =  m.getResource(prov + "Person");
240
				
241
		// these apply to every attribute annotation
242
		Individual meta1 = m.createIndividual(ont.getURI() + "#meta", provEntityClass);
243
		meta1.addProperty(identifierProperty, metadataPid.getValue());
244

    
245
		// decide who should be credited with the package
246
		Individual p1 = null;
247
		
248
		// look up creators from the EML metadata
249
		List<String> creators = dataPackage.getCreators();
250
		//creators = Arrays.asList("Matthew Jones");
251
		if (creators != null && creators.size() > 0) {	
252
			// use an orcid if we can find one from their system
253
			String orcidUri = OrcidService.lookupOrcid(null, null, creators.toArray(new String[0]));
254
			if (orcidUri != null) {
255
				p1 = m.createIndividual(orcidUri, personClass);
256
				p1.addProperty(identifierProperty, orcidUri);
257
			} else {
258
				p1 = m.createIndividual(ont.getURI() + "#person", personClass);
259
			}
260
			// include the name we have in the metadata
261
			p1.addProperty(nameProperty, creators.get(0));
262
		}
263
		
264
		// attribute the package to this creator if we have one
265
		if (p1 != null) {
266
			meta1.addProperty(wasAttributedTo, p1);
267
		}
268
		
269
		// loop through the tables and attributes
270
		int entityCount = 1;
271
		Entity[] entities = dataPackage.getEntityList();
272
		if (entities != null) {
273
			for (Entity entity: entities) {
274
				String entityName = entity.getName();
275
				
276
				Individual o1 = m.createIndividual(ont.getURI() + "#observation" + entityCount, observationClass);
277
				Resource entityConcept = lookupEntity(entityClass, entity);
278
				if (entityConcept != null) {
279
					AllValuesFromRestriction avfr = m.createAllValuesFromRestriction(null, ofEntity, entityConcept);
280
					o1.addOntClass(avfr);
281
				}
282
				
283
				logMetacat.debug("Entity name: " + entityName);
284
				Attribute[] attributes = entity.getAttributeList().getAttributes();
285
				int attributeCount = 1;
286
				if (attributes != null) {
287
					for (Attribute attribute: attributes) {
288
						
289
						// for naming the individuals uniquely
290
						String cnt = entityCount + "_" + attributeCount;
291
						
292
						String attributeName = attribute.getName();
293
						String attributeLabel = attribute.getLabel();
294
						String attributeDefinition = attribute.getDefinition();
295
						String attributeType = attribute.getAttributeType();
296
						String attributeScale = attribute.getMeasurementScale();
297
						String attributeUnitType = attribute.getUnitType();
298
						String attributeUnit = attribute.getUnit();
299
						String attributeDomain = attribute.getDomain().getClass().getSimpleName();
300
		
301
						logMetacat.debug("Attribute name: " + attributeName);
302
						logMetacat.debug("Attribute label: " + attributeLabel);
303
						logMetacat.debug("Attribute definition: " + attributeDefinition);
304
						logMetacat.debug("Attribute type: " + attributeType);
305
						logMetacat.debug("Attribute scale: " + attributeScale);
306
						logMetacat.debug("Attribute unit type: " + attributeUnitType);
307
						logMetacat.debug("Attribute unit: " + attributeUnit);
308
						logMetacat.debug("Attribute domain: " + attributeDomain);
309
					
310
						// look up the characteristic or standard subclasses
311
						Resource standard = this.lookupStandard(standardClass, attribute);
312
						Resource characteristic = this.lookupCharacteristic(characteristicClass, attribute);
313
						
314
						if (standard != null || characteristic != null) {
315
							
316
							// instances
317
							Individual m1 = m.createIndividual(ont.getURI() + "#measurement" + cnt, measurementClass);
318
							Individual a1 = m.createIndividual(ont.getURI() + "#annotation" + cnt, annotationClass);
319
							Individual t1 = m.createIndividual(ont.getURI() + "#target" + cnt, specificResourceClass);
320
							String xpointer = "xpointer(/eml/dataSet/" + entityCount + "/attributeList/" + attributeCount + ")";
321
							Individual s1 = m.createIndividual(ont.getURI() + "#" + xpointer, fragmentSelectorClass);
322
							s1.addLiteral(rdfValue, xpointer);
323
							s1.addProperty(conformsToProperty, "http://www.w3.org/TR/xptr/");
324
							
325
							// statements about the annotation
326
							a1.addProperty(hasBodyProperty, m1);
327
							a1.addProperty(hasTargetProperty, t1);
328
							t1.addProperty(hasSourceProperty, meta1);
329
							t1.addProperty(hasSelectorProperty, s1);
330
							//a1.addProperty(annotatedByProperty, p1);
331
							
332
							// describe the measurement in terms of restrictions
333
							if (standard != null) {
334
								AllValuesFromRestriction avfr = m.createAllValuesFromRestriction(null, usesStandard, standard);
335
								m1.addOntClass(avfr);
336
							}
337
							if (characteristic != null) {
338
								AllValuesFromRestriction avfr = m.createAllValuesFromRestriction(null, ofCharacteristic, characteristic);
339
								m1.addOntClass(avfr);
340
							}
341
							
342
							// attach to the observation
343
							// TODO: evaluate whether the measurement can apply to the given observed entity
344
							o1.addProperty(hasMeasurement, m1);
345
						}
346
						attributeCount++;
347
						
348
					}
349
				}
350
				entityCount++;
351
			}
352
		}
353
		
354
		StringWriter sw = new StringWriter();
355
		// only write the base model
356
		//m.write(sw, "RDF/XML-ABBREV");
357
		m.write(sw, null);
358

    
359
		return sw.toString();
360
		
361
	}
362
	
363
	private Resource lookupStandard(OntClass standardClass, Attribute attribute) {
364
		// what's our unit?
365
		String unit = attribute.getUnit().toLowerCase();
366
		List<String> tokens = Arrays.asList(unit.split(" "));
367

    
368
		boolean found = false;
369
		ExtendedIterator iter = standardClass.listSubClasses(false);
370
		if (randomize) {
371
			List subclasses = iter.toList();
372
			int size = subclasses.size();
373
			Long index = new Long(Math.round(Math.floor((Math.random() * (size-1)))));
374
			OntClass subclass = (OntClass) subclasses.get( index.intValue() );
375
			return subclass;
376
		}
377
		while (iter.hasNext()) {
378
			OntClass subclass = (OntClass) iter.next();
379
			String subclassName = subclass.getLocalName().toLowerCase();
380
			logMetacat.debug("subclass: " + subclassName);
381
			if (tokens.contains(subclassName)) {
382
				found = true;
383
			}
384
			if (subclass.hasLabel(unit, null)) {
385
				found = true;
386
			}
387
			if (found) {
388
				return subclass;
389
			}
390
		}
391
		// try to look it up if we got this far
392
		return BioPortalService.lookupAnnotationClass(standardClass, unit, OBOE_SBC);
393
	}
394
	
395
	private Resource lookupCharacteristic(OntClass characteristicClass, Attribute attribute) {
396
		// what's our label?
397
		String label = attribute.getLabel().toLowerCase();
398
		List<String> tokens = Arrays.asList(label.split(" "));
399
		
400
		boolean found = false;
401
		// find something that matches
402
		ExtendedIterator iter = characteristicClass.listSubClasses();
403
		if (randomize) {
404
			List subclasses = iter.toList();
405
			int size = subclasses.size();
406
			Long index = new Long(Math.round(Math.floor((Math.random() * (size-1)))));
407
			OntClass subclass = (OntClass) subclasses.get( index.intValue() );
408
			return subclass;
409
		}
410
		while (iter.hasNext()) {
411
			OntClass subclass = (OntClass) iter.next();
412
			String subclassName = subclass.getLocalName().toLowerCase();
413
			logMetacat.debug("subclass: " + subclassName);
414
			if (tokens.contains(subclassName)) {
415
				found = true;
416
			}
417
			if (subclass.hasLabel(label, null)) {
418
				found = true;
419
			}
420
			if (found) {
421
				return subclass;
422
			}
423
		}
424
		
425
		// try to look it up if we got this far
426
		return BioPortalService.lookupAnnotationClass(characteristicClass, attribute.getDefinition(), OBOE_SBC);
427
		
428
	}
429
	
430
	private Resource lookupEntity(OntClass entityClass, Entity entity) {
431
		// what's our description like?
432
		String name = entity.getName();
433
		String definition = entity.getDefinition();
434
		
435
		// try to look it up if we got this far
436
		return BioPortalService.lookupAnnotationClass(entityClass, definition, OBOE_SBC);
437
		
438
	}
439
	
440
	private DataPackage getDataPackage(Identifier pid) throws Exception {
441
		// for using the MN API as the MN itself
442
		MockHttpServletRequest request = new MockHttpServletRequest(null, null, null);
443
		Session session = new Session();
444
        Subject subject = MNodeService.getInstance(request).getCapabilities().getSubject(0);
445
        session.setSubject(subject);
446
		InputStream emlStream = MNodeService.getInstance(request).get(session, pid);
447

    
448
		// parse the metadata
449
		DataPackageParserInterface parser = new Eml200DataPackageParser();
450
		parser.parse(emlStream);
451
		DataPackage dataPackage = parser.getDataPackage();
452
		return dataPackage;
453
	}
454

    
455
	private void summarize(List<Identifier> identifiers) throws SQLException {
456
		
457
		DBConnection dbconn = null;
458

    
459
		try {
460
			dbconn = DBConnectionPool.getDBConnection("DatapackageSummarizer.summarize");
461
			
462
			PreparedStatement dropStatement = dbconn.prepareStatement("DROP TABLE IF EXISTS entity_summary");
463
			dropStatement.execute();
464
	
465
			PreparedStatement createStatement = dbconn.prepareStatement(
466
					"CREATE TABLE entity_summary (" +
467
					"guid text, " +
468
					"title text, " +
469
					"entity text," +
470
					"attributeName text," +
471
					"attributeLabel text," +
472
					"attributeDefinition text," +
473
					"attributeType text," +
474
					"attributeScale text," +
475
					"attributeUnitType text," +
476
					"attributeUnit text," +
477
					"attributeDomain text" +
478
					")");
479
			createStatement.execute();
480
			
481
			PreparedStatement insertStatement = dbconn.prepareStatement(
482
					"INSERT INTO entity_summary " +
483
					"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
484
			
485
			for (Identifier pid: identifiers) {
486
			
487
				logMetacat.debug("Parsing pid: " + pid.getValue());
488
				
489
				try {
490
					
491
					// get the package
492
					DataPackage dataPackage = this.getDataPackage(pid);
493
					String title = dataPackage.getTitle();
494
					logMetacat.debug("Title: " + title);
495
					
496
					Entity[] entities = dataPackage.getEntityList();
497
					if (entities != null) {
498
						for (Entity entity: entities) {
499
							String entityName = entity.getName();
500
							logMetacat.debug("Entity name: " + entityName);
501
							Attribute[] attributes = entity.getAttributeList().getAttributes();
502
							for (Attribute attribute: attributes) {
503
								String attributeName = attribute.getName();
504
								String attributeLabel = attribute.getLabel();
505
								String attributeDefinition = attribute.getDefinition();
506
								String attributeType = attribute.getAttributeType();
507
								String attributeScale = attribute.getMeasurementScale();
508
								String attributeUnitType = attribute.getUnitType();
509
								String attributeUnit = attribute.getUnit();
510
								String attributeDomain = attribute.getDomain().getClass().getSimpleName();
511
	
512
								logMetacat.debug("Attribute name: " + attributeName);
513
								logMetacat.debug("Attribute label: " + attributeLabel);
514
								logMetacat.debug("Attribute definition: " + attributeDefinition);
515
								logMetacat.debug("Attribute type: " + attributeType);
516
								logMetacat.debug("Attribute scale: " + attributeScale);
517
								logMetacat.debug("Attribute unit type: " + attributeUnitType);
518
								logMetacat.debug("Attribute unit: " + attributeUnit);
519
								logMetacat.debug("Attribute domain: " + attributeDomain);
520
								
521
								// set the values for this attribute
522
								insertStatement.setString(1, pid.getValue());
523
								insertStatement.setString(2, title);
524
								insertStatement.setString(3, entityName);
525
								insertStatement.setString(4, attributeName);
526
								insertStatement.setString(5, attributeLabel);
527
								insertStatement.setString(6, attributeDefinition);
528
								insertStatement.setString(7, attributeType);
529
								insertStatement.setString(8, attributeScale);
530
								insertStatement.setString(9, attributeUnitType);
531
								insertStatement.setString(10, attributeUnit);
532
								insertStatement.setString(11, attributeDomain);
533
								insertStatement.execute();
534
								
535
							}		
536
						}
537
					}
538
					
539
				} catch (Exception e) {
540
					logMetacat.warn("error parsing metadata for: " + pid.getValue(), e);
541
				}
542
			}
543
		} catch (SQLException sqle) {
544
			// just throw it
545
			throw sqle;
546
		} finally {
547
			if (dbconn != null) {
548
				DBConnectionPool.returnDBConnection(dbconn, 0);
549
				dbconn.close();
550
			}
551
		}
552
	}
553
	
554
	public static void main(String[] args) throws Exception {
555
		// set up the properties based on the test/deployed configuration of the workspace
556
			SortedProperties testProperties = new SortedProperties("test/test.properties");
557
			testProperties.load();
558
			String metacatContextDir = testProperties.getProperty("metacat.contextDir");
559
			PropertyService.getInstance(metacatContextDir + "/WEB-INF");
560
			
561
			testGenerate();
562
//			testSummary();
563
			System.exit(0);
564
	}
565
	
566
	public static void testGenerate() throws Exception {
567
		Identifier metadataPid = new Identifier();
568
		metadataPid.setValue("tao.1.4");
569
		DatapackageSummarizer ds = new DatapackageSummarizer();
570
		String rdfString = ds.generateAnnotation(metadataPid);
571
		logMetacat.info("RDF annotation: \n" + rdfString);
572
		
573
	}
574
	
575
	public static void testSummary() throws Exception {
576
		
577
		// summarize the packages
578
		DatapackageSummarizer ds = new DatapackageSummarizer();
579
		List<Identifier> identifiers = new ArrayList<Identifier>();
580
		Map<Integer, String> serverCodes = ReplicationService.getServerCodes();
581

    
582
		// select the metadata ids we want to summarize
583
		boolean includeReplicas = true;
584
		Iterator<Integer> codeIter = Arrays.asList(new Integer[] {1}).iterator();
585
		if (includeReplicas ) {
586
			codeIter = serverCodes.keySet().iterator();
587
		}
588
		
589
		Vector<String> idList = new Vector<String>();
590
		while (codeIter.hasNext()) {
591
			int serverLocation = codeIter.next();
592
			Vector<String> idList0 = DBUtil.getAllDocidsByType(DocumentImpl.EML2_0_0NAMESPACE, false, serverLocation);
593
			Vector<String> idList1 = DBUtil.getAllDocidsByType(DocumentImpl.EML2_0_1NAMESPACE, false, serverLocation);
594
			Vector<String> idList2 = DBUtil.getAllDocidsByType(DocumentImpl.EML2_1_0NAMESPACE, false, serverLocation);
595
			Vector<String> idList3 = DBUtil.getAllDocidsByType(DocumentImpl.EML2_1_1NAMESPACE, false, serverLocation);
596
			
597
			idList.addAll(idList0);
598
			idList.addAll(idList1);
599
			idList.addAll(idList2);
600
			idList.addAll(idList3);
601
		
602
		}
603
		
604
		// go through all the identifiers now
605
		for (String localId : idList) {
606
			try {
607
				String guid = IdentifierManager.getInstance().getGUID(
608
						DocumentUtil.getDocIdFromAccessionNumber(localId), 
609
						DocumentUtil.getRevisionFromAccessionNumber(localId));
610
				Identifier pid = new Identifier();
611
				pid.setValue(guid);
612
				identifiers.add(pid);
613
			} catch (McdbDocNotFoundException nfe) {
614
				// just skip it
615
				continue;
616
			}
617
		}
618
		ds.summarize(identifiers);
619
		System.exit(0);
620
	}
621
	
622
}
(2-2/3)