Versions Compared

Key

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

...

To attach created validator method to model xml file see see 'model custom validators' section section.

If you need some sample code see Custom validator example in Examples section

Anchor
modelHooks
modelHooks

...

To attach created model hook method to model xml file see see 'custom model event hooks' section. section.

If you need some sample code see Model hook example in Examples section

Anchor
viewHooks
viewHooks

2.3. View hooks

...

To attach created view hook method to view xml file see see 'view hooks' section section.

If you need some sample code see View hook example in Examples section.

Anchor
viewListeners
viewListeners

...

To attach created view listener method to view xml file see see 'view listeners' section section.

If you need some sample code see View listener example in Examples section.

Anchor
rowStyleResolvers
rowStyleResolvers

...

(Grid's) Row style resolvers returns set of CSS clas names for given row entity. You can use them to mark specified grid rows, e.g. whith negative balance of some arbitrary values. 
Resolver method is invoked for each of row entity.

Code Block
themeEclipse
languagejava
linenumberstrue
public Set<String> rowStyleResolverName(final Entity rowEntity) {

        // GRID ROW STYLE RESOLVER BODY

}

...

CSS class nameConstantDescription
redBgRowStyle.RED_BACKGROUNDset row background to red
boldFontRowStyle.BOLD_FONTset row font weight to bold
Info

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

Info

You can also create infinity number of your own, custom row styles - see Customizing GUI appearance section.

To see how to bind created resolver method with view component go to 'row style resolvers' section.

3. Example

3.1 Custom validator example

...

themeEclipse
languagejava
linenumberstrue

...

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.

Info

You can also create infinity number of your own, custom row styles - see Customizing GUI appearance section.

To see how to bind created resolver method with view component go to 'row style resolvers' in 'Hook an Listeners' section.

If you need some sample code see Row style resolver example in Examples section.

Anchor
criteriaModifier
criteriaModifier

2.6. Criteria modifier

(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 criteriaModifierName(final SearchCriteriaBuilder searchCriteriaBuilder) {

        // CRITERIA MODIFIER BODY

}

Where

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

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 ) {

        // 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);
            comment.requestComponentUpdateState();
        } else {
            comment.setRequired(false);
        }

    }

Anchor
viewListenerExample
viewListenerExample

3.3 View listener hook example

Code Block
themeEclipse
languagejava
linenumberstrue
    public void checkAcceptedDefectsQuantity(final ViewDefinitionState viewDefinitionState, final ComponentState state,
            final String[] args) {
        if (!(state instanceof FieldComponentState)) {
            throw new IllegalStateException("component is not input");
        }
if
(order == null) {     FieldComponentState acceptedDefectsQuantity = (FieldComponentState) state;

  return true;     FieldComponentState comment =  }(FieldComponentState) viewDefinitionState.getComponentByReference("comment");

        if (orderacceptedDefectsQuantity.getFieldgetFieldValue("technology") !== null) {
            if entity.addError(dataDefinition.getField("order"), "products.validate.global.error.orderMustHaveTechnology");(isNumber(acceptedDefectsQuantity.getFieldValue().toString())
                 return false;  && (new BigDecimal(acceptedDefectsQuantity.getFieldValue().toString())).compareTo(BigDecimal.ZERO) >    } else 0) {
            return true;         }
    }

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) {comment.setRequired(true);
            } else {
  if (("02inProgress".equals(entity.getField("state")) || "03done".equals(entity.getField("state")))           comment.setRequired(false);
     && entity.getField("effectiveDateFrom") == null) {   }
         entity.setField("effectiveDateFrom", new Date());
  }
    }

Anchor
rowStyleResolverExample
rowStyleResolverExample

3.4 Row style resolver example

Code Block
themeEclipse
languagejava
linenumberstrue
    public Set<String> productsListRowStyleResolver(final Entity product)  entity.setField("startWorker", securityService.getCurrentUserName());{
        final Set<String> }entityStyles   = Sets.newHashSet();
     if ("03done".equals(entity.getField("state")) && entity.getField("effectiveDateTo") == null) {
  final String materialType = product.getStringField(ProductFields.GLOBAL_TYPE_OF_MATERIAL);

        if entity.setField("effectiveDateTo", new Date(04waste".equals(materialType)); {
           entity.setField("endWorker", securityService.getCurrentUserName() entityStyles.add(RowStyle.RED_BACKGROUND);
        }

    }

3.3 View hook (preRender) example

Code Block
themeEclipse
languagejava
linenumberstrue
   public void checkIfCommentIsRequiredBasedOnResult(final ViewDefinitionState stateif (StringUtils.isBlank(product.getStringField(ProductFields.EAN))) {
        FieldComponentState comment = (FieldComponentState) stateentityStyles.getComponentByReference("comment");add(RowStyle.BOLD_FONT);
        }
        return entityStyles;
FieldComponentState controlResult = (FieldComponentState) state.getComponentByReference("controlResult");

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

Anchor
criteriaModifierExample
criteriaModifierExample

3.5 Criteria modifier example

  1. Single parameter example:

    Code Block
    themeEclipse
    languagejava
    linenumberstrue
    public void filterOutNotBelongingToCurrentUser(final SearchCriteriaBuilder scb) {
        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
    languagehtml/xml
    linenumberstrue
    <view name="shopDetails" modelName="shop" ...>
         <component type="window" name="window">      

...

  1.  

...

  1.  

...

  1.    
    			...
             

...

  1.   

...

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

...

3.3 View listener hook example

...

themeEclipse
languagejava
linenumberstrue
  1.     

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.   ...
              

...

  1.  

...

  1.  

...

  1.  

...

  1.        <component 

...

  1. type="lookup" name="industry" reference="industryLookup" 
    								field="industries" defaultVisible="false" persistent="false" hasLabel="false">
               

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.  

...

  1.        ...
     

...

  1.           

...

  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. </component>
    					...
               </component>
         </component>
        

...

  1. <hooks>
    		<beforeRender class="com.qcadoo.sdt.basic.hooks.view.ShopDetailsBeforeRenderService" 
    					method="setCriteriaModifierParameters"/>
        </hooks>
    </view>
    
    
    Code Block
    themeEclipse
    languagejava
    linenumberstrue
    @Service
    public class ShopDetailsBeforeRenderService {
    
        public void 

...

  1. setCriteriaModifierParameters(final ViewDefinitionState state) {
            LookupComponent 

...

  1. industryLookup 

...

  1. = 

...

  1. (LookupComponent) state.getComponentByReference("industryLookup");
            FormComponent form = 

...

  1. (FormComponent) state.getComponentByReference("form");
    
            Entity shop 

...

  1. = form.getEntity();
            

...

3.4 Row style resolver example

...

themeEclipse
languagejava
linenumberstrue
  1. if (shop.getId() != null) {
             

...

  1.  

...

  1.  

...

  1.  Entity

...

  1.  chain = shop.getBelongsToField(ShopFields.CHAIN_FIELD);
               

...

  1.  

...

  1. FilterValueHolder 

...

  1. holder = 

...

  1. industryLookup.

...

  1. getFilterValue();
             

...

  1.  

...

  1.  

...

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

...

  1.   industryLookup.setFilterValue(holder);
            }
        

...

  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("c.id", chainId));
    	}
    }