Search in sources :

Example 1 with MCRXMLMetadataManager

use of org.mycore.datamodel.common.MCRXMLMetadataManager in project mycore by MyCoRe-Org.

the class MCRObjectUtils method restore.

/**
 * Restores a MyCoRe Object to the selected revision. Please note that children and derivates
 * are not deleted or reverted!
 *
 * @param mcrId the mycore object identifier
 * @param revision The revision to restore to. If this is lower than zero, the last revision is used.
 * @return the new {@link MCRObject}
 *
 * @throws IOException An error occurred while retrieving the revision information. This is most
 *          likely due an svn error.
 * @throws MCRPersistenceException There is no such object with the given id and revision.
 * @throws ClassCastException The returning type must be the same as the type of the restored object
 */
public static <T extends MCRBase> T restore(MCRObjectID mcrId, Long revision) throws IOException, MCRPersistenceException {
    @SuppressWarnings("unchecked") T mcrBase = (T) (mcrId.getTypeId().equals("derivate") ? new MCRDerivate() : new MCRObject());
    // get content
    MCRXMLMetadataManager xmlMetadataManager = MCRXMLMetadataManager.instance();
    MCRContent content = xmlMetadataManager.retrieveContent(mcrId, revision);
    if (content == null) {
        throw new MCRPersistenceException("No such object " + mcrId + " with revision " + revision + ".");
    }
    // store it
    try {
        mcrBase.setFromJDOM(content.asXML());
        if (MCRMetadataManager.exists(mcrId)) {
            MCRMetadataManager.update(mcrBase);
        } else {
            if (mcrBase instanceof MCRObject) {
                MCRMetadataManager.create((MCRObject) mcrBase);
            } else {
                MCRMetadataManager.create((MCRDerivate) mcrBase);
            }
        }
        return mcrBase;
    } catch (Exception exc) {
        throw new MCRException("Unable to get object " + mcrId + " with revision " + revision + ".", exc);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRContent(org.mycore.common.content.MCRContent) MCRPersistenceException(org.mycore.common.MCRPersistenceException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) MCRException(org.mycore.common.MCRException)

Example 2 with MCRXMLMetadataManager

use of org.mycore.datamodel.common.MCRXMLMetadataManager in project mycore by MyCoRe-Org.

the class MCRSolrIndexer method rebuildMetadataIndex.

/**
 * Rebuilds solr's metadata index.
 *
 * @param list
 *            list of identifiers of the objects to index
 * @param solrClient
 *            solr server to index
 */
public static void rebuildMetadataIndex(List<String> list, SolrClient solrClient) {
    LOGGER.info("Re-building Metadata Index");
    if (list.isEmpty()) {
        LOGGER.info("Sorry, no documents to index");
        return;
    }
    StopWatch swatch = new StopWatch();
    swatch.start();
    int totalCount = list.size();
    LOGGER.info("Sending {} objects to solr for reindexing", totalCount);
    MCRXMLMetadataManager metadataMgr = MCRXMLMetadataManager.instance();
    MCRSolrIndexStatistic statistic = null;
    HashMap<MCRObjectID, MCRContent> contentMap = new HashMap<>((int) (BULK_SIZE * 1.4));
    int i = 0;
    for (String id : list) {
        i++;
        try {
            LOGGER.debug("Preparing \"{}\" for indexing", id);
            MCRObjectID objId = MCRObjectID.getInstance(id);
            MCRContent content = metadataMgr.retrieveContent(objId);
            contentMap.put(objId, content);
            if (i % BULK_SIZE == 0 || totalCount == i) {
                MCRSolrIndexHandler indexHandler = MCRSolrIndexHandlerFactory.getInstance().getIndexHandler(contentMap);
                indexHandler.setCommitWithin(BATCH_AUTO_COMMIT_WITHIN_MS);
                indexHandler.setSolrServer(solrClient);
                statistic = indexHandler.getStatistic();
                submitIndexHandler(indexHandler);
                contentMap = new HashMap<>((int) (BULK_SIZE * 1.4));
            }
        } catch (Exception ex) {
            LOGGER.error("Error creating index thread for object {}", id, ex);
        }
    }
    long durationInMilliSeconds = swatch.getTime();
    if (statistic != null) {
        statistic.addTime(durationInMilliSeconds);
    }
}
Also used : MCRSolrIndexStatistic(org.mycore.solr.index.statistic.MCRSolrIndexStatistic) HashMap(java.util.HashMap) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRContent(org.mycore.common.content.MCRContent) SolrServerException(org.apache.solr.client.solrj.SolrServerException) IOException(java.io.IOException) StopWatch(org.apache.commons.lang.time.StopWatch) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID)

Example 3 with MCRXMLMetadataManager

use of org.mycore.datamodel.common.MCRXMLMetadataManager in project mycore by MyCoRe-Org.

the class MCRDerivateCommands method checkObjectsInDerivates.

/**
 * Check the object links in derivates of MCR base ID for existing. It looks to the XML store on the disk to get all object IDs.
 *
 * @param base_id
 *            the base part of a MCRObjectID e.g. DocPortal_derivate
 */
@MCRCommand(syntax = "check object entries in derivates for base {0}", help = "check in all derivates of MCR base ID {0} for existing linked objects", order = 400)
public static void checkObjectsInDerivates(String base_id) throws IOException {
    if (base_id == null || base_id.length() == 0) {
        LOGGER.error("Base ID missed for check object entries in derivates for base {0}");
        return;
    }
    int project_part_position = base_id.indexOf('_');
    if (project_part_position == -1) {
        LOGGER.error("The given base ID {} has not the syntax of project_type", base_id);
        return;
    }
    MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
    List<String> id_list = mgr.listIDsForBase(base_id.substring(0, project_part_position + 1) + "derivate");
    int counter = 0;
    int maxresults = id_list.size();
    for (String derid : id_list) {
        counter++;
        LOGGER.info("Processing dataset {} from {} with ID: {}", counter, maxresults, derid);
        // get from data
        MCRObjectID mcrderid = MCRObjectID.getInstance(derid);
        MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(mcrderid);
        MCRObjectID objid = der.getOwnerID();
        if (!mgr.exists(objid)) {
            LOGGER.error("   !!! Missing object {} in database for derivate ID {}", objid, mcrderid);
        }
    }
    LOGGER.info("Check done for {} entries", Integer.toString(counter));
}
Also used : MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 4 with MCRXMLMetadataManager

use of org.mycore.datamodel.common.MCRXMLMetadataManager in project mycore by MyCoRe-Org.

the class MCRObjectCommands method checkDerivatesInObjects.

/**
 * Check the derivate links in objects of MCR base ID for existing. It looks to the XML store on the disk to get all
 * object IDs.
 *
 * @param base_id
 *            the base part of a MCRObjectID e.g. DocPortal_document
 */
@MCRCommand(syntax = "check derivate entries in objects for base {0}", help = "check in all objects with MCR base ID {0} for existing linked derivates", order = 400)
public static void checkDerivatesInObjects(String base_id) throws IOException {
    if (base_id == null || base_id.length() == 0) {
        LOGGER.error("Base ID missed for check derivate entries in objects for base {0}");
        return;
    }
    MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
    List<String> id_list = mgr.listIDsForBase(base_id);
    int counter = 0;
    int maxresults = id_list.size();
    for (String objid : id_list) {
        counter++;
        LOGGER.info("Processing dataset {} from {} with ID: {}", counter, maxresults, objid);
        // get from data
        MCRObjectID mcrobjid = MCRObjectID.getInstance(objid);
        MCRObject obj = MCRMetadataManager.retrieveMCRObject(mcrobjid);
        List<MCRMetaLinkID> derivate_entries = obj.getStructure().getDerivates();
        for (MCRMetaLinkID derivate_entry : derivate_entries) {
            String derid = derivate_entry.getXLinkHref();
            if (!mgr.exists(MCRObjectID.getInstance(derid))) {
                LOGGER.error("   !!! Missing derivate {} in database for base ID {}", derid, base_id);
            }
        }
    }
    LOGGER.info("Check done for {} entries", Integer.toString(counter));
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 5 with MCRXMLMetadataManager

use of org.mycore.datamodel.common.MCRXMLMetadataManager in project mycore by MyCoRe-Org.

the class MCRIFSCommands method checkMCRFSNODESForDerivatesWithProjectID.

@MCRCommand(syntax = "check derivates of mcrfsnodes with project id {0}", help = "check the entries of MCRFSNODES for all derivates with project ID {0}")
public static void checkMCRFSNODESForDerivatesWithProjectID(String project_id) {
    LOGGER.info("Start check of MCRFSNODES for derivates with project ID {}", project_id);
    if (project_id == null || project_id.length() == 0) {
        LOGGER.error("Project ID missed for check MCRFSNODES entries of derivates with project ID {0}");
        return;
    }
    Map<String, MCRContentStore> availableStores = MCRContentStoreFactory.getAvailableStores();
    Session session = MCRHIBConnection.instance().getSession();
    MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
    List<String> id_list = mgr.listIDsForBase(project_id + "_derivate");
    int counter = 0;
    int maxresults = id_list.size();
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
    Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
    ParameterExpression<String> ownerID = cb.parameter(String.class);
    TypedQuery<MCRFSNODES> typedQuery = em.createQuery(query.where(cb.equal(nodes.get(MCRFSNODES_.owner), ownerID), cb.equal(nodes.get(MCRFSNODES_.type), "F")).orderBy(cb.asc(nodes.get(MCRFSNODES_.storageid))));
    for (String derid : id_list) {
        counter++;
        LOGGER.info("Processing dataset {} from {} with ID: {}", counter, maxresults, derid);
        // check mcrfsnodes entries
        try {
            AtomicInteger nodeCount = new AtomicInteger();
            typedQuery.setParameter(ownerID, derid);
            typedQuery.getResultList().stream().forEach(fsNode -> {
                nodeCount.incrementAndGet();
                String store_name = fsNode.getStoreid();
                String storageid = fsNode.getStorageid();
                String name = fsNode.getName();
                long size = fsNode.getSize();
                Date date = fsNode.getDate();
                GregorianCalendar datecal = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
                datecal.setTime(date);
                String fctid = fsNode.getFctid();
                String md5 = fsNode.getMd5();
                session.evict(fsNode);
                LOGGER.debug("File for [owner] {} [name] {} [storeid] {} [storageid] {} [fctid] {} [size] {} [md5] {}", derid, name, store_name, storageid, fctid, size, md5);
                // get path of file
                MCRContentStore fs_store = availableStores.get(store_name);
                if (fs_store == null) {
                    LOGGER.error("Can't find content store {}", store_name);
                    return;
                }
                try {
                    File content_file = fs_store.getLocalFile(storageid);
                    if (content_file == null || !content_file.canRead()) {
                        LOGGER.error("   !!!! Can't access to file {} of store {}", storageid, store_name);
                    }
                } catch (Exception e) {
                    LOGGER.error("   !!!! Can't access to file {} of store {}", storageid, store_name);
                }
            });
            if (nodeCount.get() == 0) {
                LOGGER.error("   !!!! Can't find file entries in MCRFSNODES for {}", derid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.clear();
        }
    }
    LOGGER.info("Check done for {} entries", Integer.toString(counter));
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) GregorianCalendar(java.util.GregorianCalendar) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) Date(java.util.Date) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SAXException(org.xml.sax.SAXException) MCRException(org.mycore.common.MCRException) NoSuchElementException(java.util.NoSuchElementException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) NotDirectoryException(java.nio.file.NotDirectoryException) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) MCRFile(org.mycore.datamodel.ifs.MCRFile) File(java.io.File) Session(org.hibernate.Session) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRXMLMetadataManager (org.mycore.datamodel.common.MCRXMLMetadataManager)8 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 IOException (java.io.IOException)4 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)4 MCRException (org.mycore.common.MCRException)3 MCRContent (org.mycore.common.content.MCRContent)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 NotDirectoryException (java.nio.file.NotDirectoryException)2 Date (java.util.Date)2 GregorianCalendar (java.util.GregorianCalendar)2 NoSuchElementException (java.util.NoSuchElementException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 EntityManager (javax.persistence.EntityManager)2 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)2 Session (org.hibernate.Session)2 MCRFSNODES (org.mycore.backend.hibernate.tables.MCRFSNODES)2 MCRContentStore (org.mycore.datamodel.ifs.MCRContentStore)2