Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

3.5 Criteria modifier example

  1. Single parameter example:

    Code Block
    themeEclipse
    languagejava
    linenumberstrue

...

  1. public void filterOutNotBelongingToCurrentUser(final SearchCriteriaBuilder scb) {

...

  1. 
        

...

  1. scb.add(SearchRestrictions.eq("owner", securityService.getCurrentUserName()));

...

  1. 
    }
  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">           
    			...
               <component type="form" name="form" reference="form">
                       ...
                        <component type="lookup" name="industry" reference="industryLookup" 
    								field="industries" defaultVisible="false" persistent="false" hasLabel="false">
                           ...
                                <criteriaModifier 
    								class="com.qcadoo.sdt.basic.criteriaModifiers.ShopIndustryLookupCriteriaModifier"						    
    								method="restrictIndustriesToChainContext" />
                        </component>
    					...
               </component>
         </component>
        <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 setCriteriaModifierParameters(final ViewDefinitionState state) {
            LookupComponent industryLookup = (LookupComponent) state.getComponentByReference("industryLookup");
            FormComponent form = (FormComponent) state.getComponentByReference("form");
    
            Entity shop = form.getEntity();
            if (shop.getId() != null) {
                Entity chain = shop.getBelongsToField(ShopFields.CHAIN_FIELD);
                FilterValueHolder holder = industryLookup.getFilterValue();
                holder.put(ShopIndustryLookupCriteriaModifier.CHAIN_PARAMETER, chain.getId());
                industryLookup.setFilterValue(holder);
            }
        }
    }

     

    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));
    	}
    }