use of org.jdom2.Content in project mycore by MyCoRe-Org.
the class MCRMetsSave method updateOnFileAdd.
// TODO: should use mets-model api
/**
* Alters the mets file
*
* @param mets
* the unmodified source
* @param file
* the file to add
* @return the modified mets or null if an exception occures
*/
private static Document updateOnFileAdd(Document mets, MCRPath file) {
try {
// check for file existance (if a derivate with mets.xml is uploaded
String relPath = MCRXMLFunctions.encodeURIPath(file.getOwnerRelativePath().substring(1));
// Check if file already exists -> if yes do nothing
String fileExistPathString = "mets:mets/mets:fileSec/mets:fileGrp/mets:file/mets:FLocat[@xlink:href='" + relPath + "']";
XPathExpression<Element> xpath = XPathFactory.instance().compile(fileExistPathString, Filters.element(), null, MCRConstants.METS_NAMESPACE, MCRConstants.XLINK_NAMESPACE);
if (xpath.evaluate(mets).size() > 0) {
String msgTemplate = "The File : '%s' already exists in mets.xml";
LOGGER.warn(String.format(Locale.ROOT, msgTemplate, relPath));
return null;
} else {
String msgTemplate = "The File : '%s' does not exists in mets.xml";
LOGGER.warn(String.format(Locale.ROOT, msgTemplate, relPath));
}
// add to file section
String contentType = MCRContentTypes.probeContentType(file);
LOGGER.warn(MessageFormat.format("Content Type is : {0}", contentType));
String fileGrpUSE = getFileGroupUse(file);
String fileId = MessageFormat.format("{0}_{1}", fileGrpUSE.toLowerCase(Locale.ROOT), getFileBase(relPath));
org.mycore.mets.model.files.File fileAsMetsFile = new org.mycore.mets.model.files.File(fileId, contentType);
FLocat fLocat = new FLocat(LOCTYPE.URL, relPath);
fileAsMetsFile.setFLocat(fLocat);
Element fileSec = getFileGroup(mets, fileGrpUSE);
fileSec.addContent(fileAsMetsFile.asElement());
if (fileGrpUSE.equals(MCRMetsFileUse.DEFAULT.toString())) {
updateOnImageFile(mets, fileId, relPath);
} else {
updateOnCustomFile(mets, fileId, relPath);
}
} catch (Exception ex) {
LOGGER.error("Error occured while adding file {} to the existing mets file", file, ex);
return null;
}
return mets;
}
use of org.jdom2.Content in project mycore by MyCoRe-Org.
the class MCRMODSMetadataShareAgent method distributeMetadata.
/* (non-Javadoc)
* @see org.mycore.datamodel.metadata.share.MCRMetadataShareAgent#inheritMetadata(org.mycore.datamodel.metadata.MCRObject)
*/
@Override
public void distributeMetadata(MCRObject holder) throws MCRPersistenceException, MCRAccessException {
MCRMODSWrapper holderWrapper = new MCRMODSWrapper(holder);
List<MCRMetaLinkID> children = holder.getStructure().getChildren();
if (!children.isEmpty()) {
LOGGER.info("Update inherited metadata");
for (MCRMetaLinkID childIdRef : children) {
MCRObjectID childId = childIdRef.getXLinkHrefID();
if (MCRMODSWrapper.isSupported(childId)) {
LOGGER.info("Update: {}", childIdRef);
MCRObject child = MCRMetadataManager.retrieveMCRObject(childId);
MCRMODSWrapper childWrapper = new MCRMODSWrapper(child);
inheritToChild(holderWrapper, childWrapper);
LOGGER.info("Saving: {}", childIdRef);
try {
MCRMetadataManager.update(child);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Error while updating inherited metadata", e);
}
}
}
}
Collection<String> recipientIds = MCRLinkTableManager.instance().getSourceOf(holder.getId(), MCRLinkTableManager.ENTRY_TYPE_REFERENCE);
for (String rId : recipientIds) {
MCRObjectID recipientId = MCRObjectID.getInstance(rId);
if (MCRMODSWrapper.isSupported(recipientId)) {
LOGGER.info("distribute metadata to {}", rId);
MCRObject recipient = MCRMetadataManager.retrieveMCRObject(recipientId);
MCRMODSWrapper recipientWrapper = new MCRMODSWrapper(recipient);
for (Element relatedItem : recipientWrapper.getLinkedRelatedItems()) {
String holderId = relatedItem.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
if (holder.getId().toString().equals(holderId)) {
@SuppressWarnings("unchecked") Filter<Content> sharedMetadata = (Filter<Content>) Filters.element("part", MCRConstants.MODS_NAMESPACE).negate();
relatedItem.removeContent(sharedMetadata);
relatedItem.addContent(holderWrapper.getMODS().cloneContent());
LOGGER.info("Saving: {}", recipientId);
try {
MCRMetadataManager.update(recipient);
} catch (MCRPersistenceException | MCRAccessException e) {
throw new MCRPersistenceException("Error while updating shared metadata", e);
}
}
}
}
}
}
use of org.jdom2.Content in project mycore by MyCoRe-Org.
the class MCRDFGLinkServlet method getMetsSource.
/**
* Returns the mets document wrapped in a {@link MCRContent} object.
*
* @param job
* @param useExistingMets
* @return
* @throws Exception
*/
private static MCRContent getMetsSource(MCRServletJob job, boolean useExistingMets, String derivate) throws Exception {
MCRPath metsFile = MCRPath.getPath(derivate, "/mets.xml");
try {
job.getRequest().setAttribute("XSL.derivateID", derivate);
job.getRequest().setAttribute("XSL.objectID", MCRLinkTableManager.instance().getSourceOf(derivate).iterator().next());
} catch (Exception x) {
LOGGER.warn("Unable to set \"XSL.objectID\" attribute to current request", x);
}
boolean metsExists = Files.exists(metsFile);
if (metsExists && useExistingMets) {
MCRContent content = new MCRPathContent(metsFile);
content.setDocType("mets");
return content;
} else {
Document mets = MCRMETSGeneratorFactory.create(metsFile.getParent()).generate().asDocument();
return new MCRJDOMContent(mets);
}
}
use of org.jdom2.Content in project mycore by MyCoRe-Org.
the class MCRMETSServlet method getMetsSource.
/**
* Returns the mets document wrapped in a {@link MCRContent} object.
*/
static MCRContent getMetsSource(MCRServletJob job, boolean useExistingMets, String derivate) throws Exception {
MCRPath metsPath = MCRPath.getPath(derivate, "/mets.xml");
try {
job.getRequest().setAttribute("XSL.derivateID", derivate);
String objectid = MCRLinkTableManager.instance().getSourceOf(derivate).iterator().next();
if (objectid == null || objectid.length() == 0) {
MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
MCRObjectID ownerID = derObj.getOwnerID();
objectid = ownerID.toString();
}
job.getRequest().setAttribute("XSL.objectID", objectid);
} catch (Exception x) {
LOGGER.warn("Unable to set \"XSL.objectID\" attribute to current request", x);
}
boolean metsExists = Files.exists(metsPath);
if (metsExists && useExistingMets) {
MCRContent content = new MCRPathContent(metsPath);
content.setDocType("mets");
return content;
} else {
Document mets = MCRMETSGeneratorFactory.create(MCRPath.getPath(derivate, "/")).generate().asDocument();
if (!metsExists && STORE_METS_ON_GENERATE) {
MCRMetsSave.saveMets(mets, MCRObjectID.getInstance(derivate));
}
return new MCRJDOMContent(mets);
}
}
use of org.jdom2.Content in project mycore by MyCoRe-Org.
the class MCRSolrAltoExtractor method extract.
private void extract(Element root, SolrInputDocument document) {
StringBuilder altoContent = new StringBuilder();
for (Element stringElement : STRING_EXP.evaluate(root)) {
String content = stringElement.getAttributeValue("CONTENT");
String hpos = stringElement.getAttributeValue("HPOS");
String vpos = stringElement.getAttributeValue("VPOS");
String width = stringElement.getAttributeValue("WIDTH");
String height = stringElement.getAttributeValue("HEIGHT");
if (hpos == null || vpos == null || width == null || height == null) {
continue;
}
String regEx = "\\.0";
String altoWord = String.join("|", content, hpos.replaceAll(regEx, ""), vpos.replaceAll(regEx, ""), width.replaceAll(regEx, ""), height.replaceAll(regEx, ""));
altoContent.append(content).append(' ');
document.addField("alto_words", altoWord);
}
document.addField("alto_content", altoContent.toString().trim());
}
Aggregations