use of de.digitalcollections.iiif.model.PropertyValue in project cdmlib by cybertaxonomy.
the class ManifestComposer method manifestFor.
<T extends IdentifiableEntity> Manifest manifestFor(EntityMediaContext<T> entityMediaContext, String onEntitiyType, String onEntityUuid) throws IOException {
// Logger.getLogger(MediaUtils.class).setLevel(Level.DEBUG);
// logger.setLevel(Level.DEBUG);
List<Canvas> canvases = null;
try {
canvases = entityMediaContext.getMedia().parallelStream().map(m -> {
try {
return createCanvas(onEntitiyType, onEntityUuid, m);
} catch (IOException e) {
throw new RuntimeException(e);
}
}).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
} catch (RuntimeException re) {
// re-throw IOException from lambda expression
throw new IOException(re.getCause());
}
Sequence sequence = null;
if (canvases.size() > 0) {
sequence = new Sequence(iiifID(onEntitiyType, onEntityUuid, Sequence.class, "default"));
sequence.setViewingDirection(ViewingDirection.LEFT_TO_RIGHT);
sequence.setCanvases(canvases);
sequence.setStartCanvas(canvases.get(0).getIdentifier());
}
Manifest manifest = new Manifest(iiifID(onEntitiyType, onEntityUuid, Manifest.class, null));
if (sequence != null) {
// manifest.setLabel(new PropertyValue("Media for " + onEntitiyType + "[" + onEntityUuid + "]")); // TODO better label!!
manifest.addSequence(sequence);
} else {
// TODO better label!!
manifest.setLabel(new PropertyValue("No media found for " + onEntitiyType + "[" + onEntityUuid + "]"));
}
List<MetadataEntry> entityMetadata = entityMetadata(entityMediaContext.getEntity());
manifest.addMetadata(entityMetadata.toArray(new MetadataEntry[entityMetadata.size()]));
copyAttributionAndLicenseToManifest(manifest);
return manifest;
}
use of de.digitalcollections.iiif.model.PropertyValue in project cdmlib by cybertaxonomy.
the class ManifestComposer method createCanvas.
public Optional<Canvas> createCanvas(String onEntitiyType, String onEntityUuid, Media media) throws IOException {
Canvas canvas;
MediaRepresentation thumbnailRepresentation = mediaTools.processAndFindBestMatchingRepresentation(media, null, null, 100, 100, thumbnailMimetypes, MediaUtils.MissingValueStrategy.MAX);
MediaRepresentation fullSizeRepresentation = mediaTools.processAndFindBestMatchingRepresentation(media, null, null, Integer.MAX_VALUE, Integer.MAX_VALUE, null, MediaUtils.MissingValueStrategy.MAX);
// MediaRepresentation thumbnailRepresentation = MediaUtils.findBestMatchingRepresentation(media, null, null, 100, 100, tumbnailMimetypes, MediaUtils.MissingValueStrategy.MAX);
if (logger.isDebugEnabled()) {
logger.debug("fullSizeRepresentation: " + fullSizeRepresentation.getParts().get(0).getUri());
logger.debug("thumbnailRepresentation: " + thumbnailRepresentation.getParts().get(0).getUri());
}
List<ImageContent> thumbnailImageContents;
List<ImageContent> fullSizeImageContents;
// FIXME the below only makes sense if the media is an Image!!!!!
if (fullSizeRepresentation != null) {
fullSizeImageContents = representationPartsToImageContent(fullSizeRepresentation);
} else {
fullSizeImageContents = new ArrayList<>(0);
}
if (Objects.equals(fullSizeRepresentation, thumbnailRepresentation)) {
thumbnailImageContents = fullSizeImageContents;
} else {
thumbnailImageContents = representationPartsToImageContent(thumbnailRepresentation);
}
if (fullSizeRepresentation == null) {
return Optional.empty();
}
canvas = new Canvas(iiifID(onEntitiyType, onEntityUuid, Canvas.class, "media-" + media.getUuid()));
for (Language lang : media.getAllTitles().keySet()) {
LanguageString titleLocalized = media.getAllTitles().get(lang);
canvas.addLabel(titleLocalized.getText());
}
canvas.setLabel(new PropertyValue(media.getTitleCache()));
canvas.setThumbnails(thumbnailImageContents);
for (ImageContent image : fullSizeImageContents) {
canvas.addImage(image);
}
// explanation
if (useThumbnailDimensionsForCanvas && !thumbnailImageContents.isEmpty()) {
if (thumbnailImageContents.get(0).getHeight() != null && thumbnailImageContents.get(0).getHeight() > 0 && thumbnailImageContents.get(0).getWidth() != null && thumbnailImageContents.get(0).getWidth() > 0) {
canvas.setHeight(thumbnailImageContents.get(0).getHeight());
canvas.setWidth(thumbnailImageContents.get(0).getWidth());
}
}
List<MetadataEntry> mediaMetadata = mediaMetaData(media);
List<MetadataEntry> representationMetadata;
try {
representationMetadata = mediaService.readResourceMetadataFiltered(fullSizeRepresentation).entrySet().stream().map(e -> new MetadataEntry(e.getKey(), e.getValue())).collect(Collectors.toList());
mediaMetadata.addAll(representationMetadata);
} catch (IOException e) {
logger.error("Error reading media metadata", e);
} catch (HttpException e) {
logger.error("Error accessing remote media resource", e);
}
// extractAndAddDesciptions(canvas, mediaMetadata);
mediaMetadata = deduplicateMetadata(mediaMetadata);
canvas = addAttributionAndLicense(media, canvas, mediaMetadata);
orderMedatadaItems(canvas);
canvas.addMetadata(mediaMetadata.toArray(new MetadataEntry[mediaMetadata.size()]));
return Optional.of(canvas);
}
use of de.digitalcollections.iiif.model.PropertyValue in project cdmlib by cybertaxonomy.
the class ManifestComposer method mediaMetaData.
private List<MetadataEntry> mediaMetaData(Media media) {
List<MetadataEntry> metadata = new ArrayList<>();
List<Language> languages = LocaleContext.getLanguages();
if (media.getTitle() != null) {
// TODO get localized titleCache
metadata.add(new MetadataEntry("Title", media.getTitleCache()));
}
if (media.getArtist() != null) {
metadata.add(new MetadataEntry("Artist", media.getArtist().getTitleCache()));
}
if (media.getAllDescriptions().size() > 0) {
// TODO get localized description
PropertyValue descriptionValues = new PropertyValue();
for (LanguageString description : media.getAllDescriptions().values()) {
descriptionValues.addValue(description.getText());
}
metadata.add(new MetadataEntry(new PropertyValue("Description"), descriptionValues));
}
if (media.getMediaCreated() != null) {
// TODO is this correct to string conversion?
metadata.add(new MetadataEntry("Created on", media.getMediaCreated().toString()));
}
if (!media.getIdentifiers().isEmpty()) {
PropertyValue identifierValues = new PropertyValue();
for (Identifier identifier : media.getIdentifiers()) {
if (identifier.getIdentifier() != null) {
identifierValues.addValue(identifier.getIdentifier());
}
}
metadata.add(new MetadataEntry(new PropertyValue("Identifiers"), identifierValues));
}
if (!media.getSources().isEmpty()) {
PropertyValue descriptionValues = new PropertyValue();
for (IdentifiableSource source : media.getSources()) {
descriptionValues.addValue(sourceAsHtml(source));
}
metadata.add(new MetadataEntry(new PropertyValue("Sources"), descriptionValues));
}
return metadata;
}
use of de.digitalcollections.iiif.model.PropertyValue in project cdmlib by cybertaxonomy.
the class ManifestComposer method addAttributionAndLicense.
private <T extends Resource<T>> T addAttributionAndLicense(IdentifiableEntity<?> entity, T resource, List<MetadataEntry> metadata) {
List<Language> languages = LocaleContext.getLanguages();
List<String> rightsTexts = new ArrayList<>();
List<String> creditTexts = new ArrayList<>();
List<URI> license = new ArrayList<>();
if (entity.getRights() != null && entity.getRights().size() > 0) {
for (Rights right : entity.getRights()) {
String rightText = "";
// --- LICENSE or NULL
if (right.getType() == null || right.getType().equals(RightsType.LICENSE())) {
String licenseText = "";
String licenseAbbrev = "";
if (right.getText() != null) {
licenseText = right.getText();
}
if (right.getAbbreviatedText() != null) {
licenseAbbrev = right.getAbbreviatedText().trim();
}
if (right.getUri() != null) {
if (!licenseAbbrev.isEmpty()) {
licenseAbbrev = htmlLink(right.getUri().getJavaUri(), licenseAbbrev);
} else if (!licenseText.isEmpty()) {
licenseText = htmlLink(right.getUri().getJavaUri(), licenseText);
} else {
licenseText = htmlLink(right.getUri().getJavaUri(), right.getUri().toString());
}
license.add(right.getUri().getJavaUri());
}
rightText = licenseAbbrev + (licenseText.isEmpty() ? "" : " ") + licenseText;
} else // --- COPYRIGHT
if (right.getType().equals(RightsType.COPYRIGHT())) {
// titleCache + agent
String copyRightText = "";
if (right.getText() != null) {
copyRightText = right.getText();
// sanitize potential '(c)' away
copyRightText = copyRightText.replace("(c)", "").trim();
}
if (right.getAgent() != null) {
// may only apply to RightsType.accessRights
copyRightText += " " + right.getAgent().getTitleCache();
}
if (!copyRightText.isEmpty()) {
copyRightText = "© " + copyRightText;
}
rightText = copyRightText;
} else if (right.getType().equals(RightsType.ACCESS_RIGHTS())) {
// titleCache + agent
String accessRights = right.getText();
if (right.getAgent() != null) {
// may only apply to RightsType.accessRights
accessRights = " " + right.getAgent().getTitleCache();
}
rightText = accessRights;
}
if (!rightText.isEmpty()) {
rightsTexts.add(rightText);
}
}
}
if (entity.getCredits() != null && entity.getCredits().size() > 0) {
for (Credit credit : entity.getCredits()) {
String creditText = "";
if (credit.getText() != null) {
creditText += credit.getText();
}
if (creditText.isEmpty() && credit.getAbbreviatedText() != null) {
creditText += credit.getAbbreviatedText();
}
if (credit.getAgent() != null) {
// may only apply to RightsType.accessRights
creditText += " " + credit.getAgent().getTitleCache();
}
creditTexts.add(creditText);
}
}
if (rightsTexts.size() > 0) {
String joinedRights = rightsTexts.stream().collect(Collectors.joining(", "));
resource.addAttribution(joinedRights);
if (metadata != null) {
metadata.add(new MetadataEntry(new PropertyValue("Copyright"), new PropertyValue(joinedRights)));
}
}
if (creditTexts.size() > 0) {
String joinedCredits = creditTexts.stream().collect(Collectors.joining(", "));
resource.addAttribution(joinedCredits);
if (metadata != null) {
metadata.add(new MetadataEntry(new PropertyValue("Credit"), new PropertyValue(joinedCredits)));
}
}
resource.setLicenses(license);
return resource;
}
use of de.digitalcollections.iiif.model.PropertyValue in project cdmlib by cybertaxonomy.
the class ManifestComposer method copyAttributionAndLicenseToManifest.
/**
* Due to limitations in universal viewer it seems not to be able
* to show attribution and licenses, therefore we copy this data to
* also to the metadata
*
* <b>NOTE:</b> This method expects that the canvas attributions and
* licenses are not localized!!!!
*
* @param canvas
*/
private void copyAttributionAndLicenseToManifest(Manifest manifest) {
PropertyValue attributions = new PropertyValue();
List<URI> licenses = new ArrayList<>();
String firstAttributionString = null;
boolean hasAttributions = false;
boolean hasLicenses = false;
boolean hasDiversAttributions = false;
boolean hasDiversLicenses = false;
String firstLicensesString = null;
if (manifest.getSequences() == null) {
// nothing to do, skip!
return;
}
for (Sequence sequence : manifest.getSequences()) {
for (Canvas canvas : sequence.getCanvases()) {
if (canvas.getAttribution() != null) {
canvas.getAttribution().getValues().stream().forEachOrdered(val -> attributions.addValue(val));
String thisAttributionString = canvas.getAttribution().getValues().stream().sorted().collect(Collectors.joining());
if (firstAttributionString == null) {
firstAttributionString = thisAttributionString;
hasAttributions = true;
} else {
hasDiversAttributions |= !firstAttributionString.equals(thisAttributionString);
}
}
if (canvas.getLicenses() != null && canvas.getLicenses().size() > 0) {
licenses.addAll(canvas.getLicenses());
String thisLicensesString = canvas.getLicenses().stream().map(URI::toString).sorted().collect(Collectors.joining());
if (firstLicensesString == null) {
firstLicensesString = thisLicensesString;
hasLicenses = true;
} else {
hasDiversLicenses |= !firstLicensesString.equals(thisLicensesString);
}
}
}
}
String diversityInfo = "";
if (hasAttributions || hasLicenses) {
String dataTypes;
if (hasAttributions && hasLicenses) {
dataTypes = "attributions and licenses";
} else if (hasAttributions) {
dataTypes = "attributions";
} else {
dataTypes = "licenses";
}
if (hasDiversAttributions || hasDiversLicenses) {
diversityInfo = "Individual " + dataTypes + " per Item:";
} else {
diversityInfo = "Same " + dataTypes + " for any Item:";
}
if (hasAttributions) {
List<String> attrs = new ArrayList<>(attributions.getValues());
attrs = attrs.stream().sorted().distinct().collect(Collectors.toList());
if (doJoinAttributions) {
attrs.add(0, diversityInfo + "<br/>" + attrs.get(0));
attrs.remove(1);
manifest.addAttribution(attrs.stream().sorted().distinct().collect(Collectors.joining("; ")));
} else {
manifest.addAttribution(diversityInfo, attrs.toArray(new String[attributions.getValues().size()]));
}
}
licenses.stream().map(URI::toString).sorted().distinct().forEachOrdered(l -> manifest.addLicense(l));
}
}
Aggregations