Highlighted

java.lang.IllegalArgumentException: Value for key (property name) can't be put into node

radhakrishnan72

25-01-2018

Hi everyone,

                     i am trying to add/update new/existing property with value to asset metadata and when i say resolver.commit(), it results in following exception

java.lang.IllegalArgumentException: Value for key (property name here) can't be put into node: [Ljava.lang.String;@1455d8b2

at org.apache.sling.jcr.resource.internal.JcrModifiableValueMap.put(JcrModifiableValueMap.java:458)

at org.apache.sling.jcr.resource.internal.JcrModifiableValueMap.put(JcrModifiableValueMap.java:47)

aused by: javax.jcr.version.VersionException: Cannot set property. Node [path/to/asset/jcr:content/metadata] is checked in.

at org.apache.jackrabbit.oak.jcr.session.NodeImpl$36.checkPreconditions(NodeImpl.java:1404)

at org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate.prePerform(SessionDelegate.java:615)

at org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate.perform(SessionDelegate.java:205)

at org.apache.jackrabbit.oak.jcr.session.ItemImpl.perform(ItemImpl.java:112)

at org.apache.jackrabbit.oak.jcr.session.NodeImpl.internalSetProperty(NodeImpl.java:1399)

at org.apache.jackrabbit.oak.jcr.session.NodeImpl.setProperty(NodeImpl.java:378)

at org.apache.sling.jcr.resource.internal.JcrModifiableValueMap.put(JcrModifiableValueMap.java:453)

... 19 common frames omitted

E2: java.lang.IllegalArgumentException: Value for key (some other property)  can't be put into node: update (value)

at org.apache.sling.jcr.resource.internal.JcrModifiableValueMap.put(JcrModifiableValueMap.java:458)

at org.apache.sling.jcr.resource.internal.JcrModifiableValueMap.put(JcrModifiableValueMap.java:47)

aused by: javax.jcr.version.VersionException: Cannot set property. Node [path/to/asset/jcr:content/metadata] is checked in.

at org.apache.jackrabbit.oak.jcr.session.NodeImpl$35.checkPreconditions(NodeImpl.java:1368)

at org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate.prePerform(SessionDelegate.java:615)

at org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate.perform(SessionDelegate.java:205)

at org.apache.jackrabbit.oak.jcr.session.ItemImpl.perform(ItemImpl.java:112)

at org.apache.jackrabbit.oak.jcr.session.NodeImpl.internalSetProperty(NodeImpl.java:1363)

at org.apache.jackrabbit.oak.jcr.session.NodeImpl.setProperty(NodeImpl.java:351)

at org.apache.sling.jcr.resource.internal.JcrModifiableValueMap.put(JcrModifiableValueMap.java:455)

... 21 common frames omitted

When i reprocess the same asset for creating/updating same metadata property, it works well. Please suggest any improvements here.

Replies

Highlighted

radhakrishnan72

25-01-2018

Thanks for letting me know, smacdonald2008

Here is the code snippet for the same,

String customValue = "update";

Resource assetRsc = resolver.getResource("path/to/asset/metadata");

if(assetRsc !=null) {

    ModifiableValueMap properties = assetRsc.adaptTo(ModifiableValueMap.class);

     properties.put("custom_property", customValue );

     // some more properties to be set here

     saveToRepo(resolver);

}

private void saveToRepo(ResourceResolver resolver){

try {

resolver.commit();

} catch (PersistenceException e) {

logger.error("Exception while saving changes :", e);

}

}

I hope this helps understanding. Please let me know, if any other information is required.

Highlighted

BrijeshYadav

26-01-2018

Try Asset API. Asset ("The Adobe AEM Quickstart and Web Application.")

Asset is created by AssetManager and can be retrieved either via AssetManager or by adapting a asset Resource to an Asset.

     eg:

     // to create an asset

     AssetManager assetManager = resolver.adaptTo(AssetManager.class);

     Asset newAsset = assetManager.createAsset("/path/to/asset/document.pdf");

     // to get an existing asset

     Asset asset = assetManager.getAsset("/path/to/asset/document.pdf");

     // to get asset by adapting

     Asset asset = resource.adaptTo(Asset.class);

Asset properties can be retrieved or set by adapting it to a ValueMap.

     eg:

     // to get Asset properties value map

     ValueMap map = asset.adaptTo(ValueMap.class);

/Brijesh Yadav

Highlighted

radhakrishnan72

26-01-2018

Hi rmahendr

                          the values(create/update/delete) i use are of string type. The second property takes string[] type values.

Thanks & Regards,

Radhakrishna.

Highlighted

radhakrishnan72

26-01-2018

Hi Yadav Brijesh,

                            thanks for your suggestion. I would look to understand your suggestion using Asset API. What advantage are we going to get when compared to Resource. Can you please help putting your view here and how that helps this problem to be solved?

Thanks & Regards,

Radhakrishna.

Highlighted

smacdonald2008

26-01-2018

When working with AEM - if there is a corresponding API - like Asset Manager API, Tag Manager API, etc, you should ways use these strongly types APIs when programmatically working with AEM.

Here is URL to the Javadocs - "The CQ5 Quickstart and Web Application."

** MAY MAKE A GOOD Ask the AEM Community Session**

Highlighted

radhakrishnan72

26-01-2018

Hi smacdonald2008,

                                    thanks for making a point to use specific API when using corresponding functionality. I do follow Yadav Brijesh and your recommendations here on. It would be great information for everyone, if we have exact difference between them.

I am very much interested to know about this exception.

Thanks & Regards,

Radhakrishna.