use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRIView2Tools method getSupportedMainFile.
/**
* @param derivateID
* ID of derivate
* @return empty String or absolute path to main file of derivate if file is supported.
*/
public static String getSupportedMainFile(String derivateID) {
try {
MCRDerivate deriv = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivateID));
String nameOfMainFile = deriv.getDerivate().getInternals().getMainDoc();
// verify support
if (nameOfMainFile != null && !nameOfMainFile.equals("")) {
MCRPath mainFile = MCRPath.getPath(derivateID, '/' + nameOfMainFile);
if (mainFile != null && isFileSupported(mainFile))
return mainFile.getRoot().relativize(mainFile).toString();
}
} catch (Exception e) {
LOGGER.warn("Could not get main file of derivate.", e);
}
return "";
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRMetsResolver method resolve.
@Override
public Source resolve(String href, String base) throws TransformerException {
String id = href.substring(href.indexOf(":") + 1);
LOGGER.debug("Reading METS for ID {}", id);
MCRObjectID objId = MCRObjectID.getInstance(id);
if (!objId.getTypeId().equals("derivate")) {
String derivateID = getDerivateFromObject(id);
if (derivateID == null) {
return new JDOMSource(new Element("mets", Namespace.getNamespace("mets", "http://www.loc.gov/METS/")));
}
id = derivateID;
}
MCRPath metsPath = MCRPath.getPath(id, "/mets.xml");
try {
if (Files.exists(metsPath)) {
// ignoreNodes.add(metsFile);
return new MCRPathContent(metsPath).getSource();
}
Document mets = MCRMETSGeneratorFactory.create(MCRPath.getPath(id, "/")).generate().asDocument();
return new JDOMSource(mets);
} catch (Exception e) {
throw new TransformerException(e);
}
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRMetsSave method isComplete.
/**
* @return true if all files owned by the derivate appearing in the master file group or false otherwise
*/
public static boolean isComplete(Mets mets, MCRObjectID derivateId) {
try {
FileGrp fileGroup = mets.getFileSec().getFileGroup(FileGrp.USE_MASTER);
MCRPath rootPath = MCRPath.getPath(derivateId.toString(), "/");
return isComplete(fileGroup, rootPath);
} catch (Exception ex) {
LOGGER.error("Error while validating mets", ex);
return false;
}
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MCRMetsSave method saveMets.
/**
* Saves the content of the given document to file, if no mets present and then adds the file to
* the derivate with the given id. The name of the file depends on property
* 'MCR.Mets.Filename'. If this property has not been set 'mets.xml' is used
* as a default filename.
*
* @param overwrite
* if true existing mets-file will be overwritten
* @param validate
* if true the document will be validated before its stored
* @return
* true if the given document was successfully saved, otherwise false
*/
public static synchronized boolean saveMets(Document document, MCRObjectID derivateId, boolean overwrite, boolean validate) {
// add the file to the existing derivate in ifs
MCRPath metsFile = getMetsFile(derivateId.toString());
if (metsFile == null) {
metsFile = createMetsFile(derivateId.toString());
} else if (!overwrite) {
return false;
}
if (validate && !Mets.isValid(document)) {
LOGGER.warn("Storing mets.xml for {} failed cause the given document was invalid.", derivateId);
return false;
}
try (OutputStream metsOut = Files.newOutputStream(metsFile)) {
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
xout.output(document, metsOut);
LOGGER.info("Storing file content from \"{}\" to derivate \"{}\"", getMetsFileName(), derivateId);
} catch (Exception e) {
LOGGER.error(e);
return false;
}
return true;
}
use of org.mycore.datamodel.niofs.MCRPath in project mycore by MyCoRe-Org.
the class MetsResource method get.
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/crud/{derivateId}")
public String get(@PathParam("derivateId") String derivateId) {
MCRObjectID derivateIdObject = MCRObjectID.getInstance(derivateId);
checkDerivateExists(derivateIdObject);
checkDerivateAccess(derivateIdObject, MCRAccessManager.PERMISSION_READ);
MCRPath rootPath = MCRPath.getPath(derivateId, "/");
if (!Files.isDirectory(rootPath)) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
MCRPath metsPath = MCRPath.getPath(derivateId, METS_XML_PATH);
try {
return MCRSimpleModelJSONConverter.toJSON(MCRXMLSimpleModelConverter.fromXML(getMetsDocument(metsPath)));
} catch (Exception e) {
throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
Aggregations