-
Notifications
You must be signed in to change notification settings - Fork 86
/
AbstractOperation.java
93 lines (80 loc) · 3.04 KB
/
AbstractOperation.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
/* $This file is distributed under the terms of the license in LICENSE$ */
package edu.cornell.mannlib.vitro.webapp.dynapi.components.operations;
import java.util.Collections;
import java.util.Map;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.OperationResult;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameter;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameters;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.ProcedureDescriptor;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.Data;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.DataStore;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public abstract class AbstractOperation implements Operation {
private static final Log log = LogFactory.getLog(AbstractOperation.class);
protected Parameters inputParams = new Parameters();
protected Parameters outputParams = new Parameters();
public Parameters getInputParams() {
return inputParams;
}
public Parameters getOutputParams() {
return outputParams;
}
public OperationResult run(DataStore dataStore) {
OperationResult result = OperationResult.ok();
if (!isInputValid(dataStore)) {
return OperationResult.internalServerError();
}
try {
result = runOperation(dataStore);
} catch (Exception t) {
log.error(t, t);
return OperationResult.internalServerError();
}
if (!isOutputValid(dataStore)) {
return OperationResult.internalServerError();
}
return result;
}
protected abstract OperationResult runOperation(DataStore dataStore) throws Exception;
public boolean isInputValid(DataStore dataStore) {
if (!isValid()) {
log.debug("Component configuration is invalid");
return false;
}
if (dataStore == null) {
log.error("data store is null");
return false;
}
Parameters inputParams = getInputParams();
for (String name : inputParams.getNames()) {
if (!dataStore.contains(name)) {
log.error("Parameter " + name + " not found");
return false;
}
Parameter param = inputParams.get(name);
Data data = dataStore.getData(name);
if (!param.isValid(name, data)) {
return false;
}
}
return true;
}
public boolean isOutputValid(DataStore dataStore) {
Parameters providedParams = getOutputParams();
if (providedParams != null) {
for (String name : providedParams.getNames()) {
if (!dataStore.contains(name)) {
log.error("Parameter " + name + " not found");
return false;
}
}
}
return true;
}
public Map<String, ProcedureDescriptor> getDependencies() {
return Collections.emptyMap();
}
public void dereference() {
}
}