diff src/main/java/de/mpiwg/gazetteer/utils/FileManager.java @ 39:37840afb7b80

new: full text search
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Fri, 04 Dec 2015 14:28:44 +0100
parents 3e62083dbcbf
children 35ed4e650a53
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/gazetteer/utils/FileManager.java	Fri Nov 20 15:37:04 2015 +0100
+++ b/src/main/java/de/mpiwg/gazetteer/utils/FileManager.java	Fri Dec 04 14:28:44 2015 +0100
@@ -1,20 +1,23 @@
 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.Paths;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
-import java.util.UUID;
+import java.util.List;
 
-import org.apache.commons.lang.RandomStringUtils;
 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 {
 
@@ -40,6 +43,140 @@
 		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,PERIOD,TimeSpan:begin,TimeSpan:end,Longitude,Latitude,PAGE,SECTION,CONTENT,BOOK_ID,Description\n";
+	
+		for (DBContents r : list) {
+			
+			DBBook book = r.getSection().getBook();
+			
+			String description = book.getVolume() + "/" + book.getAuthor() + "/" + book.getEdition();
+			
+			text += r.getCoordinatesBook().getPlace_name() + "," +
+					book.getLevel1() + "," +
+					book.getLevel2() + "," +
+					book.getName() + "," +
+					book.getPeriod() + "," +
+					book.getStart_year() + "," +
+					book.getEnd_year() + "," +
+					r.getCoordinatesBook().getX() + "," +
+					r.getCoordinatesBook().getY() + "," +
+					r.getPage() + "," +
+					r.getSection().getName() + "," +
+					r.getContent() + "," +
+					r.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() + " result(s) in the table.</dvi>"
+				+ "<div class='label'>searching by keywords (possibly with filters): " + '"' + "<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'>period</td>"
+						+ "<td class='tableTitle'>section name</td>"
+						+ "<td class='tableTitle'>page</td>"
+						+ "<td class='tableTitle'>content</td>"
+		 			+ "<tr>";
+		for (DBContents r : list) {			
+			
+			DBBook book = r.getSection().getBook();
+
+			text += "<tr>" +
+						"<td>" + r.getInx() + "</td>" +
+						"<td>" + r.getBookId() + "</td>" +
+						"<td>" + book.getName() + "</td>" +
+						"<td>" + book.getLevel1() + "</td>" +
+						"<td>" + book.getLevel2() + "</td>" +
+						"<td>" + book.getPeriod() + "</td>" +
+						"<td>" + r.getSection().getName() + "</td>" +
+						"<td>" + r.getPage() + "</td>" +
+						"<td class='content'>" + r.getContent() + "</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() + "/";
@@ -82,6 +219,17 @@
 		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");
+		
+	}
 	
 	
 }