Search in sources :

Example 6 with MCRContentStore

use of org.mycore.datamodel.ifs.MCRContentStore in project mycore by MyCoRe-Org.

the class MCRIFSCommands method moveContentToNewStore.

private static List<String> moveContentToNewStore(String source_store, String target_store, String select_key, String select_value) {
    // check stores
    MCRContentStore from_store = MCRContentStoreFactory.getStore(source_store);
    @SuppressWarnings("unused") MCRContentStore to_store = MCRContentStoreFactory.getStore(target_store);
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    MCRStreamQuery<MCRFSNODES> streamQuery = MCRStreamQuery.getInstance(em, "from MCRFSNODES where storeid=:storeid and " + select_key + "=:selectValue order by owner", MCRFSNODES.class).setParameter("storeid", from_store.getID()).setParameter("selectValue", select_value);
    try (Stream<MCRFSNODES> resultStream = streamQuery.getResultStream()) {
        return resultStream.peek(em::detach).map(MCRFSNODES::getId).map(ifsId -> String.format(Locale.ROOT, "move ifs node %s to store %s", ifsId, target_store)).collect(Collectors.toList());
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Arrays(java.util.Arrays) Date(java.util.Date) StreamResult(javax.xml.transform.stream.StreamResult) FileTime(java.nio.file.attribute.FileTime) MCRStreamQuery(org.mycore.backend.jpa.MCRStreamQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) URI(java.net.URI) Method(java.lang.reflect.Method) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ParameterExpression(javax.persistence.criteria.ParameterExpression) GregorianCalendar(java.util.GregorianCalendar) TimeZone(java.util.TimeZone) StandardOpenOption(java.nio.file.StandardOpenOption) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRUtils(org.mycore.common.MCRUtils) List(java.util.List) Attributes2Impl(org.xml.sax.ext.Attributes2Impl) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) SAXException(org.xml.sax.SAXException) MCRContentStoreFactory(org.mycore.datamodel.ifs.MCRContentStoreFactory) Session(org.hibernate.Session) MCRConfiguration(org.mycore.common.config.MCRConfiguration) TypedQuery(javax.persistence.TypedQuery) MCRException(org.mycore.common.MCRException) MessageFormat(java.text.MessageFormat) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) MCRFSNODES_(org.mycore.backend.hibernate.tables.MCRFSNODES_) Charset(java.nio.charset.Charset) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) Root(javax.persistence.criteria.Root) MCRFile(org.mycore.datamodel.ifs.MCRFile) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) NotDirectoryException(java.nio.file.NotDirectoryException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EntityManager(javax.persistence.EntityManager) OutputKeys(javax.xml.transform.OutputKeys) Field(java.lang.reflect.Field) File(java.io.File) NameFileComparator(org.apache.commons.io.comparator.NameFileComparator) TransformerHandler(javax.xml.transform.sax.TransformerHandler) LogManager(org.apache.logging.log4j.LogManager) EntityManager(javax.persistence.EntityManager) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES)

Example 7 with MCRContentStore

use of org.mycore.datamodel.ifs.MCRContentStore in project mycore by MyCoRe-Org.

the class MCRIFS2Commands method repairMcrdataXmlForDerivate.

@MCRCommand(syntax = "repair mcrdata.xml for derivate {0} in content store {1}", help = "repair the entries in mcrdata.xml with data from content store {1} for MCRDerivate {0}")
public static void repairMcrdataXmlForDerivate(String derivate_id, String content_store) {
    LOGGER.info("Start repair of mcrdata.xml for derivate {} in store {}", derivate_id, content_store);
    // check input;
    MCRObjectID mcr_derivate_id;
    try {
        mcr_derivate_id = MCRObjectID.getInstance(derivate_id);
    } catch (MCRException e) {
        LOGGER.error("Wrong derivate parameter, it is not a MCRObjectID");
        return;
    }
    if (content_store == null || content_store.length() == 0) {
        LOGGER.error("Empty content store parameter");
        return;
    }
    MCRContentStore store = MCRContentStoreFactory.getStore(content_store);
    if (!(store instanceof MCRCStoreIFS2)) {
        LOGGER.error("The content store is not a IFS2 type");
        return;
    }
    // repair
    try {
        MCRFileCollection file_collection = ((MCRCStoreIFS2) store).getIFS2FileCollection(mcr_derivate_id);
        file_collection.repairMetadata();
    } catch (IOException e) {
        LOGGER.error("Erroe while repair derivate with ID {}", mcr_derivate_id);
    }
}
Also used : MCRCStoreIFS2(org.mycore.datamodel.ifs2.MCRCStoreIFS2) MCRException(org.mycore.common.MCRException) MCRFileCollection(org.mycore.datamodel.ifs2.MCRFileCollection) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 8 with MCRContentStore

use of org.mycore.datamodel.ifs.MCRContentStore in project mycore by MyCoRe-Org.

the class MCRIFS2Commands method fixMCRFSNODESForDerivate.

private static void fixMCRFSNODESForDerivate(String content_store, String derivate_id, boolean check_only) {
    // check input
    MCRObjectID mcr_derivate_id;
    try {
        mcr_derivate_id = MCRObjectID.getInstance(derivate_id);
    } catch (MCRException e) {
        LOGGER.error("Wrong derivate parameter, it is not a MCRObjectID");
        return;
    }
    if (content_store == null || content_store.length() == 0) {
        LOGGER.error("Empty content store parameter");
        return;
    }
    MCRContentStore store = MCRContentStoreFactory.getStore(content_store);
    if (!(store instanceof MCRCStoreIFS2)) {
        LOGGER.error("The content store is not a IFS2 type");
        return;
    }
    // list all files
    try {
        MCRFileCollection file_collection = ((MCRCStoreIFS2) store).getIFS2FileCollection(mcr_derivate_id);
        File root_node = file_collection.getLocalFile();
        String storage_base = root_node.getAbsolutePath();
        storage_base = storage_base.substring(0, storage_base.length() - derivate_id.length());
        fixMCRFSNODESForNode(root_node, content_store, derivate_id, storage_base, check_only);
    } catch (IOException e) {
        LOGGER.error("Error while list all files of derivate with ID {}", mcr_derivate_id);
        e.printStackTrace();
    }
    Session session = MCRHIBConnection.instance().getSession();
    Transaction tx = session.getTransaction();
    if (tx.getStatus().isOneOf(TransactionStatus.ACTIVE)) {
        tx.commit();
    }
}
Also used : MCRCStoreIFS2(org.mycore.datamodel.ifs2.MCRCStoreIFS2) MCRException(org.mycore.common.MCRException) Transaction(org.hibernate.Transaction) MCRFileCollection(org.mycore.datamodel.ifs2.MCRFileCollection) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) File(java.io.File) Session(org.hibernate.Session) MCRSession(org.mycore.common.MCRSession)

Aggregations

MCRContentStore (org.mycore.datamodel.ifs.MCRContentStore)8 IOException (java.io.IOException)7 MCRException (org.mycore.common.MCRException)7 File (java.io.File)6 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)6 FileNotFoundException (java.io.FileNotFoundException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 NotDirectoryException (java.nio.file.NotDirectoryException)5 NoSuchElementException (java.util.NoSuchElementException)5 EntityManager (javax.persistence.EntityManager)5 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 MCRFile (org.mycore.datamodel.ifs.MCRFile)5 SAXException (org.xml.sax.SAXException)5 MCRFSNODES (org.mycore.backend.hibernate.tables.MCRFSNODES)4 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 Transformer (javax.xml.transform.Transformer)3 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)3 TransformerHandler (javax.xml.transform.sax.TransformerHandler)3 StreamResult (javax.xml.transform.stream.StreamResult)3