-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPhotoTriplesGenerator.java
More file actions
232 lines (199 loc) · 8.6 KB
/
Copy pathPhotoTriplesGenerator.java
File metadata and controls
232 lines (199 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package edu.cornell.vivo.photolinksgenerator;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;
public class PhotoTriplesGenerator {
private static final Logger LOGGER = Logger.getLogger(PhotoTriplesGenerator.class.getName());
private static Boolean newDataExists = false;
private String currentDate;
private String personURINamespace;
public void generateTriples(String photosFolder, String outputFolder, String outputfilename, String ns) {
personURINamespace = ns;
currentDate = getCurrentDate();
Model rdfmodel = ModelFactory.createDefaultModel();
Property mainImage = rdfmodel.createProperty("http://vitro.mannlib.cornell.edu/ns/vitro/public#mainImage");
Property mimeType = rdfmodel.createProperty("http://vitro.mannlib.cornell.edu/ns/vitro/public#mimeType");
Property filename = rdfmodel.createProperty("http://vitro.mannlib.cornell.edu/ns/vitro/public#filename");
Property modTime = rdfmodel.createProperty("http://vitro.mannlib.cornell.edu/ns/vitro/0.7#modTime");
Property thumbnailImage = rdfmodel.createProperty("http://vitro.mannlib.cornell.edu/ns/vitro/public#thumbnailImage");
Property downloadLocation = rdfmodel.createProperty("http://vitro.mannlib.cornell.edu/ns/vitro/public#downloadLocation");
Property directDownloadUrl = rdfmodel.createProperty("http://vitro.mannlib.cornell.edu/ns/vitro/public#directDownloadUrl");
Resource File = rdfmodel.createResource("http://vitro.mannlib.cornell.edu/ns/vitro/public#File");
Resource FileByteStream = rdfmodel.createResource("http://vitro.mannlib.cornell.edu/ns/vitro/public#FileByteStream");
File inputFolder = new File(photosFolder);
if(!inputFolder.isDirectory()) {
System.err.println(inputFolder.getAbsolutePath() +" is not a directoty");
return;
}
Set<String> newFoldersToBePushed = new HashSet<String>();
for(File file: inputFolder.listFiles()){
if(file.isDirectory() || file.getName().startsWith(".")) continue;
String personURILocalName = file.getName().substring(0, file.getName().indexOf("."));
if(personAlreadyProcessed(file.getName(), outputFolder)) {
LOGGER.info(personURILocalName + " already processed..continuing!");
continue; // already exists
}else{
newFoldersToBePushed.add(personURILocalName.substring(0, 3));
newDataExists = true;
}
Resource person = rdfmodel.createResource(personURINamespace+personURILocalName);
Resource person_mainImage = rdfmodel.createResource(personURINamespace+personURILocalName+"_mainImage");
person.addProperty(mainImage, person_mainImage);
person_mainImage.addProperty(mimeType, "image/jpeg");
person_mainImage.addProperty(filename, personURILocalName+".jpg");
person_mainImage.addProperty(RDF.type, File);
person_mainImage.addProperty(modTime, currentDate);
Resource person_thumbnailImage = rdfmodel.createResource(personURINamespace+personURILocalName+"_thumbnailImage");
person_mainImage.addProperty(thumbnailImage, person_thumbnailImage);
person_thumbnailImage.addProperty(mimeType, "image/jpeg");
person_thumbnailImage.addProperty(filename, personURILocalName+".jpg");
person_thumbnailImage.addProperty(RDF.type, File);
person_thumbnailImage.addProperty(modTime, currentDate);
// PATH 1
Resource person_thumbnailImage_downloadLocation = rdfmodel.createResource(personURINamespace+"n"+personURILocalName+"tdL77");
person_thumbnailImage.addProperty(downloadLocation, person_thumbnailImage_downloadLocation);
String localName = person_thumbnailImage_downloadLocation.getLocalName();
String path = getPath(localName);
File filedir = new File(new File(outputFolder), currentDate+"/"+path);
filedir.mkdirs();
Path src = FileSystems.getDefault().getPath(inputFolder.getAbsolutePath(), personURILocalName+".jpg");
Path tgt = FileSystems.getDefault().getPath(filedir.getAbsolutePath()+"/");
try {
if(!tgt.resolve(src.getFileName()).toFile().exists()){
Files.copy(src, tgt.resolve(src.getFileName()));
}
} catch (IOException e) {
e.printStackTrace();
}
person_thumbnailImage_downloadLocation.addProperty(directDownloadUrl, "/file/"+localName+"/"+personURILocalName+".jpg");
person_thumbnailImage_downloadLocation.addProperty(RDF.type, FileByteStream);
person_thumbnailImage_downloadLocation.addProperty(modTime, currentDate);
// PATH 2
Resource person_downloadLocation = rdfmodel.createResource(personURINamespace+"n"+personURILocalName+"dL77");
person_mainImage.addProperty(downloadLocation, person_downloadLocation);
String localName2 = person_downloadLocation.getLocalName();
String path2 = getPath(localName2);
File filedir2 = new File(new File(outputFolder), currentDate+"/"+path2);
filedir2.mkdirs();
Path src2 = FileSystems.getDefault().getPath(inputFolder.getAbsolutePath(), personURILocalName+".jpg");
Path tgt2 = FileSystems.getDefault().getPath(filedir2.getAbsolutePath()+"/");
try {
if(!tgt2.resolve(src2.getFileName()).toFile().exists()){
Files.copy(src2, tgt2.resolve(src2.getFileName()));
}
} catch (IOException e) {
e.printStackTrace();
}
person_downloadLocation.addProperty(directDownloadUrl, "/file/"+localName+"/"+personURILocalName+".jpg");
person_downloadLocation.addProperty(RDF.type, FileByteStream);
person_downloadLocation.addProperty(modTime, currentDate);
}
if(newDataExists){
// save RDF model.
try {
LOGGER.info("Save Photo Triples RDF Model...");
saveRDFModel(rdfmodel, getNewFileName(outputFolder+"/"+outputfilename));
LOGGER.info("Save Photo Triples RDF Model...completed");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}else{
LOGGER.info("Photo Triples: NO NEW DATA FOUND!!...");
}
}
private String getNewFileName(String filePath){
int i = 1;
File file = new File(filePath);
while(file.exists()){
filePath = file.getAbsolutePath();
filePath = filePath.substring(0, filePath.lastIndexOf(getCurrentDate())) +getCurrentDate()+"-"+i+".nt";
file = new File(filePath);
i++;
}
return filePath;
}
private boolean personAlreadyProcessed(String imageFileName, String outputFolder) {
Set<String> result = searchDirectory(new File(outputFolder), imageFileName);
if(result != null && result.size() > 0){
return true;
}else {
return false;
}
}
public Set<String> searchDirectory(File directory, String fileNameToSearch) {
Set<String> result = null;
if (directory.isDirectory()) {
result = search(directory, fileNameToSearch);
} else {
System.err.println("Error: "+directory.getAbsoluteFile() + " is not a directory!");
}
return result;
}
private Set<String> search(File file, String fileNameToSearch) {
Set<String> result = new HashSet<String>();
if (file.isDirectory()) {
//System.out.println("Searching directory ... " + file.getAbsoluteFile());
//do you have permission to read this directory?
if (file.canRead()) {
for (File temp : file.listFiles()) {
if (temp.isDirectory()) {
search(temp, fileNameToSearch);
} else {
if (fileNameToSearch.equals(temp.getName().toLowerCase())) {
result.add(temp.getAbsoluteFile().toString());
}
}
}
} else {
System.out.println(file.getAbsoluteFile() + "Permission Denied");
}
}
return result;
}
private String getPath(String localName) {
//System.out.println(localName);
String path = "";
int start = 1;
int end = 4;
while(localName.length() > 3){
String split = localName.substring(start, end);
path = path.concat(split+"/");
localName = localName.substring(end);
start = 0;
end = 3;
}
path = path + localName+"/";
//System.out.println(path);
return path;
}
private void saveRDFModel(Model rdfModel, String filePath) throws FileNotFoundException {
PrintWriter printer = null;
printer = new PrintWriter(filePath);
rdfModel.write(printer, "N-Triples");
printer.flush();
printer.close();
}
private String getCurrentDate() {
String date = null;
Date now = new Date();
SimpleDateFormat dateFormatter = new SimpleDateFormat("E, y-M-d 'at' h:m:s a z");
dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
date = dateFormatter.format(now);
LOGGER.info(date);
return date;
}
}