use of ugh.dl.Fileformat in project goobi-workflow by intranda.
the class Metadaten method AddMetadaFromOpacPpn.
/**
* eine PPN aus dem Opac abfragen und dessen Metadaten dem aktuellen Strukturelement zuweisen
* ================================================================
*/
public String AddMetadaFromOpacPpn() {
StringTokenizer tokenizer = new StringTokenizer(this.additionalOpacPpns, "\n");
while (tokenizer.hasMoreTokens()) {
String tok = tokenizer.nextToken();
try {
ConfigOpacCatalogue coc = ConfigOpac.getInstance().getCatalogueByName(getOpacKatalog());
IOpacPlugin iopac = (IOpacPlugin) PluginLoader.getPluginByTitle(PluginType.Opac, coc.getOpacType());
Fileformat addrdf = iopac.search(this.opacSuchfeld, tok, coc, this.myPrefs);
if (addrdf != null) {
// remove empty default elements
List<Metadata> metadataList = this.myDocStruct.getAllMetadata();
if (metadataList != null) {
List<Metadata> metadataListClone = new ArrayList<>(metadataList);
for (Metadata md : metadataListClone) {
if (md.getValue() == null || md.getValue().isEmpty()) {
this.myDocStruct.removeMetadata(md, true);
}
}
}
List<Person> personList = myDocStruct.getAllPersons();
if (personList != null) {
List<Person> personListClone = new ArrayList<>(personList);
for (Person p : personListClone) {
if (p.getFirstname().isEmpty() && p.getLastname().isEmpty()) {
myDocStruct.removePerson(p);
}
}
}
/* die Liste aller erlaubten Metadatenelemente erstellen */
List<String> erlaubte = new ArrayList<>();
for (MetadataType mt : this.myDocStruct.getAddableMetadataTypes(false)) {
erlaubte.add(mt.getName());
}
/*
* wenn der Metadatentyp in der Liste der erlaubten Typen, dann hinzufügen
*/
for (Metadata m : addrdf.getDigitalDocument().getLogicalDocStruct().getAllMetadata()) {
if (erlaubte.contains(m.getType().getName())) {
this.myDocStruct.addMetadata(m);
}
}
for (Person m : addrdf.getDigitalDocument().getLogicalDocStruct().getAllPersons()) {
if (erlaubte.contains(m.getType().getName())) {
this.myDocStruct.addPerson(m);
}
}
for (MetadataGroup m : addrdf.getDigitalDocument().getLogicalDocStruct().getAllMetadataGroups()) {
if (myDocStruct.getAddableMetadataGroupTypes().contains(m.getType())) {
myDocStruct.addMetadataGroup(m);
}
}
MetadatenalsTree3Einlesen1(this.tree3, this.currentTopstruct, false);
} else {
Helper.setMeldung(null, "Opac abgefragt: ", "kein Ergebnis");
}
} catch (Exception e) {
log.error("Error while importing from catalogue: " + e.getMessage());
}
}
MetadatenalsBeanSpeichern(this.myDocStruct);
this.modusAnsicht = "Metadaten";
return "";
}
use of ugh.dl.Fileformat in project goobi-workflow by intranda.
the class XmlArtikelZaehlen method getNumberOfUghElements.
/**
* Anzahl der Strukturelemente ermitteln
*
* @param myProzess
*/
public int getNumberOfUghElements(Process myProzess, CountType inType) {
int rueckgabe = 0;
/* --------------------------------
* Dokument einlesen
* --------------------------------*/
Fileformat gdzfile;
try {
gdzfile = myProzess.readMetadataFile();
} catch (Exception e) {
Helper.setFehlerMeldung("xml error", e.getMessage());
return -1;
}
/* --------------------------------
* DocStruct rukursiv durchlaufen
* --------------------------------*/
DigitalDocument mydocument = null;
try {
mydocument = gdzfile.getDigitalDocument();
DocStruct logicalTopstruct = mydocument.getLogicalDocStruct();
rueckgabe += getNumberOfUghElements(logicalTopstruct, inType);
} catch (PreferencesException e1) {
Helper.setFehlerMeldung("[" + myProzess.getId() + "] Can not get DigitalDocument: ", e1.getMessage());
log.error(e1);
rueckgabe = 0;
}
/* --------------------------------
* die ermittelte Zahl im Prozess speichern
* --------------------------------*/
myProzess.setSortHelperArticles(Integer.valueOf(rueckgabe));
try {
ProcessManager.saveProcess(myProzess);
} catch (DAOException e) {
log.error(e);
}
return rueckgabe;
}
use of ugh.dl.Fileformat in project goobi-workflow by intranda.
the class ExportPdf method startExport.
@Override
public boolean startExport(Process myProzess, String inZielVerzeichnis) throws IOException, InterruptedException, PreferencesException, WriteException, DocStructHasNoTypeException, MetadataTypeNotAllowedException, ExportFileException, UghHelperException, ReadException, SwapException, DAOException, TypeNotAllowedForParentException {
/*
* -------------------------------- Read Document --------------------------------
*/
Fileformat gdzfile = myProzess.readMetadataFile();
String zielVerzeichnis = prepareUserDirectory(inZielVerzeichnis);
this.myPrefs = myProzess.getRegelsatz().getPreferences();
/*
* -------------------------------- first of all write mets-file in images-Folder of process --------------------------------
*/
Path metsTempFile = StorageProvider.getInstance().createTemporaryFile(myProzess.getTitel(), ".xml");
writeMetsFile(myProzess, metsTempFile.toString(), gdzfile, true);
Helper.setMeldung(null, myProzess.getTitel() + ": ", "mets file created");
Helper.setMeldung(null, myProzess.getTitel() + ": ", "start pdf generation now");
log.debug("METS file created: " + metsTempFile);
FacesContext context = FacesContextHelper.getCurrentFacesContext();
HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getRequest();
String fullpath = req.getRequestURL().toString();
String servletpath = context.getExternalContext().getRequestServletPath();
String myBasisUrl = fullpath.substring(0, fullpath.indexOf(servletpath));
Path imagesPath = Paths.get(myProzess.getImagesTifDirectory(true));
if (!StorageProvider.getInstance().isFileExists(imagesPath) || StorageProvider.getInstance().list(imagesPath.toString(), NIOFileUtils.imageNameFilter).isEmpty()) {
imagesPath = Paths.get(myProzess.getImagesOrigDirectory(true));
}
Path pdfPath = Paths.get(myProzess.getOcrPdfDirectory());
Path altoPath = Paths.get(myProzess.getOcrAltoDirectory());
if (!ConfigurationHelper.getInstance().isPdfAsDownload()) {
/*
* -------------------------------- use contentserver api for creation of pdf-file --------------------------------
*/
CreatePdfFromServletThread pdf = new CreatePdfFromServletThread();
pdf.setMetsURL(metsTempFile.toUri().toURL());
pdf.setTargetFolder(Paths.get(zielVerzeichnis));
pdf.setInternalServletPath(myBasisUrl);
pdf.setImagePath(imagesPath);
pdf.setPdfPath(pdfPath);
pdf.setAltoPath(altoPath);
log.debug("Taget directory: " + zielVerzeichnis);
log.debug("Using ContentServer2 base URL: " + myBasisUrl);
pdf.initialize(myProzess);
pdf.start();
} else {
try {
/*
* -------------------------------- define path for mets and pdfs --------------------------------
*/
URL goobiContentServerUrl = null;
String contentServerUrl = ConfigurationHelper.getInstance().getGoobiContentServerUrl();
String imageSource = "&imageSource=" + imagesPath.toUri();
String pdfSource = "&pdfSource=" + pdfPath.toUri();
String altoSource = "&altoSource=" + altoPath.toUri();
if (StorageProvider.getInstance().isFileExists(metsTempFile)) {
/* if no contentserverurl defined use internal goobiContentServerServlet */
if (contentServerUrl == null || contentServerUrl.length() == 0) {
contentServerUrl = myBasisUrl + "/gcs/gcs?action=pdf&metsFile=";
}
goobiContentServerUrl = new URL(contentServerUrl + metsTempFile.toUri().toURL() + imageSource + pdfSource + altoSource + "&targetFileName=" + myProzess.getTitel() + ".pdf");
/*
* -------------------------------- mets data does not exist or is invalid --------------------------------
*/
} else {
if (contentServerUrl == null || contentServerUrl.length() == 0) {
contentServerUrl = myBasisUrl + "/cs/cs?action=pdf&images=";
}
String url = "";
List<Path> meta = StorageProvider.getInstance().listFiles(myProzess.getImagesTifDirectory(true), NIOFileUtils.imageNameFilter);
ArrayList<String> filenames = new ArrayList<>();
for (Path data : meta) {
String file = "";
file += data.toUri().toURL();
filenames.add(file);
}
Collections.sort(filenames, new MetadatenHelper(null, null));
for (String f : filenames) {
url = url + f + "$";
}
String imageString = url.substring(0, url.length() - 1);
String targetFileName = "&targetFileName=" + myProzess.getTitel() + ".pdf";
goobiContentServerUrl = new URL(contentServerUrl + imageString + imageSource + pdfSource + altoSource + targetFileName);
}
if (!context.getResponseComplete()) {
log.debug("Redirecting pdf request to " + goobiContentServerUrl.toString());
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
String fileName = myProzess.getTitel() + ".pdf";
ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
String contentType = servletContext.getMimeType(fileName);
response.setContentType(contentType);
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
response.sendRedirect(goobiContentServerUrl.toString());
context.responseComplete();
}
// if (Files.exists(metsTempFile)) {
// Path tempMets = Paths.get(metsTempFile.toUri().toURL().toString());
// Files.delete(metsTempFile);
// }
} catch (Exception e) {
/*
* -------------------------------- report Error to User as Error-Log --------------------------------
*/
Writer output = null;
String text = "error while pdf creation: " + e.getMessage();
Path file = Paths.get(zielVerzeichnis, myProzess.getTitel() + ".PDF-ERROR.log");
try {
log.error(e);
output = new BufferedWriter(new FileWriter(file.toFile()));
output.write(text);
output.close();
} catch (IOException e1) {
}
return false;
}
}
return true;
}
use of ugh.dl.Fileformat in project goobi-workflow by intranda.
the class ExportDms method startExport.
/**
* DMS-Export an eine gewünschte Stelle
*
* @param myProzess
* @param zielVerzeichnis
* @throws InterruptedException
* @throws IOException
* @throws WriteException
* @throws PreferencesException
* @throws UghHelperException
* @throws ExportFileException
* @throws MetadataTypeNotAllowedException
* @throws DocStructHasNoTypeException
* @throws DAOException
* @throws SwapException
* @throws TypeNotAllowedForParentException
*/
@Override
public boolean startExport(Process myProzess, String inZielVerzeichnis) throws IOException, InterruptedException, WriteException, PreferencesException, DocStructHasNoTypeException, MetadataTypeNotAllowedException, ExportFileException, UghHelperException, SwapException, DAOException, TypeNotAllowedForParentException {
this.myPrefs = myProzess.getRegelsatz().getPreferences();
String atsPpnBand = myProzess.getTitel();
/*
* -------------------------------- Dokument einlesen --------------------------------
*/
Fileformat gdzfile;
// Fileformat newfile;
ExportFileformat newfile = MetadatenHelper.getExportFileformatByName(myProzess.getProjekt().getFileFormatDmsExport(), myProzess.getRegelsatz());
try {
gdzfile = myProzess.readMetadataFile();
newfile.setDigitalDocument(gdzfile.getDigitalDocument());
gdzfile = newfile;
} catch (Exception e) {
Helper.setFehlerMeldung(Helper.getTranslation("exportError") + myProzess.getTitel(), e);
log.error("Export abgebrochen, xml-LeseFehler", e);
problems.add("Export cancelled: " + e.getMessage());
return false;
}
trimAllMetadata(gdzfile.getDigitalDocument().getLogicalDocStruct());
VariableReplacer replacer = new VariableReplacer(gdzfile.getDigitalDocument(), this.myPrefs, myProzess, null);
if (ConfigurationHelper.getInstance().isUseMetadataValidation()) {
MetadatenVerifizierung mv = new MetadatenVerifizierung();
if (!mv.validate(gdzfile, this.myPrefs, myProzess)) {
problems.add("Export cancelled because of validation errors");
problems.addAll(mv.getProblems());
return false;
}
}
/*
* -------------------------------- Speicherort vorbereiten und downloaden --------------------------------
*/
String zielVerzeichnis;
Path benutzerHome;
if (myProzess.getProjekt().isUseDmsImport()) {
zielVerzeichnis = myProzess.getProjekt().getDmsImportImagesPath();
zielVerzeichnis = replacer.replace(zielVerzeichnis);
benutzerHome = Paths.get(zielVerzeichnis);
/* ggf. noch einen Vorgangsordner anlegen */
if (myProzess.getProjekt().isDmsImportCreateProcessFolder()) {
benutzerHome = Paths.get(benutzerHome.toString(), myProzess.getTitel());
zielVerzeichnis = benutzerHome.toString();
/* alte Import-Ordner löschen */
if (!StorageProvider.getInstance().deleteDir(benutzerHome)) {
Helper.setFehlerMeldung("Export canceled, Process: " + myProzess.getTitel(), "Import folder could not be cleared");
problems.add("Export cancelled: Import folder could not be cleared.");
return false;
}
/* alte Success-Ordner löschen */
String successPath = myProzess.getProjekt().getDmsImportSuccessPath();
successPath = replacer.replace(successPath);
Path successFile = Paths.get(successPath, myProzess.getTitel());
if (!StorageProvider.getInstance().deleteDir(successFile)) {
Helper.setFehlerMeldung("Export canceled, Process: " + myProzess.getTitel(), "Success folder could not be cleared");
problems.add("Export cancelled: Success folder could not be cleared.");
return false;
}
/* alte Error-Ordner löschen */
String importPath = myProzess.getProjekt().getDmsImportErrorPath();
importPath = replacer.replace(importPath);
Path errorfile = Paths.get(importPath, myProzess.getTitel());
if (!StorageProvider.getInstance().deleteDir(errorfile)) {
Helper.setFehlerMeldung("Export canceled, Process: " + myProzess.getTitel(), "Error folder could not be cleared");
problems.add("Export cancelled: Error folder could not be cleared.");
return false;
}
if (!StorageProvider.getInstance().isFileExists(benutzerHome)) {
StorageProvider.getInstance().createDirectories(benutzerHome);
}
}
} else {
zielVerzeichnis = inZielVerzeichnis + atsPpnBand;
zielVerzeichnis = replacer.replace(zielVerzeichnis) + FileSystems.getDefault().getSeparator();
// wenn das Home existiert, erst löschen und dann neu anlegen
benutzerHome = Paths.get(zielVerzeichnis);
if (!StorageProvider.getInstance().deleteDir(benutzerHome)) {
Helper.setFehlerMeldung("Export canceled: " + myProzess.getTitel(), "Could not delete home directory");
problems.add("Export cancelled: Could not delete home directory.");
return false;
}
prepareUserDirectory(zielVerzeichnis);
}
/*
* -------------------------------- der eigentliche Download der Images --------------------------------
*/
try {
if (this.exportWithImages) {
imageDownload(myProzess, benutzerHome, atsPpnBand, DIRECTORY_SUFFIX);
}
if (this.exportFulltext) {
fulltextDownload(myProzess, benutzerHome, atsPpnBand, DIRECTORY_SUFFIX);
}
String ed = myProzess.getExportDirectory();
Path exportFolder = Paths.get(ed);
if (StorageProvider.getInstance().isFileExists(exportFolder) && StorageProvider.getInstance().isDirectory(exportFolder)) {
List<Path> filesInExportFolder = StorageProvider.getInstance().listFiles(ed);
for (Path exportFile : filesInExportFolder) {
if (StorageProvider.getInstance().isDirectory(exportFile) && !StorageProvider.getInstance().list(exportFile.toString()).isEmpty()) {
if (!exportFile.getFileName().toString().matches(".+\\.\\d+")) {
String suffix = exportFile.getFileName().toString().substring(exportFile.getFileName().toString().lastIndexOf("_"));
Path destination = Paths.get(benutzerHome.toString(), atsPpnBand + suffix);
if (!StorageProvider.getInstance().isFileExists(destination)) {
StorageProvider.getInstance().createDirectories(destination);
}
List<Path> files = StorageProvider.getInstance().listFiles(exportFile.toString());
for (Path file : files) {
Path target = Paths.get(destination.toString(), file.getFileName().toString());
StorageProvider.getInstance().copyFile(file, target);
}
}
} else {
// if it is a regular file, export it to source folder
Path destination = Paths.get(benutzerHome.toString(), atsPpnBand + "_src");
if (!StorageProvider.getInstance().isFileExists(destination)) {
StorageProvider.getInstance().createDirectories(destination);
}
Path target = Paths.get(destination.toString(), exportFile.getFileName().toString());
StorageProvider.getInstance().copyFile(exportFile, target);
}
}
}
} catch (AccessDeniedException e) {
Helper.setFehlerMeldung("Export canceled, Process: " + myProzess.getTitel(), "Access to " + e.getMessage() + " was denied");
problems.add("Export cancelled: Access to " + e.getMessage() + " was denied");
return false;
} catch (Exception e) {
Helper.setFehlerMeldung("Export canceled, Process: " + myProzess.getTitel(), e);
problems.add("Export cancelled: " + e.getMessage());
return false;
}
/*
* -------------------------------- zum Schluss Datei an gewünschten Ort
* exportieren entweder direkt in den Import-Ordner oder ins
* Benutzerhome anschliessend den Import-Thread starten
* --------------------------------
*/
boolean externalExport = MetadatenHelper.getExportFileformatByName(myProzess.getProjekt().getFileFormatDmsExport(), myProzess.getRegelsatz()) != null;
if (myProzess.getProjekt().isUseDmsImport()) {
Helper.setMeldung(null, myProzess.getTitel() + ": ", "DMS-Export started");
if (externalExport) {
/* Wenn METS, dann per writeMetsFile schreiben... */
writeMetsFile(myProzess, benutzerHome.toString() + "/" + atsPpnBand + ".xml", gdzfile, false);
} else {
/* ...wenn nicht, nur ein Fileformat schreiben. */
gdzfile.write(benutzerHome.toString() + "/" + atsPpnBand + ".xml");
}
if (!ConfigurationHelper.getInstance().isExportWithoutTimeLimit()) {
DmsImportThread agoraThread = new DmsImportThread(myProzess, atsPpnBand);
agoraThread.start();
try {
/* xx Sekunden auf den Thread warten, evtl. killen */
agoraThread.join(myProzess.getProjekt().getDmsImportTimeOut().longValue());
if (agoraThread.isAlive()) {
agoraThread.stopThread();
}
} catch (InterruptedException e) {
Helper.setFehlerMeldung(myProzess.getTitel() + ": error on export - ", e.getMessage());
problems.add("Export problems: " + e.getMessage());
log.error(myProzess.getTitel() + ": error on export", e);
}
if (agoraThread.rueckgabe.length() > 0) {
Helper.setFehlerMeldung(myProzess.getTitel() + ": ", agoraThread.rueckgabe);
} else {
Helper.setMeldung(null, myProzess.getTitel() + ": ", "ExportFinished");
/* Success-Ordner wieder löschen */
if (myProzess.getProjekt().isDmsImportCreateProcessFolder()) {
String sf = myProzess.getProjekt().getDmsImportSuccessPath();
sf = replacer.replace(sf);
Path successFile = Paths.get(sf, myProzess.getTitel());
StorageProvider.getInstance().deleteDir(successFile);
}
}
}
} else {
/* ohne Agora-Import die xml-Datei direkt ins Home schreiben */
if (externalExport) {
writeMetsFile(myProzess, zielVerzeichnis + atsPpnBand + ".xml", gdzfile, false);
} else {
gdzfile.write(zielVerzeichnis + atsPpnBand + ".xml");
}
Helper.setMeldung(null, myProzess.getTitel() + ": ", "ExportFinished");
}
return true;
}
use of ugh.dl.Fileformat in project goobi-workflow by intranda.
the class ExportMets method downloadMets.
public void downloadMets(Process process) throws ReadException, PreferencesException, WriteException, IOException, InterruptedException, SwapException, DAOException, TypeNotAllowedForParentException {
this.myPrefs = process.getRegelsatz().getPreferences();
String atsPpnBand = process.getTitel();
Fileformat gdzfile = process.readMetadataFile();
// String zielVerzeichnis = prepareUserDirectory(inZielVerzeichnis);
// only save file in /tmp/ directory
Path targetDir = Files.createTempDirectory("mets_export");
String targetFileName = targetDir.resolve(atsPpnBand + "_mets.xml").toAbsolutePath().toString();
writeMetsFile(process, targetFileName, gdzfile, false, true);
// download File
// check if it is a zip:
Path targetFile = Paths.get(targetFileName);
if (!StorageProvider.getInstance().isFileExists(targetFile)) {
targetFile = Paths.get(targetFileName.replace(".xml", ".zip"));
}
try (InputStream in = StorageProvider.getInstance().newInputStream(targetFile)) {
FacesContext facesContext = FacesContextHelper.getCurrentFacesContext();
ExternalContext ec = facesContext.getExternalContext();
ec.responseReset();
ec.setResponseHeader("Content-Disposition", "attachment; filename=" + targetFile.getFileName());
ec.setResponseContentLength((int) StorageProvider.getInstance().getFileSize(targetFile));
IOUtils.copy(in, ec.getResponseOutputStream());
facesContext.responseComplete();
Helper.setMeldung(null, process.getTitel() + ": ", "Download Finished");
// delete file from directory
StorageProvider.getInstance().deleteDir(targetDir);
} catch (Exception e) {
log.error(e);
}
}
Aggregations