1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.io.greece;
|
10
|
|
11
|
import java.io.File;
|
12
|
import java.io.IOException;
|
13
|
import java.net.URI;
|
14
|
import java.util.ArrayList;
|
15
|
|
16
|
import org.apache.http.HttpException;
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.apache.sanselan.ImageReadException;
|
19
|
import org.apache.sanselan.Sanselan;
|
20
|
import org.apache.sanselan.common.IImageMetadata;
|
21
|
import org.apache.sanselan.common.ImageMetadata.Item;
|
22
|
import org.joda.time.DateTime;
|
23
|
import org.joda.time.DateTimeZone;
|
24
|
import org.joda.time.format.DateTimeFormat;
|
25
|
import org.joda.time.format.DateTimeFormatter;
|
26
|
|
27
|
import eu.etaxonomy.cdm.common.media.ImageInfo;
|
28
|
import eu.etaxonomy.cdm.io.common.utils.ImportDeduplicationHelper;
|
29
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
30
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
31
|
import eu.etaxonomy.cdm.model.description.TextData;
|
32
|
import eu.etaxonomy.cdm.model.media.ImageFile;
|
33
|
import eu.etaxonomy.cdm.model.media.Media;
|
34
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
35
|
|
36
|
/**
|
37
|
* @author a.mueller
|
38
|
* @date 13.05.2017
|
39
|
*
|
40
|
*/
|
41
|
public class ImageImportTest {
|
42
|
|
43
|
@SuppressWarnings("unused")
|
44
|
private static final Logger logger = Logger.getLogger(FloraHellenicaImageImport.class);
|
45
|
|
46
|
private static final String BASE_URL = "https://media.e-taxonomy.eu/flora-greece/";
|
47
|
private static final String IMAGE_FOLDER = "////BGBM-PESIHPC/Greece/thumbs/";
|
48
|
private ImportDeduplicationHelper deduplicationHelper = null;
|
49
|
|
50
|
protected void doInvoke() {
|
51
|
for (int plate = 1; plate < 22 ; plate++){
|
52
|
System.out.println("Plate: " + plate);
|
53
|
String fill = plate < 10 ? "0" : "";
|
54
|
String plateStr = "Plate_" + fill + plate + "/";
|
55
|
String fullFolderUrl = BASE_URL + plateStr;
|
56
|
String fullThumbUrl = BASE_URL + "thumbs/" + plateStr;
|
57
|
String folderStr = IMAGE_FOLDER + plateStr;
|
58
|
File file = new File(folderStr);
|
59
|
String[] list = file.list();
|
60
|
System.out.println(DateTimeZone.getAvailableIDs());
|
61
|
for (String fileStr : list){
|
62
|
String[] taxonNameAndArtist = getTaxonName(fileStr);
|
63
|
String taxonNameStr = taxonNameAndArtist[0];
|
64
|
String artistStr = taxonNameAndArtist[1];
|
65
|
|
66
|
|
67
|
if(false){
|
68
|
continue;
|
69
|
}
|
70
|
Taxon taxon = getAcceptedTaxon(taxonNameStr);
|
71
|
TaxonDescription imageGallery = taxon.getImageGallery(true);
|
72
|
TextData textData = TextData.NewInstance();
|
73
|
|
74
|
URI uri = URI.create(fullFolderUrl + fileStr);
|
75
|
|
76
|
//image metadata
|
77
|
File imageFile = new File("");
|
78
|
IImageMetadata metadata;
|
79
|
try {
|
80
|
metadata = Sanselan.getMetadata(uri.toURL().openStream(), null);
|
81
|
ArrayList<?> items = metadata.getItems();
|
82
|
for (Object object : items){
|
83
|
Item item = (Item) object;
|
84
|
System.out.println(item.getKeyword() + ": " + item.getText());
|
85
|
String value = removeQuots(item.getText());
|
86
|
if("Image Description".equalsIgnoreCase(item.getKeyword())){
|
87
|
// media.putDescription(Language.DEFAULT(), item.getText());
|
88
|
}else if ("date time original".equalsIgnoreCase(item.getKeyword())){
|
89
|
DateTimeFormatter f = DateTimeFormat.forPattern("yyyy:MM:dd HH:mm:ss");
|
90
|
DateTime created = f.withZone(DateTimeZone.forID("Europe/Athens")).parseDateTime(value);
|
91
|
System.out.println(created);
|
92
|
|
93
|
}
|
94
|
}
|
95
|
} catch (ImageReadException | IOException e1) {
|
96
|
// TODO Auto-generated catch block
|
97
|
e1.printStackTrace();
|
98
|
}
|
99
|
|
100
|
|
101
|
|
102
|
|
103
|
ImageInfo imageMetaData;
|
104
|
try {
|
105
|
imageMetaData = ImageInfo.NewInstance(uri, 0);
|
106
|
|
107
|
String mimeType = imageMetaData.getMimeType();
|
108
|
String suffix = null;
|
109
|
int height = imageMetaData.getHeight();
|
110
|
int width = imageMetaData.getWidth();
|
111
|
Integer size = null;
|
112
|
DateTime mediaCreated = null;
|
113
|
AgentBase<?> artist = null;
|
114
|
Media media = ImageFile.NewMediaInstance(mediaCreated, artist, uri, mimeType, suffix, size, height, width);
|
115
|
|
116
|
textData.addMedia(media);
|
117
|
imageGallery.addElement(textData);
|
118
|
} catch (IOException | HttpException e) {
|
119
|
// TODO Auto-generated catch block
|
120
|
e.printStackTrace();
|
121
|
}
|
122
|
}
|
123
|
|
124
|
}
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* @param text
|
129
|
* @return
|
130
|
*/
|
131
|
private String removeQuots(String text) {
|
132
|
if (text.startsWith("'") && text.endsWith("'")){
|
133
|
return text.substring(1, text.length() -1);
|
134
|
}else{
|
135
|
return text;
|
136
|
}
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* @param taxonNameStr
|
141
|
* @return
|
142
|
*/
|
143
|
private Taxon getAcceptedTaxon(String taxonNameStr) {
|
144
|
return Taxon.NewInstance(null, null);
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* @param fileStr
|
149
|
* @return
|
150
|
*/
|
151
|
private String[] getTaxonName(String fileStr) {
|
152
|
String[] result = new String[2];
|
153
|
fileStr = fileStr.split("\\.")[0];
|
154
|
fileStr = fileStr.replaceAll("[0-9]", "");
|
155
|
String[] x = fileStr.split("_");
|
156
|
if (x.length == 2){
|
157
|
result[1] = x[1];
|
158
|
}
|
159
|
|
160
|
fileStr = splitCamelCase(x[0]);
|
161
|
String[] split = fileStr.split(" ");
|
162
|
String name = split[0] + " " + split[1].toLowerCase() +
|
163
|
(split.length > 2 ? " subsp. " + split[2].toLowerCase() : "");
|
164
|
result[0] = name;
|
165
|
System.out.println(result[0] + (result[1] != null ? " Artist: " + result[1]: ""));
|
166
|
return result;
|
167
|
}
|
168
|
|
169
|
//from http://stackoverflow.com/questions/2559759/how-do-i-convert-camelcase-into-human-readable-names-in-java
|
170
|
static String splitCamelCase(String s) {
|
171
|
return s.replaceAll(
|
172
|
String.format("%s",
|
173
|
// "(?<=[A-Z])(?=[A-Z][a-z])",
|
174
|
"(?<=[^A-Z])(?=[A-Z])"
|
175
|
// "(?<=[A-Za-z])(?=[^A-Za-z])"
|
176
|
),
|
177
|
" "
|
178
|
);
|
179
|
}
|
180
|
|
181
|
public static void main(String[] str){
|
182
|
ImageImportTest test = new ImageImportTest();
|
183
|
test.doInvoke();
|
184
|
}
|
185
|
}
|