Search in sources :

Example 6 with MCRFSNODES

use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.

the class MCRIFSCommands method writeReport.

private static void writeReport(File targetDir, FSNodeChecker checker) throws TransformerFactoryConfigurationError, SAXException, IOException, TransformerConfigurationException {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    MCRStreamQuery<MCRFSNODES> query = MCRStreamQuery.getInstance(em, "from MCRFSNODES where type='F' order by storeid, owner, name", MCRFSNODES.class);
    Map<String, MCRContentStore> availableStores = MCRContentStoreFactory.getAvailableStores();
    String currentStoreId = null;
    MCRContentStore currentStore = null;
    File currentStoreBaseDir = null;
    StreamResult streamResult = null;
    String nameOfProject = MCRConfiguration.instance().getString("MCR.NameOfProject", "MyCoRe");
    SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler th = null;
    Attributes2Impl atts = new Attributes2Impl();
    final String rootName = checker.getName();
    final String elementName = ELEMENT_FILE;
    final String ATT_BASEDIR = "basedir";
    final String nsURI = NS_URI;
    final String ATT_TYPE = CDATA;
    String owner = null;
    try (Stream<MCRFSNODES> resultStream = query.getResultStream()) {
        Iterator<MCRFSNODES> fsnodes = resultStream.iterator();
        while (fsnodes.hasNext()) {
            MCRFSNODES fsNode = fsnodes.next();
            String storeID = fsNode.getStoreid();
            String storageID = fsNode.getStorageid();
            em.detach(fsNode);
            if (!storeID.equals(currentStoreId)) {
                // initialize current store
                currentStoreId = storeID;
                currentStore = availableStores.get(storeID);
                if (th != null) {
                    th.endElement(nsURI, rootName, rootName);
                    th.endDocument();
                    OutputStream outputStream = streamResult.getOutputStream();
                    if (outputStream != null) {
                        outputStream.close();
                    }
                }
                File outputFile = new File(targetDir, MessageFormat.format("{0}-{1}-{2}.xml", nameOfProject, storeID, rootName));
                streamResult = new StreamResult(new FileOutputStream(outputFile));
                th = tf.newTransformerHandler();
                Transformer serializer = th.getTransformer();
                serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                serializer.setOutputProperty(OutputKeys.INDENT, "yes");
                th.setResult(streamResult);
                LOGGER.info("Writing to file: {}", outputFile.getAbsolutePath());
                th.startDocument();
                atts.clear();
                atts.addAttribute(nsURI, "project", "project", ATT_TYPE, nameOfProject);
                try {
                    currentStoreBaseDir = currentStore.getBaseDir();
                    atts.addAttribute(nsURI, ATT_BASEDIR, ATT_BASEDIR, ATT_TYPE, currentStoreBaseDir.getAbsolutePath());
                } catch (Exception e) {
                    LOGGER.warn("Could not get baseDir of store: {}", storeID, e);
                    currentStoreBaseDir = null;
                }
                th.startElement(nsURI, rootName, rootName, atts);
            }
            if (!fsNode.getOwner().equals(owner)) {
                owner = fsNode.getOwner();
                LOGGER.info("Checking owner/derivate: {}", owner);
            }
            File f = null;
            try {
                f = currentStore.getLocalFile(storageID);
            } catch (IOException e) {
                LOGGER.warn("Missing file with storageID: {}", storageID);
            }
            if (!checker.checkNode(fsNode, f, atts)) {
                th.startElement(nsURI, elementName, elementName, atts);
                th.endElement(nsURI, elementName, elementName);
            }
        }
    } finally {
        em.clear();
        if (th != null) {
            try {
                th.endElement(nsURI, rootName, rootName);
                th.endDocument();
                OutputStream outputStream = streamResult.getOutputStream();
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e1) {
                LOGGER.warn("Error while closing file.", e1);
            }
        }
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Attributes2Impl(org.xml.sax.ext.Attributes2Impl) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) IOException(java.io.IOException) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) 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) FileOutputStream(java.io.FileOutputStream) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) MCRFile(org.mycore.datamodel.ifs.MCRFile) File(java.io.File)

Example 7 with MCRFSNODES

use of org.mycore.backend.hibernate.tables.MCRFSNODES 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 8 with MCRFSNODES

use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.

the class MCRHIBFileMetadataStore method retrieveNode.

@Override
public MCRFilesystemNode retrieveNode(String ID) throws MCRPersistenceException {
    Session session = getSession();
    MCRFSNODES node = session.get(MCRFSNODES.class, ID);
    if (node == null) {
        LOGGER.warn("There is no FSNODE with ID = {}", ID);
        return null;
    }
    return buildNode(node);
}
Also used : MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) Session(org.hibernate.Session)

Example 9 with MCRFSNODES

use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.

the class MCRHIBFileMetadataStore method getOwnerIDs.

@Override
public Iterable<String> getOwnerIDs() throws MCRPersistenceException {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<String> query = cb.createQuery(String.class);
    Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
    return em.createQuery(query.distinct(true).select(nodes.get(MCRFSNODES_.owner))).getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES)

Example 10 with MCRFSNODES

use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.

the class MCRHIBFileMetadataStore method storeNode.

@Override
public void storeNode(MCRFilesystemNode node) throws MCRPersistenceException {
    String ID = node.getID();
    String PID = node.getParentID();
    String OWNER = node.getOwnerID();
    String NAME = node.getName();
    String LABEL = node.getLabel();
    long SIZE = node.getSize();
    GregorianCalendar DATE = node.getLastModified();
    String TYPE = null;
    String STOREID = null;
    String STORAGEID = null;
    String FCTID = null;
    String MD5 = null;
    int NUMCHDD = 0;
    int NUMCHDF = 0;
    int NUMCHTD = 0;
    int NUMCHTF = 0;
    if (node instanceof MCRFile) {
        MCRFile file = (MCRFile) node;
        TYPE = "F";
        STOREID = file.getStoreID();
        STORAGEID = file.getStorageID();
        FCTID = file.getContentTypeID();
        MD5 = file.getMD5();
    } else if (node instanceof MCRDirectory) {
        MCRDirectory dir = (MCRDirectory) node;
        TYPE = "D";
        NUMCHDD = dir.getNumChildren(MCRDirectory.DIRECTORIES, MCRDirectory.HERE);
        NUMCHDF = dir.getNumChildren(MCRDirectory.FILES, MCRDirectory.HERE);
        NUMCHTD = dir.getNumChildren(MCRDirectory.DIRECTORIES, MCRDirectory.TOTAL);
        NUMCHTF = dir.getNumChildren(MCRDirectory.FILES, MCRDirectory.TOTAL);
    } else {
        throw new MCRPersistenceException("MCRFilesystemNode must be either MCRFile or MCRDirectory");
    }
    Session session = getSession();
    MCRFSNODES fs = session.get(MCRFSNODES.class, ID);
    if (fs == null) {
        fs = new MCRFSNODES();
        fs.setId(ID);
    }
    fs.setPid(PID);
    fs.setType(TYPE);
    fs.setOwner(OWNER);
    fs.setName(NAME);
    fs.setLabel(LABEL);
    fs.setSize(SIZE);
    fs.setDate(new Timestamp(DATE.getTime().getTime()));
    fs.setStoreid(STOREID);
    fs.setStorageid(STORAGEID);
    fs.setFctid(FCTID);
    fs.setMd5(MD5);
    fs.setNumchdd(NUMCHDD);
    fs.setNumchdf(NUMCHDF);
    fs.setNumchtd(NUMCHTD);
    fs.setNumchtf(NUMCHTF);
    if (!session.contains(fs)) {
        session.saveOrUpdate(fs);
    }
}
Also used : MCRFile(org.mycore.datamodel.ifs.MCRFile) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) GregorianCalendar(java.util.GregorianCalendar) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Timestamp(java.sql.Timestamp) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) Session(org.hibernate.Session)

Aggregations

MCRFSNODES (org.mycore.backend.hibernate.tables.MCRFSNODES)14 EntityManager (javax.persistence.EntityManager)12 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)8 IOException (java.io.IOException)7 MCRException (org.mycore.common.MCRException)7 File (java.io.File)6 Session (org.hibernate.Session)6 MCRFile (org.mycore.datamodel.ifs.MCRFile)6 FileNotFoundException (java.io.FileNotFoundException)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 NotDirectoryException (java.nio.file.NotDirectoryException)5 Date (java.util.Date)5 NoSuchElementException (java.util.NoSuchElementException)5 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 MCRContentStore (org.mycore.datamodel.ifs.MCRContentStore)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 SAXException (org.xml.sax.SAXException)5 GregorianCalendar (java.util.GregorianCalendar)4 BufferedWriter (java.io.BufferedWriter)3 FileInputStream (java.io.FileInputStream)3