Trouble assessing boolean property value of an object

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Trouble assessing boolean property value of an object

BrianJones
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!
Reply | Threaded
Open this post in threaded view
|

Re: Trouble assessing boolean property value of an object

Jerome Velociter
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!

_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: Trouble assessing boolean property value of an object

BrianJones
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?
Reply | Threaded
Open this post in threaded view
|

Re: Trouble assessing boolean property value of an object

Sergiu Dumitriu-2
In reply to this post by Jerome Velociter
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
> [hidden email]
> http://lists.xwiki.org/mailman/listinfo/users
>


--
Sergiu Dumitriu
http://purl.org/net/sergiu/
_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: Trouble assessing boolean property value of an object

BrianJones
Sergiu,

Thanks again, but just to clarify things, does TRUE==0 and FALSE==1?

Actually, upon testing this method you suggested Sergiu, it gives me the same results as the original method.  The 'True' count is now counting all the objects again, while the 'False' count remains at zero.

I'm using the exact syntax you suggested:  #if (!$object.getProperty("example_bool").value == 1).  I've also tried the opposite: #if (!$object.getProperty("example_bool").value == 0).
This resulted in the exact same result, with the 'True' count representing the total number of objects, and the 'False' count still remaining at zero.

Is there something I'm missing here?

Sergiu Dumitriu-2 wrote
#if (!$object.getProperty("example_bool").value == 1)

The property value is an integer, not a boolean.
Reply | Threaded
Open this post in threaded view
|

Re: Trouble assessing boolean property value of an object

Sergiu Dumitriu-2
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/
_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: Trouble assessing boolean property value of an object

BrianJones
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.
Reply | Threaded
Open this post in threaded view
|

Re: Trouble assessing boolean property value of an object

Sergiu Dumitriu-2
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/
_______________________________________________
users mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/users
Reply | Threaded
Open this post in threaded view
|

Re: Trouble assessing boolean property value of an object

BrianJones
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'))