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++;
}
}
}
Aggregations