use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.
the class MCRHIBFileMetadataStore method retrieveChild.
@Override
public MCRFilesystemNode retrieveChild(String parentID, String name) {
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
TypedQuery<MCRFSNODES> childQuery = em.createNamedQuery("MCRFSNODES.getChild", MCRFSNODES.class);
childQuery.setParameter("pid", parentID);
childQuery.setParameter("name", name);
try {
MCRFSNODES node = childQuery.getSingleResult();
return buildNode(node);
} catch (NoResultException e) {
return null;
}
}
use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.
the class MCRHIBFileMetadataStore method retrieveChildren.
@Override
public List<MCRFilesystemNode> retrieveChildren(String parentID) throws MCRPersistenceException {
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
TypedQuery<MCRFSNODES> childQuery = em.createNamedQuery("MCRFSNODES.getChildren", MCRFSNODES.class);
childQuery.setParameter("pid", parentID);
return childQuery.getResultList().stream().map(this::buildNode).collect(Collectors.toList());
}
use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.
the class MCRIFS2Commands method getParentID.
private static String getParentID(File node, String derivate_id) throws NoResultException, NonUniqueResultException {
File parent_node = node.getParentFile();
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
MCRFSNODES fsNode = em.createQuery(query.where(cb.equal(nodes.get(MCRFSNODES_.owner), derivate_id), cb.equal(nodes.get(MCRFSNODES_.name), parent_node.getName()), cb.equal(nodes.get(MCRFSNODES_.type), "D"))).getSingleResult();
LOGGER.debug("Found directory entry for {}", parent_node.getName());
em.detach(fsNode);
return fsNode.getId();
}
use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.
the class MCRIFS2Commands method fixDirectoryEntry.
private static void fixDirectoryEntry(File node, String derivate_id, String storage_base, boolean check_only) {
String name = node.getName();
LOGGER.debug("fixDirectoryEntry : name = {}", name);
Session session = MCRHIBConnection.instance().getSession();
Transaction tx = session.getTransaction();
if (tx.getStatus().isNotOneOf(TransactionStatus.ACTIVE)) {
tx.begin();
}
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
try {
em.detach(em.createQuery(query.where(cb.equal(nodes.get(MCRFSNODES_.owner), derivate_id), cb.equal(nodes.get(MCRFSNODES_.name), name), cb.equal(nodes.get(MCRFSNODES_.type), "D"))).getSingleResult());
LOGGER.debug("Found directory entry for {}", name);
return;
} catch (NoResultException e) {
LOGGER.error("Can't find directory entry for {}", name);
if (check_only)
return;
} catch (NonUniqueResultException e) {
LOGGER.error("Non unique directory entry for {}", name);
return;
} catch (Exception e) {
e.printStackTrace();
}
// fix entry
LOGGER.info("Fix entry for directory {}", name);
MCRFileMetadataManager fmmgr = MCRFileMetadataManager.instance();
String id = fmmgr.createNodeID();
String pid = null;
try {
pid = getParentID(node, derivate_id);
} catch (NoResultException e1) {
if (!derivate_id.equals(name)) {
LOGGER.error("Can't find parent id for directory {}", name);
return;
}
} catch (NonUniqueResultException e1) {
LOGGER.error("The directory entry for {} and {} is not unique!", derivate_id, node.getParentFile().getName());
return;
}
try {
MCRFSNODES mcrfsnodes = new MCRFSNODES();
mcrfsnodes.setId(id);
mcrfsnodes.setPid(pid);
mcrfsnodes.setType("D");
mcrfsnodes.setOwner(derivate_id);
mcrfsnodes.setName(node.getName());
mcrfsnodes.setDate(new Date(node.lastModified()));
em.persist(mcrfsnodes);
tx.commit();
LOGGER.debug("Entry {} fixed.", name);
} catch (HibernateException he) {
if (tx != null) {
tx.rollback();
}
he.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations