Search in sources :

Example 16 with MCRPersistenceException

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

the class MCRHIBLinkTableStore method create.

/**
 * The method create a new item in the datastore.
 *
 * @param from
 *            a string with the link ID MCRFROM
 * @param to
 *            a string with the link ID MCRTO
 * @param type
 *            a string with the link ID MCRTYPE
 * @param attr
 *            a string with the link ID MCRATTR
 */
@Override
public final void create(String from, String to, String type, String attr) {
    if (from == null || (from = from.trim()).length() == 0) {
        throw new MCRPersistenceException("The from value is null or empty.");
    }
    if (to == null || (to = to.trim()).length() == 0) {
        throw new MCRPersistenceException("The to value is null or empty.");
    }
    if (type == null || (type = type.trim()).length() == 0) {
        throw new MCRPersistenceException("The type value is null or empty.");
    }
    if (attr == null || (attr = attr.trim()).length() == 0) {
        attr = "";
    }
    EntityManager entityMananger = MCREntityManagerProvider.getCurrentEntityManager();
    LOGGER.debug("Inserting {}/{}/{} into database MCRLINKHREF", from, to, type);
    MCRLINKHREFPK key = getKey(from, to, type);
    MCRLINKHREF linkHref = entityMananger.find(MCRLINKHREF.class, key);
    if (linkHref != null) {
        linkHref.setMcrattr(attr);
    } else {
        linkHref = new MCRLINKHREF();
        linkHref.setKey(key);
        linkHref.setMcrattr(attr);
        entityMananger.persist(linkHref);
    }
}
Also used : EntityManager(javax.persistence.EntityManager) MCRLINKHREFPK(org.mycore.backend.jpa.links.MCRLINKHREFPK) MCRLINKHREF(org.mycore.backend.jpa.links.MCRLINKHREF) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 17 with MCRPersistenceException

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

the class MCRHIBLinkTableStore method delete.

/**
 * The method removes a item for the from ID from the datastore.
 *
 * @param from
 *            a string with the link ID MCRFROM
 * @param to
 *            a string with the link ID MCRTO
 * @param type
 *            a string with the link ID MCRTYPE
 */
@Override
public final void delete(String from, String to, String type) {
    if (from == null || (from = from.trim()).length() == 0) {
        throw new MCRPersistenceException("The from value is null or empty.");
    }
    StringBuilder sb = new StringBuilder();
    sb.append("from ").append(classname).append(" where MCRFROM = '").append(from).append("'");
    if (to != null && (to = to.trim()).length() > 0) {
        sb.append(" and MCRTO = '").append(to).append("'");
    }
    if (type != null && (type = type.trim()).length() > 0) {
        sb.append(" and MCRTYPE = '").append(type).append("'");
    }
    LOGGER.debug("Deleting {} from database MCRLINKHREF", from);
    Session session = getSession();
    for (MCRLINKHREF mcrlinkhref : session.createQuery(sb.toString(), MCRLINKHREF.class).getResultList()) {
        session.delete(mcrlinkhref);
    }
}
Also used : MCRLINKHREF(org.mycore.backend.jpa.links.MCRLINKHREF) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Session(org.hibernate.Session)

Example 18 with MCRPersistenceException

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

the class MCRCStoreIFS2 method exists.

@Override
protected boolean exists(org.mycore.datamodel.ifs.MCRFile fr) {
    int slotID = getSlotID(fr.getOwnerID());
    String base = getBase(fr.getOwnerID());
    MCRFileStore store = getStore(base);
    try {
        MCRFileCollection slot = store.retrieve(slotID);
        if (slot == null)
            return false;
        String path = fr.getAbsolutePath();
        MCRNode node = slot.getNodeByPath(path);
        return (node != null);
    } catch (IOException ex) {
        String msg = "Exception checking existence of file " + fr.getAbsolutePath();
        throw new MCRPersistenceException(msg, ex);
    }
}
Also used : IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 19 with MCRPersistenceException

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

the class MCRDirectoryStream method deleteFileSystemNode.

/**
 * Deletes {@link MCRFilesystemNode} if it exists.
 *
 * @param path
 *            relative or absolute
 * @throws IOException
 */
private void deleteFileSystemNode(MCRPath path) throws IOException {
    checkClosed();
    if (path.isAbsolute()) {
        Files.delete(path);
    }
    MCRFilesystemNode childByPath = dir.getChildByPath(path.toString());
    if (childByPath == null) {
        throw new NoSuchFileException(dir.toPath().toString(), path.toString(), null);
    }
    try {
        childByPath.delete();
    } catch (MCRPersistenceException e) {
        throw new IOException("Error whil deleting file system node.", e);
    }
}
Also used : MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 20 with MCRPersistenceException

use of org.mycore.common.MCRPersistenceException 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

MCRPersistenceException (org.mycore.common.MCRPersistenceException)36 IOException (java.io.IOException)18 MCRAccessException (org.mycore.access.MCRAccessException)13 JDOMException (org.jdom2.JDOMException)9 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)8 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)8 MCRPath (org.mycore.datamodel.niofs.MCRPath)8 MCRException (org.mycore.common.MCRException)7 SAXException (org.xml.sax.SAXException)6 File (java.io.File)5 PersistenceException (javax.persistence.PersistenceException)5 Path (java.nio.file.Path)4 UncheckedIOException (java.io.UncheckedIOException)3 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3 Date (java.util.Date)3 EntityManager (javax.persistence.EntityManager)3 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)3 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)3 SignedJWT (com.nimbusds.jwt.SignedJWT)2 PrintWriter (java.io.PrintWriter)2