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);
}
}
}
}
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());
}
}
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);
}
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();
}
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);
}
}
Aggregations