Mercurial > hg > LGServices
view src/main/java/de/mpiwg/gazetteer/utils/FileManager.java @ 54:a00efd5d9e77
new: adding delete saved table function
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Thu, 04 Feb 2016 11:30:46 +0100 |
parents | cf747a960516 |
children | b8ad346e39a0 |
line wrap: on
line source
package de.mpiwg.gazetteer.utils; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import de.mpiwg.gazetteer.bo.LGBranch; import de.mpiwg.gazetteer.bo.LGFile; import de.mpiwg.gazetteer.bo.LGFullTextSearchFile; import de.mpiwg.gazetteer.db.DBBook; import de.mpiwg.gazetteer.db.DBContents; public class FileManager { private static Logger logger = Logger.getLogger(FileManager.class); private static DateFormat dfmt = new SimpleDateFormat( "yyyy.MM.dd_hh.mm.ss.SSS" ); private static String getRootPath() throws Exception{ String base = PropertiesUtils.getPropValue("files_root"); if(StringUtils.isEmpty(base)){ throw new Exception("Property files_root no found. The files can be stored without this property."); } if(!base.endsWith("/")){ base += "/"; } logger.info("files_root=" + base); return base; } public static void saveFullTextSearchFileAsCsv(LGFullTextSearchFile file, Long userId, List<DBContents> list) throws Exception{ // String absolutePath = PropertiesUtils.getPropValue("ftsearch_root") + "/csv/" + userId.toString() + "/"; String absolutePath = PropertiesUtils.getPropValue("lgmap_datasets") + "/"; String fileName = file.getUserId() + "_" + file.getFileName() + ".csv"; File folder = new File(absolutePath); folder.mkdirs(); logger.info("Trying to save file " + absolutePath + fileName + "."); PrintWriter out = new PrintWriter(absolutePath + fileName); String text = new String(); // make it from list text += "Address,LEVEL1,LEVEL2,Name,DYNASTY,PERIOD,ADMIN_TYPE,TimeSpan:begin,TimeSpan:end,Longitude,Latitude,PAGE,SECTION,CONTENT,BOOK_ID,Description\n"; for (DBContents c : list) { if (c.getSection() != null && !c.isRemoved()){ // only those are not removed to csv which means to LGMap DBBook book = c.getSection().getBook(); String description = book.getVolume() + "/" + book.getAuthor() + "/" + book.getEdition(); text += c.getCoordinatesBook().getPlace_name() + "," + book.getLevel1() + "," + book.getLevel2() + "," + book.getName() + "," + book.getDynasty() + "," + book.getPeriod() + "," + book.getAdmin_type() + "," + book.getStart_year() + "," + book.getEnd_year() + "," + c.getCoordinatesBook().getX() + "," + c.getCoordinatesBook().getY() + "," + c.getPage() + "," + c.getSection().getName() + "," + c.getContent() + "," + c.getBookId() + "," + description + "\n"; } } text = text.substring(0, text.length()-2); // cut the last 2 chars, which are "\n" here out.println(text); out.close(); logger.info("The file " + fileName + " has been saved correctly."); } private static String getLGMapUrl(LGFullTextSearchFile file) throws IOException{ String lgmap = PropertiesUtils.getPropValue("lgmap") + "&file=" + file.getUserId().toString() + "_" + file.getFileName() + ".csv&name=" + file.getSearchTerms(); return lgmap; } public static void saveFullTextSearchFileAsHtml(LGFullTextSearchFile file, Long userId, List<DBContents> list) throws Exception{ String absolutePath = PropertiesUtils.getPropValue("ftsearch_root") + "/html/" + userId.toString() + "/"; String fileName = file.getFileName() + ".html"; File folder = new File(absolutePath); folder.mkdirs(); logger.info("Trying to save file " + absolutePath + fileName + "."); PrintWriter out = new PrintWriter(absolutePath + fileName); String text = new String(); String header = new String(); text += "<html>"; String root_server = PropertiesUtils.getPropValue("root_server"); header = "<head>" + "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>" + "<link href='" + root_server + "/resources/css/style.css' type='text/css' rel='stylesheet'/>" + "<script src='" + root_server + "/resources/js/jquery.min.js' type='text/javascript'></script>" + "<script src='" + root_server + "/resources/js/LGSearch.js' type='text/javascript'></script>" + "</head>"; text += header; text += "<body>" + "<div class='subTitel'>Table name: " + file.getFileName() + "<div>" + "<div class='label'>" + list.size() + " row(s) in the table.</dvi>" + "<div class='label'>searching by keywords: " + '"' + "<span id='searchTerm'>" + file.getSearchTerms() + "</span>" + '"' + "</div>" + "<div class='label'><a href='" + getLGMapUrl(file) + "' target='_blank'>view on LGMap</a><div>" + "<br>" + "<table class='pageTable'>" + "<tr>" + "<td class='tableTitle'>#</td>" + "<td class='tableTitle'>book id</td>" + "<td class='tableTitle'>book name</td>" + "<td class='tableTitle'>level1</td>" + "<td class='tableTitle'>level2</td>" + "<td class='tableTitle'>dynasty</td>" + "<td class='tableTitle'>period</td>" + "<td class='tableTitle'>admin type</td>" + "<td class='tableTitle'>section name</td>" + "<td class='tableTitle'>page</td>" + "<td class='tableTitle'>content</td>" + "<td class='tableTitle'>content id</td>" + "<td class='tableTitle'>isRemoved</td>" + "<tr>"; for (DBContents c : list) { //if ( c.getSection() != null && !c.isRemoved()){ if ( c.getSection() != null){ DBBook book = c.getSection().getBook(); text += "<tr>" + "<td>" + c.getInx() + "</td>" + "<td>" + c.getBookId() + "</td>" + "<td>" + book.getName() + "</td>" + "<td>" + book.getLevel1() + "</td>" + "<td>" + book.getLevel2() + "</td>" + "<td>" + book.getDynasty() + "</td>" + "<td>" + book.getPeriod() + "</td>" + "<td>" + book.getAdmin_type() + "</td>" + "<td>" + c.getSection().getName() + "</td>" + "<td>" + c.getPage() + "</td>" + "<td class='content'>" + c.getContent() + "</td>" + "<td>" + c.getId() + "</td>" + "<td>" + c.isRemoved() + "</td>" + "</tr>"; } } text += "</table></body></html>"; out.println(text); out.close(); logger.info("The file " + fileName + " has been saved correctly."); } public static String saveFile(LGBranch branch, LGFile file, Date date, Long userId) throws Exception{ String absolutePath = getRootPath() + branch.getSectionId() + "/"; String fileName = branch.getSectionId() + "_" + branch.getId() + "_" + file.getId() + "_" + dfmt.format(date) + "_" + userId + ".txt"; File folder = new File(absolutePath); folder.mkdirs(); logger.info("Trying to save file " + absolutePath + fileName + "."); PrintWriter out = new PrintWriter(absolutePath + fileName); out.println(file.getContent()); out.close(); logger.info("The file " + fileName + " has been saved correctly."); return fileName; } public static String getFileAsText(LGFile file) throws Exception{ LGBranch branch = DataProvider.getInstance().getBranch(file.getBranchId()); String absolutePath = getRootPath() + branch.getSectionId() + "/" + file.getFileName(); System.out.println("Loading: " + absolutePath); byte[] encoded = Files.readAllBytes(Paths.get(absolutePath)); return new String(encoded, "UTF8"); } public static File getFileAsFile(LGFile file) throws Exception{ LGBranch branch = DataProvider.getInstance().getBranch(file.getBranchId()); String absolutePath = getRootPath() + branch.getSectionId() + "/" + file.getFileName(); System.out.println("Loading: " + absolutePath); return new File(absolutePath); } public static String getFullTextSearchHtmlFileText(LGFullTextSearchFile file) throws Exception { String absolutePath = PropertiesUtils.getPropValue("ftsearch_root") + "/html/" + file.getUserId().toString() + "/" + file.getFileName() + ".html"; System.out.println("Loading: " + absolutePath); byte[] encoded = Files.readAllBytes(Paths.get(absolutePath)); return new String(encoded, "UTF8"); } protected static void deleteFileFromFileSystemByAbsolutePath(String absolutePath) { try{ File file = new File(absolutePath); if(file.delete()){ System.out.println(file.getName() + " is deleted!"); }else{ System.out.println("Delete operation is failed."); } }catch(Exception e){ e.printStackTrace(); } } public static void deleteFullTextSearchFileCsv(LGFullTextSearchFile file) throws IOException { // csv file is in LGMap/datasets/ String absolutePath = PropertiesUtils.getPropValue("lgmap_datasets") + "/" + file.getUserId() + "_" + file.getFileName() + ".csv"; deleteFileFromFileSystemByAbsolutePath(absolutePath); } public static void deleteFullTextSearchFileHtml(LGFullTextSearchFile file) throws IOException { // html file is in ftsearch-data/... String absolutePath = PropertiesUtils.getPropValue("ftsearch_root") + "/html/" + file.getUserId().toString() + "/" + file.getFileName() + ".html"; deleteFileFromFileSystemByAbsolutePath(absolutePath); } }