Skip to content

Commit 05d8345

Browse files
author
Olaf Hartig
committed
added 'LinkedDataFragment' and 'LinkedDataFragmentBase'
1 parent 38b4d9c commit 05d8345

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.linkeddatafragments.datasource;
2+
3+
import com.hp.hpl.jena.rdf.model.StmtIterator;
4+
5+
/**
6+
* Represents any possible Linked Data Fragment.
7+
*
8+
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
9+
*/
10+
public interface LinkedDataFragment
11+
{
12+
/**
13+
* Returns an iterator over the RDF data of this fragment (possibly only
14+
* partial if the data is paged, as indicated by {@link #isPageOnly()}).
15+
*/
16+
StmtIterator getTriples();
17+
18+
/**
19+
* Returns true if {@link #getTriples()} returns a page of data only.
20+
* In this case, {@link #getPageNumber()} can be used to obtain the
21+
* corresponding page number.
22+
*/
23+
boolean isPageOnly();
24+
25+
/**
26+
* Returns the number of the page of data returned by {@link #getTriples()}
27+
* if the data is paged (that is, if {@link #isPageOnly()} returns true).
28+
*
29+
* If the data is not paged, this method throws an exception.
30+
*
31+
* @throws UnsupportedOperationException
32+
* If the data of this fragment is not paged.
33+
*/
34+
long getPageNumber() throws UnsupportedOperationException;
35+
36+
/**
37+
* Returns true if {@link #getTriples()} returns a page of data only and
38+
* this is the last page of the fragment.
39+
*
40+
* If the data is not paged (i.e., if {@link #isPageOnly()} returns false),
41+
* this method throws an exception.
42+
*
43+
* @throws UnsupportedOperationException
44+
* If the data of this fragment is not paged.
45+
*/
46+
boolean isLastPage() throws UnsupportedOperationException;
47+
48+
/**
49+
* Returns the maximum number of triples per page if {@link #getTriples()}
50+
* returns a page of data only (that is, if {@link #isPageOnly()} returns
51+
* true).
52+
*
53+
* If the data is not paged, this method throws an exception.
54+
*
55+
* @throws UnsupportedOperationException
56+
* If the data of this fragment is not paged.
57+
*/
58+
long getMaxPageSize() throws UnsupportedOperationException;
59+
60+
/**
61+
* Returns an iterator over the metadata of this fragment.
62+
*/
63+
StmtIterator getMetadata();
64+
65+
/**
66+
* Returns an iterator over an RDF description of the controls associated
67+
* with this fragment.
68+
*/
69+
StmtIterator getControls();
70+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package org.linkeddatafragments.datasource;
2+
3+
import java.net.URISyntaxException;
4+
5+
import org.apache.http.client.utils.URIBuilder;
6+
import org.linkeddatafragments.util.CommonResources;
7+
8+
import com.hp.hpl.jena.rdf.model.Model;
9+
import com.hp.hpl.jena.rdf.model.ModelFactory;
10+
import com.hp.hpl.jena.rdf.model.Resource;
11+
import com.hp.hpl.jena.rdf.model.StmtIterator;
12+
13+
/**
14+
* Base class of any implementation of {@link LinkedDataFragment} that uses
15+
* paging.
16+
*
17+
* @author <a href="http://olafhartig.de">Olaf Hartig</a>
18+
*/
19+
public abstract class LinkedDataFragmentBase implements LinkedDataFragment
20+
{
21+
public final String fragmentURL;
22+
public final String datasetURL;
23+
public final long pageNumber;
24+
public final boolean isLastPage;
25+
26+
protected LinkedDataFragmentBase( final String fragmentURL,
27+
final String datasetURL,
28+
final long pageNumber,
29+
final boolean isLastPage )
30+
{
31+
this.fragmentURL = fragmentURL;
32+
this.datasetURL = datasetURL;
33+
this.pageNumber = pageNumber;
34+
this.isLastPage = isLastPage;
35+
}
36+
37+
@Override
38+
public boolean isPageOnly() {
39+
return true;
40+
}
41+
42+
@Override
43+
public long getPageNumber() {
44+
return pageNumber;
45+
}
46+
47+
@Override
48+
public boolean isLastPage() {
49+
return isLastPage;
50+
}
51+
52+
@Override
53+
public long getMaxPageSize() {
54+
return LinkedDataFragmentRequest.TRIPLESPERPAGE;
55+
}
56+
57+
/**
58+
* This implementation uses {@link #addMetadata(Model)}, which should be
59+
* overridden in subclasses (instead of overriding this method).
60+
*/
61+
@Override
62+
public StmtIterator getMetadata()
63+
{
64+
final Model output = ModelFactory.createDefaultModel();
65+
addMetadata( output );
66+
return output.listStatements();
67+
}
68+
69+
/**
70+
* This implementation uses {@link #addControls(Model)}, which should be
71+
* overridden in subclasses (instead of overriding this method).
72+
*/
73+
@Override
74+
public StmtIterator getControls()
75+
{
76+
final Model output = ModelFactory.createDefaultModel();
77+
addControls( output );
78+
return output.listStatements();
79+
}
80+
81+
/**
82+
* Adds some basic metadata to the given RDF model.
83+
* This method may be overridden in subclasses.
84+
*/
85+
public void addMetadata( final Model model )
86+
{
87+
final Resource datasetId = model.createResource( getDatasetURI() );
88+
final Resource fragmentId = model.createResource( fragmentURL );
89+
90+
datasetId.addProperty( CommonResources.RDF_TYPE, CommonResources.VOID_DATASET );
91+
datasetId.addProperty( CommonResources.RDF_TYPE, CommonResources.HYDRA_COLLECTION );
92+
datasetId.addProperty( CommonResources.VOID_SUBSET, fragmentId );
93+
94+
fragmentId.addProperty( CommonResources.RDF_TYPE, CommonResources.HYDRA_COLLECTION );
95+
fragmentId.addProperty( CommonResources.RDF_TYPE, CommonResources.HYDRA_PAGEDCOLLECTION );
96+
}
97+
98+
/**
99+
* Adds an RDF description of page links to the given RDF model.
100+
* This method may be overridden in subclasses.
101+
*/
102+
public void addControls( final Model model )
103+
{
104+
final URIBuilder pagedURL;
105+
try {
106+
pagedURL = new URIBuilder( fragmentURL );
107+
}
108+
catch ( URISyntaxException e ) {
109+
throw new IllegalArgumentException( e );
110+
}
111+
112+
final Resource fragmentId = model.createResource( fragmentURL );
113+
114+
final Resource firstPageId =
115+
model.createResource(
116+
pagedURL.setParameter(LinkedDataFragmentRequest.PARAMETERNAME_PAGE,
117+
"1").toString() );
118+
119+
fragmentId.addProperty( CommonResources.HYDRA_FIRSTPAGE, firstPageId );
120+
121+
if ( pageNumber > 1) {
122+
final String prevPageNumber = Long.toString( pageNumber - 1 );
123+
final Resource prevPageId =
124+
model.createResource(
125+
pagedURL.setParameter(LinkedDataFragmentRequest.PARAMETERNAME_PAGE,
126+
prevPageNumber).toString() );
127+
128+
fragmentId.addProperty( CommonResources.HYDRA_PREVIOUSPAGE, prevPageId );
129+
}
130+
131+
if ( ! isLastPage ) {
132+
final String nextPageNumber = Long.toString( pageNumber + 1 );
133+
final Resource nextPageId =
134+
model.createResource(
135+
pagedURL.setParameter(LinkedDataFragmentRequest.PARAMETERNAME_PAGE,
136+
nextPageNumber).toString() );
137+
138+
fragmentId.addProperty( CommonResources.HYDRA_NEXTPAGE, nextPageId );
139+
}
140+
}
141+
142+
public String getDatasetURI() {
143+
return datasetURL + "#dataset";
144+
}
145+
146+
}

0 commit comments

Comments
 (0)