15
15
*/
16
16
package edu .unc .lib .dl .cdr .services ;
17
17
18
- import java .util .ArrayList ;
19
- import java .util .Collection ;
20
- import java .util .Iterator ;
21
- import java .util .List ;
22
- import java .util .Map ;
23
-
24
- import org .apache .commons .io .IOUtils ;
25
18
import org .slf4j .Logger ;
26
19
import org .slf4j .LoggerFactory ;
27
20
import org .springframework .context .ApplicationContext ;
28
21
import org .springframework .context .ApplicationContextAware ;
29
22
30
23
import edu .unc .lib .dl .cdr .services .exception .EnhancementException ;
31
24
import edu .unc .lib .dl .cdr .services .model .EnhancementMessage ;
32
- import edu .unc .lib .dl .cdr .services .model .LabeledPID ;
33
25
import edu .unc .lib .dl .cdr .services .processing .MessageDirector ;
34
26
import edu .unc .lib .dl .fedora .ManagementClient ;
35
- import edu .unc .lib .dl .fedora .PID ;
36
27
import edu .unc .lib .dl .util .JMSMessageUtil ;
37
28
import edu .unc .lib .dl .util .TripleStoreQueryService ;
38
29
@@ -42,9 +33,6 @@ public abstract class AbstractFedoraEnhancementService implements ObjectEnhancem
42
33
protected TripleStoreQueryService tripleStoreQueryService = null ;
43
34
protected ManagementClient managementClient = null ;
44
35
protected boolean active = false ;
45
- protected List <String > findCandidatesQueries ;
46
- protected String findStaleCandidatesQuery ;
47
- protected List <String > isApplicableQueries ;
48
36
49
37
private ApplicationContext applicationContext ;
50
38
@@ -94,153 +82,4 @@ public ManagementClient getManagementClient() {
94
82
public void setManagementClient (ManagementClient managementClient ) {
95
83
this .managementClient = managementClient ;
96
84
}
97
-
98
- @ SuppressWarnings ("unchecked" )
99
- @ Override
100
- public List <PID > findStaleCandidateObjects (int maxResults , String priorToDate ) throws EnhancementException {
101
- return (List <PID >) this .findCandidateObjects (maxResults , 0 , priorToDate , false );
102
- }
103
-
104
- @ SuppressWarnings ("unchecked" )
105
- @ Override
106
- public List <PID > findCandidateObjects (int maxResults , int offset ) throws EnhancementException {
107
- return (List <PID >) this .findCandidateObjects (maxResults , offset , null , false );
108
- }
109
-
110
- @ Override
111
- public int countCandidateObjects () throws EnhancementException {
112
- return (Integer ) this .findCandidateObjects (-1 , 0 , null , true );
113
- }
114
-
115
- public Object findCandidateObjects (int maxResults , int offset , String priorToDate , boolean countQuery )
116
- throws EnhancementException {
117
- if (priorToDate == null ) {
118
- return this .executeCandidateQueries (this .findCandidatesQueries , countQuery , maxResults , offset );
119
- } else {
120
- String limitClause = "" ;
121
- if (maxResults >= 0 && !countQuery )
122
- limitClause = "LIMIT " + maxResults ;
123
- return this .executeCandidateQuery (String .format (this .findStaleCandidatesQuery , this .getTripleStoreQueryService ()
124
- .getResourceIndexModelUri (), priorToDate , limitClause ) + limitClause , countQuery );
125
- }
126
- }
127
-
128
- @ SuppressWarnings ("unchecked" )
129
- protected Object executeCandidateQueries (List <String > queries , boolean count , int limit , int offset ) {
130
- int resultCount = 0 ;
131
- List <PID > results = new MaxSizeList <PID >(limit );
132
- for (String queryOriginal : queries ) {
133
- String query = queryOriginal ;
134
- if (!count )
135
- query += " LIMIT " + limit ;
136
- query += " OFFSET " + offset ;
137
- Object result = this .executeCandidateQuery (query , count );
138
- if (count ) {
139
- resultCount += ((Integer )result ).intValue ();
140
- } else {
141
- List <PID > queryResults = (List <PID >) result ;
142
- results .addAll (queryResults );
143
- if (results .size () >= limit )
144
- return results ;
145
- }
146
- }
147
-
148
- if (count )
149
- return resultCount ;
150
- return results ;
151
- }
152
-
153
- @ SuppressWarnings ({ "unchecked" , "rawtypes" })
154
- protected Object executeCandidateQuery (String query , boolean countQuery ) {
155
- String format = "json" ;//((countQuery) ? "count/json" : "json");
156
- Map results = this .getTripleStoreQueryService ().sendSPARQL (query , format );
157
- List <Map > bindings = (List <Map >) ((Map ) results .get ("results" )).get ("bindings" );
158
-
159
- if (LOG .isDebugEnabled ())
160
- LOG .debug (results .toString ());
161
- if (countQuery ) {
162
- // TODO Mulgara doesn't support count queries in SPARQL, will need to redo for other triple stores
163
- return bindings .size ();
164
- /*Map binding = bindings.get(0);
165
- int count = Integer.parseInt((String) ((Map) binding.get("count")).get("value"));
166
- return count;*/
167
- } else {
168
- List <PID > result = new ArrayList <PID >();
169
- for (Map binding : bindings ) {
170
- String pidURI = (String ) ((Map ) binding .get ("pid" )).get ("value" );
171
- String label = (String ) ((Map ) binding .get ("label" )).get ("value" );
172
- result .add (new LabeledPID (pidURI , label ));
173
- }
174
-
175
- return result ;
176
- }
177
- }
178
-
179
- @ Override
180
- public boolean isApplicable (EnhancementMessage message ) throws EnhancementException {
181
- // Automatically isApplicable if the message is specifically asking for this service.
182
- String action = message .getQualifiedAction ();
183
- if ((JMSMessageUtil .ServicesActions .APPLY_SERVICE_STACK .equals (action ) || JMSMessageUtil .ServicesActions .APPLY_SERVICE .equals (action ))
184
- && this .getClass ().getName ().equals (message .getServiceName ()))
185
- return true ;
186
-
187
- return askQueries (this .isApplicableQueries , message );
188
- }
189
-
190
- protected boolean askQueries (List <String > queries , EnhancementMessage message ) {
191
- for (String query : queries )
192
- if (askQuery (query , message ))
193
- return true ;
194
- return false ;
195
- }
196
-
197
- @ SuppressWarnings ("unchecked" )
198
- protected boolean askQuery (String query , EnhancementMessage message ) {
199
- query = String .format (query ,
200
- this .tripleStoreQueryService .getResourceIndexModelUri (), message .getPid ().getURI ());
201
- Map <String , Object > result = this .getTripleStoreQueryService ().sendSPARQL (query );
202
- return (Boolean .TRUE .equals (result .get ("boolean" )));
203
- }
204
-
205
- /**
206
- * @param filePath
207
- * name of file to open. The file can reside anywhere in the classpath
208
- */
209
- protected String readFileAsString (String filePath ) throws java .io .IOException {
210
- return IOUtils .toString (this .getClass ().getResourceAsStream (filePath ), "UTF-8" );
211
- }
212
-
213
- protected class MaxSizeList <E > extends ArrayList <E > {
214
- private static final long serialVersionUID = 1L ;
215
- private int limit = 10 ;
216
-
217
- public MaxSizeList (int limit ) {
218
- this .limit = limit ;
219
- }
220
-
221
- @ Override
222
- public boolean add (E element ) {
223
- if (this .size () >= limit ) return true ;
224
- return super .add (element );
225
- }
226
-
227
- @ Override
228
- public void add (int index , E element ) {
229
- if (this .size () >= limit ) return ;
230
- super .add (index , element );
231
- }
232
-
233
- @ Override
234
- public boolean addAll (Collection <? extends E > c ) {
235
- if (c .size () + this .size () < limit )
236
- return super .addAll (c );
237
- Iterator <? extends E > it = c .iterator ();
238
- while (it .hasNext ()) {
239
- this .add (it .next ());
240
- if (this .size () == limit )
241
- return true ;
242
- }
243
- return false ;
244
- }
245
- }
246
85
}
0 commit comments