Project

General

Profile

1 8747 leinfelder
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2013 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author$'
7
 *     '$Date$'
8
 * '$Revision$'
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 8760 walker
import java.util.ArrayList;
30 8747 leinfelder
31
import org.apache.commons.logging.Log;
32
import org.apache.commons.logging.LogFactory;
33
import org.dataone.service.types.v1.Identifier;
34 8810 leinfelder
import org.dataone.service.types.v2.SystemMetadata;
35 8747 leinfelder
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.util.DocumentUtil;
43
44
45
46
/**
47
 * Updates existing DOI registrations for EML versions
48 8760 walker
 * @author leinfelder, walker
49 8747 leinfelder
 *
50
 */
51
public class UpdateDOI implements UpgradeUtilityInterface {
52
53
	private static Log log = LogFactory.getLog(UpdateDOI.class);
54
55
	private int serverLocation = 1;
56
57
	public int getServerLocation() {
58
		return serverLocation;
59
	}
60
61
	public void setServerLocation(int serverLocation) {
62
		this.serverLocation = serverLocation;
63
	}
64
65 8760 walker
	/**
66
	 * Update the registration of a list of DOIs
67
	 * @param identifiers - DOIs to update
68
	 */
69 8747 leinfelder
	private void updateDOIRegistration(List<String> identifiers) {
70 8789 leinfelder
71
		// look up the prefix - NOTE we have used different shoulders over time, so might consider updating anything with "doi:..."
72
		String prefix = "doi:";
73 8791 leinfelder
//		try {
74
//			prefix = PropertyService.getProperty("guid.ezid.doishoulder." + serverLocation);
75
//		} catch (PropertyNotFoundException pnfe) {
76
//			log.error("Could not look up the doi shoulder for this server", pnfe);
77
//			return;
78
//		}
79 8789 leinfelder
80 8747 leinfelder
		for (String pid: identifiers) {
81
			try {
82 8789 leinfelder
				// skip if not a DOI
83
				if (!pid.startsWith(prefix)) {
84
					continue;
85
				}
86
87 8760 walker
				//Create an identifier and retrieve the SystemMetadata for this guid
88 8747 leinfelder
				Identifier identifier = new Identifier();
89 8760 walker
				identifier.setValue(pid);
90
				SystemMetadata sysMeta = HazelcastService.getInstance().getSystemMetadataMap().get(identifier);
91
92
				//Update the registration
93 8747 leinfelder
				DOIService.getInstance().registerDOI(sysMeta);
94
			} catch (Exception e) {
95
				// what to do? nothing
96
				e.printStackTrace();
97
				continue;
98
			}
99
100
		}
101
	}
102
103 8760 walker
	/**
104
	 * Update the DOI registration of all ids in this server with EML formatIds
105
	 */
106 8747 leinfelder
    public boolean upgrade() throws AdminException {
107
        boolean success = true;
108
109
        try {
110
        	// get only local ids for this server
111
            List<String> idList = null;
112
113
            idList = DBUtil.getAllDocidsByType(DocumentImpl.EML2_0_0NAMESPACE, true, serverLocation);
114
            Collections.sort(idList);
115
            updateDOIRegistration(idList);
116
117
            idList = DBUtil.getAllDocidsByType(DocumentImpl.EML2_0_1NAMESPACE, true, serverLocation);
118
            Collections.sort(idList);
119
            updateDOIRegistration(idList);
120
121
            idList = DBUtil.getAllDocidsByType(DocumentImpl.EML2_1_0NAMESPACE, true, serverLocation);
122
            Collections.sort(idList);
123
            updateDOIRegistration(idList);
124
125
            idList = DBUtil.getAllDocidsByType(DocumentImpl.EML2_1_1NAMESPACE, true, serverLocation);
126
            Collections.sort(idList);
127
            updateDOIRegistration(idList);
128
129
		} catch (Exception e) {
130
			String msg = "Problem updating DOIs: " + e.getMessage();
131
			log.error(msg, e);
132
			success = false;
133
			throw new AdminException(msg);
134
		}
135
136
137
        return success;
138
    }
139
140 8760 walker
	/**
141
	 * Update the registration of all DOIs with the specified guids in this server
142
	 * @param ids - a List of DOIs to update
143
	 */
144
    public boolean upgradeById(List<String> ids) throws AdminException {
145
        boolean success = true;
146
147
        try{
148
        	updateDOIRegistration(ids);
149
	    } catch (Exception e) {
150
			String msg = "Problem updating DOIs: " + e.getMessage();
151
			log.error(msg, e);
152
			success = false;
153
			throw new AdminException(msg);
154
		}
155
        return success;
156
    }
157
158
    /**
159
     * Update the registration of all DOIs in this server with the specified formatId
160
     * @param formatIds - a List of formatIDs used to filter DOI selection
161
     */
162
    public boolean upgradeByFormatId(List<String> formatIds) throws AdminException {
163
        boolean success = true;
164
        List<String> idList = new ArrayList<String>();
165
166
        try{
167
        	for (String formatId: formatIds) {
168
	        	//Get all the docids with this formatId
169
        		List<String> docids = DBUtil.getAllDocidsByType(formatId, true, serverLocation);
170
171
        		//get the guids for each docid and add to our list
172
	        	for(String id: docids){
173
	        		String docid = DocumentUtil.getDocIdFromAccessionNumber(id);
174
					int rev = DocumentUtil.getRevisionFromAccessionNumber(id);
175
					String guid = IdentifierManager.getInstance().getGUID(docid, rev);
176
					idList.add(guid);
177
	        	}
178
179
	        	//Update the registration for all these guids
180
	            Collections.sort(idList);
181
	            updateDOIRegistration(idList);
182
        	}
183
	    } catch (Exception e) {
184
			String msg = "Problem updating DOIs: " + e.getMessage();
185
			log.error(msg, e);
186
			success = false;
187
			throw new AdminException(msg);
188
		}
189
        return success;
190
    }
191
192 8747 leinfelder
}