cancel
Showing results for 
Search instead for 
Did you mean: 

[Resolved] How to know if a nodeRef has the property

dranakan
Champ on-the-rise
Champ on-the-rise
Hello,

I would like to test if a nodeRef has the property "myProperty". If I do this :

QName PROP_QNAME_MY_PROPERTY = QName.createQName("http://www.custom.ch/model/custom/1.0", "myProperty");
Object value = nodeService.getProperty(destination, PROP_QNAME_MY_PROPERTY);
if (value==null){
  …
}else{
  …
}
Will be null if the nodeRef has not the property in the model but also null is this value has not been setted.

How can I know if the nodeRef has this property  :?:
It's possible to set a property to a nodeRef which has not this property in the model… is it normal  :?:

Thanks
4 REPLIES 4

jayjayecl
Confirmed Champ
Confirmed Champ
I don't know the context of your needs, but maybe you can attach this single property to a dedicated aspect, and then test if the Node has the aspect, with the nodeService.hasAspect method.

Would that be OK ?

rogier_oudshoor
Champ in-the-making
Champ in-the-making
Alfresco uses a very liberal property policy, which allows you to add non-defined QNAME's to nodes (via setProperty). That means that the reverse method, (getProperty) does not know if the property has been defined; it only checks if it has been set. If you want to see which properties are defined, you have to retrieve the node type and aspect types - and use the dictionary service to cross reference these types to their definitions.

It would be certainly easier to use an aspect if this is a one-time check though Smiley Wink

dranakan
Champ on-the-rise
Champ on-the-rise
Hello,

Thanks JayJayECL and rogier.oudshoorn.

>>this single property to a dedicated aspect
I already to that in some cases and it's work perfectly. But in my model I have define a property at top level (concern all documents), therefore I use simple property.

>>use the dictionary service
Ok, I think I need to get the URI of the NodeRef and compare to the URI of my models… but I wanted avoid this because if I create in the futur a new model extending this with the property that I check, I have to add this new model URI in the comparaison…

dranakan
Champ on-the-rise
Champ on-the-rise
To know if a property (setted or not setted) to a document :


Node currentNode =new Node(actionedUponNodeRef);
String currentNodeURI=currentNode.getType().getNamespaceURI();
if (currentNodeURI.equals("http://www.custom.ch/model/general/1.0")){
    // I know that the property is setted on the document in this model http://www.custom.ch/model/general/1.0.
    …
In this exemple the property to check is setted to a main document in the model http://www.custom.ch/model/general/1.0. (All documents in this model have the properties). If someone want to check a property which is not apply to all documents : compare the URI + type (compare the QNames).

The disadvantage of the comparaison with the URI is when we create a new model extending http://www.custom.ch/model/general/1.0, (the property will be also on all document), we have to add in the test…

Another way to compare if a property is in noderef is to set a value by default and compare with the method described in the firt post.