[xwiki-users] Trouble assessing boolean property value of an object
Hi all, I'm having trouble using Velocity to analyze the boolean value of a property of a class. For example, I have class X, with a property Y (I'll call this property 'example_bool' in the lower example code) which is of type 'Boolean'. I've configured this property to use radio buttons so the user is only able to choose one value. I've created several objects of this class, making sure to utilize both the 'Yes' and 'No' values for some of these objects (to ensure for testing purposes I have some of both). This class also has another property, a static list, of location strings, such as 'Location X', 'Location Y', etc. Now, on a page, I am trying to calculate how many of each location have the boolean value of Yes and no with the following velocity/HQL statement: #set ($results = $xwiki.search("select obj.name from BaseObject obj, StringProperty prop where obj.className='Example.ExampleClass' and obj.name<>'Example.ExampleClassTemplate' and prop.id.id=obj.id and prop.name='location' and prop.value='Location X' order by obj.name asc")) #set ($numTrue = 0) #set ($numFalse = 0) #foreach ($item in $results) #set ($object = $xwiki.getDocument(${item})) #if (!$object.get("example_bool")) #set ($numFalse = $numFalse + 1) #else #set ($numTrue = $numTrue + 1) #end #end This code is being executed without error, however, the 'True' count is always being evaluated as the total number of objects, while the 'False' count always stays at 0, even though I KNOW there are objects in this group that have the value of 'No' for the 'example_bool' property. Does anyone know what I'm doing wrong, or have any ideas that might help me out? Thanks in advance gang. On a side note, I've also noted that when using the Boolean property, the user has the choice of 'Yes', 'No' and '---'. What is this later value ('---') and why is it always included? To me, the meaning of Boolean means 'True OR False' and nothing else. Does this third value have a purpose? And is there a way of excluding it from the choices the user sees? Thanks again everyone! -- View this message in context: http://n2.nabble.com/Trouble-assessing-boolean-property-value-of-an-object-t... Sent from the XWiki- Users mailing list archive at Nabble.com.
Hi, See below: BrianJones wrote:
Hi all,
I'm having trouble using Velocity to analyze the boolean value of a property of a class.
For example, I have class X, with a property Y (I'll call this property 'example_bool' in the lower example code) which is of type 'Boolean'. I've configured this property to use radio buttons so the user is only able to choose one value. I've created several objects of this class, making sure to utilize both the 'Yes' and 'No' values for some of these objects (to ensure for testing purposes I have some of both).
This class also has another property, a static list, of location strings, such as 'Location X', 'Location Y', etc.
Now, on a page, I am trying to calculate how many of each location have the boolean value of Yes and no with the following velocity/HQL statement:
#set ($results = $xwiki.search("select obj.name from BaseObject obj, StringProperty prop where obj.className='Example.ExampleClass' and obj.name<>'Example.ExampleClassTemplate' and prop.id.id=obj.id and prop.name='location' and prop.value='Location X' order by obj.name asc")) #set ($numTrue = 0) #set ($numFalse = 0) #foreach ($item in $results) #set ($object = $xwiki.getDocument(${item})) #if (!$object.get("example_bool")) #set ($numFalse = $numFalse + 1) #else #set ($numTrue = $numTrue + 1) #end #end Try to replace
#if (!$object.get("example_bool")) by #if (!$object.getProperty("example_bool").value) Hope this helps (haven't tested, I admit) Jerome.
This code is being executed without error, however, the 'True' count is always being evaluated as the total number of objects, while the 'False' count always stays at 0, even though I KNOW there are objects in this group that have the value of 'No' for the 'example_bool' property.
Does anyone know what I'm doing wrong, or have any ideas that might help me out? Thanks in advance gang.
On a side note, I've also noted that when using the Boolean property, the user has the choice of 'Yes', 'No' and '---'. What is this later value ('---') and why is it always included? To me, the meaning of Boolean means 'True OR False' and nothing else. Does this third value have a purpose? And is there a way of excluding it from the choices the user sees?
Thanks again everyone!
Jerome, Thanks for your suggestion, but it didn't seem to work... Jerome Velociter-2 wrote:
Try to replace
#if (!$object.get("example_bool"))
by
#if (!$object.getProperty("example_bool").value)
Hope this helps (haven't tested, I admit)
Jerome.
I tried using: #if (!$object.get("example_bool").value) #if (!$object.getProperty("example_bool")) #if (!$object.getProperty("example_bool").value) All of these variations produce similar results, but not exactly the same as the original method I tried. The only difference now is that all are being equated to 'False' and none are 'True' (the exact opposite of the behavior when using #if (!$object.get("example_bool"))). Still not the correct output. Any other ideas? -- View this message in context: http://n2.nabble.com/Trouble-assessing-boolean-property-value-of-an-object-t... Sent from the XWiki- Users mailing list archive at Nabble.com.
Jerome Velociter wrote:
Hi,
See below:
BrianJones wrote:
Hi all,
I'm having trouble using Velocity to analyze the boolean value of a property of a class.
For example, I have class X, with a property Y (I'll call this property 'example_bool' in the lower example code) which is of type 'Boolean'. I've configured this property to use radio buttons so the user is only able to choose one value. I've created several objects of this class, making sure to utilize both the 'Yes' and 'No' values for some of these objects (to ensure for testing purposes I have some of both).
This class also has another property, a static list, of location strings, such as 'Location X', 'Location Y', etc.
Now, on a page, I am trying to calculate how many of each location have the boolean value of Yes and no with the following velocity/HQL statement:
#set ($results = $xwiki.search("select obj.name from BaseObject obj, StringProperty prop where obj.className='Example.ExampleClass' and obj.name<>'Example.ExampleClassTemplate' and prop.id.id=obj.id and prop.name='location' and prop.value='Location X' order by obj.name asc")) #set ($numTrue = 0) #set ($numFalse = 0) #foreach ($item in $results) #set ($object = $xwiki.getDocument(${item})) #if (!$object.get("example_bool")) #set ($numFalse = $numFalse + 1) #else #set ($numTrue = $numTrue + 1) #end #end Try to replace
#if (!$object.get("example_bool"))
by
#if (!$object.getProperty("example_bool").value)
#if (!$object.getProperty("example_bool").value == 1) The property value is an integer, not a boolean.
Hope this helps (haven't tested, I admit)
Jerome.
This code is being executed without error, however, the 'True' count is always being evaluated as the total number of objects, while the 'False' count always stays at 0, even though I KNOW there are objects in this group that have the value of 'No' for the 'example_bool' property.
Does anyone know what I'm doing wrong, or have any ideas that might help me out? Thanks in advance gang.
On a side note, I've also noted that when using the Boolean property, the user has the choice of 'Yes', 'No' and '---'. What is this later value ('---') and why is it always included? To me, the meaning of Boolean means 'True OR False' and nothing else. Does this third value have a purpose? And is there a way of excluding it from the choices the user sees?
Thanks again everyone!
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
-- Sergiu Dumitriu http://purl.org/net/sergiu/
Sergiu, Thanks again, but just to clarify things, does TRUE==0 and FALSE==1? Sergiu Dumitriu-2 wrote:
#if (!$object.getProperty("example_bool").value == 1)
The property value is an integer, not a boolean.
-- View this message in context: http://n2.nabble.com/Trouble-assessing-boolean-property-value-of-an-object-t... Sent from the XWiki- Users mailing list archive at Nabble.com.
BrianJones wrote:
Sergiu,
Thanks again, but just to clarify things, does TRUE==0 and FALSE==1?
--- = -1 False = 0 True = 1 And to answer another of your questions, --- means something like unselected, not answered, neither. I know it doesn't quite fit in the boolean logic, but otherwise there's no way of having neither true nor false for a field.
Sergiu Dumitriu-2 wrote:
#if (!$object.getProperty("example_bool").value == 1)
The property value is an integer, not a boolean.
-- Sergiu Dumitriu http://purl.org/net/sergiu/
Sergiu Dumitriu-2 wrote:
--- = -1 False = 0 True = 1
And to answer another of your questions, --- means something like unselected, not answered, neither. I know it doesn't quite fit in the boolean logic, but otherwise there's no way of having neither true nor false for a field.
Thanks for your reply Sergiu, this helps clarify things, however.... Sergiu Dumitriu-2 wrote:
#if (!$object.getProperty("example_bool").value == 1)
I tried this method as you suggested, and got the same result as the original, the 'True' count is still representing all the objects found with the HQL query, and the 'False' count remains at zero. I also tried this: #if ($object.getProperty("example_bool").value == 0) When I tried this, I got the same result on the page, it rendered, and the 'True' count represents all the objects, while the 'False' count remains at zero, BUT, I get an error in the Tomcat window: [ERROR] Left side ($object.getProperty("example_bool").value) of '==' operation has null value. If a reference, it may not be in the context. Operation not possilbe. ExampleSpace.ExamplePage [line 90, column 56] So obviously I'm doing something wrong still. The syntax I'm using seems to be correct, can anyone point me in the right direction? This is frustrating, as it seems that testing the value of a boolean property should be relatively easy. Thanks again. -- View this message in context: http://n2.nabble.com/Trouble-assessing-boolean-property-value-of-an-object-t... Sent from the XWiki- Users mailing list archive at Nabble.com.
BrianJones wrote:
Sergiu Dumitriu-2 wrote:
--- = -1 False = 0 True = 1
And to answer another of your questions, --- means something like unselected, not answered, neither. I know it doesn't quite fit in the boolean logic, but otherwise there's no way of having neither true nor false for a field.
Thanks for your reply Sergiu, this helps clarify things, however....
Sergiu Dumitriu-2 wrote:
#if (!$object.getProperty("example_bool").value == 1)
I tried this method as you suggested, and got the same result as the original, the 'True' count is still representing all the objects found with the HQL query, and the 'False' count remains at zero. I also tried this:
#if ($object.getProperty("example_bool").value == 0)
When I tried this, I got the same result on the page, it rendered, and the 'True' count represents all the objects, while the 'False' count remains at zero, BUT, I get an error in the Tomcat window:
[ERROR] Left side ($object.getProperty("example_bool").value) of '==' operation has null value. If a reference, it may not be in the context. Operation not possilbe. ExampleSpace.ExamplePage [line 90, column 56]
So obviously I'm doing something wrong still. The syntax I'm using seems to be correct, can anyone point me in the right direction? This is frustrating, as it seems that testing the value of a boolean property should be relatively easy.
Thanks again.
Yes, I read the whole snippet again. The problem is that $object is not an object, but a document. You must replace: #set ($object = $xwiki.getDocument(${item})) with #set($object = $xwiki.getDocument(${item}).getObject('Example.ExampleClass')) -- Sergiu Dumitriu http://purl.org/net/sergiu/
Sergiu, Thanks for all your help, everything is working perfectly now! Sergiu Dumitriu-2 wrote:
Yes, I read the whole snippet again. The problem is that $object is not an object, but a document. You must replace:
#set ($object = $xwiki.getDocument(${item}))
with
#set($object = $xwiki.getDocument(${item}).getObject('Example.ExampleClass'))
-- View this message in context: http://n2.nabble.com/Trouble-assessing-boolean-property-value-of-an-object-t... Sent from the XWiki- Users mailing list archive at Nabble.com.
participants (3)
-
BrianJones -
Jerome Velociter -
Sergiu Dumitriu