Search in sources :

Example 1 with VirtualFileGroup

use of ugh.dl.VirtualFileGroup in project goobi-workflow by intranda.

the class ExportMets method collectMetadataToSave.

private ExportFileformat collectMetadataToSave(Process myProzess, Fileformat gdzfile, boolean writeLocalFilegroup, ConfigurationHelper config) throws IOException, InterruptedException, SwapException, DAOException, PreferencesException, TypeNotAllowedForParentException {
    ExportFileformat mm = MetadatenHelper.getExportFileformatByName(myProzess.getProjekt().getFileFormatDmsExport(), myProzess.getRegelsatz());
    mm.setWriteLocal(writeLocalFilegroup);
    mm.setCreateUUIDs(config.isExportCreateUUIDsAsFileIDs());
    String imageFolderPath = myProzess.getImagesTifDirectory(true);
    Path imageFolder = Paths.get(imageFolderPath);
    /*
         * before creating mets file, change relative path to absolute -
         */
    DigitalDocument dd = gdzfile.getDigitalDocument();
    MetadatenImagesHelper mih = new MetadatenImagesHelper(this.myPrefs, dd);
    if (dd.getFileSet() == null || dd.getFileSet().getAllFiles().isEmpty()) {
        Helper.setMeldung(myProzess.getTitel() + ": digital document does not contain images; adding them for mets file creation");
        mih.createPagination(myProzess, null);
        try {
            myProzess.writeMetadataFile(gdzfile);
        } catch (UGHException | IOException | InterruptedException | SwapException | DAOException e) {
            log.error(e);
        }
    } else {
        mih.checkImageNames(myProzess, imageFolder.getFileName().toString());
    }
    /*
         * get the topstruct element of the digital document depending on anchor property
         */
    DocStruct topElement = dd.getLogicalDocStruct();
    if (topElement.getType().isAnchor()) {
        if (topElement.getAllChildren() == null || topElement.getAllChildren().size() == 0) {
            throw new PreferencesException(myProzess.getTitel() + ": the topstruct element is marked as anchor, but does not have any children for physical docstrucs");
        } else {
            topElement = topElement.getAllChildren().get(0);
        }
    }
    if (config.isExportValidateImages()) {
        if (topElement.getAllToReferences("logical_physical") == null || topElement.getAllToReferences("logical_physical").size() == 0) {
            if (dd.getPhysicalDocStruct() != null && dd.getPhysicalDocStruct().getAllChildren() != null) {
                Helper.setMeldung(myProzess.getTitel() + ": topstruct element does not have any referenced images yet; temporarily adding them for mets file creation");
                for (DocStruct mySeitenDocStruct : dd.getPhysicalDocStruct().getAllChildren()) {
                    topElement.addReferenceTo(mySeitenDocStruct, "logical_physical");
                }
            } else {
                Helper.setFehlerMeldung(myProzess.getTitel() + ": could not find any referenced images, export aborted");
                dd = null;
                return null;
            }
        }
        for (ContentFile cf : dd.getFileSet().getAllFiles()) {
            String location = cf.getLocation();
            // use the file protocol.
            if (!location.contains("://")) {
                if (!location.matches("^[A-Z]:.*") && !location.matches("^\\/.*")) {
                    // is a relative path
                    Path f = Paths.get(imageFolder.toString(), location);
                    location = f.toString();
                }
                location = "file://" + location;
            }
            cf.setLocation(location);
        }
    }
    mm.setDigitalDocument(dd);
    // if configured, extract metadata from files and store them as techMd premis
    if (config.isExportCreateTechnicalMetadata()) {
        int counter = 1;
        for (DocStruct page : dd.getPhysicalDocStruct().getAllChildren()) {
            Path path = Paths.get(page.getImageName());
            if (!path.isAbsolute()) {
                path = Paths.get(imageFolder.toString(), page.getImageName());
            }
            Element techMd = createTechMd(path);
            if (techMd != null) {
                Md md = new Md(techMd);
                md.setType("techMD");
                md.setId(String.format("AMD_%04d", counter++));
                dd.addTechMd(md);
                page.setAdmId(md.getId());
            }
        }
    }
    Map<String, String> additionalMetadataMap = config.getExportWriteAdditionalMetadata();
    if (!additionalMetadataMap.isEmpty()) {
        String projectMetadataName = additionalMetadataMap.get("Project");
        String institutionMetadataName = additionalMetadataMap.get("Institution");
        Prefs prefs = myProzess.getRegelsatz().getPreferences();
        if (StringUtils.isNotBlank(projectMetadataName)) {
            MetadataType mdt = prefs.getMetadataTypeByName(projectMetadataName);
            if (mdt != null) {
                try {
                    ugh.dl.Metadata md = new ugh.dl.Metadata(mdt);
                    md.setValue(myProzess.getProjekt().getTitel());
                    topElement.addMetadata(md);
                } catch (MetadataTypeNotAllowedException e) {
                    log.warn("Configured metadata for project name is unknown or not allowed.");
                }
                if (topElement.getParent() != null) {
                    try {
                        ugh.dl.Metadata md = new ugh.dl.Metadata(mdt);
                        md.setValue(myProzess.getProjekt().getTitel());
                        topElement.getParent().addMetadata(md);
                    } catch (MetadataTypeNotAllowedException e) {
                        log.warn("Configured metadata for project name is unknown or not allowed.");
                    }
                }
            }
        }
        if (StringUtils.isNotBlank(institutionMetadataName)) {
            MetadataType mdt = prefs.getMetadataTypeByName(institutionMetadataName);
            if (mdt != null) {
                try {
                    ugh.dl.Metadata md = new ugh.dl.Metadata(mdt);
                    md.setValue(myProzess.getProjekt().getInstitution().getLongName());
                    topElement.addMetadata(md);
                } catch (MetadataTypeNotAllowedException e) {
                    log.warn("Configured metadata for institution name is unknown or not allowed.");
                }
                if (topElement.getParent() != null) {
                    try {
                        ugh.dl.Metadata md = new ugh.dl.Metadata(mdt);
                        md.setValue(myProzess.getProjekt().getInstitution().getLongName());
                        topElement.getParent().addMetadata(md);
                    } catch (MetadataTypeNotAllowedException e) {
                        log.warn("Configured metadata for institution name is unknown or not allowed.");
                    }
                }
            }
        }
    }
    /*
         * -------------------------------- wenn Filegroups definiert wurden, werden diese jetzt in die Metsstruktur übernommen
         * --------------------------------
         */
    // Replace all pathes with the given VariableReplacer, also the file
    // group pathes!
    VariableReplacer vp = new VariableReplacer(mm.getDigitalDocument(), this.myPrefs, myProzess, null);
    List<ProjectFileGroup> myFilegroups = myProzess.getProjekt().getFilegroups();
    boolean useOriginalFiles = false;
    if (myFilegroups != null && myFilegroups.size() > 0) {
        for (ProjectFileGroup pfg : myFilegroups) {
            if (pfg.isUseOriginalFiles()) {
                useOriginalFiles = true;
            }
            // check if source files exists
            if (pfg.getFolder() != null && pfg.getFolder().length() > 0) {
                String foldername = myProzess.getMethodFromName(pfg.getFolder());
                if (foldername != null) {
                    Path folder = Paths.get(myProzess.getMethodFromName(pfg.getFolder()));
                    if (folder != null && StorageProvider.getInstance().isFileExists(folder) && !StorageProvider.getInstance().list(folder.toString()).isEmpty()) {
                        VirtualFileGroup v = createFilegroup(vp, pfg);
                        mm.getDigitalDocument().getFileSet().addVirtualFileGroup(v);
                    }
                }
            } else {
                VirtualFileGroup v = createFilegroup(vp, pfg);
                mm.getDigitalDocument().getFileSet().addVirtualFileGroup(v);
            }
        }
    }
    if (useOriginalFiles) {
        // check if media folder contains images
        List<Path> filesInFolder = StorageProvider.getInstance().listFiles(myProzess.getImagesTifDirectory(false));
        if (!filesInFolder.isEmpty()) {
            // compare image names with files in mets file
            List<DocStruct> pages = dd.getPhysicalDocStruct().getAllChildren();
            if (pages != null && pages.size() > 0) {
                for (DocStruct page : pages) {
                    Path completeNameInMets = Paths.get(page.getImageName());
                    String filenameInMets = completeNameInMets.getFileName().toString();
                    int dotIndex = filenameInMets.lastIndexOf('.');
                    if (dotIndex != -1) {
                        filenameInMets = filenameInMets.substring(0, dotIndex);
                    }
                    for (Path imageNameInFolder : filesInFolder) {
                        String imageName = imageNameInFolder.getFileName().toString();
                        dotIndex = imageName.lastIndexOf('.');
                        if (dotIndex != -1) {
                            imageName = imageName.substring(0, dotIndex);
                        }
                        if (filenameInMets.toLowerCase().equals(imageName.toLowerCase())) {
                            // found matching filename
                            page.setImageName(imageNameInFolder.toString());
                            break;
                        }
                    }
                }
            // replace filename in mets file
            }
        }
    }
    // Replace rights and digiprov entries.
    mm.setRightsOwner(vp.replace(myProzess.getProjekt().getMetsRightsOwner()));
    mm.setRightsOwnerLogo(vp.replace(myProzess.getProjekt().getMetsRightsOwnerLogo()));
    mm.setRightsOwnerSiteURL(vp.replace(myProzess.getProjekt().getMetsRightsOwnerSite()));
    mm.setRightsOwnerContact(vp.replace(myProzess.getProjekt().getMetsRightsOwnerMail()));
    mm.setDigiprovPresentation(vp.replace(myProzess.getProjekt().getMetsDigiprovPresentation()));
    mm.setDigiprovReference(vp.replace(myProzess.getProjekt().getMetsDigiprovReference()));
    mm.setDigiprovPresentationAnchor(vp.replace(myProzess.getProjekt().getMetsDigiprovPresentationAnchor()));
    mm.setDigiprovReferenceAnchor(vp.replace(myProzess.getProjekt().getMetsDigiprovReferenceAnchor()));
    mm.setMetsRightsLicense(vp.replace(myProzess.getProjekt().getMetsRightsLicense()));
    mm.setMetsRightsSponsor(vp.replace(myProzess.getProjekt().getMetsRightsSponsor()));
    mm.setMetsRightsSponsorLogo(vp.replace(myProzess.getProjekt().getMetsRightsSponsorLogo()));
    mm.setMetsRightsSponsorSiteURL(vp.replace(myProzess.getProjekt().getMetsRightsSponsorSiteURL()));
    mm.setPurlUrl(vp.replace(myProzess.getProjekt().getMetsPurl()));
    mm.setContentIDs(vp.replace(myProzess.getProjekt().getMetsContentIDs()));
    String pointer = myProzess.getProjekt().getMetsPointerPath();
    pointer = vp.replace(pointer);
    mm.setMptrUrl(pointer);
    String anchor = myProzess.getProjekt().getMetsPointerPathAnchor();
    pointer = vp.replace(anchor);
    mm.setMptrAnchorUrl(pointer);
    mm.setGoobiID(String.valueOf(myProzess.getId()));
    mm.setIIIFUrl(vp.replace(myProzess.getProjekt().getMetsIIIFUrl()));
    mm.setSruUrl(vp.replace(myProzess.getProjekt().getMetsSruUrl()));
    // if (!ConfigMain.getParameter("ImagePrefix", "\\d{8}").equals("\\d{8}")) {
    List<String> images = new ArrayList<>();
    if (config.isExportValidateImages()) {
        try {
            images = new MetadatenImagesHelper(this.myPrefs, dd).getDataFiles(myProzess, imageFolderPath);
            int sizeOfPagination = dd.getPhysicalDocStruct().getAllChildren().size();
            if (images != null) {
                int sizeOfImages = images.size();
                if (sizeOfPagination == sizeOfImages) {
                    dd.overrideContentFiles(images);
                } else {
                    String[] param = { String.valueOf(sizeOfPagination), String.valueOf(sizeOfImages) };
                    Helper.setFehlerMeldung(Helper.getTranslation("imagePaginationError", param));
                    return null;
                }
            }
        } catch (IndexOutOfBoundsException e) {
            log.error(e);
            return null;
        } catch (InvalidImagesException e) {
            log.error(e);
            return null;
        }
    } else {
        // create pagination out of virtual file names
        dd.addAllContentFiles();
    }
    return mm;
}
Also used : ContentFile(ugh.dl.ContentFile) ProjectFileGroup(org.goobi.beans.ProjectFileGroup) SwapException(de.sub.goobi.helper.exceptions.SwapException) Element(org.w3c.dom.Element) Metadata(com.drew.metadata.Metadata) ArrayList(java.util.ArrayList) VariableReplacer(de.sub.goobi.helper.VariableReplacer) Prefs(ugh.dl.Prefs) DAOException(de.sub.goobi.helper.exceptions.DAOException) MetadatenImagesHelper(de.sub.goobi.metadaten.MetadatenImagesHelper) VirtualFileGroup(ugh.dl.VirtualFileGroup) DocStruct(ugh.dl.DocStruct) Path(java.nio.file.Path) MetadataType(ugh.dl.MetadataType) UGHException(ugh.exceptions.UGHException) IOException(java.io.IOException) DigitalDocument(ugh.dl.DigitalDocument) InvalidImagesException(de.sub.goobi.helper.exceptions.InvalidImagesException) MetadataTypeNotAllowedException(ugh.exceptions.MetadataTypeNotAllowedException) Md(ugh.dl.Md) PreferencesException(ugh.exceptions.PreferencesException) ExportFileformat(ugh.dl.ExportFileformat)

Example 2 with VirtualFileGroup

use of ugh.dl.VirtualFileGroup in project goobi-workflow by intranda.

the class ExportMets method createFilegroup.

private VirtualFileGroup createFilegroup(VariableReplacer variableRplacer, ProjectFileGroup projectFileGroup) {
    VirtualFileGroup v = new VirtualFileGroup();
    v.setName(projectFileGroup.getName());
    v.setPathToFiles(variableRplacer.replace(projectFileGroup.getPath()));
    v.setMimetype(projectFileGroup.getMimetype());
    // check for null to stop NullPointerException
    String projectFileSuffix = projectFileGroup.getSuffix();
    if (projectFileSuffix != null) {
        v.setFileSuffix(projectFileSuffix.trim());
    } else {
        v.setFileSuffix(projectFileSuffix);
    }
    v.setFileExtensionsToIgnore(projectFileGroup.getIgnoreMimetypes());
    v.setIgnoreConfiguredMimetypeAndSuffix(projectFileGroup.isUseOriginalFiles());
    if (projectFileGroup.getName().equals("PRESENTATION")) {
        v.setMainGroup(true);
    }
    return v;
}
Also used : VirtualFileGroup(ugh.dl.VirtualFileGroup)

Aggregations

VirtualFileGroup (ugh.dl.VirtualFileGroup)2 Metadata (com.drew.metadata.Metadata)1 VariableReplacer (de.sub.goobi.helper.VariableReplacer)1 DAOException (de.sub.goobi.helper.exceptions.DAOException)1 InvalidImagesException (de.sub.goobi.helper.exceptions.InvalidImagesException)1 SwapException (de.sub.goobi.helper.exceptions.SwapException)1 MetadatenImagesHelper (de.sub.goobi.metadaten.MetadatenImagesHelper)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 ProjectFileGroup (org.goobi.beans.ProjectFileGroup)1 Element (org.w3c.dom.Element)1 ContentFile (ugh.dl.ContentFile)1 DigitalDocument (ugh.dl.DigitalDocument)1 DocStruct (ugh.dl.DocStruct)1 ExportFileformat (ugh.dl.ExportFileformat)1 Md (ugh.dl.Md)1 MetadataType (ugh.dl.MetadataType)1 Prefs (ugh.dl.Prefs)1 MetadataTypeNotAllowedException (ugh.exceptions.MetadataTypeNotAllowedException)1