Skip to content

Commit

Permalink
add forgotten files
Browse files Browse the repository at this point in the history
  • Loading branch information
eroux committed Mar 21, 2023
1 parent 6be00e2 commit 224890e
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.bdrc.iiif.image.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.ResIterator;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.ResourceFactory;
import org.apache.jena.rdf.model.Statement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.bdrc.iiif.core.Application;
import io.bdrc.iiif.exceptions.IIIFException;
import io.bdrc.iiif.model.ContentLocation;
import io.bdrc.iiif.resolver.AppConstants;
import io.bdrc.libraries.Models;



public class ContentLocationService extends ConcurrentResourceService<List<ContentLocation>> {

private static final Logger logger = LoggerFactory.getLogger(ContentLocationService.class);
public static final ContentLocationService Instance = new ContentLocationService();

static final Property pageStart = ResourceFactory.createProperty(AppConstants.BDO+"contentLocationPage");
static final Property pageEnd = ResourceFactory.createProperty(AppConstants.BDO+"contentLocationEndPage");
static final Property volumeStart = ResourceFactory.createProperty(AppConstants.BDO+"contentLocationVolume");
static final Property volumeEnd = ResourceFactory.createProperty(AppConstants.BDO+"contentLocationEndVolume");
static final Property clInstance = ResourceFactory.createProperty(AppConstants.BDO+"contentLocationInstance");

final Integer getVal(final Resource s, final Property p) {
Statement st = s.getProperty(p);
if (st == null)
return null;
return st.getInt();
}

@Override
public final List<ContentLocation> getFromApi(final String resQname) throws IIIFException {
final String uri = Models.BDR + resQname.substring(4);
final String sparqlStr = "construct { ?cls ?clp ?clo } where { <"+uri+"> <"+AppConstants.BDO+"contentLocation> ?cls . ?cls ?clp ?clo . }";
final Query q = QueryFactory.create(sparqlStr);
final QueryExecution qe = QueryExecution.service(Application.getProperty("fusekiUrl")).query(q).build();
final Model clm = qe.execConstruct();
if (clm.isEmpty()) {
logger.info("no content location found for {}", resQname);
qe.close();
return null;
}
final List<ContentLocation> cll = new ArrayList<>();
final ResIterator cli = clm.listSubjects();
while (cli.hasNext()) {
final Resource cl = cli.next();
final Resource clInst = cl.getPropertyResourceValue(clInstance);
final ContentLocation clo = new ContentLocation(clInst, getVal(cl, volumeStart), getVal(cl, volumeEnd), getVal(cl, pageStart), getVal(cl, pageEnd));
cll.add(clo);
}
cll.sort(null);
qe.close();
return cll;
}

ContentLocationService() {
super("contentlocation", AppConstants.CACHEPREFIX_CL);
}

}
48 changes: 48 additions & 0 deletions src/main/java/io/bdrc/iiif/model/ContentLocation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.bdrc.iiif.model;

import java.util.Comparator;

import org.apache.jena.rdf.model.Resource;

import io.bdrc.iiif.resolver.AppConstants;

public class ContentLocation implements Comparator<ContentLocation> {
public final int vol_start;
public final int vol_end;
public final Integer page_start;
public final Integer page_end;
public final String iiQname;

public ContentLocation(final Resource inst, final Integer vol_start, final Integer vol_end, final Integer page_start, final Integer page_end) {
this.page_start = page_start;
this.page_end = page_end;
if (vol_start == null || vol_start < 1) {
this.vol_start = 1;
} else {
this.vol_start = vol_start;
}
if (vol_end == null || vol_end < this.vol_start) {
this.vol_end = this.vol_start;
} else {
this.vol_end = vol_end;
}
if (inst.getURI().startsWith(AppConstants.BDR)) {
this.iiQname = "bdr:"+inst.getURI().substring(AppConstants.BDR_len);
} else {
this.iiQname = null;
}
}

@Override
public final int compare(final ContentLocation o1, final ContentLocation o2) {
if (!o1.iiQname.equals(o2))
return o1.iiQname.compareTo(o2.iiQname);
if (o1.vol_start != o2.vol_start)
return Integer.compare(o1.vol_start, o2.vol_start);
if (o1.vol_end != o2.vol_end)
return Integer.compare(o1.vol_end, o2.vol_end);
if (o1.page_start != o2.page_start)
return Integer.compare(o1.page_start, o2.page_start);
return Integer.compare(o1.page_end, o2.page_end);
}
}

0 comments on commit 224890e

Please sign in to comment.