/
HarvestDefinition.java
403 lines (352 loc) · 12.7 KB
/
HarvestDefinition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* #%L
* Netarchivesuite - harvester
* %%
* Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
* the National Library of France and the Austrian National Library.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package dk.netarkivet.harvester.datamodel;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.inject.Provider;
import dk.netarkivet.common.exceptions.ArgumentNotValid;
import dk.netarkivet.common.utils.Named;
import dk.netarkivet.harvester.datamodel.dao.DAOProviderFactory;
import dk.netarkivet.harvester.datamodel.extendedfield.ExtendableEntity;
import dk.netarkivet.harvester.datamodel.extendedfield.ExtendedFieldDAO;
import dk.netarkivet.harvester.datamodel.extendedfield.ExtendedFieldTypes;
import dk.netarkivet.harvester.datamodel.extendedfield.ExtendedFieldValue;
/**
* This abstract class models the general properties of a harvest definition, i.e. object id , name, comments, and
* submission date
* <p>
* The specializing classes FullHarvest and PartielHarvest contains the specific properties and operations of snapshot
* harvestdefinitions and all other kinds of harvestdefinitions, respectively.
* <p>
* Methods exist to generate jobs from this harvest definition.
*/
public abstract class HarvestDefinition extends ExtendableEntity implements Named {
protected Long oid;
protected String harvestDefName;
/** The intended audience for the harvest. */
protected String audience;
/** The time this harvest definition was first written. */
protected Date submissionDate;
protected String comments;
/** Edition is used by the DAO to keep track of changes. */
protected long edition = -1;
/**
* Determines if the harvest definition is active and ready for scheduling. When true the jobs should be scheduled
* otherwise the scheduler should ignore the definition. Initially a definition is assumed active - the original
* behaviour before the isActive flag was introduced.
*/
protected boolean isActive = true;
/** The number of times this event has already run. */
protected int numEvents;
/** The id of the associated harvest channel, or null if the default one is to be used. */
protected Long channelId;
protected HarvestDefinition(Provider<ExtendedFieldDAO> extendedFieldDAO) {
super(extendedFieldDAO);
}
/**
* Create new instance of a PartialHavest configured according to the properties of the supplied
* DomainConfiguration.
*
* @param domainConfigurations a list of domain configurations
* @param schedule the harvest definition schedule
* @param harvestDefName the name of the harvest definition
* @param comments comments
* @return the newly created PartialHarvest
*/
public static PartialHarvest createPartialHarvest(List<DomainConfiguration> domainConfigurations,
Schedule schedule, String harvestDefName, String comments, String audience) {
return new PartialHarvest(domainConfigurations, schedule, harvestDefName, comments, audience);
}
/**
* Create snapshot harvestdefinition. A snapshot harvestdefinition creates jobs for all domains, using the default
* configuration for each domain. The HarvestDefinition is scheduled to run once as soon as possible.
* <p>
* When a previous harvest definition is supplied, only domains not completely harvested by the previous
* harvestdefinition are included in this harvestdefinition. indexready set to false.
*
* @param harvestDefName the name of the harvest definition
* @param comments description of the harvestdefinition
* @param prevHarvestOid an id of a previous harvest to use as basis for this definition, ignored when null.
* @param maxCountObjects the maximum number of objects harvested from any domain
* @param maxBytes the maximum number of bytes harvested from any domain
* @param maxJobRunningTime The maximum running time for each job
* @return a snapshot harvestdefinition
*/
public static FullHarvest createFullHarvest(String harvestDefName, String comments, Long prevHarvestOid,
long maxCountObjects, long maxBytes, long maxJobRunningTime) {
return new FullHarvest(harvestDefName, comments, prevHarvestOid, maxCountObjects, maxBytes, maxJobRunningTime,
false, DAOProviderFactory.getHarvestDefinitionDAOProvider(), DAOProviderFactory.getJobDAOProvider(),
DAOProviderFactory.getExtendedFieldDAOProvider(), DAOProviderFactory.getDomainDAOProvider());
}
/**
* Set the object ID of this harvest definition.
*
* @param oid The oid
* @throws ArgumentNotValid if the oid is null
*/
public void setOid(Long oid) {
ArgumentNotValid.checkNotNull(oid, "oid");
this.oid = oid;
}
/**
* Return the object ID of this harvest definition.
*
* @return The object id, or null if none.
*/
public Long getOid() {
return oid;
}
/**
* Check if this harvestdefinition has an ID set yet (doesn't happen until the DBDAO persists it).
*
* @return true, if this harvestdefinition has an ID set
*/
boolean hasID() {
return oid != null;
}
/**
* Set the submission date.
*
* @param submissionDate the time when the harvestdefinition was created
*/
public void setSubmissionDate(Date submissionDate) {
this.submissionDate = submissionDate;
}
/**
* Returns the submission date.
*
* @return the submission date
*/
public Date getSubmissionDate() {
return submissionDate;
}
/**
* Returns the name of the harvest definition.
*
* @return the harvest definition name
*/
public String getName() {
return harvestDefName;
}
/**
* Returns the comments for this harvest definition.
*
* @return the comments for this harvest definition.
*/
public String getComments() {
return comments;
}
/**
* Set the comments for this harvest definition.
*
* @param comments A user-entered string.
*/
public void setComments(String comments) {
ArgumentNotValid.checkNotNull(comments, "comments");
this.comments = comments;
}
/**
* Get the edition number.
*
* @return The edition number
*/
public long getEdition() {
return edition;
}
/**
* Set the edition number.
*
* @param theEdition the new edition of the harvestdefinition
*/
public void setEdition(long theEdition) {
edition = theEdition;
}
/**
* Get the number of times this harvest definition has been run so far.
*
* @return That number
*/
public int getNumEvents() {
return numEvents;
}
/**
* Set the number of times this harvest definition has been run so far.
*
* @param numEvents The number.
* @throws ArgumentNotValid if numEvents is negative
*/
public void setNumEvents(int numEvents) {
ArgumentNotValid.checkNotNegative(numEvents, "numEvents");
this.numEvents = numEvents;
}
/**
* Set's activation status. Only active harvestdefinitions should be scheduled.
*
* @param active new activation status
*/
public void setActive(boolean active) {
isActive = active;
}
/**
* Returns the activation status.
*
* @return activation status
*/
public boolean getActive() {
return isActive;
}
/**
* Returns a iterator of domain configurations for this harvest definition.
*
* @return Iterator containing information about the domain configurations
*/
public abstract Iterator<DomainConfiguration> getDomainConfigurations();
/**
* Return a human-readable string representation of this object.
*
* @return A human-readable string representation of this object
*/
public String toString() {
return "HD #" + oid + ": '" + getName() + "'";
}
/**
* Tests whether some other object is "equal to" this HarvestDefinition. Cfr. documentation of
* java.lang.Object.equals()
*
* @param o
* @return True or false, indicating equality.
*/
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof HarvestDefinition)) {
return false;
}
final HarvestDefinition harvestDefinition = (HarvestDefinition) o;
if (!comments.equals(harvestDefinition.comments)) {
return false;
}
if (!harvestDefName.equals(harvestDefinition.harvestDefName)) {
return false;
}
if (oid != null ? !oid.equals(harvestDefinition.oid) : harvestDefinition.oid != null) {
return false;
}
if ((extendedFieldValues == null && harvestDefinition.getExtendedFieldValues() != null)
|| (extendedFieldValues != null && harvestDefinition.getExtendedFieldValues() == null)) {
return false;
}
if (extendedFieldValues != null && harvestDefinition.getExtendedFieldValues() != null) {
if (extendedFieldValues.size() != harvestDefinition.getExtendedFieldValues().size()) {
return false;
}
for (int i = 0; i < extendedFieldValues.size(); i++) {
ExtendedFieldValue e1 = extendedFieldValues.get(i);
ExtendedFieldValue e2 = harvestDefinition.getExtendedFieldValues().get(i);
if ((e1 == null && e2 != null) || (e1 != null && e2 == null)) {
return false;
}
if (e1 != null && e2 != null) {
if (!e1.equals(e2)) {
return false;
}
}
}
}
return true;
}
/**
* Returns a hashcode of this object generated on fields oid, harvestDefName, and comments.
*
* @return the hashCode
*/
public int hashCode() {
int result;
result = (oid != null ? oid.hashCode() : 0);
result = 29 * result + harvestDefName.hashCode();
result = 29 * result + comments.hashCode();
return result;
}
/**
* Check if this harvest definition should be run, given the time now.
*
* @param now The current time
* @return true if harvest definition should be run
*/
public abstract boolean runNow(Date now);
/**
* Used to check if a harvestdefinition is a snapshot harvestdefinition.
*
* @return true if this harvestdefinition defines a snapshot harvest
*/
public abstract boolean isSnapShot();
/**
* Returns how many objects to harvest per domain, or 0 for no limit.
*
* @return how many objects to harvest per domain
*/
public abstract long getMaxCountObjects();
/**
* Returns how many bytes to harvest per domain, or -1 for no limit.
*
* @return how many bytes to harvest per domain
*/
public abstract long getMaxBytes();
/**
* @return the intended audience for this harvest.
*/
public String getAudience() {
return this.audience;
}
/**
* Set the audience.
*
* @param audience the audience.
*/
public void setAudience(String audience) {
this.audience = audience;
}
public Long getChannelId() {
return channelId;
}
protected void setChannelId(Long channelId) {
this.channelId = channelId;
}
/**
* All derived classes allow ExtendedFields from Type ExtendedFieldTypes.HARVESTDEFINITION
*
* @return ExtendedFieldTypes.HARVESTDEFINITION
*/
protected int getExtendedFieldType() {
return ExtendedFieldTypes.HARVESTDEFINITION;
}
/**
* Change the name of the Harvestdefinition to newName.
* @param newName The new name of the Harvestdefinition
*/
public void setName(String newName) {
this.harvestDefName = newName;
}
}