Project

General

Profile

« Previous | Next » 

Revision 7512

refactor DOI registration into separate class. http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5513

View differences:

MNodeService.java
28 28
import java.io.InputStream;
29 29
import java.math.BigInteger;
30 30
import java.security.NoSuchAlgorithmException;
31
import java.text.SimpleDateFormat;
32 31
import java.util.ArrayList;
33 32
import java.util.Calendar;
34 33
import java.util.Date;
35
import java.util.HashMap;
36 34
import java.util.List;
37 35
import java.util.Set;
38 36
import java.util.Timer;
......
99 97
import org.dataone.service.util.Constants;
100 98

  
101 99
import edu.ucsb.nceas.ezid.EZIDException;
102
import edu.ucsb.nceas.ezid.EZIDService;
103
import edu.ucsb.nceas.ezid.profile.DataCiteProfile;
104
import edu.ucsb.nceas.ezid.profile.DataCiteProfileResourceTypeValues;
105
import edu.ucsb.nceas.ezid.profile.ErcMissingValueCode;
106
import edu.ucsb.nceas.ezid.profile.InternalProfile;
107
import edu.ucsb.nceas.ezid.profile.InternalProfileValues;
108 100
import edu.ucsb.nceas.metacat.DBQuery;
109 101
import edu.ucsb.nceas.metacat.EventLog;
110 102
import edu.ucsb.nceas.metacat.IdentifierManager;
......
361 353
            
362 354
            // attempt to register the identifier - it checks if it is a doi
363 355
            try {
364
    			registerDOI(sysmeta);
356
    			DOIService.getInstance().registerDOI(sysmeta);
365 357
    		} catch (EZIDException e) {
366 358
                throw new ServiceFailure("1190", "Could not register DOI: " + e.getMessage());
367 359
    		}
......
414 406
        
415 407
        // attempt to register the identifier - it checks if it is a doi
416 408
        try {
417
			registerDOI(sysmeta);
409
			DOIService.getInstance().registerDOI(sysmeta);
418 410
		} catch (EZIDException e) {
419 411
			ServiceFailure sf = new ServiceFailure("1190", "Could not register DOI: " + e.getMessage());
420 412
			sf.initCause(e);
......
1253 1245
			UUID uuid = UUID.randomUUID();
1254 1246
            identifier.setValue(UUID_PREFIX + uuid.toString());
1255 1247
		} else if (scheme.equalsIgnoreCase(DOI_SCHEME)) {
1256
			// for DOIs
1257
			// prepend the doi shoulder to the generated identifier
1258
			String shoulder = null;
1259
			String ezidUsername = null;
1260
			String ezidPassword = null;
1261
			boolean doiEnabled = false;
1248
			// generate a DOI
1262 1249
			try {
1263
	            doiEnabled = new Boolean(PropertyService.getProperty("guid.ezid.enabled")).booleanValue();
1264
				shoulder = PropertyService.getProperty("guid.ezid.doishoulder.1");
1265
				ezidUsername = PropertyService.getProperty("guid.ezid.username");
1266
				ezidPassword = PropertyService.getProperty("guid.ezid.password");
1267
			} catch (PropertyNotFoundException e1) {
1268
				throw new InvalidRequest("2193", "DOI shoulder is not configured at this node.");
1269
			}
1270
			
1271
			// only continue if we have the feature turned on
1272
			if (!doiEnabled) {
1273
				throw new InvalidRequest("2193", "DOI scheme is not enabled at this node.");
1274
			}
1275
			
1276
			// add only the minimal metadata required for this DOI
1277
			HashMap<String, String> metadata = new HashMap<String, String>();
1278
			metadata.put(DataCiteProfile.TITLE.toString(), ErcMissingValueCode.UNKNOWN.toString());
1279
			metadata.put(DataCiteProfile.CREATOR.toString(), ErcMissingValueCode.UNKNOWN.toString());
1280
			metadata.put(DataCiteProfile.PUBLISHER.toString(), ErcMissingValueCode.UNKNOWN.toString());
1281
			metadata.put(DataCiteProfile.PUBLICATION_YEAR.toString(), ErcMissingValueCode.UNKNOWN.toString());
1282
			metadata.put(InternalProfile.STATUS.toString(), InternalProfileValues.RESERVED.toString());
1283
			metadata.put(InternalProfile.EXPORT.toString(), InternalProfileValues.NO.toString());
1284

  
1285
			try {
1286
				// call the EZID service
1287
				String ezidServiceBaseUrl = null;
1288
				try {
1289
					ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
1290
				} catch (PropertyNotFoundException e) {
1291
					logMetacat.warn("Using default EZID baseUrl");
1292
				}
1293
				EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
1294
				ezid.login(ezidUsername, ezidPassword);
1295
				String doi = null;
1296
				// TODO: perhaps we want to use the identifier as given
1297
				boolean mintDoi = true;
1298
				if (!mintDoi) {
1299
					doi = shoulder + identifier.getValue();
1300
					identifier.setValue(doi);
1301
					doi = ezid.createIdentifier(identifier.getValue(), metadata);
1302
				} else {
1303
					doi = ezid.mintIdentifier(shoulder, metadata);
1304
				}
1305
				identifier.setValue(doi);
1306
				ezid.logout();
1250
				identifier = DOIService.getInstance().generateDOI();
1307 1251
			} catch (EZIDException e) {
1308
				throw new ServiceFailure("2191", "Error registering DOI identifier: " + e.getMessage());
1252
				ServiceFailure sf = new ServiceFailure("2191", "Could not generate DOI: " + e.getMessage());
1253
				sf.initCause(e);
1254
				throw sf;
1309 1255
			}
1310 1256
		} else {
1311 1257
			// default if we don't know the scheme
......
1519 1465
		}
1520 1466
		return null;
1521 1467
	}
1522
	
1523
	/**
1524
	 * submits DOI metadata information about the object to EZID
1525
	 * @param sysMeta
1526
	 * @return
1527
	 * @throws EZIDException 
1528
	 * @throws ServiceFailure 
1529
	 * @throws NotImplemented 
1530
	 */
1531
	private boolean registerDOI(SystemMetadata sysMeta) throws EZIDException, NotImplemented, ServiceFailure {
1532
		
1533
		// for DOIs
1534
		String ezidUsername = null;
1535
		String ezidPassword = null;
1536
		String shoulder = null;
1537
		boolean doiEnabled = false;
1538
		try {
1539
            doiEnabled = new Boolean(PropertyService.getProperty("guid.ezid.enabled")).booleanValue();
1540
			shoulder = PropertyService.getProperty("guid.ezid.doishoulder.1");
1541
			ezidUsername = PropertyService.getProperty("guid.ezid.username");
1542
			ezidPassword = PropertyService.getProperty("guid.ezid.password");
1543
		} catch (PropertyNotFoundException e) {
1544
			logMetacat.warn("DOI support is not configured at this node.", e);
1545
			return false;
1546
		}
1547
		
1548
		// only continue if we have the feature turned on
1549
		if (doiEnabled) {
1550
			
1551
			String identifier = sysMeta.getIdentifier().getValue();
1552
			
1553
			// only continue if this DOI is in our configured shoulder
1554
			if (identifier.startsWith(shoulder)) {
1555
				
1556
				// enter metadata about this identifier
1557
				HashMap<String, String> metadata = null;
1558
				
1559
				// login to EZID service
1560
				String ezidServiceBaseUrl = null;
1561
				try {
1562
					ezidServiceBaseUrl = PropertyService.getProperty("guid.ezid.baseurl");
1563
				} catch (PropertyNotFoundException e) {
1564
					logMetacat.warn("Using default EZID baseUrl");
1565
				}
1566
				EZIDService ezid = new EZIDService(ezidServiceBaseUrl);
1567
				ezid.login(ezidUsername, ezidPassword);
1568
				
1569
				// check for existing metadata
1570
				boolean create = false;
1571
				try {
1572
					metadata = ezid.getMetadata(identifier);
1573
				} catch (EZIDException e) {
1574
					// expected much of the time
1575
					logMetacat.warn("No metadata found for given identifier: " + e.getMessage());
1576
				}
1577
				if (metadata == null) {
1578
					create = true;
1579
				}
1580
				// confuses the API if we send the original metadata that it gave us, so start from scratch
1581
				metadata = new HashMap<String, String>();
1582
				
1583
				// TODO: set title and creator to better values
1584
				Node node = getCapabilities();
1585
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
1586
				String year = sdf .format(sysMeta.getDateUploaded());
1587
				metadata.put(DataCiteProfile.TITLE.toString(), ErcMissingValueCode.UNKNOWN.toString());
1588
				metadata.put(DataCiteProfile.CREATOR.toString(), sysMeta.getRightsHolder().getValue());
1589
				metadata.put(DataCiteProfile.PUBLISHER.toString(), node.getName());
1590
				metadata.put(DataCiteProfile.PUBLICATION_YEAR.toString(), year);
1591
				metadata.put(DataCiteProfile.RESOURCE_TYPE.toString(), DataCiteProfileResourceTypeValues.DATASET.toString() + "/" + sysMeta.getFormatId().getValue());
1592
				metadata.put(InternalProfile.TARGET.toString(), node.getBaseURL() + "/v1/object/" + identifier);
1593
				metadata.put(InternalProfile.STATUS.toString(), InternalProfileValues.PUBLIC.toString());
1594
				metadata.put(InternalProfile.EXPORT.toString(), InternalProfileValues.YES.toString());
1595
	
1596
				// set using the API
1597
				if (create) {
1598
					ezid.createIdentifier(identifier, metadata);
1599
				} else {
1600
					ezid.setMetadata(identifier, metadata);
1601
				}
1602
				
1603
				ezid.logout();
1604
			}
1605
			
1606
		}
1607
		
1608
		return true;
1609
	}
1610 1468
    
1611 1469
}

Also available in: Unified diff