...
1. What is custom method?
...
Custom methods service is basically normal JAVA class. Only additional element is Spring '@Service' annotation.
Code Block |
---|
theme | Eclipse |
---|
language | java | theme | Eclipse |
---|
linenumbers | true |
---|
|
import org.springframework.stereotype.Service;
@Service
public class ClassName {
// CLASS BODY
}
|
...
Access to outside service are made by Spring '@Autowired' annotation.
Code Block |
---|
theme | Eclipse |
---|
language | java | theme |
---|
Eclipse | linenumbers | true |
---|
|
import org.springframework.beans.factory.annotation.Autowired;
...
@Autowired
private ServiceType setviceName;
|
...
Custom validator function has structure:
Code Block |
---|
theme | Eclipse |
---|
language | java | theme |
---|
Eclipse | linenumbers | true |
---|
|
public boolean validatorMethodName(final DataDefinition dataDefinition, final Entity entity) {
// VALIDATOR METHOD BODY
}
|
...
When validation was unsuccessfull you can also add validation message to entity field using construction:
Code Block |
---|
theme | Eclipse |
---|
language | java | theme | Eclipse |
---|
linenumbers | true |
---|
|
entity.addError(dataDefinition.getField("fieldName"), "validationMessage");
|
...
Model hooks are methods that is executed on specific model actions (defined in xml file). Model hook method has structure:
Code Block |
---|
theme | Eclipse |
---|
language | java | theme | Eclipse |
---|
linenumbers | true |
---|
|
public void modelHookMethodName(final DataDefinition dataDefinition, final Entity entity) {
// MODEL HOOK BODY
}
|
...
View hooks are methods that is executed always where request is send to server. View hook method has structure:
Code Block |
---|
theme | Eclipse |
---|
language | java | theme | Eclipse |
---|
linenumbers | true |
---|
|
public void viewHookMethodName(final ViewDefinitionState state) {
// VIEW HOOK BODY
}
|
...
View listeners are methods that is executed when specified event is fired. View listener method has structure:
Code Block |
---|
theme | Eclipse |
---|
language | java |
---|
theme | Eclipse |
---|
linenumbers | true |
---|
|
public void viewListenerMethodName(final ViewDefinitionState state, final ComponentState componentState, final String[] args) {
// VIEW LISTENER BODY
}
|
...
To attach created view listener method to view xml file see 'view listeners' section.
Anchor |
---|
| viewListeners |
---|
| viewListeners |
---|
|
2.5. Grid's row styles resolver
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 entities.
Code Block |
---|
theme | Eclipse |
---|
language | java |
---|
linenumbers | true |
---|
|
public Set<String> gridRowStyleResolverName(final Entity rowEntity) {
// GRID ROW STYLE RESOLVER BODY
}
|
Where
- gridRowStyleResolverName - arbitrary name of resolver method
- rowEntity - entity which will be shown in row
To attach created resolver method to grid or lookup component see 'grid row style resolvers' section.
3. Example
3.1 Custom validator example
Code Block |
---|
theme | Eclipse |
---|
language | java | theme | Eclipse |
---|
linenumbers | true |
---|
|
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;
}
} |
...
3.3 View hook (preRender) example
Code Block |
---|
theme | Eclipse |
---|
language | java |
---|
theme | Eclipse |
---|
linenumbers | true |
---|
|
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);
}
} |
3.3 View listener hook example
Code Block |
---|
theme | Eclipse |
---|
language | java |
---|
theme | Eclipse |
---|
linenumbers | true |
---|
|
public void checkAcceptedDefectsQuantity(final ViewDefinitionState viewDefinitionState, final ComponentState state,
final String[] args) {
if (!(state instanceof FieldComponentState)) {
throw new IllegalStateException("component is not input");
}
FieldComponentState acceptedDefectsQuantity = (FieldComponentState) state;
FieldComponentState comment = (FieldComponentState) viewDefinitionState.getComponentByReference("comment");
if (acceptedDefectsQuantity.getFieldValue() != null) {
if (isNumber(acceptedDefectsQuantity.getFieldValue().toString())
&& (new BigDecimal(acceptedDefectsQuantity.getFieldValue().toString())).compareTo(BigDecimal.ZERO) > 0) {
comment.setRequired(true);
} else {
comment.setRequired(false);
}
}
} |
3.4 Grid row style resolver example
Code Block |
---|
theme | Eclipse |
---|
language | java |
---|
linenumbers | true |
---|
|
public Set<String> testResolverMethod(final Entity product) {
final Set<String> entityStyles = Sets.newHashSet();
final String materialType = product.getStringField(ProductFields.GLOBAL_TYPE_OF_MATERIAL);
if ("04waste".equals(materialType)) {
entityStyles.add(GridComponent.CSS_RED_BG);
}
if (StringUtils.isBlank(product.getStringField(ProductFields.EAN))) {
entityStyles.add(GridComponent.CSS_BOLD_FONT);
}
return entityStyles;
} |