Revision 2031
Added by Duane Costa about 20 years ago
HarvestDocument.java | ||
---|---|---|
13 | 13 |
import java.net.MalformedURLException; |
14 | 14 |
import java.net.URL; |
15 | 15 |
|
16 |
import edu.ucsb.nceas.metacat.client.*; |
|
16 |
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException; |
|
17 |
import edu.ucsb.nceas.metacat.client.Metacat; |
|
18 |
import edu.ucsb.nceas.metacat.client.MetacatException; |
|
19 |
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException; |
|
17 | 20 |
import edu.ucsb.nceas.utilities.IOUtil; |
18 | 21 |
|
19 | 22 |
|
... | ... | |
26 | 29 |
public class HarvestDocument { |
27 | 30 |
|
28 | 31 |
private String documentName; |
29 |
private String documentType;
|
|
30 |
private String documentURL;
|
|
32 |
String documentType; |
|
33 |
String documentURL; |
|
31 | 34 |
private Harvester harvester; |
32 | 35 |
private HarvestSiteSchedule harvestSiteSchedule; |
33 |
private int identifier;
|
|
34 |
private int revision;
|
|
35 |
private String scope;
|
|
36 |
int identifier; |
|
37 |
int revision; |
|
38 |
String scope; |
|
36 | 39 |
|
37 | 40 |
|
38 | 41 |
/** |
... | ... | |
86 | 89 |
inputStreamReader = new InputStreamReader(inputStream); |
87 | 90 |
documentString = IOUtil.getAsString(inputStreamReader, true); |
88 | 91 |
stringReader = new StringReader(documentString); |
89 |
System.out.println(" Successfully read document: " + documentURL); |
|
92 |
harvester.addLogEntry(0, "", "GetDocSuccess", |
|
93 |
harvestSiteSchedule.siteScheduleID, null, ""); |
|
90 | 94 |
} |
91 | 95 |
catch (MalformedURLException e) { |
92 |
System.err.println("MalformedURLException: " + e.getMessage()); |
|
96 |
harvester.addLogEntry(1, "MalformedURLException", "GetDocError", |
|
97 |
harvestSiteSchedule.siteScheduleID, this, |
|
98 |
"MalformedURLException: " + e.getMessage()); |
|
93 | 99 |
} |
94 | 100 |
catch (IOException e) { |
95 |
System.err.println("IOException: " + e.getMessage()); |
|
101 |
harvester.addLogEntry(1, "IOException", "GetDocError", |
|
102 |
harvestSiteSchedule.siteScheduleID, this, |
|
103 |
"IOException: " + e.getMessage()); |
|
96 | 104 |
} |
97 | 105 |
|
98 | 106 |
return stringReader; |
... | ... | |
116 | 124 |
* into metacat. |
117 | 125 |
*/ |
118 | 126 |
if (metacatHasDocument()) { |
119 |
System.out.println(" metacat has document"); |
|
120 | 127 |
highestRevision = metacatHighestRevision(); |
121 |
System.out.println(" metacatHighestRevision: " + highestRevision); |
|
128 |
harvester.addLogEntry(0, |
|
129 |
"Metacat has document: " + documentName + |
|
130 |
", highest revision: " + highestRevision, |
|
131 |
"MetacatHasDoc", |
|
132 |
harvestSiteSchedule.siteScheduleID, null, ""); |
|
122 | 133 |
} |
123 | 134 |
else { |
124 | 135 |
stringReader = getSiteDocument(); |
125 | 136 |
if (stringReader != null) { |
126 | 137 |
if (parseDocument()) { |
127 |
metacatReturnString = putMetacatDocument(stringReader); |
|
128 |
System.out.println(" " + metacatReturnString); |
|
138 |
putMetacatDocument(stringReader); |
|
129 | 139 |
} |
130 |
else { |
|
131 |
System.out.println("Error parsing document."); |
|
132 |
} |
|
133 | 140 |
} |
134 |
else { |
|
135 |
System.out.print(" Error reading document at URL: "); |
|
136 |
System.out.println(documentURL); |
|
137 |
} |
|
138 | 141 |
} |
139 | 142 |
} |
140 |
|
|
143 |
|
|
141 | 144 |
|
142 | 145 |
/** |
146 |
* Logs a metacat document error to the harvest detail log. |
|
147 |
* |
|
148 |
* @param insert true if insert operation, false is update |
|
149 |
* @param metacatReturnString string returned from the insert or update |
|
150 |
* @param exceptionName name of the exception class |
|
151 |
* @param e the exception object |
|
152 |
*/ |
|
153 |
private void logMetacatError (boolean insert, |
|
154 |
String metacatReturnString, |
|
155 |
String exceptionName, |
|
156 |
Exception e |
|
157 |
) { |
|
158 |
if (insert) { |
|
159 |
harvester.addLogEntry(1, metacatReturnString, "InsertDocError", |
|
160 |
harvestSiteSchedule.siteScheduleID, |
|
161 |
this, exceptionName + ": " + e.getMessage()); |
|
162 |
} |
|
163 |
else { |
|
164 |
harvester.addLogEntry(1, metacatReturnString, "UpdateDocError", |
|
165 |
harvestSiteSchedule.siteScheduleID, |
|
166 |
this, exceptionName + ": " + e.getMessage()); |
|
167 |
} |
|
168 |
} |
|
169 |
|
|
170 |
|
|
171 |
/** |
|
143 | 172 |
* Boolean to determine whether Metacat already has this document. |
144 | 173 |
* |
145 | 174 |
* @return true if Metacat has the document, otherwise false |
... | ... | |
172 | 201 |
private boolean parseDocument () { |
173 | 202 |
boolean success = true; |
174 | 203 |
|
204 |
if (success) { |
|
205 |
harvester.addLogEntry(0, "", "ValidateDocSuccess", |
|
206 |
harvestSiteSchedule.siteScheduleID, null, ""); |
|
207 |
} |
|
208 |
else { |
|
209 |
harvester.addLogEntry(1, "Error validating document", "ValidateDocError", |
|
210 |
harvestSiteSchedule.siteScheduleID, this, ""); |
|
211 |
} |
|
212 |
|
|
175 | 213 |
return success; |
176 | 214 |
} |
177 | 215 |
|
... | ... | |
179 | 217 |
/** |
180 | 218 |
* Print the data fields and values in this HarvestDocument object. |
181 | 219 |
*/ |
182 |
void printOutput() |
|
183 |
{ |
|
184 |
System.out.println(""); |
|
185 |
System.out.println(" scope: " + scope); |
|
186 |
System.out.println(" identifier: " + identifier); |
|
187 |
System.out.println(" revision: " + revision); |
|
188 |
System.out.println(" documentType: " + documentType); |
|
189 |
System.out.println(" documentURL: " + documentURL); |
|
190 |
System.out.println(" documentName: " + documentName); |
|
220 |
void printOutput() { |
|
221 |
System.out.println("scope: " + scope); |
|
222 |
System.out.println("identifier: " + identifier); |
|
223 |
System.out.println("revision: " + revision); |
|
224 |
System.out.println("documentType: " + documentType); |
|
225 |
System.out.println("documentURL: " + documentURL); |
|
226 |
System.out.println("documentName: " + documentName); |
|
191 | 227 |
} |
192 | 228 |
|
193 | 229 |
|
194 | 230 |
/** |
195 | 231 |
* Insert or update this document to Metacat. If revision equals 1, do an |
196 | 232 |
* insert; otherwise, do an update. |
197 |
* |
|
198 |
* @return the Metacat return string from the insert or update operation |
|
199 | 233 |
*/ |
200 |
private String putMetacatDocument(StringReader stringReader) {
|
|
234 |
private void putMetacatDocument(StringReader stringReader) {
|
|
201 | 235 |
String docid = scope + "." + identifier + "." + revision; |
236 |
boolean insert = (revision == 1); |
|
202 | 237 |
Metacat metacat = harvester.metacat; |
203 | 238 |
String metacatReturnString = ""; |
204 |
|
|
239 |
|
|
205 | 240 |
if (harvester.connectToMetacat()) { |
206 | 241 |
try { |
207 |
if (revision == 1) { |
|
208 |
System.out.println(" Inserting document to metacat: " + docid); |
|
242 |
if (insert) { |
|
209 | 243 |
metacatReturnString = metacat.insert(docid, stringReader, null); |
244 |
harvester.addLogEntry(0, docid + " : " + metacatReturnString, |
|
245 |
"InsertDocSuccess", |
|
246 |
harvestSiteSchedule.siteScheduleID, |
|
247 |
null, ""); |
|
210 | 248 |
} |
211 | 249 |
else { |
212 |
System.out.println(" Updating document to metacat: " + docid); |
|
213 | 250 |
metacatReturnString = metacat.update(docid, stringReader, null); |
251 |
harvester.addLogEntry(0, docid + " : " + metacatReturnString, |
|
252 |
"UpdateDocSuccess", |
|
253 |
harvestSiteSchedule.siteScheduleID, |
|
254 |
null, ""); |
|
214 | 255 |
} |
215 | 256 |
} |
216 | 257 |
catch (MetacatInaccessibleException e) { |
217 |
System.err.println("MetacatInaccessibleException: " + e.getMessage()); |
|
258 |
logMetacatError(insert, metacatReturnString, |
|
259 |
"MetacatInaccessibleException", e); |
|
218 | 260 |
} |
219 | 261 |
catch (InsufficientKarmaException e) { |
220 |
System.err.println("InsufficientKarmaException: " + e.getMessage()); |
|
262 |
logMetacatError(insert, metacatReturnString, |
|
263 |
"InsufficientKarmaException", e); |
|
221 | 264 |
} |
222 | 265 |
catch (MetacatException e) { |
223 |
System.err.println("MetacatException: " + e.getMessage());
|
|
266 |
logMetacatError(insert, metacatReturnString, "MetacatException", e);
|
|
224 | 267 |
} |
225 | 268 |
catch (IOException e) { |
226 |
System.err.println("IOException: " + e.getMessage());
|
|
269 |
logMetacatError(insert, metacatReturnString, "IOException", e);
|
|
227 | 270 |
} |
228 | 271 |
} |
229 |
else { |
|
230 |
metacatReturnString = "Not putting document to metacat"; |
|
231 |
} |
|
232 |
|
|
233 |
return metacatReturnString; |
|
234 | 272 |
} |
235 |
|
|
236 | 273 |
} |
Also available in: Unified diff
Additional development of Harvester implementation