Revision d99452fb
Added by Andreas Müller almost 7 years ago
app-import/src/main/java/eu/etaxonomy/cdm/app/cyprus/CyprusImagesActivator.java | ||
---|---|---|
76 | 76 |
// static final ICdmDataSource cdmDestination = CdmDestinations.cdm_cyprus_dev(); |
77 | 77 |
static final ICdmDataSource cdmDestination = CdmDestinations.cdm_cyprus_production(); |
78 | 78 |
|
79 |
boolean testOnly = false; |
|
79 |
static boolean testOnly = false; |
|
80 |
static boolean update_notCreate = true; |
|
81 |
static boolean forceUpdate = false; |
|
80 | 82 |
|
81 | 83 |
private static final String path = "//media/digitalimages/EditWP6/Zypern/photos/"; |
82 | 84 |
private static final String urlPath = "http://media.bgbm.org/erez/erez?src=EditWP6/zypern/photos/"; |
... | ... | |
99 | 101 |
Pattern pattern = Pattern.compile(regEx); |
100 | 102 |
|
101 | 103 |
for (String fileName : fileList){ |
104 |
|
|
102 | 105 |
Matcher matcher = pattern.matcher(fileName); |
103 | 106 |
if (matcher.matches()){ |
104 | 107 |
// System.out.println(fileName); |
... | ... | |
152 | 155 |
try { |
153 | 156 |
String fullName = urlPath + fileName; |
154 | 157 |
Media media = getImageMedia(fullName, null, true); |
155 |
makeMetaData(media, fileName); |
|
156 |
String title = fileName.replace("_s_"," subsp. ") |
|
157 |
.replace("_"," ").replace(".jpg","").replace(".JPG",""); |
|
158 |
media.putTitle(Language.LATIN(), title); |
|
158 |
makeMetaData(media, fileName, false); |
|
159 |
makeTitle(media, fileName, false); |
|
159 | 160 |
if (!testOnly){ |
160 | 161 |
makeTextData(fileName, media, taxon); |
161 | 162 |
} |
... | ... | |
168 | 169 |
|
169 | 170 |
/** |
170 | 171 |
* @param media |
172 |
* @param fileName |
|
173 |
* @param b |
|
171 | 174 |
*/ |
172 |
private void makeMetaData(Media media, String fileName) { |
|
173 |
//image metadata |
|
175 |
private void makeTitle(Media media, String fileName, boolean updateOnly) { |
|
176 |
String title = fileName.replace("_s_"," subsp. ") |
|
177 |
.replace("_"," ").replace(".jpg","").replace(".JPG",""); |
|
178 |
if ( (!updateOnly) || media.getAllTitles().isEmpty()){ |
|
179 |
media.putTitle(Language.LATIN(), title); |
|
180 |
} |
|
181 |
} |
|
182 |
|
|
183 |
/** |
|
184 |
* @param media |
|
185 |
*/ |
|
186 |
private void makeMetaData(Media media, String fileName, boolean updateOnly) { |
|
174 | 187 |
|
175 |
Map<String, String> keywords = new HashMap<>(); |
|
176 | 188 |
File file = new File(path + fileName); |
189 |
if (!file.exists()){ |
|
190 |
logger.warn("File for filename " + fileName + " does not exist."); |
|
191 |
return; |
|
192 |
} |
|
193 |
|
|
194 |
Map<String, String> keywords = new HashMap<>(); |
|
177 | 195 |
String copyright = null; |
178 | 196 |
String artistStr = null; |
197 |
String created = null; |
|
179 | 198 |
try{ |
180 | 199 |
IImageMetadata metadata = Sanselan.getMetadata(file); |
181 | 200 |
ArrayList<?> items = metadata.getItems(); |
... | ... | |
197 | 216 |
}else if ("artist".equals(keyword)){ |
198 | 217 |
artistStr = value; |
199 | 218 |
}else if ("date time original".equalsIgnoreCase(item.getKeyword())){ |
200 |
DateTimeFormatter f = DateTimeFormat.forPattern("yyyy:MM:dd HH:mm:ss"); |
|
201 |
DateTime created; |
|
202 |
try { |
|
203 |
created = f/*.withZone(DateTimeZone.forID("Europe/Athens"))*/.parseDateTime(value); |
|
204 |
media.setMediaCreated(created); |
|
205 |
} catch (Exception e) { |
|
206 |
logger.warn("Exception (" + e.getMessage() + ") when parsing create date " + value + " for file " + fileName); |
|
207 |
} |
|
219 |
created = value; |
|
208 | 220 |
} |
209 | 221 |
} |
210 | 222 |
} catch (ImageReadException | IOException e1) { |
211 | 223 |
logger.warn(" Problem (" + e1.getMessage() + ") when reading metadata from file: " + fileName); |
212 | 224 |
} |
225 |
|
|
226 |
|
|
227 |
AgentBase<?> artistAgent = null; |
|
228 |
Rights right = null; |
|
229 |
DateTime createdDate = null; |
|
230 |
String locality = null; |
|
231 |
|
|
232 |
//artist |
|
213 | 233 |
if (keywords.get("photographer") != null){ |
214 | 234 |
String artist = keywords.get("photographer"); |
215 |
Person person = makePerson(artist, fileName); |
|
216 |
media.setArtist(person); |
|
235 |
artistAgent = makePerson(artist, fileName); |
|
217 | 236 |
} |
218 | 237 |
if (artistStr != null){ |
219 | 238 |
if (keywords.get("photographer") == null){ |
220 |
Person person = makePerson(artistStr, fileName); |
|
221 |
media.setArtist(person); |
|
239 |
artistAgent = makePerson(artistStr, fileName); |
|
222 | 240 |
}else if (!keywords.get("photographer").toLowerCase().replace(" ", "") |
223 | 241 |
.contains(artistStr.toLowerCase().replace(" ", ""))){ |
224 | 242 |
logger.warn("Artist '" + artistStr + "' could not be handled for " + fileName); |
225 | 243 |
} |
226 | 244 |
} |
245 |
|
|
246 |
//locality |
|
227 | 247 |
if (keywords.get("locality") != null){ |
228 |
String locality = keywords.get("locality"); |
|
229 |
media.putDescription(Language.ENGLISH(), locality); |
|
248 |
locality = keywords.get("locality"); |
|
230 | 249 |
} |
250 |
|
|
251 |
//copyright |
|
231 | 252 |
if (copyright != null){ |
232 |
if (rightsMap.get(copyright)!= null){ |
|
233 |
media.addRights(rightsMap.get(copyright)); |
|
253 |
AgentBase<?> agent; |
|
254 |
if (copyright.equals("Botanic Garden and Botanical Museum Berlin-Dahlem (BGBM)")){ |
|
255 |
agent = Institution.NewNamedInstance(copyright); |
|
234 | 256 |
}else{ |
235 |
AgentBase<?> agent; |
|
236 |
if (copyright.equals("Botanic Garden and Botanical Museum Berlin-Dahlem (BGBM)")){ |
|
237 |
agent = Institution.NewNamedInstance(copyright); |
|
238 |
}else{ |
|
239 |
agent = makePerson(copyright, fileName); |
|
240 |
} |
|
241 |
Rights r = Rights.NewInstance(null, null, RightsType.COPYRIGHT()); |
|
242 |
r.setAgent(agent); |
|
243 |
media.addRights(r); |
|
244 |
rightsMap.put(copyright, r); |
|
257 |
agent = makePerson(copyright, fileName); |
|
258 |
} |
|
259 |
right = Rights.NewInstance(null, null, RightsType.COPYRIGHT()); |
|
260 |
right.setAgent(agent); |
|
261 |
right = deduplicationHelper.getExistingCopyright(null, right); |
|
262 |
} |
|
263 |
|
|
264 |
//created |
|
265 |
if (created != null){ |
|
266 |
DateTimeFormatter f = DateTimeFormat.forPattern("yyyy:MM:dd HH:mm:ss"); |
|
267 |
try { |
|
268 |
createdDate = f/*.withZone(DateTimeZone.forID("Europe/Athens"))*/.parseDateTime(created); |
|
269 |
} catch (Exception e) { |
|
270 |
logger.warn("Exception (" + e.getMessage() + ") when parsing create date " + created + " for file " + fileName); |
|
245 | 271 |
} |
246 | 272 |
} |
247 |
} |
|
248 | 273 |
|
249 |
private static Map<String, Rights> rightsMap = new HashMap<>(); |
|
274 |
boolean force = !updateOnly || forceUpdate; |
|
275 |
//add to media |
|
276 |
if (artistAgent != null && (force || media.getArtist() == null)){ |
|
277 |
media.setArtist(artistAgent); |
|
278 |
} |
|
279 |
if (right != null && (force || media.getRights().isEmpty())){ |
|
280 |
media.removeRights(right); |
|
281 |
media.addRights(right); |
|
282 |
} |
|
283 |
if (createdDate != null && (force || media.getMediaCreated() == null)){ |
|
284 |
media.setMediaCreated(createdDate); |
|
285 |
} |
|
286 |
if (locality != null && (force || media.getDescription(Language.ENGLISH()) == null)){ |
|
287 |
media.putDescription(Language.ENGLISH(), locality); |
|
288 |
} |
|
289 |
} |
|
250 | 290 |
|
251 | 291 |
/** |
252 | 292 |
* @param artist |
... | ... | |
464 | 504 |
return taxonNameStr; |
465 | 505 |
} |
466 | 506 |
|
467 |
public void test(){
|
|
507 |
private void test(){
|
|
468 | 508 |
File f = new File(path); |
469 | 509 |
String[] list = f.list(); |
470 | 510 |
List<String> fullFileNames = new ArrayList<>(); |
... | ... | |
476 | 516 |
} |
477 | 517 |
} |
478 | 518 |
|
519 |
private void updateMetadata(ICdmDataSource cdmDestination){ |
|
520 |
CdmApplicationController app = CdmIoApplicationController.NewInstance(cdmDestination, hbm2dll); |
|
521 |
TransactionStatus tx = app.startTransaction(); |
|
522 |
|
|
523 |
deduplicationHelper = (ImportDeduplicationHelper<SimpleExcelTaxonImportState<?>>)ImportDeduplicationHelper.NewInstance(app); |
|
524 |
|
|
525 |
List<Media> list = app.getMediaService().list(Media.class, null, null, null, null); |
|
526 |
for (Media media : list){ |
|
527 |
String fileName = getUrlStringForMedia(media); |
|
528 |
if (fileName.startsWith(urlPath)){ |
|
529 |
fileName = fileName.replace(urlPath, ""); |
|
530 |
// System.out.println(fileName); |
|
531 |
// makeMetaData(media, fileName, true); |
|
532 |
makeTitle(media, fileName, true); |
|
533 |
}else{ |
|
534 |
logger.warn("Filename does not start with standard url path: " + fileName); |
|
535 |
} |
|
536 |
} |
|
537 |
|
|
538 |
if (testOnly){ |
|
539 |
tx.setRollbackOnly(); |
|
540 |
} |
|
541 |
app.commitTransaction(tx); |
|
542 |
|
|
543 |
} |
|
544 |
|
|
479 | 545 |
/** |
546 |
* @param media |
|
547 |
* @return |
|
548 |
*/ |
|
549 |
private String getUrlStringForMedia(Media media) { |
|
550 |
String result = null; |
|
551 |
for (MediaRepresentation rep : media.getRepresentations()){ |
|
552 |
for (MediaRepresentationPart part : rep.getParts()){ |
|
553 |
URI uri = part.getUri(); |
|
554 |
if (uri != null){ |
|
555 |
if (result != null){ |
|
556 |
logger.warn("More than 1 uri exists for media "+ media.getId()); |
|
557 |
}else{ |
|
558 |
result = uri.toString(); |
|
559 |
} |
|
560 |
} |
|
561 |
} |
|
562 |
} |
|
563 |
return result; |
|
564 |
} |
|
565 |
|
|
566 |
/** |
|
480 | 567 |
* @param args |
481 | 568 |
*/ |
482 | 569 |
public static void main(String[] args) { |
483 | 570 |
CyprusImagesActivator me = new CyprusImagesActivator(); |
484 |
me.doImport(cdmDestination); |
|
571 |
if (update_notCreate){ |
|
572 |
me.updateMetadata(cdmDestination); |
|
573 |
}else{ |
|
574 |
me.doImport(cdmDestination); |
|
575 |
} |
|
485 | 576 |
// me.test(); |
486 | 577 |
System.exit(0); |
487 | 578 |
} |
Also available in: Unified diff
fix #5744 finish Cyprus image import