use of org.mycore.imagetiler.MCRTileEventHandler in project mycore by MyCoRe-Org.
the class MCRTilingAction method run.
/**
* takes a {@link MCRTileJob} and tiles the referenced {@link MCRImage} instance.
*
* Also this updates tileJob properties of {@link MCRTileJob} in the database.
*/
public void run() {
tileJob.setStart(new Date());
MCRImage image;
Path tileDir = MCRIView2Tools.getTileDir();
try {
image = getMCRImage();
image.setTileDir(tileDir);
} catch (IOException e) {
LOGGER.error("Error while retrieving image for job: {}", tileJob, e);
return;
}
MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
mcrSession.setUserInformation(MCRSystemUserInformation.getSystemUserInstance());
Transaction transaction = null;
try (Session session = MCRHIBConnection.instance().getSession()) {
MCRTileEventHandler tileEventHandler = new MCRTileEventHandler() {
Transaction transaction;
@Override
public void preImageReaderCreated() {
transaction = session.beginTransaction();
}
@Override
public void postImageReaderCreated() {
// beside tileJob, no write access so far
session.clear();
if (transaction.getStatus().isOneOf(TransactionStatus.ACTIVE)) {
transaction.commit();
}
}
};
try {
MCRTiledPictureProps picProps = image.tile(tileEventHandler);
tileJob.setFinished(new Date());
tileJob.setStatus(MCRJobState.FINISHED);
tileJob.setHeight(picProps.getHeight());
tileJob.setWidth(picProps.getWidth());
tileJob.setTiles(picProps.getTilesCount());
tileJob.setZoomLevel(picProps.getZoomlevel());
} catch (IOException e) {
LOGGER.error("IOException occured while tiling a queued picture", e);
throw e;
}
transaction = session.beginTransaction();
session.update(tileJob);
transaction.commit();
} catch (Exception e) {
LOGGER.error("Error while getting next tiling job.", e);
if (transaction != null && transaction.getStatus().isOneOf(TransactionStatus.ACTIVE)) {
transaction.rollback();
}
try {
Files.deleteIfExists(MCRImage.getTiledFile(tileDir, tileJob.getDerivate(), tileJob.getPath()));
} catch (IOException e1) {
LOGGER.error("Could not delete tile file after error!", e);
}
} finally {
MCRSessionMgr.releaseCurrentSession();
mcrSession.close();
}
}
Aggregations