AnsweredAssumed Answered

Read / Write values of d:mltext type

Question asked by von_zeppelin on Oct 19, 2011
Hello!

  To be able to read and write all values from / to a property of d:mltext type, I wrote a utility class which is registered as root JS objects in web scripts.
  Here is the code to read values, and it works pretty much fine:

public NativeObject getMLValues(ScriptNode node, String property) {
    NativeObject values = new NativeObject();
    QName propertyQName = QName.resolveToQName(node.getNamespacePrefixResolver(), property);
    // get stored values of a property from the NodeService first
    boolean wasMLAware = MLPropertyInterceptor.setMLAware(true);
    try {
        Map<QName, Serializable> properties = nodeService.getProperties(node.getNodeRef());
        if (properties.containsKey(propertyQName)) {
            for (Map.Entry<Locale, String> e : ((MLText) properties.get(propertyQName)).entrySet()) {
                values.defineProperty(e.getKey().toString(), e.getValue(), NativeObject.PERMANENT);
            }
        }
    } finally {
        MLPropertyInterceptor.setMLAware(wasMLAware);
    }
    // then try to get current value of a property from the ScriptNode itself
    Map<String, Object> properties = node.getProperties();
    if (properties.containsKey(propertyQName.toString())) {
        values.defineProperty(I18NUtil.getContentLocale().toString(), properties.get(propertyQName.toString()).toString(), NativeObject.PERMANENT);
    }
    return values;
}

Here is the code to write values, and it doesn't store any values at all or store only part of them:

public void setMLValues(ScriptNode node, String property, NativeObject values) {
   if (values != null) {
       QName propertyQName = QName.resolveToQName(node.getNamespacePrefixResolver(), property);
       MLText mlText = null;
       boolean wasMLAware = MLPropertyInterceptor.setMLAware(true);
       // updating specified property; if it doesn't exist, it will be created
       try {
           Map<QName, Serializable> properties = nodeService.getProperties(node.getNodeRef());
           mlText = (properties.containsKey(propertyQName)) ? (MLText) properties.get(propertyQName)
                                                            : new MLText();
           mlText.clear();
           for (Object id : NativeObject.getPropertyIds(values)) {
               String propertyName = id.toString();
               mlText.addValue(parseLocale(propertyName), NativeObject.getProperty(values, propertyName).toString());
           }
           nodeService.setProperty(node.getNodeRef(), propertyQName, mlText);
       } finally {
           MLPropertyInterceptor.setMLAware(wasMLAware);
       }
       // updating value of the property of the ScriptNode itself too
       Map<String, Object> properties = node.getProperties();
       properties.put(propertyQName.toString(), mlText.getClosestValue(I18NUtil.getContentLocale()));
   }
}

So is there any way to read/write values of d:mltext property? What's wrong with the code?

Outcomes