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.DatasetFactory;
35
import com.hp.hpl.jena.query.Query;
36
import com.hp.hpl.jena.query.QueryExecution;
37
import com.hp.hpl.jena.query.QueryExecutionFactory;
38
import com.hp.hpl.jena.query.QueryFactory;
39
import com.hp.hpl.jena.query.QuerySolution;
40
import com.hp.hpl.jena.query.ResultSet;
41
import com.hp.hpl.jena.rdf.model.ModelFactory;
42
import com.hp.hpl.jena.rdf.model.Property;
43
import com.hp.hpl.jena.rdf.model.Resource;
44
import com.hp.hpl.jena.tdb.TDBFactory;
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
    public void indexEphemeralAnnotation(Identifier metadataPid) throws Exception {
83

    
84
    	// generate an annotation for the metadata given
85
		String rdfContent = this.generateAnnotation(metadataPid);
86
		
87
		// load to triple store
88
		//Dataset dataset = TDBFactory.createDataset("./tbd");
89
		Dataset dataset = DatasetFactory.createMem();
90
		
91
    	// read the annotation into the triplestore
92
		InputStream source = IOUtils.toInputStream(rdfContent, "UTF-8");
93
    	String name = "http://annotation/" + metadataPid.getValue();
94
    	boolean loaded = dataset.containsNamedModel(name);
95
    	if (loaded) {
96
    		dataset.removeNamedModel(name);
97
    		loaded = false;
98
    	}
99
		OntModel ontModel = null;
100
		if (!loaded) {
101
			ontModel = ModelFactory.createOntologyModel();
102
			ontModel.read(source, name);
103
			dataset.addNamedModel(name, ontModel);
104
		}
105
		
106
		// query for fields to add to index
107
        Map<String, List<Object>> fields = new HashMap<String, List<Object>>();
108
		
109
        // TODO: look up the query to use (support multiple like in the indexing project)
110
        List<String> queries = new ArrayList<String>();        
111
        queries.add("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
112
        	+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
113
        	+ "PREFIX owl: <http://www.w3.org/2002/07/owl#> " 
114
			+ "PREFIX oboe-core: <http://ecoinformatics.org/oboe/oboe.1.0/oboe-core.owl#> "
115
			+ "PREFIX oa: <http://www.w3.org/ns/oa#> "
116
			+ "PREFIX dcterms: <http://purl.org/dc/terms/> "
117
			+ "SELECT ?standard_sm ?pid "
118
			+ "FROM <$GRAPH_NAME> "
119
			+ "WHERE { "
120
			+ "		?measurement rdf:type oboe-core:Measurement . "
121
			+ "		?measurement rdf:type ?restriction . "
122
			+ "		?restriction owl:onProperty oboe-core:usesStandard . "
123
			+ "		?restriction owl:allValuesFrom ?standard . "
124
			+ "		?standard rdfs:subClassOf+ ?standard_sm . "
125
			+ "		?standard_sm rdfs:subClassOf oboe-core:Standard . "				
126
			+ "		?annotation oa:hasBody ?measurement . "												
127
			+ "		?annotation oa:hasTarget ?target . "
128
			+ "		?target oa:hasSource ?metadata . "
129
			+ "		?metadata dcterms:identifier ?pid . " 
130
			+ "}");
131
        
132
        queries.add("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
133
    		+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
134
    		+ "PREFIX owl: <http://www.w3.org/2002/07/owl#> "
135
    		+ "PREFIX oboe-core: <http://ecoinformatics.org/oboe/oboe.1.0/oboe-core.owl#> "
136
    		+ "PREFIX oa: <http://www.w3.org/ns/oa#> "
137
    		+ "PREFIX dcterms: <http://purl.org/dc/terms/> "
138
    		+ "SELECT ?characteristic_sm ?pid "
139
    		+ "FROM <$GRAPH_NAME>"
140
    		+ "WHERE { "
141
    		+ "		?measurement rdf:type oboe-core:Measurement . "
142
    		+ "		?measurement rdf:type ?restriction . "
143
			+ "		?restriction owl:onProperty oboe-core:ofCharacteristic . "
144
			+ "		?restriction owl:allValuesFrom ?characteristic . "
145
			+ "		?characteristic rdfs:subClassOf+ ?characteristic_sm . "
146
			+ "		?characteristic_sm rdfs:subClassOf oboe-core:Characteristic . "
147
			+ "		?annotation oa:hasBody ?measurement .	"											
148
			+ "		?annotation oa:hasTarget ?target . "
149
			+ "		?target oa:hasSource ?metadata . "
150
			+ "		?metadata dcterms:identifier ?pid . " 
151
			+ "}");
152
        
153
        for (String q: queries) {
154
	        q = q.replaceAll("\\$GRAPH_NAME", name);
155
			Query query = QueryFactory.create(q);
156
			QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
157
			ResultSet results = qexec.execSelect();
158
			
159
			while (results.hasNext()) {
160
				QuerySolution solution = results.next();
161
				System.out.println(solution.toString());
162
				
163
				// find the index document we are trying to augment with the annotation
164
				if (solution.contains("pid")) {
165
					String id = solution.getLiteral("pid").getString();
166
					if (!id.equals(metadataPid.getValue())) {
167
						// skip any solution that does not annotate the given pid
168
						continue;
169
					}
170
					
171
				}
172
				// loop through the solution variables, add an index value for each
173
				Iterator<String> varNameIter = solution.varNames();
174
				while (varNameIter.hasNext()) {
175
					String key = varNameIter.next();
176
					if (key.equals("pid")) {
177
						// don't include the id
178
						continue;
179
					}
180
					String value = solution.get(key).toString();
181
					List<Object> values = fields.get(key);
182
					if (values  == null) {
183
						values = new ArrayList<Object>();
184
					}
185
					values.add(value);
186
					fields.put(key, values);
187
				}
188
			}
189
        }
190

    
191
        // remove the graph to save storage
192
//        ontModel.removeAll();
193
//        ontModel.commit();
194
//        ontModel.close();
195
		dataset.removeNamedModel(name);
196
        
197
		// clean up the triple store
198
		TDBFactory.release(dataset);
199
        
200
		// add to index
201
		MetacatSolrIndex.getInstance().submit(metadataPid, null, fields, true);
202
		
203
		
204
	}
205

    
206
    /**
207
     * Generate annotation for given metadata identifier
208
     * @param metadataPid
209
     */
210
    public String generateAnnotation(Identifier metadataPid) throws Exception {
211
    	
212
    	DataPackage dataPackage = this.getDataPackage(metadataPid);
213
    	
214
		OntModel m = ModelFactory.createOntologyModel();
215
		Ontology ont = m.createOntology("http://annotation/" + metadataPid.getValue());
216
		
217
		// TODO: import the ontologies we use
218
		ont.addImport(m.createResource(oboe));
219
		m.addSubModel(ModelFactory.createOntologyModel().read(oboe));
220
		
221
		ont.addImport(m.createResource(oboe_sbc));
222
		m.addSubModel(ModelFactory.createOntologyModel().read(oboe_sbc));
223
		
224
		ont.addImport(m.createResource(oa));
225
		m.addSubModel(ModelFactory.createOntologyModel().read(oa_source));
226

    
227
		ont.addImport(m.createResource(dcterms));
228
		m.addSubModel(ModelFactory.createOntologyModel().read(dcterms_source));
229

    
230
		ont.addImport(m.createResource(foaf));
231
		m.addSubModel(ModelFactory.createOntologyModel().read(foaf_source));
232
		
233
		ont.addImport(m.createResource(prov));
234
		//m.addSubModel(ModelFactory.createOntologyModel().read(prov_source));
235

    
236
		ont.addImport(m.createResource(cito));
237
		
238
		// properties
239
		ObjectProperty hasBodyProperty = m.getObjectProperty(oa + "hasBody");
240
		ObjectProperty hasTargetProperty = m.getObjectProperty(oa + "hasTarget");
241
		ObjectProperty hasSourceProperty = m.getObjectProperty(oa + "hasSource");
242
		ObjectProperty hasSelectorProperty = m.getObjectProperty(oa + "hasSelector");
243
		ObjectProperty annotatedByProperty = m.getObjectProperty(oa + "annotatedBy");
244
		Property identifierProperty = m.getProperty(dcterms + "identifier");
245
		Property conformsToProperty = m.getProperty(dcterms + "conformsTo");
246
		Property wasAttributedTo = m.getProperty(prov + "wasAttributedTo");
247
		Property nameProperty = m.getProperty(foaf + "name");
248
		Property rdfValue = m.getProperty(rdf + "value");
249
		
250
		ObjectProperty ofCharacteristic = m.getObjectProperty(oboe_core + "ofCharacteristic");
251
		ObjectProperty usesStandard = m.getObjectProperty(oboe_core + "usesStandard");
252
		ObjectProperty ofEntity = m.getObjectProperty(oboe_core + "ofEntity");
253
		ObjectProperty hasMeasurement = m.getObjectProperty(oboe_core + "hasMeasurement");
254

    
255
		// classes
256
		OntClass entityClass =  m.getOntClass(oboe_core + "Entity");
257
		OntClass observationClass =  m.getOntClass(oboe_core + "Observation");
258
		OntClass measurementClass =  m.getOntClass(oboe_core + "Measurement");
259
		OntClass characteristicClass = m.getOntClass(oboe_core + "Characteristic");
260
		OntClass standardClass =  m.getOntClass(oboe_core + "Standard");
261
		
262
		Resource annotationClass =  m.getOntClass(oa + "Annotation");
263
		Resource specificResourceClass =  m.getOntClass(oa + "SpecificResource");
264
		Resource fragmentSelectorClass =  m.getOntClass(oa + "FragmentSelector");
265
		Resource provEntityClass =  m.getResource(prov + "Entity");
266
		Resource personClass =  m.getResource(prov + "Person");
267
				
268
		// these apply to every attribute annotation
269
		Individual meta1 = m.createIndividual(ont.getURI() + "#meta", provEntityClass);
270
		meta1.addProperty(identifierProperty, metadataPid.getValue());
271

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

    
386
		return sw.toString();
387
		
388
	}
389
	
390
	private Resource lookupStandard(OntClass standardClass, Attribute attribute) {
391
		// what's our unit?
392
		String unit = attribute.getUnit().toLowerCase();
393
		
394
		/*
395
		boolean found = false;
396
		List<String> tokens = Arrays.asList(unit.split(" "));
397
		ExtendedIterator iter = standardClass.listSubClasses(false);
398
		while (iter.hasNext()) {
399
			OntClass subclass = (OntClass) iter.next();
400
			String subclassName = subclass.getLocalName().toLowerCase();
401
			logMetacat.debug("subclass: " + subclassName);
402
			if (tokens.contains(subclassName)) {
403
				found = true;
404
			}
405
			if (subclass.hasLabel(unit, null)) {
406
				found = true;
407
			}
408
			if (found) {
409
				return subclass;
410
			}
411
		}
412
		*/
413
		
414
		// try to look it up if we got this far
415
		return BioPortalService.lookupAnnotationClass(standardClass, unit, OBOE_SBC);
416
	}
417
	
418
	private Resource lookupCharacteristic(OntClass characteristicClass, Attribute attribute) {
419
		// what are we looking for?
420
		String label = attribute.getLabel().toLowerCase();
421
		String definition = attribute.getDefinition();
422
		String text = label + " " + definition;
423
		
424
		/*
425
		// find something that matches		
426
		boolean found = false;
427
		List<String> tokens = Arrays.asList(label.split(" "));
428
		ExtendedIterator iter = characteristicClass.listSubClasses();
429
		while (iter.hasNext()) {
430
			OntClass subclass = (OntClass) iter.next();
431
			String subclassName = subclass.getLocalName().toLowerCase();
432
			logMetacat.debug("subclass: " + subclassName);
433
			if (tokens.contains(subclassName)) {
434
				found = true;
435
			}
436
			if (subclass.hasLabel(label, null)) {
437
				found = true;
438
			}
439
			if (found) {
440
				return subclass;
441
			}
442
		}
443
		*/
444
		
445
		// try to look it up from the service
446
		return BioPortalService.lookupAnnotationClass(characteristicClass, text, OBOE_SBC);
447
		
448
	}
449
	
450
	private Resource lookupEntity(OntClass entityClass, Entity entity) {
451
		// what's our description like?
452
		String name = entity.getName();
453
		String definition = entity.getDefinition();
454
		
455
		// try to look it up if we got this far
456
		return BioPortalService.lookupAnnotationClass(entityClass, definition, OBOE_SBC);
457
		
458
	}
459
	
460
	private DataPackage getDataPackage(Identifier pid) throws Exception {
461
		// for using the MN API as the MN itself
462
		MockHttpServletRequest request = new MockHttpServletRequest(null, null, null);
463
		Session session = new Session();
464
        Subject subject = MNodeService.getInstance(request).getCapabilities().getSubject(0);
465
        session.setSubject(subject);
466
		InputStream emlStream = MNodeService.getInstance(request).get(session, pid);
467

    
468
		// parse the metadata
469
		DataPackageParserInterface parser = new Eml200DataPackageParser();
470
		parser.parse(emlStream);
471
		DataPackage dataPackage = parser.getDataPackage();
472
		return dataPackage;
473
	}
474

    
475
	private void summarize(List<Identifier> identifiers) throws SQLException {
476
		
477
		DBConnection dbconn = null;
478

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

    
602
		// select the metadata ids we want to summarize
603
		boolean includeReplicas = true;
604
		Iterator<Integer> codeIter = Arrays.asList(new Integer[] {1}).iterator();
605
		if (includeReplicas ) {
606
			codeIter = serverCodes.keySet().iterator();
607
		}
608
		
609
		Vector<String> idList = new Vector<String>();
610
		while (codeIter.hasNext()) {
611
			int serverLocation = codeIter.next();
612
			Vector<String> idList0 = DBUtil.getAllDocidsByType(DocumentImpl.EML2_0_0NAMESPACE, false, serverLocation);
613
			Vector<String> idList1 = DBUtil.getAllDocidsByType(DocumentImpl.EML2_0_1NAMESPACE, false, serverLocation);
614
			Vector<String> idList2 = DBUtil.getAllDocidsByType(DocumentImpl.EML2_1_0NAMESPACE, false, serverLocation);
615
			Vector<String> idList3 = DBUtil.getAllDocidsByType(DocumentImpl.EML2_1_1NAMESPACE, false, serverLocation);
616
			
617
			idList.addAll(idList0);
618
			idList.addAll(idList1);
619
			idList.addAll(idList2);
620
			idList.addAll(idList3);
621
		
622
		}
623
		
624
		// go through all the identifiers now
625
		for (String localId : idList) {
626
			try {
627
				String guid = IdentifierManager.getInstance().getGUID(
628
						DocumentUtil.getDocIdFromAccessionNumber(localId), 
629
						DocumentUtil.getRevisionFromAccessionNumber(localId));
630
				Identifier pid = new Identifier();
631
				pid.setValue(guid);
632
				identifiers.add(pid);
633
			} catch (McdbDocNotFoundException nfe) {
634
				// just skip it
635
				continue;
636
			}
637
		}
638
		ds.summarize(identifiers);
639
		System.exit(0);
640
	}
641
	
642
}
(2-2/3)