physicaldocstruct = this.mydocument.createDocStruct(dst);
/*--------------------------------
* Probleme mit dem FilePath
* -------------------------------- */
MetadataType MDTypeForPath = this.myPrefs.getMetadataTypeByName("pathimagefiles");
try {
Metadata mdForPath = new Metadata(MDTypeForPath);
if (SystemUtils.IS_OS_WINDOWS) {
mdForPath.setValue("file:/" + inProzess.getImagesTifDirectory(false));
} else {
mdForPath.setValue("file://" + inProzess.getImagesTifDirectory(false));
}
physicaldocstruct.addMetadata(mdForPath);
} catch (MetadataTypeNotAllowedException e1) {
} catch (DocStructHasNoTypeException e1) {
}
this.mydocument.setPhysicalDocStruct(physicaldocstruct);
}
if (directory == null) {
checkIfImagesValid(inProzess.getTitel(), inProzess.getImagesTifDirectory(true));
} else {
checkIfImagesValid(inProzess.getTitel(), inProzess.getImagesDirectory() + directory);
}
/*-------------------------------
* retrieve existing pages/images
* -------------------------------*/
DocStructType newPage = this.myPrefs.getDocStrctTypeByName("page");
List<DocStruct> oldPages = physicaldocstruct.getAllChildrenByTypeAndMetadataType("page", "*");
if (oldPages == null) {
oldPages = new ArrayList<DocStruct>();
}
/*--------------------------------
* add new page/images if necessary
* --------------------------------*/
if (oldPages.size() == this.myLastImage) {
return;
}
String defaultPagination = ConfigMain.getParameter("MetsEditorDefaultPagination", "uncounted");
Map<String, DocStruct> assignedImages = new HashMap<String, DocStruct>();
List<DocStruct> pageElementsWithoutImages = new ArrayList<DocStruct>();
List<String> imagesWithoutPageElements = new ArrayList<String>();
if (physicaldocstruct.getAllChildren() != null && !physicaldocstruct.getAllChildren().isEmpty()) {
for (DocStruct page : physicaldocstruct.getAllChildren()) {
if (page.getImageName() != null) {
File imageFile = null;
if (directory == null) {
imageFile = new File(inProzess.getImagesTifDirectory(true), page.getImageName());
} else {
imageFile = new File(inProzess.getImagesDirectory() + directory, page.getImageName());
}
if (imageFile.exists()) {
assignedImages.put(page.getImageName(), page);
} else {
try {
page.removeContentFile(page.getAllContentFiles().get(0));
pageElementsWithoutImages.add(page);
} catch (ContentFileNotLinkedException e) {
logger.error(e);
}
}
} else {
pageElementsWithoutImages.add(page);
}
}
}
try {
List<String> imageNamesInMediaFolder = getDataFiles(inProzess);
for (String imageName : imageNamesInMediaFolder) {
if (!assignedImages.containsKey(imageName)) {
imagesWithoutPageElements.add(imageName);
}
}
} catch (InvalidImagesException e1) {
logger.error(e1);
}
// handle possible cases
// 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<Reference>(pageToRemove.getAllFromReferences());
for (ugh.dl.Reference ref : refs) {
ref.getSource().removeReferenceTo(pageToRemove);
}
}
}
// case 2: no page docs but images (some images are added)
else if (pageElementsWithoutImages.isEmpty() && !imagesWithoutPageElements.isEmpty()) {
int currentPhysicalOrder = assignedImages.size();
for (String newImage : imagesWithoutPageElements) {
DocStruct dsPage = this.mydocument.createDocStruct(newPage);
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);
log.addReferenceTo(dsPage, "logical_physical");
// image name
ContentFile cf = new ContentFile();
if (SystemUtils.IS_OS_WINDOWS) {
cf.setLocation("file:/" + inProzess.getImagesTifDirectory(false) + newImage);
} else {
cf.setLocation("file://" + inProzess.getImagesTifDirectory(false) + newImage);
}
dsPage.addContentFile(cf);
} catch (TypeNotAllowedAsChildException e) {
logger.error(e);
} catch (MetadataTypeNotAllowedException e) {
logger.error(e);
}
}
}
// case 3: empty page docs and unassinged images
else {
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:/" + inProzess.getImagesTifDirectory(false) + newImageName);
} else {
cf.setLocation("file://" + inProzess.getImagesTifDirectory(false) + newImageName);
}
page.addContentFile(cf);
} else {
// remove page
physicaldocstruct.removeChild(page);
List<Reference> refs = new ArrayList<Reference>(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) {
DocStruct dsPage = this.mydocument.createDocStruct(newPage);
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);
log.addReferenceTo(dsPage, "logical_physical");
// image name
ContentFile cf = new ContentFile();
if (SystemUtils.IS_OS_WINDOWS) {
cf.setLocation("file:/" + inProzess.getImagesTifDirectory(false) + newImage);
} else {
cf.setLocation("file://" + inProzess.getImagesTifDirectory(false) + newImage);
}
dsPage.addContentFile(cf);
} catch (TypeNotAllowedAsChildException e) {
logger.error(e);
} catch (MetadataTypeNotAllowedException e) {
logger.error(e);
}
}
}
}
int currentPhysicalOrder = 1;
MetadataType mdt = this.myPrefs.getMetadataTypeByName("physPageNumber");
if (physicaldocstruct.getAllChildrenByTypeAndMetadataType("page", "*") != null) {
if (physicaldocstruct.getAllChildrenByTypeAndMetadataType("page", "*") != null) {
for (DocStruct page : physicaldocstruct.getAllChildrenByTypeAndMetadataType("page", "*")) {
List<? extends Metadata> pageNoMetadata = page.getAllMetadataByType(mdt);
if (pageNoMetadata == null || pageNoMetadata.size() == 0) {