Foreach (element)

From JRapid

(Difference between revisions)
Jump to: navigation, search
(Usage)
(Examples)
 
(4 intermediate revisions not shown)
Line 1: Line 1:
-
The "foreach" element is useful when you have an entity A which specifies an [[embedded inline]] [[collection]] property of an entity B and you want that collection to be automatically loaded with elements of an entity C.
+
'''foreach''' is an element, child of [[property (element)|property]].
 +
 
 +
==Description==
 +
The '''foreach''' element is useful when you have an entity A which specifies an [[embedded inline]] [[collection]] property of an entity B and you want that collection to be automatically loaded with elements of an entity C.
A first approach to solve this requirement will be overriding the ''find()'' method of the entity A. The new ''find()'' method can query all the C elements and then '''for each''' element of the entity C, create a new element B to hold that element. Finally, return the entity A with the collection of B elements.
A first approach to solve this requirement will be overriding the ''find()'' method of the entity A. The new ''find()'' method can query all the C elements and then '''for each''' element of the entity C, create a new element B to hold that element. Finally, return the entity A with the collection of B elements.
-
JRapid's "foreach" does the same job. You only need to provide an [[EL]] expression for querying all the C elements and specify which is the property of the B entity which will hold each element.  
+
[http://www.jrapid.com JRapid's]] '''foreach''' does the same job. You only need to provide an [[EL]] expression for querying all the C elements and specify which is the property of the B entity which will hold each element.  
-
The "foreach" element is specified inside the property which is an embedded inline collection.
+
The '''foreach''' element is specified inside the embedded inline collection property.
-
==Usage==
+
Attributes:
 +
* select: EL expression for getting the collection of elements to iterate.
 +
* setproperty: the property of the embedded inline entity for storing the items of the collection returned by the ''select'' expression.
 +
* property: ''only applicable when the foreach is inside a defaultset''. It indicates the properties where it will be applied.
-
{{Foreach spec}}
 
-
# select: EL expression for getting the collection of elements to be added.
+
==Usage==
-
# setproperty: the property of the embedded inline entity for storing the items of the collection returned by the ''select'' expression.
+
<property
-
# property: ''only applicable when the ''foreach'' is inside a subset or defaultset''. It indicates the properties where it will be applied.
+
    collection=COLLECTION_TYPE
 +
    embedded="inline"
 +
    entity=ENTITY
 +
    name=NAME
 +
    ... >
 +
      <foreach
 +
          select=EL_EXPRESSION
 +
          setproperty=PROPERTY
 +
          property=PROPERTY
 +
      />
 +
</property>
==Examples==
==Examples==
Line 22: Line 37:
<pre class="brush:xml">
<pre class="brush:xml">
-
//Entity A
+
<!-- Entity A -->
<entity label="Company" name="Company">
<entity label="Company" name="Company">
   ...
   ...
Line 32: Line 47:
   ...
   ...
</entity>
</entity>
-
</pre>
 
-
<pre class="brush:xml">
+
<!-- Entity B -->
-
//Entity B
+
<entity label="AdditionalInfoRow" name="AdditionalInfoRow">
<entity label="AdditionalInfoRow" name="AdditionalInfoRow">
   <property entity="Company" label="Empresa" name="company"/> //Connects entity A with B
   <property entity="Company" label="Empresa" name="company"/> //Connects entity A with B
Line 41: Line 54:
   <property display="secondary" label="Value" name="value"/>
   <property display="secondary" label="Value" name="value"/>
</entity>
</entity>
-
</pre>
 
-
<pre class="brush:xml">
+
<!-- Entity C -->
-
//Entity C
+
<entity label="Data" name="Data">
<entity label="Data" name="Data">
   <property display="primary" label="Data" name="data"/>
   <property display="primary" label="Data" name="data"/>
Line 50: Line 61:
</pre>
</pre>
-
Generated find() method in CompanyServicesAbstract.java:
+
Generated code will populate the ''additionalData'' collection with one instance of ''DataValue'' entity for each instance of ''Data'' entity retrieved by the select expression.
-
 
+
-
<pre class="brush:java">
+
-
    // code for find
+
-
    public Company find(String id) throws ELException {
+
-
        Company obj = null;
+
-
       
+
-
        // step 1: build map
+
-
        ExpressionVariableResolver evr = new ExpressionVariableResolver();
+
-
        FunctionMapper fm = new FunctionMapper();
+
-
        Session session = Session.getMySession();
+
-
           
+
-
        if (id.equals("0")) {   
+
-
            // step 2: new object
+
-
            obj = new Company();
+
-
           
+
-
            // step 3: setvalues             
+
-
            obj.setComments((String)el.evaluate("${'Comments for this company...'}",
+
-
            String.class, evr, fm));
+
-
        } else {
+
-
            obj = Company.DAO().findById(id);
+
-
        }
+
-
       
+
-
        // step 4: locking
+
-
        long time = new java.util.Date().getTime();
+
-
       
+
-
        obj.setReadLastUser(session.getUsername());
+
-
        obj.setReadTimestamp(time);
+
-
       
+
-
        // step 5: foreach
+
-
        for (Object o:((Iterable)el.evaluate("${Data:findAll()}", Object.class, evr, fm))) {
+
-
            if (!obj.containsAdditionalData(o)) {
+
-
                DataValue newObject = new DataValue();
+
-
                newObject.setData((Data)o);
+
-
                newObject.setCompany(obj);
+
-
                obj.getAdditionalData().add(newObject);
+
-
            }
+
-
        }
+
-
         
+
-
        // step 6: return
+
-
        return obj;
+
-
    }
+
-
</pre>
+
* DailyCurrenyRates example:
* DailyCurrenyRates example:

Current revision as of 18:43, 27 May 2011

foreach is an element, child of property.

Contents

Description

The foreach element is useful when you have an entity A which specifies an embedded inline collection property of an entity B and you want that collection to be automatically loaded with elements of an entity C.

A first approach to solve this requirement will be overriding the find() method of the entity A. The new find() method can query all the C elements and then for each element of the entity C, create a new element B to hold that element. Finally, return the entity A with the collection of B elements.

JRapid's] foreach does the same job. You only need to provide an EL expression for querying all the C elements and specify which is the property of the B entity which will hold each element.

The foreach element is specified inside the embedded inline collection property.

Attributes:

  • select: EL expression for getting the collection of elements to iterate.
  • setproperty: the property of the embedded inline entity for storing the items of the collection returned by the select expression.
  • property: only applicable when the foreach is inside a defaultset. It indicates the properties where it will be applied.


Usage

<property
   collection=COLLECTION_TYPE
   embedded="inline" 
   entity=ENTITY 
   name=NAME
   ... >
      <foreach
         select=EL_EXPRESSION
         setproperty=PROPERTY
         property=PROPERTY
      />
</property>

Examples

  • Loading a company's additional data fields:
"foreach" example
<!-- Entity A -->
<entity label="Company" name="Company">
   ...
   <property childproperty="company" collection="sortedset" embedded="inline"
      entity="DataValue" label="Additional Info" labelposition="top" name="additionalData"
      setorder="data">
      <foreach select="Data:findAll()" setproperty="data"/>
   </property>
   ...
</entity>

<!-- Entity B -->
<entity label="AdditionalInfoRow" name="AdditionalInfoRow">
   <property entity="Company" label="Empresa" name="company"/> //Connects entity A with B
   <property display="primary" entity="Data" label="Data" name="data"/> //Connects entity B with C
   <property display="secondary" label="Value" name="value"/>
</entity>

<!-- Entity C -->
<entity label="Data" name="Data">
   <property display="primary" label="Data" name="data"/>
</entity>

Generated code will populate the additionalData collection with one instance of DataValue entity for each instance of Data entity retrieved by the select expression.

  • DailyCurrenyRates example:
"foreach" example
    <entity label="Currency" name="Currency">
        <property display="primary" label="Name" name="name"/>
    </entity>

    <entity label="CurrencyValue" name="CurrencyValue">
        <property entity="Currency" fixed="fixed" label="Currency"
          name="currency"/>
        <property label="Value" name="value" type="double"/>
        <property entity="DailyCurrencyRates" label="Daily Currency Rates"
          name="dailyCurrencyRates"/>
    </entity>

    <entity label="Daily Currency Rates" name="DailyCurrencyRates">
        <property default="now" label="Date" name="rateDate" type="date"/>
        <property childproperty="dailyCurrencyRates" collection="sortedset"
         embedded="inline" entity="CurrencyValue" label="Values" 
         labelposition="fieldset" name="currencyValues" setorder="currency">
            <foreach select="Currency:findAll()" setproperty="currency"/>
        </property>
    </entity> 

See also

Personal tools