Expand my Community achievements bar.

SOLVED

How to setProperty multiple values?

Avatar

Level 4

Hi Friends,

I wrote a function to getProperty of an node and set its for an other node.

Example: The property name: benefits , type: String[] , value: 1,2,5

When I view properties of a page, select the benefits and click Save button. It will get the benefits values and set for an other page.

The function as below work when I select only one benefits. But, It doesn't work when I select more than one benefits.

public void syncNode(ResourceChange change, String[] langCodes, String[] properties) {

        try {

            ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);

            Session session = resourceResolver.adaptTo(Session.class);

            Resource resourceProperty = resourceResolver.getResource(change.getPath());

            for (String langCode : langCodes) {

                String pathSyncProperties = getPathByLangCode(change.getPath(), langCode);

                if (!pathSyncProperties.equals("") && findExistingPath(pathSyncProperties, session) != null) {

                    Node node = JcrUtil.createPath(pathSyncProperties, JcrConstants.NT_UNSTRUCTURED, session);

                    for (String property : properties) {

                        String propertyData = resourceProperty.getValueMap().get(property, String.class);

                        if (propertyData == null || property.equals("")) {

                            continue;

                        }

                        node.getProperty(property).getString();

                        node.setProperty(property, propertyData);

                    }

                }

            }

            resourceResolver.commit();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

How I can set multiple values with setProperty for a node?

Please help me,

Thank you so much,

BienHV

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

You need to set multiple value as array of Value Object

setProperty(java.lang.String name, Value[] values, int type)

          Sets the multi-value property of this node called name to the specified array of values.

Node (Content Repository for Java Technology API Version 2.0)



Arun Patidar

View solution in original post

7 Replies

Avatar

Correct answer by
Community Advisor

Hi,

You need to set multiple value as array of Value Object

setProperty(java.lang.String name, Value[] values, int type)

          Sets the multi-value property of this node called name to the specified array of values.

Node (Content Repository for Java Technology API Version 2.0)



Arun Patidar

Avatar

Level 5

can we set data in Value object? I have an arraylist<String> which needs to be set as multi-valued property. What is the best way for this?

I tried iterating it a loop but that adds only last value in arraylist.
As per this link, the below should work but it gave error -

class java.util.ArrayList cannot be cast to class javax.jcr.Value
    String[] strings = new String[things.size()];
    int idx = 0;
    for (T thing : things) {
        strings[idx++] = thing.toString();
    }

    node.setProperty(property, strings);

 

Avatar

Level 4

Thank you for your help,

I known what you share. But I don't know the value will set for "int type". Could you help me how to set in my code above to replace for the code line:

node.setProperty(property, propertyData);

Thank you so much,

BienHV

Avatar

Community Advisor

Hi,

You can use another method where you don't need to explicitly specify type.

https://docs.adobe.com/docs/en/spec/jsr170/javadocs/jcr-2.0/javax/jcr/Property.htmlsetProperty(java.lang.String name, Value[] values)

The property type of the property will be that specified by the node type of this node. If the property type of one or more of the supplied Value objects is different from that required, then a best-effort conversion is attempted, according to an implemention-dependent definition of "best effort". If the conversion fails, a ValueFormatException is thrown.



Arun Patidar

Avatar

Level 4

Thank you for your help,

My function work with the code as below:

public void syncNode(ResourceChange change, String[] langCodes, String[] properties) {

   try {

  ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
   Session session = resourceResolver.adaptTo(Session.class);
   Resource resourceProperty = resourceResolver.getResource(change.getPath());

  for (String langCode : langCodes) {

  String pathSyncProperties = getPathByLangCode(change.getPath(), langCode);
  if (!pathSyncProperties.equals("") && findExistingPath(pathSyncProperties, session) != null) {

  Node node = JcrUtil.createPath(pathSyncProperties, JcrConstants.NT_UNSTRUCTURED, session);
  for (String property : properties) {

  String[] propertyData = resourceProperty.getValueMap().get(property, String[].class);
  if (propertyData == null || property.equals("")) {

   continue;
   }

  node.setProperty(property, propertyData, 1); //PropertyType. to know int type
   }

  }

  }

  resourceResolver.commit();

   } catch (Exception e) {

  e.printStackTrace();
   }

}