Skip to content

Commit

Permalink
Make LocalizedAsset compatible with all JcrAssets (instead of only da…
Browse files Browse the repository at this point in the history
…m workspace asset)
  • Loading branch information
eschleb committed Nov 22, 2023
1 parent 96f671d commit b26053b
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package com.merkle.oss.magnolia.imaging.flexible.model;

import com.merkle.oss.magnolia.imaging.flexible.util.LocalizedPropertyNameProvider;
import info.magnolia.context.MgnlContext;
import info.magnolia.dam.api.Asset;
import info.magnolia.dam.api.AssetDecorator;
import info.magnolia.dam.jcr.AbstractJcrItem;
import info.magnolia.dam.jcr.AssetNodeTypes;
import info.magnolia.dam.jcr.DamConstants;
import info.magnolia.jcr.util.PropertyUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import java.lang.invoke.MethodHandles;
import java.util.Locale;
import java.util.Optional;
Expand Down Expand Up @@ -86,14 +82,18 @@ private String getDescription(final Locale locale, final Asset asset) {
}

private Optional<String> getLocalizedStringFromNode(final Asset asset, final Locale locale, final String key) {
try {
final Session damSession = MgnlContext.getJCRSession(DamConstants.WORKSPACE);
final Node assetNode = damSession.getNode(asset.getPath());
return Optional.ofNullable(PropertyUtil.getString(assetNode, localizedPropertyNameProvider.get(assetNode, key, locale)));
} catch (RepositoryException e) {
LOG.error("failed to get localized string from node locale: " + locale + " key " + key + " asset: " + asset.getPath(), e);
return Optional.empty();
}
return Optional
.of(asset)
.filter(AbstractJcrItem.class::isInstance)
.map(AbstractJcrItem.class::cast)
.map(AbstractJcrItem::getNode)
.map(assetNode ->
PropertyUtil.getString(assetNode, localizedPropertyNameProvider.get(assetNode, key, locale))
)
.or(() -> {
LOG.error("failed to get localized string from node locale: {} key {} asset: {}", locale, key, asset.getPath());
return Optional.empty();
});
}
}
}

0 comments on commit b26053b

Please sign in to comment.