Table of Contents |
---|
Wiki Markup |
---|
{toc} ---- h3. 1. What is custom method? |
...
You can create custom method and attach it to defined model or view. Using this elements, you can connect defined xml to JAVA code. |
...
To create custom method first you must create service, then implement one or more custom methods. Then you insert reference to created method in xml files. |
...
h3. 2. Create custom methods service |
...
h4. 2.1. service structure |
...
Custom methods service is basicly normal JAVA class. Only additional element is Spring '@Service' annotation. |
...
{code |
} import org.springframework.stereotype.Service; @Service public class ClassName { // CLASS BODY } {code} h4. 2.2. additional services access |
...
In created service you can access many additional services that helps you manipulate data or view. |
...
Access to outside service are made by Spring '@Autowired' annotation. |
...
{code |
} import org.springframework.beans.factory.annotation.Autowired; ... @Autowired private ServiceType setviceName; {code} h4. 2.3. additional services |
...
h5. 2.3.1. DataDefinitionService |
...
This service provides both information about entities model and access to database operations. |
...
h5. 2.3.2. TranslationService |
...
Service used to execute translation to users language. h5. |
...
2.3.3. SecurityService |
...
This service allow you to access informations about current user. |
...
h3. 3. custom methods |
...
{anchor |
...
:customValidators} h4. 3.1. Custom validators |
...
Custom validator is used by model to validate entities. |
...
Custom validator function has structure: |
...
{code |
}
public boolean validatorMethodName(final DataDefinition dataDefinition, final Entity entity) {
// VALIDATOR METHOD BODY
}
|
Where
- validatorMethodName - name of validator method
- dataDefinition - dataDefinition of validated entity
- entity - entity to validate
This method should return true if validation was successfull and false otherwise.
When validation was unsuccessfull you can also add validation message to entity field using construction:
Code Block |
---|
{code} Where * validatorMethodName - name of validator method * dataDefinition - dataDefinition of validated entity * entity - entity to validate This method should return true if validation was successfull and false otherwise. When validation was unsuccessfull you can also add validation message to entity field using construction: {code} entity.addError(dataDefinition.getField("fieldName"), "validationMessage"); {code} Where |
...
* fieldName - name of entity model field |
...
* validationMessage - validation message |
...
To attach created validator method to model xml file see ['model custom validators'|QCDMESDOC:Model Definition Overview#customValidators] section. |
...
{anchor |
...
:modelHooks} h4. 2.2. Model hooks |
...
Model hooks are methods that is executed on specific model actions (defined in xml file). Model hook method has structure: |
...
{code |
} public void modelHookMethodName(final DataDefinition dataDefinition, final Entity entity) { // MODEL HOOK BODY } {code} Where |
...
* modelHookMethodName - name of model hook method |
...
* dataDefinition - dataDefinition of hook event entity |
...
* entity - hook event entity |
...
To attach created model hook method to model xml file see ['custom model event hooks'|QCDMESDOC:Model Definition Overview#customEventHooks] section. |
...
{anchor |
...
:viewHooks} h4. 2.3. View hooks |
...
View hooks are methods that is executed always where request is send to |
...
server. View hook method has structure: |
...
{code |
} public void viewHookMethodName(final ViewDefinitionState state, final Locale locale) { // VIEW HOOK BODY } {code} Where |
...
* viewHookMethodName - name of view hook method |
...
* state - view state |
...
* locale - users locale |
...
To attach created view hook method to view xml file see ['view hooks'|QCDMESDOC:Hooks and Listeners#viewHooks] section. |
...
{anchor |
...
:viewListeners} h4. 2.4. View listeners |
...
View listeners are methods that is executed when specified event is fired. View listener method has structure: |
...
{code |
}
public void viewListenerMethodName(final ViewDefinitionState state, final ComponentState componentState, final String[] args) {
// VIEW LISTENER BODY
}
|
Where
- viewListenerMethodName - name of view listener method
- state - view state
- componentState - component that fired event
- args - array of event arguments
To attach created view listener method to view xml file see 'view listeners' section.
3. Example
...
{code}
Where
* viewListenerMethodName - name of view listener method
* state - view state
* componentState - component that fired event
* args - array of event arguments
To attach created view listener method to view xml file see ['view listeners'|QCDMESDOC:Hooks and Listeners#viewListeners] section.
h3. 3. Example
h4. 3.1 Custom validator example:
{code} 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;
}
}{code}
h4. 3.2 Model hook example:
(this particular example is used within the <onSave> tag:
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());
}
}
h4. 3.3 View hook (preRender) example:
{code} public void checkIfCommentIsRequiredBasedOnResult(final ViewDefinitionState state, final Locale locale) {
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);
}
}{code}
h4. 3.3 View listener hook example:
{code} 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);
}
}
}{code} |