1
|
/**
|
2
|
* Copyright (C) 2020 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.remote.controller.iiif;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.net.URI;
|
13
|
import java.net.URISyntaxException;
|
14
|
import java.util.ArrayList;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.stream.Collectors;
|
19
|
|
20
|
import org.apache.http.HttpException;
|
21
|
import org.apache.log4j.Logger;
|
22
|
|
23
|
import de.digitalcollections.iiif.model.ImageContent;
|
24
|
import de.digitalcollections.iiif.model.MetadataEntry;
|
25
|
import de.digitalcollections.iiif.model.MimeType;
|
26
|
import de.digitalcollections.iiif.model.PropertyValue;
|
27
|
import de.digitalcollections.iiif.model.enums.ViewingDirection;
|
28
|
import de.digitalcollections.iiif.model.sharedcanvas.Canvas;
|
29
|
import de.digitalcollections.iiif.model.sharedcanvas.Manifest;
|
30
|
import de.digitalcollections.iiif.model.sharedcanvas.Resource;
|
31
|
import de.digitalcollections.iiif.model.sharedcanvas.Sequence;
|
32
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
33
|
import eu.etaxonomy.cdm.api.service.l10n.LocaleContext;
|
34
|
import eu.etaxonomy.cdm.api.service.media.MediaInfoFactory;
|
35
|
import eu.etaxonomy.cdm.common.media.CdmImageInfo;
|
36
|
import eu.etaxonomy.cdm.model.common.Credit;
|
37
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
38
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
39
|
import eu.etaxonomy.cdm.model.common.Identifier;
|
40
|
import eu.etaxonomy.cdm.model.common.Language;
|
41
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
42
|
import eu.etaxonomy.cdm.model.media.ExternalLink;
|
43
|
import eu.etaxonomy.cdm.model.media.ImageFile;
|
44
|
import eu.etaxonomy.cdm.model.media.Media;
|
45
|
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
|
46
|
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
|
47
|
import eu.etaxonomy.cdm.model.media.MediaUtils;
|
48
|
import eu.etaxonomy.cdm.model.media.Rights;
|
49
|
import eu.etaxonomy.cdm.model.media.RightsType;
|
50
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
51
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
52
|
import eu.etaxonomy.cdm.remote.controller.TaxonPortalController.EntityMediaContext;
|
53
|
import eu.etaxonomy.cdm.remote.controller.util.IMediaToolbox;
|
54
|
import eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper;
|
55
|
import eu.etaxonomy.cdm.strategy.cache.reference.ReferenceDefaultCacheStrategy;
|
56
|
|
57
|
/**
|
58
|
* Factory class for creating iiif manifests.
|
59
|
* <p>
|
60
|
* This class is not state less therefore it is not a spring bean.
|
61
|
*
|
62
|
* @author a.kohlbecker
|
63
|
* @since Aug 18, 2020
|
64
|
*/
|
65
|
public class ManifestComposer {
|
66
|
|
67
|
public static final Logger logger = Logger.getLogger(ManifestComposer.class);
|
68
|
|
69
|
private IMediaToolbox mediaTools;
|
70
|
|
71
|
private IMediaService mediaService;
|
72
|
|
73
|
private MediaInfoFactory mediaInfoFactory; // FIXME define and use interface
|
74
|
|
75
|
private String iiifIdPrefix;
|
76
|
|
77
|
private String[] thumbnailMimetypes = new String[] {"image/.*", ".*"};
|
78
|
|
79
|
|
80
|
private boolean doJoinAttributions = false;
|
81
|
|
82
|
private boolean useThumbnailDimensionsForCanvas = false;
|
83
|
|
84
|
|
85
|
public String getIiifIdPrefix() {
|
86
|
return iiifIdPrefix;
|
87
|
}
|
88
|
|
89
|
public void setIiifIdPrefix(String iiifIdPrefix) {
|
90
|
this.iiifIdPrefix = iiifIdPrefix;
|
91
|
}
|
92
|
|
93
|
|
94
|
public String[] getThumbnailMimetypes() {
|
95
|
return thumbnailMimetypes;
|
96
|
}
|
97
|
|
98
|
public void setThumbnailMimetypes(String[] thumbnailMimetypes) {
|
99
|
this.thumbnailMimetypes = thumbnailMimetypes;
|
100
|
}
|
101
|
|
102
|
public boolean isDoJoinAttributions() {
|
103
|
return doJoinAttributions;
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Universal viewer only shows one attribution value in the popup panel
|
108
|
* Therefore it makes sense to join all of them.
|
109
|
*/
|
110
|
public void setDoJoinAttributions(boolean doJoinAttributions) {
|
111
|
this.doJoinAttributions = doJoinAttributions;
|
112
|
}
|
113
|
|
114
|
public boolean isUseThumbnailDimensionsForCanvas() {
|
115
|
return useThumbnailDimensionsForCanvas;
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* Width and height of the thumbnail image will be used for the canvas size when this is true.
|
120
|
* Normally the canvas dimensions conform to the image dimension.
|
121
|
* This trick is necessary to achieve a pleasant presentation of the thumbnails in universal viewer,
|
122
|
* see {@linkplain https://dev.e-taxonomy.eu/redmine/issues/9132#note-21} and
|
123
|
* {@linkplain https://github.com/UniversalViewer/universalviewer/issues/743}
|
124
|
*
|
125
|
*/
|
126
|
public void setUseThumbnailDimensionsForCanvas(boolean useThumbnailDimensionsForCanvas) {
|
127
|
this.useThumbnailDimensionsForCanvas = useThumbnailDimensionsForCanvas;
|
128
|
}
|
129
|
|
130
|
|
131
|
public ManifestComposer(String iiifIdPrefix, IMediaToolbox mediaTools, IMediaService mediaService, MediaInfoFactory mediaInfoFactory) {
|
132
|
this.mediaTools = mediaTools;
|
133
|
this.iiifIdPrefix = iiifIdPrefix;
|
134
|
this.mediaService = mediaService;
|
135
|
this.mediaInfoFactory = mediaInfoFactory;
|
136
|
}
|
137
|
|
138
|
<T extends IdentifiableEntity> Manifest manifestFor(EntityMediaContext<T> entityMediaContext, String onEntitiyType, String onEntityUuid) throws IOException {
|
139
|
|
140
|
List<Canvas> canvases = new ArrayList<>(entityMediaContext.getMedia().size());
|
141
|
|
142
|
// Logger.getLogger(MediaUtils.class).setLevel(Level.DEBUG);
|
143
|
// logger.setLevel(Level.DEBUG);
|
144
|
|
145
|
int mediaID = 0;
|
146
|
for(Media media : entityMediaContext.getMedia()){
|
147
|
|
148
|
MediaRepresentation thumbnailRepresentation = mediaTools.processAndFindBestMatchingRepresentation(media, null, null, 100, 100, thumbnailMimetypes, MediaUtils.MissingValueStrategy.MAX);
|
149
|
MediaRepresentation fullSizeRepresentation = mediaTools.processAndFindBestMatchingRepresentation(media, null, null, Integer.MAX_VALUE, Integer.MAX_VALUE, null, MediaUtils.MissingValueStrategy.MAX);
|
150
|
// MediaRepresentation fullSizeRepresentation = MediaUtils.findBestMatchingRepresentation(media, null, null, Integer.MAX_VALUE, Integer.MAX_VALUE, null, MediaUtils.MissingValueStrategy.MAX);
|
151
|
// MediaRepresentation thumbnailRepresentation = MediaUtils.findBestMatchingRepresentation(media, null, null, 100, 100, tumbnailMimetypes, MediaUtils.MissingValueStrategy.MAX);
|
152
|
if(logger.isDebugEnabled()){
|
153
|
logger.debug("fullSizeRepresentation: " + fullSizeRepresentation.getParts().get(0).getUri());
|
154
|
logger.debug("thumbnailRepresentation: " + thumbnailRepresentation.getParts().get(0).getUri());
|
155
|
}
|
156
|
|
157
|
// FIXME the below only makes sense if the media is an Image!!!!!
|
158
|
List<ImageContent> fullSizeImageContents = representationPartsToImageContent(fullSizeRepresentation);
|
159
|
|
160
|
List<ImageContent> thumbnailImageContents;
|
161
|
if(fullSizeRepresentation.equals(thumbnailRepresentation)){
|
162
|
thumbnailImageContents = fullSizeImageContents;
|
163
|
} else {
|
164
|
thumbnailImageContents = representationPartsToImageContent(thumbnailRepresentation);
|
165
|
}
|
166
|
|
167
|
Canvas canvas = new Canvas(iiifID(onEntitiyType, onEntityUuid, Canvas.class, mediaID++));
|
168
|
for(Language lang : media.getAllTitles().keySet()){
|
169
|
LanguageString titleLocalized = media.getAllTitles().get(lang);
|
170
|
canvas.addLabel(titleLocalized.getText());
|
171
|
}
|
172
|
canvas.setLabel(new PropertyValue(media.getTitleCache()));
|
173
|
canvas.setThumbnails(thumbnailImageContents);
|
174
|
for(ImageContent image : fullSizeImageContents){
|
175
|
canvas.addImage(image);
|
176
|
}
|
177
|
// TODO if there is only one image canvas.addImage() internally sets the canvas width and height
|
178
|
// to the height of the image, for multiple images it is required to follow the specification:
|
179
|
//
|
180
|
// IIIF Presentation API 2.1.1:
|
181
|
// It is recommended that if there is (at the time of implementation) a single image that depicts the page,
|
182
|
// then the dimensions of the image are used as the dimensions of the canvas for simplicity. If there are
|
183
|
// multiple full images, then the dimensions of the largest image should be used. If the largest image’s
|
184
|
// dimensions are less than 1200 pixels on either edge, then the canvas’s dimensions should be double those
|
185
|
// of the image.
|
186
|
|
187
|
// apply hack for accurate thumbnail container aspect ratios see setUseThumbnailDimensionsForCanvas() for an
|
188
|
// explanation
|
189
|
if(useThumbnailDimensionsForCanvas && !thumbnailImageContents.isEmpty()) {
|
190
|
if(thumbnailImageContents.get(0).getHeight() != null && thumbnailImageContents.get(0).getHeight() > 0 && thumbnailImageContents.get(0).getWidth() != null && thumbnailImageContents.get(0).getWidth() > 0) {
|
191
|
canvas.setHeight(thumbnailImageContents.get(0).getHeight());
|
192
|
canvas.setWidth(thumbnailImageContents.get(0).getWidth());
|
193
|
}
|
194
|
}
|
195
|
|
196
|
List<MetadataEntry> mediaMetadata = mediaMetaData(media);
|
197
|
List<MetadataEntry> representationMetadata;
|
198
|
try {
|
199
|
representationMetadata = mediaService.readResourceMetadataFiltered(fullSizeRepresentation)
|
200
|
.entrySet()
|
201
|
.stream()
|
202
|
.map(e -> new MetadataEntry(e.getKey(), e.getValue())).collect(Collectors.toList());
|
203
|
mediaMetadata.addAll(representationMetadata);
|
204
|
} catch (IOException e) {
|
205
|
logger.error("Error reading media metadata", e);
|
206
|
} catch (HttpException e) {
|
207
|
logger.error("Error accessing remote media resource", e);
|
208
|
}
|
209
|
|
210
|
// extractAndAddDesciptions(canvas, mediaMetadata);
|
211
|
mediaMetadata = deduplicateMetadata(mediaMetadata);
|
212
|
canvas = addAttributionAndLicense(media, canvas, mediaMetadata);
|
213
|
orderMedatadaItems(canvas);
|
214
|
canvas.addMetadata(mediaMetadata.toArray(new MetadataEntry[mediaMetadata.size()]));
|
215
|
canvases.add(canvas);
|
216
|
}
|
217
|
|
218
|
Sequence sequence = null;
|
219
|
if(canvases.size() > 0) {
|
220
|
sequence = new Sequence(iiifID(onEntitiyType, onEntityUuid, Sequence.class, "default"));
|
221
|
sequence.setViewingDirection(ViewingDirection.LEFT_TO_RIGHT);
|
222
|
sequence.setCanvases(canvases);
|
223
|
sequence.setStartCanvas(canvases.get(0).getIdentifier());
|
224
|
}
|
225
|
|
226
|
Manifest manifest = new Manifest(iiifID(onEntitiyType, onEntityUuid, Manifest.class, null));
|
227
|
if(sequence != null){
|
228
|
// manifest.setLabel(new PropertyValue("Media for " + onEntitiyType + "[" + onEntityUuid + "]")); // TODO better label!!
|
229
|
manifest.addSequence(sequence);
|
230
|
} else {
|
231
|
manifest.setLabel(new PropertyValue("No media found for " + onEntitiyType + "[" + onEntityUuid + "]")); // TODO better label!!
|
232
|
}
|
233
|
List<MetadataEntry> entityMetadata = entityMetadata(entityMediaContext.getEntity());
|
234
|
manifest.addMetadata(entityMetadata.toArray(new MetadataEntry[entityMetadata.size()]));
|
235
|
copyAttributionAndLicenseToManifest(manifest);
|
236
|
|
237
|
return manifest;
|
238
|
}
|
239
|
|
240
|
/**
|
241
|
* Due to limitations in universal viewer it seems not to be able
|
242
|
* to show attribution and licenses, therefore we copy this data to
|
243
|
* also to the metadata
|
244
|
*
|
245
|
* <b>NOTE:</b> This method expects that the canvas attributions and
|
246
|
* licenses are not localized!!!!
|
247
|
*
|
248
|
* @param canvas
|
249
|
*/
|
250
|
private void copyAttributionAndLicenseToManifest(Manifest manifest) {
|
251
|
|
252
|
PropertyValue attributions = new PropertyValue();
|
253
|
List<URI> licenses = new ArrayList<>();
|
254
|
String firstAttributionString = null;
|
255
|
boolean hasAttributions = false;
|
256
|
boolean hasLicenses = false;
|
257
|
boolean hasDiversAttributions = false;
|
258
|
boolean hasDiversLicenses = false;
|
259
|
String firstLicensesString = null;
|
260
|
|
261
|
if(manifest.getSequences() == null){
|
262
|
// nothing to do, skip!
|
263
|
return;
|
264
|
}
|
265
|
|
266
|
for (Sequence sequence : manifest.getSequences()) {
|
267
|
for (Canvas canvas : sequence.getCanvases()) {
|
268
|
if (canvas.getAttribution() != null) {
|
269
|
canvas.getAttribution().getValues().stream().forEachOrdered(val -> attributions.addValue(val));
|
270
|
String thisAttributionString = canvas.getAttribution().getValues()
|
271
|
.stream()
|
272
|
.sorted()
|
273
|
.collect(Collectors.joining());
|
274
|
if(firstAttributionString == null){
|
275
|
firstAttributionString = thisAttributionString;
|
276
|
hasAttributions = true;
|
277
|
} else {
|
278
|
hasDiversAttributions |= !firstAttributionString.equals(thisAttributionString);
|
279
|
}
|
280
|
}
|
281
|
if (canvas.getLicenses() != null && canvas.getLicenses().size() > 0) {
|
282
|
licenses.addAll(canvas.getLicenses());
|
283
|
String thisLicensesString = canvas.getLicenses()
|
284
|
.stream()
|
285
|
.map(URI::toString)
|
286
|
.sorted()
|
287
|
.collect(Collectors.joining());
|
288
|
if(firstLicensesString == null){
|
289
|
firstLicensesString = thisLicensesString;
|
290
|
hasLicenses = true;
|
291
|
} else {
|
292
|
hasDiversLicenses |= !firstLicensesString.equals(thisLicensesString);
|
293
|
}
|
294
|
}
|
295
|
}
|
296
|
}
|
297
|
String diversityInfo = "";
|
298
|
|
299
|
if(hasAttributions || hasLicenses){
|
300
|
String dataTypes ;
|
301
|
if(hasAttributions && hasLicenses) {
|
302
|
dataTypes = "attributions and licenses";
|
303
|
} else if(hasAttributions){
|
304
|
dataTypes = "attributions";
|
305
|
} else {
|
306
|
dataTypes = "licenses";
|
307
|
}
|
308
|
if(hasDiversAttributions || hasDiversLicenses){
|
309
|
diversityInfo = "Individual " + dataTypes + " per Item:";
|
310
|
} else {
|
311
|
diversityInfo = "Same " + dataTypes + " for any Item:";
|
312
|
}
|
313
|
if(hasAttributions){
|
314
|
List<String> attrs = new ArrayList<>(attributions.getValues());
|
315
|
attrs = attrs.stream().sorted().distinct().collect(Collectors.toList());
|
316
|
if(doJoinAttributions){
|
317
|
attrs.add(0, diversityInfo + "<br/>" + attrs.get(0));
|
318
|
attrs.remove(1);
|
319
|
manifest.addAttribution(attrs.stream()
|
320
|
.sorted()
|
321
|
.distinct()
|
322
|
.collect(Collectors.joining("; ")));
|
323
|
} else {
|
324
|
manifest.addAttribution(diversityInfo, attrs.toArray(
|
325
|
new String[attributions.getValues().size()]
|
326
|
));
|
327
|
}
|
328
|
}
|
329
|
licenses.stream()
|
330
|
.map(URI::toString)
|
331
|
.sorted()
|
332
|
.distinct()
|
333
|
.forEachOrdered(l -> manifest.addLicense(l));
|
334
|
}
|
335
|
}
|
336
|
|
337
|
private void orderMedatadaItems(Canvas canvas) {
|
338
|
// TODO Auto-generated method stub
|
339
|
// order by label name, Title, description, author, license, attribution should come first.
|
340
|
}
|
341
|
|
342
|
private List<MetadataEntry> deduplicateMetadata(List<MetadataEntry> mediaMetadata) {
|
343
|
Map<String, MetadataEntry> dedupMap = new HashMap<>();
|
344
|
mediaMetadata.stream().forEach(mde -> {
|
345
|
String dedupKey = mde.getLabelString() + ":" + mde.getValueString();
|
346
|
dedupMap.put(dedupKey, mde);
|
347
|
}
|
348
|
);
|
349
|
return new ArrayList<>(dedupMap.values());
|
350
|
}
|
351
|
|
352
|
private void extractAndAddDesciptions(Resource resource, List<MetadataEntry> mediaMetadata) {
|
353
|
List<MetadataEntry> descriptions = mediaMetadata.stream()
|
354
|
.filter(mde -> mde.getLabelString().toLowerCase().matches(".*description.*|.*caption.*"))
|
355
|
.collect(Collectors.toList());
|
356
|
mediaMetadata.removeAll(descriptions);
|
357
|
// FIXME deduplicate mde.getValueString()
|
358
|
// descriptions.sream ...
|
359
|
descriptions.stream().forEach(mde -> resource.addDescription(mde.getValueString()));
|
360
|
}
|
361
|
|
362
|
private <T extends IdentifiableEntity> List<MetadataEntry> entityMetadata(T entity) {
|
363
|
|
364
|
List<MetadataEntry> metadata = new ArrayList<>();
|
365
|
if(entity instanceof TaxonBase){
|
366
|
List taggedTitle = ((TaxonBase)entity).getTaggedTitle();
|
367
|
if(taggedTitle != null){
|
368
|
//FIXME taggedTitel to HTML!!!!
|
369
|
metadata.add(new MetadataEntry(entity.getClass().getSimpleName(), TaggedCacheHelper.createString(taggedTitle)));
|
370
|
}
|
371
|
} else {
|
372
|
String titleCache = entity.getTitleCache();
|
373
|
if(titleCache != null){
|
374
|
metadata.add(new MetadataEntry(entity.getClass().getSimpleName(), titleCache));
|
375
|
}
|
376
|
}
|
377
|
|
378
|
return metadata;
|
379
|
}
|
380
|
|
381
|
/**
|
382
|
* @deprecated unused as media metadata is now read via the mediaService,
|
383
|
* see {@link IMediaService#readResourceMetadataFiltered(MediaRepresentation)}
|
384
|
*/
|
385
|
@Deprecated
|
386
|
private List<MetadataEntry> mediaRepresentationMetaData(MediaRepresentation representation) {
|
387
|
|
388
|
List<MetadataEntry> metadata = new ArrayList<>();
|
389
|
boolean needsPrefix = representation.getParts().size() > 1;
|
390
|
int partIndex = 1;
|
391
|
|
392
|
for (MediaRepresentationPart part : representation.getParts()) {
|
393
|
String prefix = "";
|
394
|
if (needsPrefix) {
|
395
|
prefix = "Part" + partIndex + " ";
|
396
|
}
|
397
|
if (part.getUri() != null) {
|
398
|
try {
|
399
|
CdmImageInfo cdmImageInfo = mediaInfoFactory.cdmImageInfoWithMetaData(part.getUri());
|
400
|
Map<String, String> result = cdmImageInfo.getMetaData();
|
401
|
if(result != null){
|
402
|
for (String key : result.keySet()) {
|
403
|
metadata.add(new MetadataEntry(key, result.get(key)));
|
404
|
}
|
405
|
}
|
406
|
} catch (IOException | HttpException e) {
|
407
|
logger.error("Problem while loading image metadata", e);
|
408
|
metadata.add(new MetadataEntry(prefix + " Error:", "Problem while loading image metadata <br/><small>(" + e.getLocalizedMessage() + ")</small>"));
|
409
|
}
|
410
|
}
|
411
|
}
|
412
|
|
413
|
return metadata;
|
414
|
}
|
415
|
|
416
|
private List<MetadataEntry> mediaMetaData(Media media) {
|
417
|
List<MetadataEntry> metadata = new ArrayList<>();
|
418
|
List<Language> languages = LocaleContext.getLanguages();
|
419
|
|
420
|
|
421
|
if(media.getTitle() != null){
|
422
|
// TODO get localized titleCache
|
423
|
metadata.add(new MetadataEntry("Title", media.getTitleCache()));
|
424
|
}
|
425
|
if(media.getArtist() != null){
|
426
|
metadata.add(new MetadataEntry("Artist", media.getArtist().getTitleCache()));
|
427
|
}
|
428
|
if(media.getAllDescriptions().size() > 0){
|
429
|
// TODO get localized description
|
430
|
PropertyValue descriptionValues = new PropertyValue();
|
431
|
for(LanguageString description : media.getAllDescriptions().values()){
|
432
|
descriptionValues.addValue(description.getText());
|
433
|
}
|
434
|
metadata.add(new MetadataEntry(new PropertyValue("Description"), descriptionValues));
|
435
|
}
|
436
|
if(media.getMediaCreated() != null){
|
437
|
metadata.add(new MetadataEntry("Created on", media.getMediaCreated().toString())); // TODO is this correct to string conversion?
|
438
|
}
|
439
|
|
440
|
if(!media.getIdentifiers().isEmpty()) {
|
441
|
PropertyValue identifierValues = new PropertyValue();
|
442
|
for(Identifier identifier : media.getIdentifiers()){
|
443
|
if(identifier.getIdentifier() != null) {
|
444
|
identifierValues.addValue(identifier.getIdentifier());
|
445
|
}
|
446
|
}
|
447
|
metadata.add(new MetadataEntry(new PropertyValue("Identifiers"), identifierValues));
|
448
|
}
|
449
|
|
450
|
if(!media.getSources().isEmpty()) {
|
451
|
PropertyValue descriptionValues = new PropertyValue();
|
452
|
for(IdentifiableSource source : media.getSources()){
|
453
|
descriptionValues.addValue(sourceAsHtml(source));
|
454
|
}
|
455
|
metadata.add(new MetadataEntry(new PropertyValue("Sources"), descriptionValues));
|
456
|
}
|
457
|
return metadata;
|
458
|
}
|
459
|
|
460
|
private String sourceAsHtml(IdentifiableSource source) {
|
461
|
|
462
|
StringBuilder html = new StringBuilder();
|
463
|
Reference citation = source.getCitation();
|
464
|
if(citation != null) {
|
465
|
ReferenceDefaultCacheStrategy strategy = ((ReferenceDefaultCacheStrategy)citation.getCacheStrategy());
|
466
|
html.append(strategy.createShortCitation(citation, source.getCitationMicroReference(), false)).append(" ");
|
467
|
if(citation.getDoi() != null) {
|
468
|
try {
|
469
|
html.append(" ").append(htmlLink(new URI("http://doi.org/" + citation.getDoiString()), citation.getDoiString()));
|
470
|
} catch (URISyntaxException e) {
|
471
|
// IGNORE, should never happen
|
472
|
}
|
473
|
}
|
474
|
}
|
475
|
if(source.getIdNamespace() != null && source.getIdInSource() != null) {
|
476
|
html.append(source.getIdNamespace()).append("/").append(source.getIdInSource()).append(" ");
|
477
|
}
|
478
|
|
479
|
String linkhtml = null;
|
480
|
for(ExternalLink extLink : source.getLinks()) {
|
481
|
if(extLink.getUri() != null) {
|
482
|
if(linkhtml != null) {
|
483
|
html.append(", ");
|
484
|
}
|
485
|
linkhtml = htmlLink(extLink.getUri().getJavaUri(), extLink.getUri().toString());
|
486
|
html.append(linkhtml);
|
487
|
}
|
488
|
}
|
489
|
return html.toString();
|
490
|
}
|
491
|
|
492
|
private <T extends Resource<T>> T addAttributionAndLicense(IdentifiableEntity<?> entity, T resource, List<MetadataEntry> metadata) {
|
493
|
|
494
|
List<Language> languages = LocaleContext.getLanguages();
|
495
|
|
496
|
List<String> rightsTexts = new ArrayList<>();
|
497
|
List<String> creditTexts = new ArrayList<>();
|
498
|
List<URI> license = new ArrayList<>();
|
499
|
|
500
|
if(entity.getRights() != null && entity.getRights().size() > 0){
|
501
|
for(Rights right : entity.getRights()){
|
502
|
String rightText = "";
|
503
|
// TODO get localized texts below
|
504
|
// --- LICENSE or NULL
|
505
|
if(right.getType() == null || right.getType().equals(RightsType.LICENSE())){
|
506
|
String licenseText = "";
|
507
|
String licenseAbbrev = "";
|
508
|
if(right.getText() != null){
|
509
|
licenseText = right.getText();
|
510
|
}
|
511
|
if(right.getAbbreviatedText() != null){
|
512
|
licenseAbbrev = right.getAbbreviatedText().trim();
|
513
|
}
|
514
|
if(right.getUri() != null){
|
515
|
if(!licenseAbbrev.isEmpty()) {
|
516
|
licenseAbbrev = htmlLink(right.getUri().getJavaUri(), licenseAbbrev);
|
517
|
} else if(!licenseText.isEmpty()) {
|
518
|
licenseText = htmlLink(right.getUri().getJavaUri(), licenseText);
|
519
|
} else {
|
520
|
licenseText = htmlLink(right.getUri().getJavaUri(), right.getUri().toString());
|
521
|
}
|
522
|
license.add(right.getUri().getJavaUri());
|
523
|
}
|
524
|
rightText = licenseAbbrev + (licenseText.isEmpty() ? "" : " ") + licenseText;
|
525
|
}
|
526
|
// --- COPYRIGHT
|
527
|
else if(right.getType().equals(RightsType.COPYRIGHT())){
|
528
|
// titleCache + agent
|
529
|
String copyRightText = "";
|
530
|
if(right.getText() != null){
|
531
|
copyRightText = right.getText();
|
532
|
// sanitize potential '(c)' away
|
533
|
copyRightText = copyRightText.replace("(c)", "").trim();
|
534
|
}
|
535
|
if(right.getAgent() != null){
|
536
|
// may only apply to RightsType.accessRights
|
537
|
copyRightText += " " + right.getAgent().getTitleCache();
|
538
|
}
|
539
|
if(!copyRightText.isEmpty()){
|
540
|
copyRightText = "© " + copyRightText;
|
541
|
}
|
542
|
rightText = copyRightText;
|
543
|
} else
|
544
|
if(right.getType().equals(RightsType.ACCESS_RIGHTS())){
|
545
|
// titleCache + agent
|
546
|
String accessRights = right.getText();
|
547
|
if(right.getAgent() != null){
|
548
|
// may only apply to RightsType.accessRights
|
549
|
accessRights = " " + right.getAgent().getTitleCache();
|
550
|
}
|
551
|
rightText = accessRights;
|
552
|
}
|
553
|
if(!rightText.isEmpty()){
|
554
|
rightsTexts.add(rightText);
|
555
|
}
|
556
|
}
|
557
|
}
|
558
|
if(entity.getCredits() != null && entity.getCredits().size() > 0){
|
559
|
for(Credit credit : entity.getCredits()){
|
560
|
String creditText = "";
|
561
|
if(credit.getText() != null){
|
562
|
creditText += credit.getText();
|
563
|
}
|
564
|
if(creditText.isEmpty() && credit.getAbbreviatedText() != null){
|
565
|
creditText += credit.getAbbreviatedText();
|
566
|
}
|
567
|
if(credit.getAgent() != null){
|
568
|
// may only apply to RightsType.accessRights
|
569
|
creditText += " " + credit.getAgent().getTitleCache();
|
570
|
}
|
571
|
creditTexts.add(creditText);
|
572
|
}
|
573
|
}
|
574
|
|
575
|
if(rightsTexts.size() > 0){
|
576
|
String joinedRights = rightsTexts.stream().collect(Collectors.joining(", "));
|
577
|
resource.addAttribution(joinedRights);
|
578
|
if(metadata != null){
|
579
|
metadata.add(new MetadataEntry(new PropertyValue("Copyright"), new PropertyValue(joinedRights)));
|
580
|
}
|
581
|
}
|
582
|
if(creditTexts.size() > 0){
|
583
|
String joinedCredits = creditTexts.stream().collect(Collectors.joining(", "));
|
584
|
resource.addAttribution(joinedCredits);
|
585
|
if(metadata != null){
|
586
|
metadata.add(new MetadataEntry(new PropertyValue("Credit"), new PropertyValue(joinedCredits)));
|
587
|
}
|
588
|
}
|
589
|
resource.setLicenses(license);
|
590
|
return resource;
|
591
|
}
|
592
|
|
593
|
private String htmlLink(URI uri, String text) {
|
594
|
return String.format(" <a href=\"%s\">%s</a>", uri, text);
|
595
|
}
|
596
|
|
597
|
private List<ImageContent> representationPartsToImageContent(MediaRepresentation representation) {
|
598
|
List<ImageContent> imageContents = new ArrayList<>();
|
599
|
for(MediaRepresentationPart part : representation.getParts()){
|
600
|
if(part.getUri() != null){
|
601
|
ImageContent ic = new ImageContent(part.getUri().toString());
|
602
|
if(part instanceof ImageFile){
|
603
|
ImageFile image = (ImageFile)part;
|
604
|
if(image.getWidth() != null && image.getWidth() > 0){
|
605
|
ic.setWidth(image.getWidth());
|
606
|
}
|
607
|
if(image.getHeight() != null && image.getHeight() > 0){
|
608
|
ic.setHeight(image.getHeight());
|
609
|
}
|
610
|
if(representation.getMimeType() != null){
|
611
|
ic.setFormat(MimeType.fromTypename(representation.getMimeType()));
|
612
|
} else {
|
613
|
ic.setFormat(MimeType.MIME_IMAGE);
|
614
|
}
|
615
|
}
|
616
|
imageContents.add(ic);
|
617
|
}
|
618
|
}
|
619
|
return imageContents;
|
620
|
}
|
621
|
|
622
|
private String iiifID(String onEntitiyType, String onEntityUuid, Class<? extends Resource> iiifType, Object index) {
|
623
|
String indexPart = "";
|
624
|
if(index != null){
|
625
|
indexPart = "/" + index.toString();
|
626
|
}
|
627
|
return this.iiifIdPrefix + onEntitiyType + "/" + onEntityUuid + "/" + iiifType.getSimpleName().toLowerCase() + indexPart;
|
628
|
}
|
629
|
|
630
|
}
|