use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.
the class MCRDerivateCommands method export.
/**
* Export any MCRDerivate's to files named <em>MCRObjectID</em> .xml in a
* directory and the objects under them named <em>MCRObjectID</em>. The
* saving starts with fromID and runs to toID. ID's they was not found will
* skiped. The method use the converter stylesheet mcr_<em>style</em>
* _object.xsl.
*
* @param fromID
* the ID of the MCRObject from be save.
* @param toID
* the ID of the MCRObject to be save.
* @param dirname
* the filename to store the object
* @param style
* the type of the stylesheet
*/
@MCRCommand(syntax = "export derivate from {0} to {1} to directory {2} with {3}", help = "The command store all derivates with MCRObjectID's between {0} and {1} to the directory {2} with the stylesheet {3}-object.xsl. For {3} save is the default.", order = 80)
public static void export(String fromID, String toID, String dirname, String style) {
// check fromID and toID
MCRObjectID fid = null;
MCRObjectID tid = null;
try {
fid = MCRObjectID.getInstance(fromID);
} catch (Exception ex) {
LOGGER.error("FromID : {}", ex.getMessage());
return;
}
try {
tid = MCRObjectID.getInstance(toID);
} catch (Exception ex) {
LOGGER.error("ToID : {}", ex.getMessage());
return;
}
// check dirname
File dir = new File(dirname);
if (dir.isFile()) {
LOGGER.error("{} is not a dirctory.", dirname);
return;
}
Transformer trans = getTransformer(style);
int k = 0;
try {
for (int i = fid.getNumberAsInteger(); i < tid.getNumberAsInteger() + 1; i++) {
exportDerivate(dir, trans, MCRObjectID.formatID(fid.getProjectId(), fid.getTypeId(), i));
k++;
}
} catch (Exception ex) {
LOGGER.error(ex.getMessage());
LOGGER.error("Exception while store file or objects to {}", dir.getAbsolutePath(), ex);
return;
}
LOGGER.info("{} Object's stored under {}.", k, dir.getAbsolutePath());
}
use of org.mycore.datamodel.metadata.MCRObjectID 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);
}
use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.
the class MCRDerivateCommands method delete.
/**
* Delete an MCRDerivate from the datastore.
*
* @param ID
* the ID of the MCRDerivate that should be deleted
* @throws MCRActiveLinkException
* @throws MCRAccessException see {@link MCRMetadataManager#delete(MCRDerivate)}
*/
@MCRCommand(syntax = "delete derivate {0}", help = "The command remove a derivate with the MCRObjectID {0}", order = 30)
public static void delete(String ID) throws MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
MCRObjectID objectID = MCRObjectID.getInstance(ID);
MCRMetadataManager.deleteMCRDerivate(objectID);
LOGGER.info("{} deleted.", objectID);
}
use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.
the class MCRObjectCommands method repairMCRLinkHrefTable.
@MCRCommand(syntax = "repair mcrlinkhref table", help = "Runs through the whole table and checks for already deleted mcr objects and deletes them.", order = 185)
public static void repairMCRLinkHrefTable() {
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
MCRStreamQuery<String> fromQuery = MCRStreamQuery.getInstance(em, "SELECT DISTINCT m.key.mcrfrom FROM MCRLINKHREF m", String.class);
MCRStreamQuery<String> toQuery = MCRStreamQuery.getInstance(em, "SELECT DISTINCT m.key.mcrto FROM MCRLINKHREF m", String.class);
String query = "DELETE FROM MCRLINKHREF m WHERE m.key.mcrfrom IN (:invalidIds) or m.key.mcrto IN (:invalidIds)";
// open streams
try (Stream<String> fromStream = fromQuery.getResultStream()) {
try (Stream<String> toStream = toQuery.getResultStream()) {
List<String> invalidIds = Stream.concat(fromStream, toStream).distinct().filter(MCRObjectID::isValid).map(MCRObjectID::getInstance).filter(MCRStreamUtils.not(MCRMetadataManager::exists)).map(MCRObjectID::toString).collect(Collectors.toList());
// delete
em.createQuery(query).setParameter("invalidIds", invalidIds).executeUpdate();
}
}
}
use of org.mycore.datamodel.metadata.MCRObjectID in project mycore by MyCoRe-Org.
the class MCRObjectCommands method listRevisions.
/**
* List revisions of an MyCoRe Object.
*
* @param id
* id of MyCoRe Object
*/
@MCRCommand(syntax = "list revisions of {0}", help = "List revisions of MCRObject.", order = 260)
public static void listRevisions(String id) {
MCRObjectID mcrId = MCRObjectID.getInstance(id);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
try {
StringBuilder log = new StringBuilder("Revisions:\n");
List<MCRMetadataVersion> revisions = MCRXMLMetadataManager.instance().listRevisions(mcrId);
for (MCRMetadataVersion revision : revisions) {
log.append(revision.getRevision()).append(" ");
log.append(revision.getType()).append(" ");
log.append(sdf.format(revision.getDate())).append(" ");
log.append(revision.getUser());
log.append("\n");
}
LOGGER.info(log.toString());
} catch (Exception exc) {
LOGGER.error("While print revisions.", exc);
}
}
Aggregations