Mercurial > hg > LGServices
comparison src/main/java/de/mpiwg/gazetteer/utils/FileManager.java @ 0:3e62083dbcbf
First commit. This project comes from LGServer. We removed the framework icefaces. Now, LGServices uses just JSP and jquery.
author | "jurzua <jurzua@mpiwg-berlin.mpg.de>" |
---|---|
date | Thu, 23 Apr 2015 15:46:01 +0200 |
parents | |
children | 37840afb7b80 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3e62083dbcbf |
---|---|
1 package de.mpiwg.gazetteer.utils; | |
2 | |
3 import java.io.File; | |
4 import java.io.PrintWriter; | |
5 import java.nio.file.Files; | |
6 import java.nio.file.Paths; | |
7 import java.text.DateFormat; | |
8 import java.text.SimpleDateFormat; | |
9 import java.util.Date; | |
10 import java.util.UUID; | |
11 | |
12 import org.apache.commons.lang.RandomStringUtils; | |
13 import org.apache.commons.lang.StringUtils; | |
14 import org.apache.log4j.Logger; | |
15 | |
16 import de.mpiwg.gazetteer.bo.LGBranch; | |
17 import de.mpiwg.gazetteer.bo.LGFile; | |
18 | |
19 public class FileManager { | |
20 | |
21 private static Logger logger = Logger.getLogger(FileManager.class); | |
22 | |
23 private static DateFormat dfmt = new SimpleDateFormat( "yyyy.MM.dd_hh.mm.ss.SSS" ); | |
24 | |
25 | |
26 | |
27 private static String getRootPath() throws Exception{ | |
28 String base = PropertiesUtils.getPropValue("files_root"); | |
29 | |
30 if(StringUtils.isEmpty(base)){ | |
31 throw new Exception("Property files_root no found. The files can be stored without this property."); | |
32 } | |
33 | |
34 if(!base.endsWith("/")){ | |
35 base += "/"; | |
36 } | |
37 | |
38 logger.info("files_root=" + base); | |
39 | |
40 return base; | |
41 } | |
42 | |
43 public static String saveFile(LGBranch branch, LGFile file, Date date, Long userId) throws Exception{ | |
44 | |
45 String absolutePath = getRootPath() + branch.getSectionId() + "/"; | |
46 String fileName = | |
47 branch.getSectionId() + "_" + | |
48 branch.getId() + "_" + | |
49 file.getId() + "_" + | |
50 dfmt.format(date) + "_" + | |
51 userId + | |
52 ".txt"; | |
53 | |
54 File folder = new File(absolutePath); | |
55 folder.mkdirs(); | |
56 | |
57 logger.info("Trying to save file " + absolutePath + fileName + "."); | |
58 | |
59 PrintWriter out = new PrintWriter(absolutePath + fileName); | |
60 out.println(file.getContent()); | |
61 out.close(); | |
62 | |
63 logger.info("The file " + fileName + " has been saved correctly."); | |
64 return fileName; | |
65 } | |
66 | |
67 public static String getFileAsText(LGFile file) throws Exception{ | |
68 | |
69 LGBranch branch = DataProvider.getInstance().getBranch(file.getBranchId()); | |
70 | |
71 String absolutePath = getRootPath() + branch.getSectionId() + "/" + file.getFileName(); | |
72 System.out.println("Loading: " + absolutePath); | |
73 byte[] encoded = Files.readAllBytes(Paths.get(absolutePath)); | |
74 return new String(encoded, "UTF8"); | |
75 } | |
76 | |
77 public static File getFileAsFile(LGFile file) throws Exception{ | |
78 | |
79 LGBranch branch = DataProvider.getInstance().getBranch(file.getBranchId()); | |
80 | |
81 String absolutePath = getRootPath() + branch.getSectionId() + "/" + file.getFileName(); | |
82 System.out.println("Loading: " + absolutePath); | |
83 return new File(absolutePath); | |
84 } | |
85 | |
86 | |
87 } |