@Override
public void save(Page p) {
String identity = HttpCookies.getCookieValue(
this.perThreadRequest.get(), "identity");
AsyncDatastoreService datastore = DatastoreServiceFactory
.getAsyncDatastoreService();
// start the query to get the original page
Page oldPage = new Page();
Future<Entity> oldPageFuture;
{
oldPageFuture = datastore.get(KeyFactory.createKey(
Page.class.getSimpleName(), p.getName()));
}
try {
Date now = new Date();
// create the page entity
{
Entity page = new Entity(KeyFactory.createKey(Page.PAGE,
URLDecoder.decode(p.getName(), "UTF-8")));
// ... set properties ...
page.setProperty(Page.CONTENT, new Text(p.getContent()));
// page.setProperty(Page.STYLE, new Text(p.getStyle()));
page.setProperty(Page.REMOTE_IP, getThreadLocalRequest()
.getRemoteAddr());
page.setProperty(Page.DATE, now);
page.setProperty(Page.USER, identity);
try {
// block on retrieving previous version
Entity e = oldPageFuture.get();
oldPage = new Page(e.getKey().getName(),
((Text) e.getProperty(Page.CONTENT)).getValue());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
datastore.put(page);
}
// create the pagehistory entity
{
Entity history = new Entity(
KeyFactory.createKey(
Page.PAGEHISTORY,
URLDecoder.decode(p.getName(), "UTF-8")
+ now.getTime()));
// ... set properties ...
history.setProperty(Page.NAME,
URLDecoder.decode(p.getName(), "UTF-8"));
history.setProperty(Page.CONTENT, new Text(p.getContent()));
// history.setProperty(Page.STYLE, new Text(p.getStyle()));
history.setProperty(Page.REMOTE_IP, getThreadLocalRequest()
.getRemoteAddr());
history.setProperty(Page.DATE, now);
history.setProperty(Page.UPDATE, p.isUpdate());
history.setProperty(Page.USER, identity);
// create the diff
diff_match_patch dmp = new diff_match_patch();
// create the diff
LinkedList<Diff> diffs = dmp.diff_main(oldPage.getContent(),
p.getContent());
// make the diff human readable
dmp.diff_cleanupSemantic(diffs);
// convert the diff to html
String diff = dmp.diff_prettyHtml(diffs);
history.setProperty(Page.DIFF, new Text(diff));
datastore.put(history);
}
try {
Cache cache = CacheManager.getInstance().getCacheFactory()
.createCache(Collections.emptyMap());