use of org.apache.sling.api.resource.ModifiableValueMap in project sling by apache.
the class ResourceCollectionImpl method orderBefore.
public void orderBefore(Resource srcResource, Resource destResource) {
if (srcResource == null) {
throw new IllegalArgumentException("Source Resource can not be null");
}
ModifiableValueMap vm = membersResource.adaptTo(ModifiableValueMap.class);
String[] order = vm.get(ResourceCollectionConstants.REFERENCES_PROP, new String[] {});
String srcPath = srcResource.getPath();
int srcIndex = ArrayUtils.indexOf(order, srcPath);
if (srcIndex < 0) {
log.warn("Collection ordering failed, as there is no resource {} in collection {} for destResource", srcPath, getPath());
return;
}
if (destResource == null) {
//add it to the end.
order = (String[]) ArrayUtils.remove(order, srcIndex);
order = (String[]) ArrayUtils.add(order, srcPath);
} else {
String destPath = destResource.getPath();
if (destPath.equals(srcPath)) {
String message = MessageFormat.format("Collection ordering failed, as source {0} and destination {1} can not be same", srcPath, destPath);
log.error(message);
throw new IllegalArgumentException(message);
}
int destIndex = ArrayUtils.indexOf(order, destPath);
if (destIndex < 0) {
log.warn("Collection ordering failed, as there is no resource {} in collection {} for destResource", destPath, getPath());
return;
}
order = (String[]) ArrayUtils.remove(order, srcIndex);
if (srcIndex < destIndex) {
//recalculate dest index
destIndex = ArrayUtils.indexOf(order, destPath);
}
order = (String[]) ArrayUtils.add(order, destIndex, srcPath);
}
vm.put(ResourceCollectionConstants.REFERENCES_PROP, order);
}
use of org.apache.sling.api.resource.ModifiableValueMap in project acs-aem-commons by Adobe-Consulting-Services.
the class OnDeployScriptBase method renameProperty.
/**
* Rename a property on a resource.
*
* @param resource Resource to update the property name on.
* @param oldPropertyName Old property name.
* @param newPropertyName New property name.
*/
protected final void renameProperty(Resource resource, String oldPropertyName, String newPropertyName) {
ModifiableValueMap properties = resource.adaptTo(ModifiableValueMap.class);
if (properties.containsKey(oldPropertyName)) {
logger.info("Renaming property '{}' to '{}' on resource: {}", new Object[] { oldPropertyName, newPropertyName, resource.getPath() });
properties.put(newPropertyName, properties.get(oldPropertyName));
properties.remove(oldPropertyName);
} else {
logger.debug("Property '{}' does not exist on resource: {}", oldPropertyName, resource.getPath());
}
}
use of org.apache.sling.api.resource.ModifiableValueMap in project acs-aem-commons by Adobe-Consulting-Services.
the class DamMetadataPropertyResetProcess method execute.
@Override
public final void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap) throws WorkflowException {
ResourceResolver resourceResolver = null;
String wfPayload = null;
try {
resourceResolver = this.getResourceResolver(workflowSession.getSession());
wfPayload = (String) workItem.getWorkflowData().getPayload();
final List<String> payloads = workflowPackageManager.getPaths(resourceResolver, wfPayload);
final Map<String, String> srcDestMap = this.getProcessArgsMap(metaDataMap);
for (final String payload : payloads) {
final Asset asset = DamUtil.resolveToAsset(resourceResolver.getResource(payload));
if (asset == null) {
log.debug("Payload path [ {} ] does not resolve to an asset", payload);
continue;
}
String metadataPath = String.format("%s/%s/%s", asset.getPath(), JcrConstants.JCR_CONTENT, DamConstants.METADATA_FOLDER);
Resource metadataResource = resourceResolver.getResource(metadataPath);
if (metadataResource == null) {
log.error("Could not find the metadata node for Asset [ " + asset.getPath() + " ]");
throw new WorkflowException("Could not find the metadata node for Asset [ " + asset.getPath() + " ]");
}
final ModifiableValueMap mvm = metadataResource.adaptTo(ModifiableValueMap.class);
for (final Map.Entry<String, String> entry : srcDestMap.entrySet()) {
final String srcProperty = entry.getValue();
final String destProperty = entry.getKey();
if (mvm.get(srcProperty) != null) {
// Remove dest property first in case Types differ
mvm.remove(destProperty);
// If the src value is NOT null, update the dest property
mvm.put(destProperty, mvm.get(srcProperty));
} else if (mvm.containsKey(srcProperty)) {
// Else if the src value IS null, AND the src property exists on the node, remove the dest property
mvm.remove(destProperty);
}
// Else leave the dest property alone since there is no source defined to overwrite it with
// Remove the source
mvm.remove(srcProperty);
}
}
} catch (LoginException e) {
throw new WorkflowException("Could not get a ResourceResolver object from the WorkflowSession", e);
} catch (RepositoryException e) {
throw new WorkflowException(String.format("Could not find the payload for '%s'", wfPayload), e);
} finally {
if (resourceResolver != null) {
resourceResolver.close();
}
}
}
use of org.apache.sling.api.resource.ModifiableValueMap in project acs-aem-commons by Adobe-Consulting-Services.
the class TwitterFeedUpdaterImpl method updateTwitterFeedComponents.
@Override
@SuppressWarnings("squid:S3776")
public void updateTwitterFeedComponents(ResourceResolver resourceResolver) {
PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
List<Resource> twitterResources = findTwitterResources(resourceResolver);
for (Resource twitterResource : twitterResources) {
Page page = pageManager.getContainingPage(twitterResource);
if (page != null) {
Twitter client = page.adaptTo(Twitter.class);
if (client != null) {
try {
ValueMap properties = twitterResource.getValueMap();
String username = properties.get("username", String.class);
if (!StringUtils.isEmpty(username)) {
log.info("Loading Twitter timeline for user {} for component {}.", username, twitterResource.getPath());
List<Status> statuses = client.getUserTimeline(username);
if (statuses != null) {
List<String> tweetsList = new ArrayList<>(statuses.size());
List<String> jsonList = new ArrayList<>(statuses.size());
for (Status status : statuses) {
tweetsList.add(processTweet(status));
jsonList.add(DataObjectFactory.getRawJSON(status));
}
if (!tweetsList.isEmpty()) {
ModifiableValueMap map = twitterResource.adaptTo(ModifiableValueMap.class);
map.put("tweets", tweetsList.toArray(new String[tweetsList.size()]));
map.put("tweetsJson", jsonList.toArray(new String[jsonList.size()]));
twitterResource.getResourceResolver().commit();
handleReplication(twitterResource);
}
}
}
} catch (PersistenceException e) {
log.error("Exception while updating twitter feed on resource:" + twitterResource.getPath(), e);
} catch (ReplicationException e) {
log.error("Exception while replicating twitter feed on resource:" + twitterResource.getPath(), e);
} catch (TwitterException e) {
log.error("Exception while loading twitter feed on resource:" + twitterResource.getPath(), e);
}
} else {
log.warn("Twitter component found on {}, but page cannot be adapted to Twitter API. Check Cloud SErvice configuration", page.getPath());
}
}
}
}
use of org.apache.sling.api.resource.ModifiableValueMap in project acs-aem-commons by Adobe-Consulting-Services.
the class AssetFolderCreator method setTitles.
private void setTitles(final Resource folder, final AssetFolderDefinition assetFolderDefinition) throws RepositoryException {
if (folder == null) {
log.error("Asset Folder resource [ {} ] is null", assetFolderDefinition.getPath());
return;
}
Resource jcrContent = folder.getChild(JcrConstants.JCR_CONTENT);
if (jcrContent == null) {
log.error("Asset Folder [ {} ] does not have a jcr:content child", assetFolderDefinition.getPath());
return;
}
final ModifiableValueMap properties = jcrContent.adaptTo(ModifiableValueMap.class);
if (!StringUtils.equals(assetFolderDefinition.getTitle(), properties.get(com.day.cq.commons.jcr.JcrConstants.JCR_TITLE, String.class))) {
// Ensure if the asset folder definition already exists that the title is set properly
properties.put(com.day.cq.commons.jcr.JcrConstants.JCR_TITLE, assetFolderDefinition.getTitle());
}
}
Aggregations