use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRObjectCommands method restoreToRevision.
/**
* This method restores a MyCoRe Object to the selected revision. Please note that children and derivates are not
* deleted or reverted!
*
* @param id
* id of MyCoRe Object
* @param revision
* revision to restore
*/
@MCRCommand(syntax = "restore {0} to revision {1}", help = "Restores the selected MCRObject to the selected revision.", order = 270)
public static void restoreToRevision(String id, long revision) {
LOGGER.info("Try to restore object {} with revision {}", id, revision);
MCRObjectID mcrId = MCRObjectID.getInstance(id);
try {
MCRObjectUtils.restore(mcrId, revision);
LOGGER.info("Object {} successfully restored!", id);
} catch (Exception exc) {
LOGGER.error("While retrieving object {} with revision {}", id, revision, exc);
}
}
use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRObjectCommands method mergeDerivatesOfObject.
@MCRCommand(syntax = "merge derivates of object {0}", help = "Retrieves the MCRObject with the MCRObjectID {0} and if it has more then one MCRDerivate, then all" + " Files will be copied to the first Derivate and all other will be deleted.", order = 190)
public static void mergeDerivatesOfObject(String id) {
MCRObjectID objectID = MCRObjectID.getInstance(id);
if (!MCRMetadataManager.exists(objectID)) {
LOGGER.error("The object with the id {} does not exist!", id);
return;
}
MCRObject object = MCRMetadataManager.retrieveMCRObject(objectID);
List<MCRMetaLinkID> derivateLinkIDs = object.getStructure().getDerivates();
List<MCRObjectID> derivateIDs = derivateLinkIDs.stream().map(MCRMetaLinkID::getXLinkHrefID).collect(Collectors.toList());
if (derivateIDs.size() <= 1) {
LOGGER.error("The object with the id {} has no Derivates to merge!", id);
return;
}
String mainID = derivateIDs.get(0).toString();
MCRPath mainDerivateRootPath = MCRPath.getPath(mainID, "/");
derivateIDs.stream().skip(1).forEach(derivateID -> {
LOGGER.info("Merge {} into {}...", derivateID, mainID);
MCRPath copyRootPath = MCRPath.getPath(derivateID.toString(), "/");
try {
MCRTreeCopier treeCopier = new MCRTreeCopier(copyRootPath, mainDerivateRootPath);
Files.walkFileTree(copyRootPath, treeCopier);
Files.walkFileTree(copyRootPath, MCRRecursiveDeleter.instance());
MCRMetadataManager.deleteMCRDerivate(derivateID);
} catch (IOException | MCRAccessException e) {
throw new MCRException(e);
}
});
}
use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRObjectCommands method deleteFromTo.
/**
* Delete MCRObject's form ID to ID from the datastore.
*
* @param IDfrom
* the start ID for deleting the MCRObjects
* @param IDto
* the stop ID for deleting the MCRObjects
* @return list of delete commands
*/
@MCRCommand(syntax = "delete object from {0} to {1}", help = "Removes MCRObjects in the number range between the MCRObjectID {0} and {1}.", order = 30)
public static List<String> deleteFromTo(String IDfrom, String IDto) {
MCRObjectID from = MCRObjectID.getInstance(IDfrom);
MCRObjectID to = MCRObjectID.getInstance(IDto);
int from_i = from.getNumberAsInteger();
int to_i = to.getNumberAsInteger();
if (from_i > to_i) {
throw new MCRException("The from-to-interval is false.");
}
List<String> cmds = new ArrayList<>(to_i - from_i);
for (int i = from_i; i < to_i + 1; i++) {
String id = MCRObjectID.formatID(from.getProjectId(), from.getTypeId(), i);
if (MCRMetadataManager.exists(MCRObjectID.getInstance(id))) {
cmds.add("delete object " + id);
}
}
return cmds;
}
use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRObjectCommands method replaceParent.
/**
* Moves object to new parent.
*
* @param sourceId
* object that should be attached to new parent
* @param newParentId
* the ID of the new parent
* @throws MCRAccessException see {@link MCRMetadataManager#update(MCRObject)}
*/
@MCRCommand(syntax = "set parent of {0} to {1}", help = "replaces a parent of an object (first parameter) to the given new one (second parameter)", order = 300)
public static void replaceParent(String sourceId, String newParentId) throws MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
// child
MCRObject sourceMCRObject = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(sourceId));
// old parent
MCRObjectID oldParentId = sourceMCRObject.getStructure().getParentID();
MCRObjectID newParentObjectID = MCRObjectID.getInstance(newParentId);
if (newParentObjectID.equals(oldParentId)) {
LOGGER.info("Object {} is already child of {}", sourceId, newParentId);
return;
}
MCRObject oldParentMCRObject = null;
if (oldParentId != null) {
try {
oldParentMCRObject = MCRMetadataManager.retrieveMCRObject(oldParentId);
} catch (Exception exc) {
LOGGER.error("Unable to get old parent object {}, its probably deleted.", oldParentId, exc);
}
}
// change href to new parent
LOGGER.info("Setting link in \"{}\" to parent \"{}\"", sourceId, newParentObjectID);
MCRMetaLinkID parentLinkId = new MCRMetaLinkID("parent", 0);
parentLinkId.setReference(newParentObjectID, null, null);
sourceMCRObject.getStructure().setParent(parentLinkId);
if (oldParentMCRObject != null) {
// remove Child in old parent
LOGGER.info("Remove child \"{}\" in old parent \"{}\"", sourceId, oldParentId);
oldParentMCRObject.getStructure().removeChild(sourceMCRObject.getId());
LOGGER.info("Update old parent \"{}\n", oldParentId);
MCRMetadataManager.update(oldParentMCRObject);
}
LOGGER.info("Update \"{}\" in datastore (saving new link)", sourceId);
MCRMetadataManager.update(sourceMCRObject);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Structure: {}", sourceMCRObject.getStructure().isValid());
LOGGER.debug("Object: {}", sourceMCRObject.isValid());
}
}
use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRSolrCommands method createObjectType.
@MCRCommand(syntax = "create solr objecttype {0} at {1}", help = "indexes all objects of an object type (e.g. document) on specific solr server core", order = 125)
public static void createObjectType(String type, String url) throws Exception {
MCRSolrCore core = new MCRSolrCore(url);
SolrClient concurrentSolrClient = core.getConcurrentClient();
MCRSolrIndexer.rebuildMetadataIndex(MCRXMLMetadataManager.instance().listIDsOfType(type), concurrentSolrClient);
concurrentSolrClient.optimize();
}
Aggregations