public void preprocessingHook() throws Exception {
TweetStore store = TweetStore.getInstance();
TweetStoreConnection c = store.createConnection();
try {
ValueFactory vf = store.getSail().getValueFactory();
SailConnection sc = c.getSailConnection();
long id = Long.valueOf(selfURI.substring(selfURI.lastIndexOf('/') + 1, selfURI.lastIndexOf('.')));
User user = new User(id);
URI personURI = vf.createURI(PersistenceContext.uriOf(new Person(user)));
// check timestamp
long lastUpdate = -1;
CloseableIteration<? extends Statement, SailException> iter
= sc.getStatements(personURI, TwitlogicVocabulary.lastUpdatedAt, null, false);
try {
if (iter.hasNext()) {
lastUpdate = ((Literal) iter.next().getObject()).calendarValue().toGregorianCalendar().getTime().getTime();
}
} finally {
iter.close();
}
long now = System.currentTimeMillis();
if (-1 == lastUpdate || (expireTime > 0 && now - lastUpdate > expireTime)) {
LOGGER.info((0 == lastUpdate ? "setting" : "updating") + " followees of " + personURI);
// fetch followees
TwitterClient client = store.getTwitterClient();
List<User> followees = client.getFollowees(user, followeeLimit);
URI foafKnows = vf.createURI(FOAF.KNOWS);
// persist self and foaf:knows edges
sc.removeStatements(personURI, foafKnows, null);
for (User f : followees) {
sc.addStatement(personURI, foafKnows, vf.createURI(PersistenceContext.uriOf(new Person(f))));
}
// update timestamp
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(new Date(now));
Literal obj = sail.getValueFactory().createLiteral(
DATATYPE_FACTORY.newXMLGregorianCalendar(cal));
sc.removeStatements(personURI, TwitlogicVocabulary.lastUpdatedAt, null);
sc.addStatement(personURI, TwitlogicVocabulary.lastUpdatedAt, obj);
sc.commit();
}
} finally {
c.close();
}
}