use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.
the class MCRIFS2Commands method repairUnicodeInDatabase.
@MCRCommand(syntax = "repair unicode in database {0}", help = "this fixes consequences of MCR-1423 in Database. If " + "{0} is false then nothing will be done (dry run).")
public static void repairUnicodeInDatabase(String execute) {
boolean dry = execute.toLowerCase(Locale.ROOT).equals(Boolean.FALSE.toString().toLowerCase(Locale.ROOT));
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MCRFSNODES> getQuery = cb.createQuery(MCRFSNODES.class);
List<MCRFSNODES> resultList = em.createQuery(getQuery.select(getQuery.from(MCRFSNODES.class))).getResultList();
resultList.stream().forEach(node -> {
String unnormalName = node.getName();
String normalName = MCRXMLFunctions.normalizeUnicode(unnormalName);
if (!unnormalName.equals(normalName)) {
LOGGER.info("{} node {} with name {}", (dry) ? "Would Fix" : "Fixing", node.getId(), unnormalName);
if (!dry) {
node.setName(normalName);
}
}
});
}
use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.
the class MCRIFS2Commands method fixFileEntry.
private static void fixFileEntry(File node, String content_store, String derivate_id, String storage_base, boolean check_only) {
LOGGER.debug("fixFileEntry : name = {}", node.getName());
String storageid = node.getAbsolutePath().substring(storage_base.length()).replace("\\", "/");
LOGGER.debug("fixFileEntry : storageid = {}", storageid);
String id = "";
String md5_old = "";
long size_old = 0;
boolean foundEntry = false;
MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
boolean transactionActive = mcrSession.isTransactionActive();
if (!transactionActive) {
mcrSession.beginTransaction();
}
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
try {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
try {
MCRFSNODES fsNode = em.createQuery(query.where(cb.equal(nodes.get(MCRFSNODES_.owner), derivate_id), cb.equal(nodes.get(MCRFSNODES_.storeid), content_store), cb.equal(nodes.get(MCRFSNODES_.storageid), storageid), cb.equal(nodes.get(MCRFSNODES_.type), "F"))).getSingleResult();
LOGGER.debug("Found file entry for {}", storageid);
foundEntry = true;
id = fsNode.getId();
md5_old = fsNode.getMd5();
size_old = fsNode.getSize();
em.detach(fsNode);
} catch (NoResultException e) {
LOGGER.error("Can't find file entry for {}", storageid);
if (check_only)
return;
} catch (NonUniqueResultException e) {
LOGGER.error("Non unique file entry for {}", storageid);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
// check fctid, size and MD5 of the file
String fctid = "";
String md5 = "";
try (MCRContentInputStream cis = new MCRContentInputStream(new FileInputStream(node))) {
byte[] header = cis.getHeader();
fctid = MCRFileContentTypeFactory.detectType(node.getName(), header).getID();
ByteStreams.copy(cis, ByteStreams.nullOutputStream());
md5 = cis.getMD5String();
} catch (MCRException | IOException e1) {
e1.printStackTrace();
return;
}
long size = node.length();
LOGGER.debug("size old : {} <--> size : {}", Long.toString(size_old), Long.toString(size));
LOGGER.debug("MD5 old : {} <--> MD5 : {}", md5_old, md5);
if (size_old == size && md5_old.equals(md5)) {
return;
}
if (foundEntry && size_old != size) {
LOGGER.warn("Wrong file size for {} : {} <-> {}", storageid, size_old, size);
}
if (foundEntry && !md5.equals(md5_old)) {
LOGGER.warn("Wrong file md5 for {} : {} <-> {}", storageid, md5_old, md5);
}
if (check_only)
return;
// fix entry
LOGGER.info("Fix entry for file {}", storageid);
if (!foundEntry) {
MCRFileMetadataManager fmmgr = MCRFileMetadataManager.instance();
id = fmmgr.createNodeID();
}
String pid = null;
try {
pid = getParentID(node, derivate_id);
} catch (NoResultException e1) {
LOGGER.error("Can't find parent id of directory for file {}", storageid);
} 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("F");
mcrfsnodes.setOwner(derivate_id);
mcrfsnodes.setName(node.getName());
mcrfsnodes.setSize(size);
mcrfsnodes.setDate(new Date(node.lastModified()));
mcrfsnodes.setStoreid(content_store);
mcrfsnodes.setStorageid(storageid);
mcrfsnodes.setFctid(fctid);
mcrfsnodes.setMd5(md5);
em.merge(mcrfsnodes);
mcrSession.commitTransaction();
LOGGER.debug("Entry {} fixed.", node.getName());
} catch (PersistenceException pe) {
mcrSession.rollbackTransaction();
pe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.
the class MCRIFSCommands method checkMCRFSNODESForDerivatesWithProjectID.
@MCRCommand(syntax = "check derivates of mcrfsnodes with project id {0}", help = "check the entries of MCRFSNODES for all derivates with project ID {0}")
public static void checkMCRFSNODESForDerivatesWithProjectID(String project_id) {
LOGGER.info("Start check of MCRFSNODES for derivates with project ID {}", project_id);
if (project_id == null || project_id.length() == 0) {
LOGGER.error("Project ID missed for check MCRFSNODES entries of derivates with project ID {0}");
return;
}
Map<String, MCRContentStore> availableStores = MCRContentStoreFactory.getAvailableStores();
Session session = MCRHIBConnection.instance().getSession();
MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
List<String> id_list = mgr.listIDsForBase(project_id + "_derivate");
int counter = 0;
int maxresults = id_list.size();
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
ParameterExpression<String> ownerID = cb.parameter(String.class);
TypedQuery<MCRFSNODES> typedQuery = em.createQuery(query.where(cb.equal(nodes.get(MCRFSNODES_.owner), ownerID), cb.equal(nodes.get(MCRFSNODES_.type), "F")).orderBy(cb.asc(nodes.get(MCRFSNODES_.storageid))));
for (String derid : id_list) {
counter++;
LOGGER.info("Processing dataset {} from {} with ID: {}", counter, maxresults, derid);
// check mcrfsnodes entries
try {
AtomicInteger nodeCount = new AtomicInteger();
typedQuery.setParameter(ownerID, derid);
typedQuery.getResultList().stream().forEach(fsNode -> {
nodeCount.incrementAndGet();
String store_name = fsNode.getStoreid();
String storageid = fsNode.getStorageid();
String name = fsNode.getName();
long size = fsNode.getSize();
Date date = fsNode.getDate();
GregorianCalendar datecal = new GregorianCalendar(TimeZone.getDefault(), Locale.getDefault());
datecal.setTime(date);
String fctid = fsNode.getFctid();
String md5 = fsNode.getMd5();
session.evict(fsNode);
LOGGER.debug("File for [owner] {} [name] {} [storeid] {} [storageid] {} [fctid] {} [size] {} [md5] {}", derid, name, store_name, storageid, fctid, size, md5);
// get path of file
MCRContentStore fs_store = availableStores.get(store_name);
if (fs_store == null) {
LOGGER.error("Can't find content store {}", store_name);
return;
}
try {
File content_file = fs_store.getLocalFile(storageid);
if (content_file == null || !content_file.canRead()) {
LOGGER.error(" !!!! Can't access to file {} of store {}", storageid, store_name);
}
} catch (Exception e) {
LOGGER.error(" !!!! Can't access to file {} of store {}", storageid, store_name);
}
});
if (nodeCount.get() == 0) {
LOGGER.error(" !!!! Can't find file entries in MCRFSNODES for {}", derid);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
session.clear();
}
}
LOGGER.info("Check done for {} entries", Integer.toString(counter));
}
use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.
the class MCRIFSCommands method writeMD5SumFile.
@MCRCommand(syntax = "generate md5sum files in directory {0}", help = "writes md5sum files for every content store in directory {0}")
public static void writeMD5SumFile(String targetDirectory) throws IOException {
File targetDir = getDirectory(targetDirectory);
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
MCRStreamQuery<MCRFSNODES> streamQuery = MCRStreamQuery.getInstance(em, "from MCRFSNODES where type='F' order by storeid, storageid", MCRFSNODES.class);
Map<String, MCRContentStore> availableStores = MCRContentStoreFactory.getAvailableStores();
String currentStoreId = null;
MCRContentStore currentStore = null;
File currentStoreBaseDir = null;
BufferedWriter bw = null;
String nameOfProject = MCRConfiguration.instance().getString("MCR.NameOfProject", "MyCoRe");
try {
Iterator<MCRFSNODES> fsnodes = streamQuery.getResultStream().iterator();
while (fsnodes.hasNext()) {
MCRFSNODES fsNode = fsnodes.next();
String storeID = fsNode.getStoreid();
String storageID = fsNode.getStorageid();
String md5 = fsNode.getMd5();
em.detach(fsNode);
if (!storeID.equals(currentStoreId)) {
// initialize current store
currentStoreId = storeID;
currentStore = availableStores.get(storeID);
if (bw != null) {
bw.close();
}
File outputFile = new File(targetDir, MessageFormat.format("{0}-{1}.md5", nameOfProject, storeID));
LOGGER.info("Writing to file: {}", outputFile.getAbsolutePath());
bw = Files.newBufferedWriter(outputFile.toPath(), Charset.defaultCharset(), StandardOpenOption.CREATE);
try {
currentStoreBaseDir = currentStore.getBaseDir();
} catch (Exception e) {
LOGGER.warn("Could not get baseDir of store: {}", storeID, e);
currentStoreBaseDir = null;
}
}
String path = currentStoreBaseDir != null ? currentStore.getLocalFile(storageID).getAbsolutePath() : storageID;
// current store initialized
String line = MessageFormat.format("{0} {1}\n", md5, path);
bw.write(line);
}
} finally {
if (bw != null) {
try {
bw.close();
} catch (IOException e1) {
LOGGER.warn("Error while closing file.", e1);
}
}
em.clear();
}
}
use of org.mycore.backend.hibernate.tables.MCRFSNODES in project mycore by MyCoRe-Org.
the class MCRIFSCommands method checkDerivatesWithProjectIDInMCRFSNODES.
@MCRCommand(syntax = "check mcrfsnodes of derivates with project id {0}", help = "check the entries of MCRFSNODES with project ID {0} that the derivate exists")
public static void checkDerivatesWithProjectIDInMCRFSNODES(String project_id) {
LOGGER.info("Start check of MCRFSNODES for derivates with project ID {}", project_id);
if (project_id == null || project_id.length() == 0) {
LOGGER.error("Project ID missed for check MCRFSNODES entries of derivates with project ID {0}");
return;
}
MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<String> query = cb.createQuery(String.class);
Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
AtomicInteger counter = new AtomicInteger();
em.createQuery(query.distinct(true).select(nodes.get(MCRFSNODES_.owner)).where(cb.like(nodes.get(MCRFSNODES_.owner), project_id + "\\_%"))).getResultList().stream().peek(ignore -> counter.incrementAndGet()).map(MCRObjectID::getInstance).filter(derID -> {
try {
return !mgr.exists(derID);
} catch (IOException e) {
LOGGER.error("Error while checking existence of {}", derID, e);
return true;
}
}).forEach(missingDerivate -> LOGGER.error(" !!!! Can't find MCRFSNODES entry {} as existing derivate", missingDerivate));
LOGGER.info("Check done for {} entries", counter.get());
}
Aggregations