Search in sources :

Example 1 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRMigrationCommands method addServFlags.

@MCRCommand(syntax = "migrate author servflags for {0}", help = "Create missing servflags for createdby and modifiedby for object {0}. (MCR-786)", order = 10)
public static void addServFlags(String id) throws IOException, MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
    MCRObjectID objectID = MCRObjectID.getInstance(id);
    MCRBase obj = MCRMetadataManager.retrieve(objectID);
    MCRObjectService service = obj.getService();
    if (!service.isFlagTypeSet(MCRObjectService.FLAG_TYPE_CREATEDBY)) {
        // the egg
        MCRVersionedMetadata versionedMetadata = MCRXMLMetadataManager.instance().getVersionedMetaData(objectID);
        String createUser = null, modifyUser = null;
        if (versionedMetadata == null) {
            LOGGER.warn("Cannot restore author servflags as there are no versions available. Setting to current user.");
            createUser = MCRSessionMgr.getCurrentSession().getUserInformation().getUserID();
            modifyUser = createUser;
        } else {
            List<MCRMetadataVersion> versions = versionedMetadata.listVersions();
            MCRMetadataVersion firstVersion = versions.get(0);
            for (MCRMetadataVersion version : versions) {
                if (version.getType() == 'A') {
                    // get last 'added'
                    firstVersion = version;
                }
            }
            MCRMetadataVersion lastVersion = versions.get(versions.size() - 1);
            createUser = firstVersion.getUser();
            modifyUser = lastVersion.getUser();
        }
        service.addFlag(MCRObjectService.FLAG_TYPE_CREATEDBY, createUser);
        LOGGER.info("{}, created by: {}", objectID, createUser);
        if (!service.isFlagTypeSet(MCRObjectService.FLAG_TYPE_MODIFIEDBY)) {
            // the chicken
            // have to restore also modifiedby from version history.
            LOGGER.info("{}, modified by: {}", objectID, modifyUser);
            service.addFlag(MCRObjectService.FLAG_TYPE_CREATEDBY, modifyUser);
        }
        obj.setImportMode(true);
        if (obj instanceof MCRDerivate) {
            MCRMetadataManager.updateMCRDerivateXML((MCRDerivate) obj);
        } else {
            MCRMetadataManager.update((MCRObject) obj);
        }
    }
}
Also used : MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 2 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRMigrationCommands method fixDerivateLinks.

@MCRCommand(syntax = "fix invalid derivate links {0} for {1}", help = "Fixes the paths of all derivate links " + "({0} -> xpath -> e.g. /mycoreobject/metadata/derivateLinks/derivateLink) for object {1}. (MCR-1267)", order = 15)
public static void fixDerivateLinks(String xpath, String id) throws IOException, JDOMException, SAXException {
    // get mcr object
    MCRObjectID objectID = MCRObjectID.getInstance(id);
    // find derivate links
    Document xml = MCRXMLMetadataManager.instance().retrieveXML(objectID);
    Element mcrObjectXML = xml.getRootElement();
    XPathExpression<Element> expression = XPathFactory.instance().compile(xpath, Filters.element());
    List<Element> derivateLinkElements = expression.evaluate(mcrObjectXML);
    // check them
    boolean changedObject = false;
    for (Element derivateLinkElement : derivateLinkElements) {
        String href = derivateLinkElement.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
        MCRMetaDerivateLink link = new MCRMetaDerivateLink();
        link.setReference(href, null, null);
        String owner = link.getOwner();
        try {
            String path = link.getPath();
            MCRPath mcrPath = MCRPath.getPath(owner, path);
            if (!Files.exists(mcrPath)) {
                // -> e.g. a?c.tif -> path (a), query (c.tif) which is obvious wrong
                if (tryRawPath(objectID, derivateLinkElement, href, link, owner)) {
                    changedObject = true;
                } else {
                    LOGGER.warn("{} of {}cannot be found on file system. This is most likly a dead link.", href, objectID);
                }
            }
        } catch (URISyntaxException uriExc) {
            // not encoded properly
            if (tryRawPath(objectID, derivateLinkElement, href, link, owner)) {
                changedObject = true;
            } else {
                LOGGER.warn("{} of {} isn't URI encoded and cannot be found on file system. This is most likly a dead link.", href, objectID);
            }
        }
    }
    // store the mcr object if its changed
    if (changedObject) {
        // we use MCRXMLMetadataMananger because we don't want to validate the old mcr object
        MCRXMLMetadataManager.instance().update(objectID, xml, new Date());
        // manually fire update event
        MCRObject newObject = MCRMetadataManager.retrieveMCRObject(objectID);
        newObject.setImportMode(true);
        MCRMetadataManager.fireUpdateEvent(newObject);
    }
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) Element(org.jdom2.Element) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) URISyntaxException(java.net.URISyntaxException) Document(org.jdom2.Document) MCRPath(org.mycore.datamodel.niofs.MCRPath) Date(java.util.Date) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 3 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRMigrationCommands method fixMissingChildren.

@MCRCommand(syntax = "add missing children to {0}", help = "Adds missing children to structure of parent {0}. (MCR-1480)", order = 15)
public static void fixMissingChildren(String id) throws IOException, JDOMException, SAXException {
    MCRObjectID parentId = MCRObjectID.getInstance(id);
    Collection<String> children = MCRLinkTableManager.instance().getSourceOf(parentId, MCRLinkTableManager.ENTRY_TYPE_PARENT);
    if (children.isEmpty()) {
        return;
    }
    MCRObject parent = MCRMetadataManager.retrieveMCRObject(parentId);
    MCRObjectStructure parentStructure = parent.getStructure();
    int sizeBefore = parentStructure.getChildren().size();
    children.stream().map(MCRObjectID::getInstance).filter(cid -> !parentStructure.getChildren().stream().anyMatch(candidate -> candidate.getXLinkHrefID().equals(cid))).sorted().map(MCRMigrationCommands::toLinkId).sequential().peek(lid -> LOGGER.info("Adding {} to {}", lid, parentId)).forEach(parentStructure::addChild);
    if (parentStructure.getChildren().size() != sizeBefore) {
        MCRMetadataManager.fireUpdateEvent(parent);
    }
}
Also used : MCRObjectService(org.mycore.datamodel.metadata.MCRObjectService) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRDNBURN(org.mycore.pi.urn.MCRDNBURN) Document(org.jdom2.Document) MCRPI(org.mycore.pi.backend.MCRPI) JDOMException(org.jdom2.JDOMException) MCRTileJob(org.mycore.iview2.services.MCRTileJob) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) Path(java.nio.file.Path) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) MCRLinkTableManager(org.mycore.datamodel.common.MCRLinkTableManager) MCRPath(org.mycore.datamodel.niofs.MCRPath) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) XPathExpression(org.jdom2.xpath.XPathExpression) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRDNBURNParser(org.mycore.pi.urn.MCRDNBURNParser) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) Element(org.jdom2.Element) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRLINKHREF(org.mycore.backend.jpa.links.MCRLINKHREF) XPathFactory(org.jdom2.xpath.XPathFactory) MCRConstants(org.mycore.common.MCRConstants) TypedQuery(javax.persistence.TypedQuery) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) MCRLINKHREFPK_(org.mycore.backend.jpa.links.MCRLINKHREFPK_) MCRXMLFunctions(org.mycore.common.xml.MCRXMLFunctions) MCRMetaDerivateLink(org.mycore.datamodel.metadata.MCRMetaDerivateLink) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) Root(javax.persistence.criteria.Root) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) Files(java.nio.file.Files) IOException(java.io.IOException) MCRLINKHREF_(org.mycore.backend.jpa.links.MCRLINKHREF_) EntityManager(javax.persistence.EntityManager) Paths(java.nio.file.Paths) EntityTransaction(javax.persistence.EntityTransaction) MCRSessionMgr(org.mycore.common.MCRSessionMgr) MCRObject(org.mycore.datamodel.metadata.MCRObject) LogManager(org.apache.logging.log4j.LogManager) Filters(org.jdom2.filter.Filters) MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 4 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRMODSCommands method loadFromDirectory.

@MCRCommand(syntax = "load all mods documents from directory {0} for project {1}", help = "Load all MODS documents as MyCoRe Objects for project {1} from directory {0}", order = 10)
public static List<String> loadFromDirectory(String directory, String projectID) {
    File dir = new File(directory);
    if (!dir.isDirectory()) {
        throw new MCRException(MessageFormat.format("File {0} is not a directory.", directory));
    }
    String[] list = dir.list();
    if (list.length == 0) {
        LOGGER.warn("No files found in directory {}", dir);
        return null;
    }
    return Arrays.stream(list).filter(file -> file.endsWith(".xml")).map(file -> MessageFormat.format("load mods document from file {0} for project {1}", new File(dir, file).getAbsolutePath(), projectID)).collect(Collectors.toList());
}
Also used : XMLReaders(org.jdom2.input.sax.XMLReaders) Arrays(java.util.Arrays) MCRConstants(org.mycore.common.MCRConstants) MCRException(org.mycore.common.MCRException) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MessageFormat(java.text.MessageFormat) MCRAccessManager(org.mycore.access.MCRAccessManager) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) SAXBuilder(org.jdom2.input.SAXBuilder) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRAbstractCommands(org.mycore.frontend.cli.MCRAbstractCommands) Collection(java.util.Collection) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) MCRAccessInterface(org.mycore.access.MCRAccessInterface) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRObject(org.mycore.datamodel.metadata.MCRObject) SAXException(org.xml.sax.SAXException) MCRXMLHelper(org.mycore.common.xml.MCRXMLHelper) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) LogManager(org.apache.logging.log4j.LogManager) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRRSSFeedImporter(org.mycore.mods.rss.MCRRSSFeedImporter) Element(org.jdom2.Element) MCRException(org.mycore.common.MCRException) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 5 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRSolrCommands method rebuildMetadataIndexForObject.

@MCRCommand(syntax = "restricted rebuild solr metadata index for object {0}", help = "rebuilds solr's metadata index for object and all its children", order = 70)
public static void rebuildMetadataIndexForObject(String id) {
    MCRObject mcrObject = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(id));
    List<MCRObject> objectList = MCRObjectUtils.getDescendantsAndSelf(mcrObject);
    List<String> idList = objectList.stream().map(obj -> obj.getId().toString()).collect(Collectors.toList());
    MCRSolrIndexer.rebuildMetadataIndex(idList);
}
Also used : MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRAbstractCommands(org.mycore.frontend.cli.MCRAbstractCommands) Collectors(java.util.stream.Collectors) SolrClient(org.apache.solr.client.solrj.SolrClient) MCRObjectUtils(org.mycore.datamodel.metadata.MCRObjectUtils) MCRObjectCommands(org.mycore.frontend.cli.MCRObjectCommands) MCRSolrSearchUtils(org.mycore.solr.search.MCRSolrSearchUtils) List(java.util.List) ConcurrentUpdateSolrClient(org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient) MCRSolrClientFactory(org.mycore.solr.MCRSolrClientFactory) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRSolrClassificationUtil(org.mycore.solr.classification.MCRSolrClassificationUtil) MCRSolrIndexer(org.mycore.solr.index.MCRSolrIndexer) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRSolrCore(org.mycore.solr.MCRSolrCore) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)106 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)37 MCRException (org.mycore.common.MCRException)34 IOException (java.io.IOException)30 File (java.io.File)22 ArrayList (java.util.ArrayList)18 Document (org.jdom2.Document)17 JDOMException (org.jdom2.JDOMException)17 MCRObject (org.mycore.datamodel.metadata.MCRObject)17 Path (java.nio.file.Path)16 SAXException (org.xml.sax.SAXException)16 EntityManager (javax.persistence.EntityManager)15 MCRAccessException (org.mycore.access.MCRAccessException)15 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)15 MCRPath (org.mycore.datamodel.niofs.MCRPath)15 FileNotFoundException (java.io.FileNotFoundException)13 SAXParseException (org.xml.sax.SAXParseException)12 List (java.util.List)11 Element (org.jdom2.Element)11 MCRPersistenceException (org.mycore.common.MCRPersistenceException)11