Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2013 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: leinfelder $'
7
 *     '$Date: 2014-05-20 10:40:26 -0700 (Tue, 20 May 2014) $'
8
 * '$Revision: 8789 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24

    
25
package edu.ucsb.nceas.metacat.admin.upgrade;
26

    
27
import java.util.Collections;
28
import java.util.List;
29
import java.util.ArrayList;
30

    
31
import org.apache.commons.logging.Log;
32
import org.apache.commons.logging.LogFactory;
33
import org.dataone.service.types.v1.Identifier;
34
import org.dataone.service.types.v1.SystemMetadata;
35

    
36
import edu.ucsb.nceas.metacat.DBUtil;
37
import edu.ucsb.nceas.metacat.DocumentImpl;
38
import edu.ucsb.nceas.metacat.IdentifierManager;
39
import edu.ucsb.nceas.metacat.admin.AdminException;
40
import edu.ucsb.nceas.metacat.dataone.DOIService;
41
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
42
import edu.ucsb.nceas.metacat.properties.PropertyService;
43
import edu.ucsb.nceas.metacat.util.DocumentUtil;
44
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
45

    
46

    
47

    
48
/**
49
 * Updates existing DOI registrations for EML versions
50
 * @author leinfelder, walker
51
 *
52
 */
53
public class UpdateDOI implements UpgradeUtilityInterface {
54
    
55
	private static Log log = LogFactory.getLog(UpdateDOI.class);
56

    
57
	private int serverLocation = 1;
58

    
59
	public int getServerLocation() {
60
		return serverLocation;
61
	}
62

    
63
	public void setServerLocation(int serverLocation) {
64
		this.serverLocation = serverLocation;
65
	}
66
	
67
	/** 
68
	 * Update the registration of a list of DOIs
69
	 * @param identifiers - DOIs to update
70
	 */
71
	private void updateDOIRegistration(List<String> identifiers) {
72
		
73
		// look up the prefix - NOTE we have used different shoulders over time, so might consider updating anything with "doi:..."
74
		String prefix = "doi:";
75
		try {
76
			prefix = PropertyService.getProperty("guid.ezid.doishoulder." + serverLocation);
77
		} catch (PropertyNotFoundException pnfe) {
78
			log.error("Could not look up the doi shoulder for this server", pnfe);
79
			return;
80
		}
81

    
82
		for (String pid: identifiers) {
83
			try {
84
				// skip if not a DOI
85
				if (!pid.startsWith(prefix)) {
86
					continue;
87
				}
88
				
89
				//Create an identifier and retrieve the SystemMetadata for this guid
90
				Identifier identifier = new Identifier();
91
				identifier.setValue(pid);
92
				SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(identifier);
93
				
94
				//Update the registration
95
				DOIService.getInstance().registerDOI(sysMeta);
96
			} catch (Exception e) {
97
				// what to do? nothing
98
				e.printStackTrace();
99
				continue;
100
			}
101
			
102
		}
103
	}
104
	
105
	/**
106
	 * Update the DOI registration of all ids in this server with EML formatIds
107
	 */
108
    public boolean upgrade() throws AdminException {
109
        boolean success = true;
110
        
111
        try {
112
        	// get only local ids for this server
113
            List<String> idList = null;
114
            
115
            idList = DBUtil.getAllDocidsByType(DocumentImpl.EML2_0_0NAMESPACE, true, serverLocation);
116
            Collections.sort(idList);
117
            updateDOIRegistration(idList);
118
            
119
            idList = DBUtil.getAllDocidsByType(DocumentImpl.EML2_0_1NAMESPACE, true, serverLocation);
120
            Collections.sort(idList);
121
            updateDOIRegistration(idList);
122
            
123
            idList = DBUtil.getAllDocidsByType(DocumentImpl.EML2_1_0NAMESPACE, true, serverLocation);
124
            Collections.sort(idList);
125
            updateDOIRegistration(idList);
126
            
127
            idList = DBUtil.getAllDocidsByType(DocumentImpl.EML2_1_1NAMESPACE, true, serverLocation);
128
            Collections.sort(idList);
129
            updateDOIRegistration(idList);
130
            
131
		} catch (Exception e) {
132
			String msg = "Problem updating DOIs: " + e.getMessage();
133
			log.error(msg, e);
134
			success = false;
135
			throw new AdminException(msg);
136
		}
137
        
138
        
139
        return success;
140
    }
141
    
142
	/**
143
	 * Update the registration of all DOIs with the specified guids in this server
144
	 * @param ids - a List of DOIs to update
145
	 */
146
    public boolean upgradeById(List<String> ids) throws AdminException {
147
        boolean success = true;
148
        
149
        try{
150
        	updateDOIRegistration(ids);
151
	    } catch (Exception e) {
152
			String msg = "Problem updating DOIs: " + e.getMessage();
153
			log.error(msg, e);
154
			success = false;
155
			throw new AdminException(msg);
156
		}
157
        return success;
158
    }
159
    
160
    /**
161
     * Update the registration of all DOIs in this server with the specified formatId
162
     * @param formatIds - a List of formatIDs used to filter DOI selection 
163
     */
164
    public boolean upgradeByFormatId(List<String> formatIds) throws AdminException {
165
        boolean success = true;  
166
        List<String> idList = new ArrayList<String>();
167
        
168
        try{
169
        	for (String formatId: formatIds) {        		
170
	        	//Get all the docids with this formatId
171
        		List<String> docids = DBUtil.getAllDocidsByType(formatId, true, serverLocation);
172
	        	
173
        		//get the guids for each docid and add to our list
174
	        	for(String id: docids){
175
	        		String docid = DocumentUtil.getDocIdFromAccessionNumber(id);
176
					int rev = DocumentUtil.getRevisionFromAccessionNumber(id);
177
					String guid = IdentifierManager.getInstance().getGUID(docid, rev);	
178
					idList.add(guid);
179
	        	}
180
	        	
181
	        	//Update the registration for all these guids
182
	            Collections.sort(idList);
183
	            updateDOIRegistration(idList);
184
        	}
185
	    } catch (Exception e) {
186
			String msg = "Problem updating DOIs: " + e.getMessage();
187
			log.error(msg, e);
188
			success = false;
189
			throw new AdminException(msg);
190
		}
191
        return success;
192
    }
193
    
194
}
(3-3/10)