Search in sources :

Example 1 with ContentFileNotLinkedException

use of ugh.exceptions.ContentFileNotLinkedException in project goobi-workflow by intranda.

the class MetadatenImagesHelper method createPagination.

/**
 * Markus baut eine Seitenstruktur aus den vorhandenen Images ---------------- Steps - ---------------- Validation of images compare existing
 * number images with existing number of page DocStructs if it is the same don't do anything if DocStructs are less add new pages to
 * physicalDocStruct if images are less delete pages from the end of pyhsicalDocStruct --------------------------------
 *
 * @return null
 * @throws TypeNotAllowedForParentException
 * @throws TypeNotAllowedForParentException
 * @throws InterruptedException
 * @throws IOException
 * @throws InterruptedException
 * @throws IOException
 * @throws DAOException
 * @throws SwapException
 */
public void createPagination(Process inProzess, String directory) throws TypeNotAllowedForParentException, IOException, InterruptedException, SwapException, DAOException {
    String mediaFolder = inProzess.getImagesTifDirectory(false);
    String mediaFolderWithFallback = inProzess.getImagesTifDirectory(true);
    DocStruct physicaldocstruct = this.mydocument.getPhysicalDocStruct();
    DocStruct logical = this.mydocument.getLogicalDocStruct();
    if (logical.getType().isAnchor()) {
        if (logical.getAllChildren() != null && logical.getAllChildren().size() > 0) {
            logical = logical.getAllChildren().get(0);
        }
    }
    MetadataType MDTypeForPath = this.myPrefs.getMetadataTypeByName("pathimagefiles");
    /*--------------------------------
         * der physische Baum wird nur
         * angelegt, wenn er noch nicht existierte
         * --------------------------------*/
    if (physicaldocstruct == null) {
        DocStructType dst = this.myPrefs.getDocStrctTypeByName("BoundBook");
        physicaldocstruct = this.mydocument.createDocStruct(dst);
        this.mydocument.setPhysicalDocStruct(physicaldocstruct);
    }
    // check for valid filepath
    try {
        List<? extends Metadata> filepath = physicaldocstruct.getAllMetadataByType(MDTypeForPath);
        if (filepath == null || filepath.isEmpty()) {
            Metadata mdForPath = new Metadata(MDTypeForPath);
            if (SystemUtils.IS_OS_WINDOWS) {
                mdForPath.setValue("file:/" + mediaFolder);
            } else {
                mdForPath.setValue("file://" + mediaFolder);
            }
            physicaldocstruct.addMetadata(mdForPath);
        }
    } catch (Exception e) {
        log.error(e);
    }
    Path folderToCheck = null;
    if (directory == null) {
        folderToCheck = Paths.get(mediaFolderWithFallback);
        checkIfImagesValid(inProzess.getTitel(), mediaFolderWithFallback);
    } else {
        folderToCheck = Paths.get(directory);
        if (!folderToCheck.isAbsolute()) {
            folderToCheck = Paths.get(inProzess.getImagesDirectory(), directory);
        }
        checkIfImagesValid(inProzess.getTitel(), folderToCheck.toString());
    }
    DocStructType typePage = this.myPrefs.getDocStrctTypeByName("page");
    DocStructType typeAudio = this.myPrefs.getDocStrctTypeByName("audio");
    DocStructType typeVideo = this.myPrefs.getDocStrctTypeByName("video");
    DocStructType type3dObject = this.myPrefs.getDocStrctTypeByName("object");
    // use fallback to 'page', if additional types are not configured in ruleset
    if (typeAudio == null) {
        typeAudio = typePage;
    }
    if (typeVideo == null) {
        typeVideo = typePage;
    }
    if (type3dObject == null) {
        type3dObject = typePage;
    }
    /*-------------------------------
         * retrieve existing pages/images
         * -------------------------------*/
    List<DocStruct> oldPages = physicaldocstruct.getAllChildren();
    if (oldPages == null) {
        oldPages = new ArrayList<>();
    }
    if (oldPages.size() == this.myLastImage) {
        return;
    }
    String defaultPagination = ConfigurationHelper.getInstance().getMetsEditorDefaultPagination();
    Map<String, DocStruct> assignedImages = new HashMap<>();
    List<DocStruct> pageElementsWithoutImages = new ArrayList<>();
    List<String> imagesWithoutPageElements = new ArrayList<>();
    if (physicaldocstruct.getAllChildren() != null && !physicaldocstruct.getAllChildren().isEmpty()) {
        List<String> imageFileList = null;
        imageFileList = StorageProvider.getInstance().list(folderToCheck.toString());
        Set<String> imageFileSet = new HashSet<>(imageFileList);
        for (DocStruct page : physicaldocstruct.getAllChildren()) {
            if (page.getImageName() != null) {
                if (imageFileSet.contains(page.getImageName())) {
                    assignedImages.put(page.getImageName(), page);
                } else {
                    try {
                        page.removeContentFile(page.getAllContentFiles().get(0));
                        pageElementsWithoutImages.add(page);
                    } catch (ContentFileNotLinkedException e) {
                        log.error(e);
                    }
                }
            } else {
                pageElementsWithoutImages.add(page);
            }
        }
    }
    try {
        List<String> imageNamesInMediaFolder = getDataFiles(inProzess, directory);
        if (imageNamesInMediaFolder != null && !imageNamesInMediaFolder.isEmpty()) {
            for (String imageName : imageNamesInMediaFolder) {
                if (!assignedImages.containsKey(imageName)) {
                    imagesWithoutPageElements.add(imageName);
                }
            }
        }
    } catch (InvalidImagesException e1) {
        log.error(e1);
    }
    // case 1: existing pages but no images (some images are removed)
    if (!pageElementsWithoutImages.isEmpty() && imagesWithoutPageElements.isEmpty()) {
        for (DocStruct pageToRemove : pageElementsWithoutImages) {
            physicaldocstruct.removeChild(pageToRemove);
            List<Reference> refs = new ArrayList<>(pageToRemove.getAllFromReferences());
            for (ugh.dl.Reference ref : refs) {
                DocStruct source = ref.getSource();
                for (Reference reference : source.getAllToReferences()) {
                    if (reference.getTarget().equals(pageToRemove)) {
                        source.getAllToReferences().remove(reference);
                        break;
                    }
                }
            }
        }
    } else // case 2: no page docs but images (some images are added)
    if (pageElementsWithoutImages.isEmpty() && !imagesWithoutPageElements.isEmpty()) {
        int currentPhysicalOrder = assignedImages.size();
        for (String newImage : imagesWithoutPageElements) {
            String mimetype = NIOFileUtils.getMimeTypeFromFile(Paths.get(newImage));
            DocStruct dsPage = null;
            if (mimetype.startsWith("image")) {
                dsPage = this.mydocument.createDocStruct(typePage);
            } else if (mimetype.startsWith("video") || mimetype.equals("application/mxf")) {
                dsPage = mydocument.createDocStruct(typeVideo);
            } else if (mimetype.startsWith("audio")) {
                dsPage = mydocument.createDocStruct(typeAudio);
            } else if (mimetype.startsWith("object")) {
                dsPage = mydocument.createDocStruct(type3dObject);
            } else if (mimetype.startsWith("model")) {
                dsPage = mydocument.createDocStruct(type3dObject);
            } else {
                // use old implementation as default
                dsPage = mydocument.createDocStruct(typePage);
            }
            try {
                // physical page no
                physicaldocstruct.addChild(dsPage);
                MetadataType mdt = this.myPrefs.getMetadataTypeByName("physPageNumber");
                Metadata mdTemp = new Metadata(mdt);
                mdTemp.setValue(String.valueOf(++currentPhysicalOrder));
                dsPage.addMetadata(mdTemp);
                // logical page no
                mdt = this.myPrefs.getMetadataTypeByName("logicalPageNumber");
                mdTemp = new Metadata(mdt);
                if (defaultPagination.equalsIgnoreCase("arabic")) {
                    mdTemp.setValue(String.valueOf(currentPhysicalOrder));
                } else if (defaultPagination.equalsIgnoreCase("roman")) {
                    RomanNumeral roman = new RomanNumeral();
                    roman.setValue(currentPhysicalOrder);
                    mdTemp.setValue(roman.getNumber());
                } else {
                    mdTemp.setValue("uncounted");
                }
                dsPage.addMetadata(mdTemp);
                logical.addReferenceTo(dsPage, "logical_physical");
                // image name
                ContentFile cf = new ContentFile();
                cf.setMimetype(mimetype);
                if (SystemUtils.IS_OS_WINDOWS) {
                    cf.setLocation("file:/" + mediaFolder + newImage);
                } else {
                    cf.setLocation("file://" + mediaFolder + newImage);
                }
                dsPage.addContentFile(cf);
            } catch (TypeNotAllowedAsChildException e) {
                log.error(e);
            } catch (MetadataTypeNotAllowedException e) {
                log.error(e);
            }
        }
    } else // case 3: empty page docs and unassinged images
    {
        for (DocStruct page : pageElementsWithoutImages) {
            if (!imagesWithoutPageElements.isEmpty()) {
                // assign new image name to page
                String newImageName = imagesWithoutPageElements.get(0);
                imagesWithoutPageElements.remove(0);
                ContentFile cf = new ContentFile();
                if (SystemUtils.IS_OS_WINDOWS) {
                    cf.setLocation("file:/" + mediaFolder + newImageName);
                } else {
                    cf.setLocation("file://" + mediaFolder + newImageName);
                }
                page.addContentFile(cf);
            } else {
                // remove page
                physicaldocstruct.removeChild(page);
                List<Reference> refs = new ArrayList<>(page.getAllFromReferences());
                for (ugh.dl.Reference ref : refs) {
                    ref.getSource().removeReferenceTo(page);
                }
            }
        }
        if (!imagesWithoutPageElements.isEmpty()) {
            // create new page elements
            int currentPhysicalOrder = physicaldocstruct.getAllChildren().size();
            for (String newImage : imagesWithoutPageElements) {
                String mimetype = NIOFileUtils.getMimeTypeFromFile(Paths.get(newImage));
                DocStruct dsPage = null;
                if (mimetype.startsWith("image")) {
                    dsPage = this.mydocument.createDocStruct(typePage);
                } else if (mimetype.startsWith("video") || mimetype.equals("application/mxf")) {
                    dsPage = mydocument.createDocStruct(typeVideo);
                } else if (mimetype.startsWith("audio")) {
                    dsPage = mydocument.createDocStruct(typeAudio);
                } else if (mimetype.startsWith("object")) {
                    dsPage = mydocument.createDocStruct(type3dObject);
                } else if (mimetype.startsWith("model")) {
                    dsPage = mydocument.createDocStruct(type3dObject);
                } else {
                    // use old implementation as default
                    dsPage = mydocument.createDocStruct(typePage);
                }
                try {
                    // physical page no
                    physicaldocstruct.addChild(dsPage);
                    MetadataType mdt = this.myPrefs.getMetadataTypeByName("physPageNumber");
                    Metadata mdTemp = new Metadata(mdt);
                    mdTemp.setValue(String.valueOf(++currentPhysicalOrder));
                    dsPage.addMetadata(mdTemp);
                    // logical page no
                    mdt = this.myPrefs.getMetadataTypeByName("logicalPageNumber");
                    mdTemp = new Metadata(mdt);
                    if (defaultPagination.equalsIgnoreCase("arabic")) {
                        mdTemp.setValue(String.valueOf(currentPhysicalOrder));
                    } else if (defaultPagination.equalsIgnoreCase("roman")) {
                        RomanNumeral roman = new RomanNumeral();
                        roman.setValue(currentPhysicalOrder);
                        mdTemp.setValue(roman.getNumber());
                    } else {
                        mdTemp.setValue("uncounted");
                    }
                    dsPage.addMetadata(mdTemp);
                    logical.addReferenceTo(dsPage, "logical_physical");
                    // image name
                    ContentFile cf = new ContentFile();
                    cf.setMimetype(mimetype);
                    if (SystemUtils.IS_OS_WINDOWS) {
                        cf.setLocation("file:/" + mediaFolder + newImage);
                    } else {
                        cf.setLocation("file://" + mediaFolder + newImage);
                    }
                    dsPage.addContentFile(cf);
                } catch (TypeNotAllowedAsChildException e) {
                    log.error(e);
                } catch (MetadataTypeNotAllowedException e) {
                    log.error(e);
                }
            }
        }
    }
    int currentPhysicalOrder = 1;
    MetadataType mdt = this.myPrefs.getMetadataTypeByName("physPageNumber");
    if (physicaldocstruct.getAllChildren() != null) {
        for (DocStruct page : physicaldocstruct.getAllChildren()) {
            List<? extends Metadata> pageNoMetadata = page.getAllMetadataByType(mdt);
            if (pageNoMetadata == null || pageNoMetadata.size() == 0) {
                currentPhysicalOrder++;
                break;
            }
            for (Metadata pageNo : pageNoMetadata) {
                pageNo.setValue(String.valueOf(currentPhysicalOrder));
            }
            currentPhysicalOrder++;
        }
    }
}
Also used : RomanNumeral(ugh.dl.RomanNumeral) ContentFile(ugh.dl.ContentFile) HashMap(java.util.HashMap) Metadata(ugh.dl.Metadata) ArrayList(java.util.ArrayList) ContentFileNotLinkedException(ugh.exceptions.ContentFileNotLinkedException) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) DocStruct(ugh.dl.DocStruct) HashSet(java.util.HashSet) Path(java.nio.file.Path) TypeNotAllowedAsChildException(ugh.exceptions.TypeNotAllowedAsChildException) Reference(ugh.dl.Reference) MetadataType(ugh.dl.MetadataType) Reference(ugh.dl.Reference) URISyntaxException(java.net.URISyntaxException) TypeNotAllowedAsChildException(ugh.exceptions.TypeNotAllowedAsChildException) DAOException(de.sub.goobi.helper.exceptions.DAOException) SwapException(de.sub.goobi.helper.exceptions.SwapException) ContentLibException(de.unigoettingen.sub.commons.contentlib.exceptions.ContentLibException) TypeNotAllowedForParentException(ugh.exceptions.TypeNotAllowedForParentException) ImageManipulatorException(de.unigoettingen.sub.commons.contentlib.exceptions.ImageManipulatorException) ImageManagerException(de.unigoettingen.sub.commons.contentlib.exceptions.ImageManagerException) IOException(java.io.IOException) MetadataTypeNotAllowedException(ugh.exceptions.MetadataTypeNotAllowedException) InvalidImagesException(de.sub.goobi.helper.exceptions.InvalidImagesException) ContentFileNotLinkedException(ugh.exceptions.ContentFileNotLinkedException) InvalidImagesException(de.sub.goobi.helper.exceptions.InvalidImagesException) DocStructType(ugh.dl.DocStructType) MetadataTypeNotAllowedException(ugh.exceptions.MetadataTypeNotAllowedException)

Aggregations

DAOException (de.sub.goobi.helper.exceptions.DAOException)1 InvalidImagesException (de.sub.goobi.helper.exceptions.InvalidImagesException)1 SwapException (de.sub.goobi.helper.exceptions.SwapException)1 ContentLibException (de.unigoettingen.sub.commons.contentlib.exceptions.ContentLibException)1 ImageManagerException (de.unigoettingen.sub.commons.contentlib.exceptions.ImageManagerException)1 ImageManipulatorException (de.unigoettingen.sub.commons.contentlib.exceptions.ImageManipulatorException)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ContentFile (ugh.dl.ContentFile)1 DocStruct (ugh.dl.DocStruct)1 DocStructType (ugh.dl.DocStructType)1 Metadata (ugh.dl.Metadata)1 MetadataType (ugh.dl.MetadataType)1 Reference (ugh.dl.Reference)1