Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

CSS class nameConstantDescription
redBgRowStyle.RED_BACKGROUNDset row background to red
boldFontRowStyle.BOLD_FONTset row font weight to bold
yellowBgRowStyle.YELLOW_BACKGROUNDset row background to yellow
brownBgRowStyle.BROWN_BACKGROUND

set row background to brown

blueBgRowStyle.BLUE_BACKGROUNDset row background to blue
greenBgRowStyle.GREEN_BACKGROUNDset row background to green
Info

Prefer constants over CSS class name literals. This will help you avoid many mistakes and keep your code easier to maintain.

...

(Grid's) Criteria modifier allow you to modify state of the SearchCriteriaBuilder used by view component to fetch row entities.

Code Block
themeEclipse
languagejava
linenumberstrue
public void criteriamodifierNamecriteriaModifierName(final SearchCriteriaBuilder searchCriteriaBuilder) {

        // CRITERIA MODIFIER BODY

}

...

  • criteriaModifierName - arbitrary name of modifier method
  • searchCriteriaBuilder - criteria builder used by component

To see how to bind modifier method with view component go to 'criteria modifiers' in 'Hook an Listeners' section.

If you need some sample code see Criteria modifier example in Examples section.

...

3. Examples

...

3.1 Custom validator example

...

themeEclipse
languagejava
linenumberstrue

...

There is also second version of criteria modifier method that you can define:

Code Block
themeEclipse
languagejava
linenumberstrue
public void criteriaModifierName(final SearchCriteriaBuilder searchCriteriaBuilder, final FilterValueHolder filterValueHolder ) {

       Entity order// = entity.getBelongsToField("order");

 CRITERIA MODIFIER BODY
}

Where

  • criteriaModifierName - arbitrary name of modifier method
  • searchCriteriaBuilder - criteria builder used by component
  • filterValueHolder - criteria value passed from before render hook. 

To set FilterValueHolder you have to get it from LookupComponent in before render hook, modify it and set it again

Code Block
themeEclipse
languagejava
linenumberstrue
public void beforeRenderHook(final ViewDefinitionState viewDefinitionState) {
	LookupComponent lookup= (LookupComponent) 
					viewDefinitionState.getComponentByReference("lookupReference");
	FilterValueHolder holder = lookup.getFilterValue();
	holder.put("key", "value");
	lookup.setFilterValue(holder);
}

To see how to bind modifier method with view component go to 'criteria modifiers' in 'Hook an Listeners' section.

If you need some sample code see Criteria modifier example in Examples section.

Anchor
examples
examples

3. Examples

Anchor
customValidatorExample
customValidatorExample

3.1 Custom validator example

Code Block
themeEclipse
languagejava
linenumberstrue
    public boolean checkIfOrderHasTechnology(final DataDefinition dataDefinition, final Entity entity) {
        Entity order = entity.getBelongsToField("order");

        if (order == null) {
            return true;
        }

        if (order.getField("technology") == null) {
            entity.addError(dataDefinition.getField("order"), "products.validate.global.error.orderMustHaveTechnology");
            return false;
        } else {
            return true;
        }
    }

Anchor
modelHookExample
modelHookExample

3.2 Model hook example:

(this particular example is used within the <onSave> tag:

Code Block
    public void fillOrderDatesAndWorkers(final DataDefinition dataDefinition, final Entity entity) {
        if (("02inProgress".equals(entity.getField("state")) || "03done".equals(entity.getField("state")))
                && entity.getField("effectiveDateFrom") == null) {
            entity.setField("effectiveDateFrom", new Date());
            entity.setField("startWorker", securityService.getCurrentUserName());
        }
        if ("03done".equals(entity.getField("state")) && entity.getField("effectiveDateTo") == null) {
            entity.setField("effectiveDateTo", new Date());
            entity.setField("endWorker", securityService.getCurrentUserName());
        }
    }

Anchor
viewHookExample
viewHookExample

3.3 View hook (preRender) example

Code Block
themeEclipse
languagejava
linenumberstrue
   public void checkIfCommentIsRequiredBasedOnResult(final ViewDefinitionState state) {
        FieldComponentState comment = (FieldComponentState) state.getComponentByReference("comment");

        FieldComponentState controlResult = (FieldComponentState) state.getComponentByReference("controlResult");

        if (controlResult != null && controlResult.getFieldValue() != null && "03objection".equals(controlResult.getFieldValue())) {
            comment.setRequired(true);
           if (order == null)comment.requestComponentUpdateState();
        } else {
            return truecomment.setRequired(false);
        }

    }

Anchor
viewListenerExample
viewListenerExample

3.3 View listener hook example

Code Block
themeEclipse
languagejava
linenumberstrue
    if (order.getField("technology") == null) {
    public void checkAcceptedDefectsQuantity(final ViewDefinitionState viewDefinitionState, final ComponentState state,
       entity.addError(dataDefinition.getField("order"), "products.validate.global.error.orderMustHaveTechnology");    final String[] args) {
     return false;  if (!(state instanceof FieldComponentState)) {
  } else {        throw new IllegalStateException("component is  return truenot input");
        }

   }

...

3.2 Model hook example:

(this particular example is used within the <onSave> tag:

Code Block
    public voidFieldComponentState fillOrderDatesAndWorkers(finalacceptedDefectsQuantity DataDefinition dataDefinition, final Entity entity) {= (FieldComponentState) state;

        FieldComponentState comment if= (("02inProgress".equals(entity.getField("state")) || "03done".equals(entity.getField("state")))
   FieldComponentState) viewDefinitionState.getComponentByReference("comment");

        if (acceptedDefectsQuantity.getFieldValue() != null) {
            && entity.getField("effectiveDateFrom") == null) {if (isNumber(acceptedDefectsQuantity.getFieldValue().toString())
                    && entity.setField("effectiveDateFrom", new Date());
  new BigDecimal(acceptedDefectsQuantity.getFieldValue().toString())).compareTo(BigDecimal.ZERO) > 0) {
         entity.setField("startWorker", securityService.getCurrentUserName());         }
comment.setRequired(true);
       if ("03done".equals(entity.getField("state")) && entity.getField("effectiveDateTo") == null)   } else {
            entity.setField("effectiveDateTo", new Date()    comment.setRequired(false);
            entity.setField("endWorker", securityService.getCurrentUserName());}
         }
    }

Anchor
viewHookExamplerowStyleResolverExampleviewHookExample
rowStyleResolverExample

3.

...

4 Row style resolver example

Code Block
themeEclipse
languagejava
linenumberstrue
    public voidSet<String> checkIfCommentIsRequiredBasedOnResultproductsListRowStyleResolver(final ViewDefinitionStateEntity stateproduct) {
        final FieldComponentStateSet<String> commententityStyles = (FieldComponentState) state.getComponentByReference("comment"Sets.newHashSet();
        final FieldComponentStateString controlResultmaterialType = (FieldComponentState) state.getComponentByReference("controlResult");
product.getStringField(ProductFields.GLOBAL_TYPE_OF_MATERIAL);

        if ("04waste".equals(materialType)) {
        if (controlResult != null && controlResult.getFieldValue() != null && "03objection".equals(controlResult.getFieldValue())) {
 entityStyles.add(RowStyle.RED_BACKGROUND);
        }

         comment.setRequired(true);if (StringUtils.isBlank(product.getStringField(ProductFields.EAN))) {
            commententityStyles.requestComponentUpdateStateadd(RowStyle.BOLD_FONT);
        }
else {       return entityStyles;
    comment.setRequired(false);
    }

Anchor
criteriaModifierExample
criteriaModifierExample

3.5 Criteria modifier example

  1. Single parameter example:

    Code Block
    themeEclipse
    languagejava
    linenumberstrue
    public void filterOutNotBelongingToCurrentUser(final SearchCriteriaBuilder scb) {
       

...

  1.  scb.add(SearchRestrictions.eq("owner", securityService.getCurrentUserName()));
    }
  2. Context example: 
    Models:
    1. Shop
    2. Chain
    3. Industry
    Relations
    1. Shop hasMany Industries
    2. Chain hasMany Industries
    3. Shop belongsTo Chain
     

    Target: In industires lookup in shop details view show only industires that are in relation with chain of the shop. 

    Code Block
    themeEclipse
    language

...

  1. html/xml
    linenumberstrue
    <view name="shopDetails" modelName="shop" ...>
         <component type="window" name="window">           
    			...
              

...

  1.  

...

  1. <component type="form" name="form" reference="form">
           

...

  1.  

...

  1.  

...

  1.           ...
      

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.          

...

  1.     <component type="lookup" name="industry" reference="industryLookup" 
    								field="industries" 

...

  1. defaultVisible="false" persistent="false" hasLabel="false">
            

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.           

...

  1.  

...

  1. ...
                    

...

  1.  

...

  1.            <criteriaModifier 
    								class="com.qcadoo.sdt.basic.criteriaModifiers.ShopIndustryLookupCriteriaModifier"						    
    								method="restrictIndustriesToChainContext" />
       

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.             

...

  1. </component>
    					...
      

...

  1.          </component>
       

...

  1.  

...

  1.  

...

  1. </component>
        <hooks>
    		<beforeRender class="com.qcadoo.sdt.basic.hooks.view.ShopDetailsBeforeRenderService" 
    					method="setCriteriaModifierParameters"/>
        </hooks>
    </view>
    
    

...

  1. Code Block
    themeEclipse
    languagejava
    linenumberstrue
    @Service
    public class ShopDetailsBeforeRenderService {
    
        public void setCriteriaModifierParameters(final ViewDefinitionState 

...

  1. state) {
           

...

  1.  LookupComponent industryLookup = 

...

3.4 Row style resolver example

...

themeEclipse
languagejava
linenumberstrue

...

  1. (LookupComponent) state.getComponentByReference("industryLookup");
            FormComponent form = (FormComponent) state.getComponentByReference("form");
    
            Entity shop = form.getEntity();
            if (shop.getId() != 

...

  1. null) 

...

  1. {
    

...

  1.  

...

  1.  

...

  1.          

...

  1.  

...

  1. Entity 

...

  1. chain = 

...

  1. shop.

...

  1. getBelongsToField(

...

  1. ShopFields.

...

  1. CHAIN_FIELD);
                FilterValueHolder holder 

...

  1. = industryLookup.getFilterValue();
                

...

  1. holder.

...

  1. put(ShopIndustryLookupCriteriaModifier.CHAIN_PARAMETER, chain.getId());
           

...

  1.      

...

  1. industryLookup.setFilterValue(holder);
            }
        

...

3.5 Criteria modifier example

...

themeEclipse
languagejava
linenumberstrue

...

  1. }
    }

     

    Code Block
    themeEclipse
    languagejava
    linenumberstrue
    @Service
    public class ShopIndustryLookupCriteriaModifier {
    
    	public static final String CHAIN_PARAMETER = "chain";
    	
    	private static final String CHAIN_REQUIRED = "Chain parameter is required";
    
    	public void restrictIndustriesToChainContext(final SearchCriteriaBuilder scb, final FilterValueHolder filterValue){
    		if(!filterValue.has(CHAIN_PARAMETER)){
    			throw new IllegalArgumentException(CHAIN_REQUIRED);
    		}
    		
    		Long chainId = filterValue.getLong(CHAIN_PARAMETER);
    		scb.createAlias(IndustryFields.CHAINS_FIELD, "c").add(SearchRestrictions.eq("

...

  1. c.id", 

...

  1. chainId));
    	}
    

...

  1. }