Search in sources :

Example 96 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRTransferPackageCommands method importTransferPackagesFromDirectory.

@MCRCommand(help = "Imports all transfer packages located in the directory {0}.", syntax = "import transfer packages from directory {0}")
public static List<String> importTransferPackagesFromDirectory(String directory) throws Exception {
    Path dir = Paths.get(directory);
    if (!(Files.exists(dir) || Files.isDirectory(dir))) {
        throw new FileNotFoundException(directory + " does not exist or is not a directory.");
    }
    List<String> importStatements = new LinkedList<>();
    try (Stream<Path> stream = Files.find(dir, 0, (path, attr) -> String.valueOf(path).endsWith(".tar") && Files.isRegularFile(path))) {
        stream.map(Path::toAbsolutePath).map(Path::toString).forEach(path -> {
            String subCommand = MessageFormat.format("import transfer package from tar {0}", path);
            importStatements.add(subCommand);
        });
    }
    return importStatements;
}
Also used : Path(java.nio.file.Path) FileNotFoundException(java.io.FileNotFoundException) LinkedList(java.util.LinkedList) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 97 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRIView2Commands method deleteImageTiles.

/**
 * Deletes all image tiles of this derivate.
 * @param derivate derivate ID
 * @param absoluteImagePath absolute path to image file
 */
@MCRCommand(syntax = "delete tiles of image {0} {1}", help = "removes tiles of a specific file identified by its derivate ID {0} and absolute path {1}", order = 110)
public static void deleteImageTiles(String derivate, String absoluteImagePath) throws IOException {
    Path tileFile = MCRImage.getTiledFile(MCRIView2Tools.getTileDir(), derivate, absoluteImagePath);
    deleteFileAndEmptyDirectories(tileFile);
    int removed = MCRTilingQueue.getInstance().remove(derivate, absoluteImagePath);
    LOGGER.info("removed tiles from {} images", removed);
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 98 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRIView2Commands method deleteAllTiles.

/**
 * Deletes all image tiles.
 */
@MCRCommand(syntax = "delete all tiles", help = "removes all tiles of all derivates", order = 80)
public static void deleteAllTiles() throws IOException {
    Path storeDir = MCRIView2Tools.getTileDir();
    Files.walkFileTree(storeDir, MCRRecursiveDeleter.instance());
    MCRTilingQueue.getInstance().clear();
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 99 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRIView2Commands method checkImage.

/**
 * checks and repairs tile of this derivate.
 * @param derivate derivate ID
 * @param absoluteImagePath absolute path to image file
 */
@MCRCommand(syntax = CHECK_TILES_OF_IMAGE_COMMAND_SYNTAX, help = "checks if tiles a specific file identified by its derivate {0} and absolute path {1} are valid or generates new one", order = 30)
public static void checkImage(String derivate, String absoluteImagePath) throws IOException {
    Path iviewFile = MCRImage.getTiledFile(MCRIView2Tools.getTileDir(), derivate, absoluteImagePath);
    // file checks
    if (!Files.exists(iviewFile)) {
        LOGGER.warn("IView2 file does not exist: {}", iviewFile);
        tileImage(derivate, absoluteImagePath);
        return;
    }
    MCRTiledPictureProps props;
    try {
        props = MCRTiledPictureProps.getInstanceFromFile(iviewFile);
    } catch (Exception e) {
        LOGGER.warn("Error while reading image metadata. Recreating tiles.", e);
        tileImage(derivate, absoluteImagePath);
        return;
    }
    if (props == null) {
        LOGGER.warn("Could not get tile metadata");
        tileImage(derivate, absoluteImagePath);
        return;
    }
    ZipFile iviewImage;
    try {
        iviewImage = new ZipFile(iviewFile.toFile());
        validateZipFile(iviewImage);
    } catch (Exception e) {
        LOGGER.warn("Error while reading Iview2 file: {}", iviewFile, e);
        tileImage(derivate, absoluteImagePath);
        return;
    }
    try (FileSystem fs = MCRIView2Tools.getFileSystem(iviewFile)) {
        Path iviewFileRoot = fs.getRootDirectories().iterator().next();
        // structure and metadata checks
        // one for metadata
        int tilesCount = iviewImage.size() - 1;
        if (props.getTilesCount() != tilesCount) {
            LOGGER.warn("Metadata tile count does not match stored tile count: {}", iviewFile);
            tileImage(derivate, absoluteImagePath);
            return;
        }
        int x = props.getWidth();
        int y = props.getHeight();
        if (MCRImage.getTileCount(x, y) != tilesCount) {
            LOGGER.warn("Calculated tile count does not match stored tile count: {}", iviewFile);
            tileImage(derivate, absoluteImagePath);
            return;
        }
        try {
            ImageReader imageReader = MCRIView2Tools.getTileImageReader();
            @SuppressWarnings("unused") BufferedImage thumbnail = MCRIView2Tools.getZoomLevel(iviewFileRoot, props, imageReader, 0);
            int maxX = (int) Math.ceil((double) props.getWidth() / MCRImage.getTileSize());
            int maxY = (int) Math.ceil((double) props.getHeight() / MCRImage.getTileSize());
            LOGGER.debug(MessageFormat.format("Image size:{0}x{1}, tiles:{2}x{3}", props.getWidth(), props.getHeight(), maxX, maxY));
            try {
                @SuppressWarnings("unused") BufferedImage sampleTile = MCRIView2Tools.readTile(iviewFileRoot, imageReader, props.getZoomlevel(), maxX - 1, 0);
            } finally {
                imageReader.dispose();
            }
        } catch (IOException | JDOMException e) {
            LOGGER.warn("Could not read thumbnail of {}", iviewFile, e);
            tileImage(derivate, absoluteImagePath);
        }
    }
}
Also used : Path(java.nio.file.Path) MCRPath(org.mycore.datamodel.niofs.MCRPath) ZipFile(java.util.zip.ZipFile) FileSystem(java.nio.file.FileSystem) IOException(java.io.IOException) MCRTiledPictureProps(org.mycore.imagetiler.MCRTiledPictureProps) ImageReader(javax.imageio.ImageReader) JDOMException(org.jdom2.JDOMException) MCRException(org.mycore.common.MCRException) JDOMException(org.jdom2.JDOMException) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 100 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRIFS2Commands method repairMcrdataXmlForDerivate.

@MCRCommand(syntax = "repair mcrdata.xml for derivate {0} in content store {1}", help = "repair the entries in mcrdata.xml with data from content store {1} for MCRDerivate {0}")
public static void repairMcrdataXmlForDerivate(String derivate_id, String content_store) {
    LOGGER.info("Start repair of mcrdata.xml for derivate {} in store {}", derivate_id, content_store);
    // check input;
    MCRObjectID mcr_derivate_id;
    try {
        mcr_derivate_id = MCRObjectID.getInstance(derivate_id);
    } catch (MCRException e) {
        LOGGER.error("Wrong derivate parameter, it is not a MCRObjectID");
        return;
    }
    if (content_store == null || content_store.length() == 0) {
        LOGGER.error("Empty content store parameter");
        return;
    }
    MCRContentStore store = MCRContentStoreFactory.getStore(content_store);
    if (!(store instanceof MCRCStoreIFS2)) {
        LOGGER.error("The content store is not a IFS2 type");
        return;
    }
    // repair
    try {
        MCRFileCollection file_collection = ((MCRCStoreIFS2) store).getIFS2FileCollection(mcr_derivate_id);
        file_collection.repairMetadata();
    } catch (IOException e) {
        LOGGER.error("Erroe while repair derivate with ID {}", mcr_derivate_id);
    }
}
Also used : MCRCStoreIFS2(org.mycore.datamodel.ifs2.MCRCStoreIFS2) MCRException(org.mycore.common.MCRException) MCRFileCollection(org.mycore.datamodel.ifs2.MCRFileCollection) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)106 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)37 MCRException (org.mycore.common.MCRException)34 IOException (java.io.IOException)30 File (java.io.File)22 ArrayList (java.util.ArrayList)18 Document (org.jdom2.Document)17 JDOMException (org.jdom2.JDOMException)17 MCRObject (org.mycore.datamodel.metadata.MCRObject)17 Path (java.nio.file.Path)16 SAXException (org.xml.sax.SAXException)16 EntityManager (javax.persistence.EntityManager)15 MCRAccessException (org.mycore.access.MCRAccessException)15 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)15 MCRPath (org.mycore.datamodel.niofs.MCRPath)15 FileNotFoundException (java.io.FileNotFoundException)13 SAXParseException (org.xml.sax.SAXParseException)12 List (java.util.List)11 Element (org.jdom2.Element)11 MCRPersistenceException (org.mycore.common.MCRPersistenceException)11