Search in sources :

Example 1 with MCRTileEventHandler

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();
    }
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) Path(java.nio.file.Path) MCRSession(org.mycore.common.MCRSession) Transaction(org.hibernate.Transaction) MCRTileEventHandler(org.mycore.imagetiler.MCRTileEventHandler) IOException(java.io.IOException) MCRTiledPictureProps(org.mycore.imagetiler.MCRTiledPictureProps) Date(java.util.Date) MCRImage(org.mycore.imagetiler.MCRImage) IOException(java.io.IOException) Session(org.hibernate.Session) MCRSession(org.mycore.common.MCRSession)

Aggregations

IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Date (java.util.Date)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 MCRSession (org.mycore.common.MCRSession)1 MCRPath (org.mycore.datamodel.niofs.MCRPath)1 MCRImage (org.mycore.imagetiler.MCRImage)1 MCRTileEventHandler (org.mycore.imagetiler.MCRTileEventHandler)1 MCRTiledPictureProps (org.mycore.imagetiler.MCRTiledPictureProps)1