How to setProperty multiple values? | Community
Skip to main content
bhoang
Level 4
August 23, 2018
Solved

How to setProperty multiple values?

  • August 23, 2018
  • 6 replies
  • 4266 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

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)

6 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
August 23, 2018

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
Level 4
September 9, 2023

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);

 

bhoang
bhoangAuthor
Level 4
August 31, 2018

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

arunpatidar
Community Advisor
Community Advisor
August 31, 2018

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
bhoang
bhoangAuthor
Level 4
September 4, 2018

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();
   }

}

arunpatidar
Community Advisor
Community Advisor
September 4, 2018
Arun Patidar
bhoang
bhoangAuthor
Level 4
September 4, 2018

Thanks you.