Dynamicvalue (element)

From JRapid

Jump to: navigation, search

dynamicvalue is an element, child of property.

Contents

Description

The dynamicvalue element is used for setting the value to a property based on data retrieved from the server side.

The expr attribute is an EL expression used for calculating the value for the property. This expression may use more than one parameter by including param elements. The value for the param is written in FPath and the resulting value can be used as part of the expression in expr.

The trigger attribute specifies which events trigger the calculation of the expression.

  • onload: the property value is calculated when the form is loaded.
  • onchange: the property value is calculated when a change is made to the properties on which it depends.
  • onchangenew: the property value is calculated the first time that the properties it depends on are set.

If the trigger attribute is not set, the property value is calculated onload and onchange.


Note: Dynamicvalue is not currently supported by properties of types 'date', 'time', 'datetime', 'timequantity' or 'timerange'.

Usage

<property>
          ...
       <dynamicvalue 
            expr = EL
            trigger = "onload" | "onchange" | "onchangenew">
            <param entity = ENTITY name= NAME value= FPATH />
            ...
       </dynamicvalue>
          ...
</property>

Example

  • This example shows a property address that is automatically filled with the address of the company that is selected.
    <entity label="Company" name="Company" menu="Menu">
        <property label="Name" name="name" display="primary" />
        <property label="Address" name="address" display="secondary" type="text" />
    </entity>

    <entity label="Visit" name="Visit" menu="Menu">
        <property label="Visit Date" name="visitDate" type="date" display="primary" widget="jdatepicker" />
        <property label="Company" name="company" entity="Company" display="primary" />
        <property disabled="disabled" label="Address" name="address" type="text">
            <dynamicvalue expr="companyParam.address">
                <param entity="Company" name="companyParam" value="company" />
            </dynamicvalue>
        </property>
    </entity>

And this screencast shows the dynamicvalue at work.


  • If you need to perform a more complex operation in order to dynamically set the value for a property, you may use the dynamicvalue element so that the necessary services are automatically generated and then overwrite the method that does the actual work.

Suppose you have the following entities. A ProductionField entity represents a field from were crops are harvested. A HarvestedCrops entity registers, for a determined ProductionField a production amount in tonnes and the date it was harvested. Last, a MonthProduction entity is created to register results for a ProductionField in a determined month.

In this last entity, a boolean property indicates if a field has achieved a minimum level of production. This property is automatically set depending on the field chosen and the date entered. If the total amount of tonnes harvested is greater than a minimum value, the property is set as true.

The entities look like this.

    <entity label="Production Field" menu="Menu" name="ProductionField">
        <property display="primary" label="Name" name="name"/>
        <property label="Description" name="description" type="text"/>
    </entity>

    <entity label="Harvested Crops" menu="Menu" name="HarvestedCrops" order="harvestDate">
        <property display="primary" entity="ProductionField" label="Field" name="field"/>
        <property display="primary" label="Date" name="harvestDate" type="date" widget="jdatepicker"/>
        <property display="secondary" label="Production (Tonnes)" name="production" type="long"/>
        <filter display="primary" label="Field Filter" name="fieldFilter" property="field"/>
        <filter display="primary" label="Month Filter" name="monthFilter" type="date">
            <restriction expr="(month(harvestDate) = month(?) and year(harvestDate) = year(?))">
                <param value="monthFilter"/>
                <param value="monthFilter"/>
            </restriction>
        </filter>
    </entity>

    <entity label="Month Production" menu="Menu" name="MonthProduction">
        <property display="primary" entity="ProductionField" label="Field" name="field"/>
        <property display="primary" label="Closing Date" name="closingDate" type="date" widget="jdatepicker"/>
        <property display="secondary" label="Minimum Level Achieved" name="minLevelAchieved" type="boolean" widget="checkbox">
            <dynamicvalue expr="true">
                <param entity="ProductionField" name="fieldParam" value="field"/>
                <param name="closingDateParam" type="date" value="closingDate"/>
            </dynamicvalue>
        </property>
    </entity>

Note the dynamicvalue in the minLevelAchieved boolean property of the MonthProduction entity. It takes two parameters: the field and the date in the same entity. This makes the dynamicvalue to be calculated again every time one of those two properties changes its value.

The exp attribute is set to true, but it will not be used because, as we mentioned before, we will be overwriting the method that calculates the value and this expression will never get evaluated.

To overwrite this method, we must edit the MonthProductionServices class and add the following code. Remember you can always take a look at the automatically generated method in the MonthProductionServicesAbstract class to find out the signature for the method you must overwrite. The logic inside the method can be as simple or as complex as it needs to be as long as it returns an object of the expected type.

private static long MIN_LEVEL = 300;
	
	@Override
	public Object getDynamicValueForMinLevelAchieved(String fieldParamId, String closingDateParamId) {
		
		ProductionField fieldParam = ProductionField.DAO().findById(fieldParamId);
		java.util.GregorianCalendar closingDateParam = Session.getMySession().parseDate(closingDateParamId);
		
		Filter filter = HarvestedCrops.DAO().getFieldFilterFilter(fieldParam);
		filter = filter.and(HarvestedCrops.DAO().getMonthFilterFilter(closingDateParam));
		
		Collection<HarvestedCrops> harvestedCrops = 
			HarvestedCrops.DAO().findMany(filter, null, 0, Integer.MAX_VALUE, null);
		
		long totalCrops = 0;
		
		for(HarvestedCrops crop: harvestedCrops) {
			totalCrops += crop.getProduction();
		}
		
		return totalCrops >= MIN_LEVEL;
	}

In this case, we make use of the generated methods for the filters in the HarvestedCrops entity. The code retrieves every record of the HarvestedCrops entity for a month and field, sums the total amount of harvested crops and evaluates if its over the minimum level of expected production.

See also

Personal tools