use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.
the class MCRDerivateCommands method processFromFile.
/**
* Loads or updates an MCRDerivates from an XML file.
*
* @param file
* the location of the xml file
* @param update
* if true, object will be updated, else object is created
* @param importMode
* if true, servdates are taken from xml file
* @throws SAXParseException
* @throws MCRAccessException see {@link MCRMetadataManager#update(MCRDerivate)}
* @throws MCRPersistenceException
*/
private static boolean processFromFile(File file, boolean update, boolean importMode) throws SAXParseException, IOException, MCRPersistenceException, MCRAccessException {
if (!file.getName().endsWith(".xml")) {
LOGGER.warn("{} ignored, does not end with *.xml", file);
return false;
}
if (!file.isFile()) {
LOGGER.warn("{} ignored, is not a file.", file);
return false;
}
LOGGER.info("Reading file {} ...", file);
MCRDerivate derivate = new MCRDerivate(file.toURI());
derivate.setImportMode(importMode);
// Replace relative path with absolute path of files
if (derivate.getDerivate().getInternals() != null) {
String path = derivate.getDerivate().getInternals().getSourcePath();
path = path.replace('/', File.separatorChar).replace('\\', File.separatorChar);
if (path.trim().length() <= 1) {
// the path is the path name plus the name of the derivate -
path = derivate.getId().toString();
}
File sPath = new File(path);
if (!sPath.isAbsolute()) {
// only change path to absolute path when relative
String prefix = file.getParent();
if (prefix != null) {
path = prefix + File.separator + path;
}
}
derivate.getDerivate().getInternals().setSourcePath(path);
LOGGER.info("Source path --> {}", path);
}
LOGGER.info("Label --> {}", derivate.getLabel());
if (update) {
MCRMetadataManager.update(derivate);
LOGGER.info("{} updated.", derivate.getId());
LOGGER.info("");
} else {
MCRMetadataManager.create(derivate);
LOGGER.info("{} loaded.", derivate.getId());
LOGGER.info("");
}
return true;
}
use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.
the class MCRBasketPersistence method updateBasket.
/**
* Updates the basket's data in the persistent store by saving its XML representation
* to a file in a derivate. The ID of the derivate is given in the basket's properties.
*/
public static void updateBasket(MCRBasket basket) throws Exception {
String derivateID = basket.getDerivateID();
MCRObjectID derivateOID = MCRObjectID.getInstance(derivateID);
MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateOID);
MCRPath file = getBasketFile(derivateID);
writeBasketToFile(basket, derivate, file);
}
use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.
the class MCRBasketPersistence method createDerivateWithBasket.
/**
* Creates a new derivate including a file basket.xml which stores the persistent
* data of the given basket.
*
* @param basket the basket to store in a new file in a new derivate
* @param ownerID the ID of the MCRObject owning the new derivate
* @throws MCRAccessException see {@link MCRMetadataManager#create(MCRDerivate)}
* @throws MCRPersistenceException
*/
public static void createDerivateWithBasket(MCRBasket basket, MCRObjectID ownerID) throws IOException, MCRPersistenceException, MCRAccessException {
String base = ownerID.getProjectId() + "_derivate";
MCRObjectID derivateOID = MCRObjectID.getNextFreeId(base);
String derivateID = derivateOID.toString();
MCRDerivate derivate = createNewDerivate(ownerID, derivateOID);
basket.setDerivateID(derivateID);
writeBasketToFile(basket, derivate, getBasketFile(derivateID));
}
use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.
the class MCRViewerConfiguration method getFilePath.
/**
* Helper method to get the path to the start file. The path is
* URI decoded and starts with a slash.
*
* @param request http request
* @return path to the file or null if the path couldn't be retrieved
*/
public static String getFilePath(HttpServletRequest request) {
try {
String fromPath = getFromPath(request.getPathInfo(), 2);
if (fromPath == null || fromPath.isEmpty() || fromPath.equals("/")) {
String derivate = getDerivate(request);
MCRDerivate deriv = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
String nameOfMainFile = deriv.getDerivate().getInternals().getMainDoc();
return "/" + nameOfMainFile;
}
return fromPath;
} catch (Exception exc) {
LOGGER.warn("Unable to get the file path of request {}", request.getRequestURI());
return null;
}
}
use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.
the class MCRDerivateCommands method setMainFile.
@MCRCommand(syntax = "set main file of {0} to {1}", help = "Sets the main file of the derivate with the id {0} to " + "the file with the path {1}")
public static void setMainFile(final String derivateIDString, final String filePath) {
if (!MCRObjectID.isValid(derivateIDString)) {
LOGGER.error("{} is not valid. ", derivateIDString);
return;
}
// check for derivate exist
final MCRObjectID derivateID = MCRObjectID.getInstance(derivateIDString);
if (!MCRMetadataManager.exists(derivateID)) {
LOGGER.error("{} does not exist!", derivateIDString);
return;
}
// remove leading slash
String cleanPath = filePath;
if (filePath.startsWith(String.valueOf(MCRAbstractFileSystem.SEPARATOR))) {
cleanPath = filePath.substring(1, filePath.length());
}
// check for file exist
final MCRPath path = MCRPath.getPath(derivateID.toString(), cleanPath);
if (!Files.exists(path)) {
LOGGER.error("File {} does not exist!", cleanPath);
return;
}
final MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateID);
derivate.getDerivate().getInternals().setMainDoc(cleanPath);
MCRMetadataManager.updateMCRDerivateXML(derivate);
LOGGER.info("The main file of {} is now '{}'!", derivateIDString, cleanPath);
}
Aggregations