2 Replies Latest reply on Dec 25, 2009 1:19 PM by Tanya Ruttenberg

    How to convert an Integer into a ValueExpression

    Tanya Ruttenberg Expert

      I have a rather complicated inter-related set of tables. The top table is called office and I want to be able to display a list of offices based on the search criteria.


      The search criteria can be any property of any of the sub-tables. The officeId can be found via some complex SQL queries, but the user must be able to search on anything he wants to no matter how distantly related to officeId it is.


      For example, someone needs to be able to enter an assetTag and find out which office contains the equipment with that assetTag. assetTag happens to be a member of AssetTags, which is related to Devices, which is related to Offices. 


      So I can get the Integer officeId from the database using a SQL query that I define in a method: Integer getOfficeIdByAssetTag()


      Once I have the officeId all I need is a single restriction:



      "o.officeId = #{officeId}"




      The difficulty is two-fold.  I can neither figure out how to create the expression officeId nor how to assign the Integer returned by getOfficeIdByAssetTag() to it.


      TDR

        • 1. Re: How to convert an Integer into a ValueExpression
          Ilya Dyoshin Novice

          you're overcomplicating.


          your method getOfficeIdByAssetTag() could be used as the ReadOnly value expression:


          o.officeId = #{myManagedBean.officeIdByAssetTag}



          Regards!


          Ilya Dyoshin

          • 2. Re: How to convert an Integer into a ValueExpression
            Tanya Ruttenberg Expert

            Your solution didn't exactly work, but pointed me in the right direction.  It seems that the component referenced in the restriction needs to be populated before you set the expression.  At least that's how it seems from my vantage point.


            Here's what actually worked:


            First inject the entity that contains the property you want to restrict one.  In my case I want to restrict on officeId, so that was the Office entity:


                 @In(required=false)
                 Office office;




            Prior to getting the restrictions via getRestrictions, you need to set the attribute on the entity with the value you want to restrict on.


            I did this in my override of getRestrictions, but I guess this can be done at any step prior to getRestrictions. In my case, I wanted officeId if hostname was so here is the code I used:



                 if (hostname!=null)
                 {
                     office.setOfficeId(getOfficeIdByHostname());
                 }



            Then I set the restriction, but the restriction can really be set in the constructor.  The important thing is that the restriction exist and that the attribute referenced in the restriction be set prior to getRestriction.


            This worked like a charm.



            TDR