I have 2 assets in the dam, lets call them A and B.
How can I programatically make a new version of A, and use the content of B as that new version?
AssetManager. createRevision makes a new version of A just fine, and I tried AssetManager.createAsset on the path of A using the stream I grabbed from asset B, but it doesnt go through.
A new version gets created but I'm having trouble adding B's content. It doesn't matter what happens to B as it will be deleted afterwards.
Thanks
Solved! Go to Solution.
Views
Replies
Total Likes
Hi @peter-g
Yes, as you mentioned we need to use assetManager.createRevision() to create initial version of asset before replacing content of Asset A by Asset B. please check below code which will create version & replace content as expected.
try (ResourceResolver resourceResolver = request.getResourceResolver()){
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
Resource resourceAssetA = resourceResolver.getResource("/content/dam/test/jane_doe.jpg");
Resource resourceAssetB = resourceResolver.getResource("/content/dam/test/larry_spiller.jpg");
if(resourceAssetA!= null && resourceAssetB!= null) {
Asset assetA = resourceAssetA.adaptTo(Asset.class);
Asset assetB = resourceAssetB.adaptTo(Asset.class);
assetManager.createRevision(assetA, "message to create initial version of asset", assetA.getName());
Rendition renditionAssetB = assetB.getOriginal();
assetManager.createAsset(assetA.getPath(), renditionAssetB.getStream(), renditionAssetB.getMimeType(), false);
}
if(resourceResolver.hasChanges()){
resourceResolver.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
Before creating version & replacing asset content :
After creating version & replacing asset content :
After deleting original asset(Asset B) from which content is copied from , it will not affect to asset(Asset A) which has copied content.
-Manjunath
Hi @peter-g
Yes, as you mentioned we need to use assetManager.createRevision() to create initial version of asset before replacing content of Asset A by Asset B. please check below code which will create version & replace content as expected.
try (ResourceResolver resourceResolver = request.getResourceResolver()){
AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);
Resource resourceAssetA = resourceResolver.getResource("/content/dam/test/jane_doe.jpg");
Resource resourceAssetB = resourceResolver.getResource("/content/dam/test/larry_spiller.jpg");
if(resourceAssetA!= null && resourceAssetB!= null) {
Asset assetA = resourceAssetA.adaptTo(Asset.class);
Asset assetB = resourceAssetB.adaptTo(Asset.class);
assetManager.createRevision(assetA, "message to create initial version of asset", assetA.getName());
Rendition renditionAssetB = assetB.getOriginal();
assetManager.createAsset(assetA.getPath(), renditionAssetB.getStream(), renditionAssetB.getMimeType(), false);
}
if(resourceResolver.hasChanges()){
resourceResolver.commit();
}
} catch (Exception e) {
e.printStackTrace();
}
Before creating version & replacing asset content :
After creating version & replacing asset content :
After deleting original asset(Asset B) from which content is copied from , it will not affect to asset(Asset A) which has copied content.
-Manjunath
Views
Replies
Total Likes
please cross check below 2 things in your code & if these 2 are taken care then please share code snippet.
1. whether you have added resourceResolver.commit(); after version & asset creation line of code.
2. do you have required permission to created asset if you are using service resource resolver or not logged-in with admin credentials. it will throw error when you don't have required permission.
Views
Replies
Total Likes
Views
Replies
Total Likes
Check whether "DAM Update Asset" workflow is enabled or not? if it is in disabled state then that will be the reason thumbnails are not getting created on Asset modification.
Access http://localhost:4502/libs/cq/workflow/content/console.html , go to Launcher tab & check status of "DAM Update Asset"(with description "Update Asset - Modification"), it should be "true" if its enabled.
Views
Replies
Total Likes
Each time a page/asset is modified cq creates a version of the same. This modified resource modification time is set in jcr:lastModified property of the page/asset. Manipulation of this property can be done to save future date and activate page on that date though its not preferred way. You can store the future date as a property in the page/asset. Then you could write a workflow or a scheduled job which activates pages/assets with a future date.
Views
Likes
Replies
Views
Likes
Replies