Skip to content

Commit

Permalink
It is now possible to define whether the single control is readonly o…
Browse files Browse the repository at this point in the history
…r not

Modifiable property of control can be also controlled with conditions
  • Loading branch information
RHarryH committed Feb 4, 2024
1 parent 2b8db02 commit 57fb891
Show file tree
Hide file tree
Showing 21 changed files with 155 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ public abstract class PropertyControl extends Control {
private Object value;
private CustomValidation customValidation;
private boolean required;
private boolean readonly;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
public class Constraints {
private Constraint visibility;
private Constraint requirement;
private Constraint modifiable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@

import com.avispa.ecm.model.configuration.propertypage.content.control.Control;

import java.util.List;

/**
* @author Rafał Hiszpański
*/
interface ControlMapper<C extends Control> {
void processControl(C control, List<String> fillBlacklist, Object context);
void processControl(C control, PropertyPageMapperConfigurer configurer, Object context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public PropertyPageContent convertToContent(PropertyPageMapperConfigurer configu
propertyPageContent.setContext(configurer.getContext());
propertyPageContent.setId(propertyPage.getId());

processControls(propertyPageContent.getControls(), configurer.getFillBlacklist(), context);
processControls(propertyPageContent.getControls(), configurer, context);

return propertyPageContent;
}
Expand All @@ -81,20 +81,20 @@ private Optional<PropertyPageContent> getPropertyPageContent(Content content) {
return Optional.empty();
}

private void processControls(List<Control> controls, List<String> fillBlacklist, Object context) {
private void processControls(List<Control> controls, PropertyPageMapperConfigurer configurer, Object context) {
for(Control control : controls) {
if (control instanceof Columns columns) {
processControls(columns.getControls(), fillBlacklist, context);
processControls(columns.getControls(), configurer, context);
} else if (control instanceof Group group) {
processControls(group.getControls(), fillBlacklist, context);
processControls(group.getControls(), configurer, context);
} else if (control instanceof Tabs tabs) {
for(Tab tab : tabs.getTabs()) {
processControls(tab.getControls(), fillBlacklist, context);
processControls(tab.getControls(), configurer, context);
}
} else if (control instanceof Table table) {
tableMapper.processControl(table, fillBlacklist, context);
tableMapper.processControl(table, configurer, context);
} else {
simpleControlMapper.processControl(control, fillBlacklist, context);
simpleControlMapper.processControl(control, configurer, context);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ public static PropertyPageMapperConfigurer edit() {
private static PropertyPageMapperConfigurer writable(PropertyPageContext context, List<String> ignoredFields) {
return new PropertyPageMapperConfigurer(context, ignoredFields);
}

public boolean isReadonly() {
return context == PropertyPageContext.READONLY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class SimpleControlMapper extends BaseControlsMapper<Control> {
this.displayService = displayService;
}

public void processControl(Control control, List<String> fillBlacklist, Object context) {
public void processControl(Control control, PropertyPageMapperConfigurer configurer, Object context) {
if (control instanceof Label label) {
try {
label.setExpression(expressionResolver.resolve(context, label.getExpression()));
Expand All @@ -60,6 +60,11 @@ public void processControl(Control control, List<String> fillBlacklist, Object c
propertyControl.setLabel(displayService.getDisplayValueFromAnnotation(context.getClass(), propertyControl.getProperty()));
}

// mark all controls as readonly if property page is in readonly mode
if (configurer.isReadonly()) {
propertyControl.setReadonly(true);
}

if (control instanceof ComboRadio comboRadio) {
if (comboRadio.getLoadSettings() instanceof DynamicLoad dynamicLoad) {
try {
Expand All @@ -71,7 +76,7 @@ public void processControl(Control control, List<String> fillBlacklist, Object c
comboRadio.setOptions(dictionaryControlLoader.loadDictionary(comboRadio, context));
}

fillPropertyValue(propertyControl, fillBlacklist, context);
fillPropertyValue(propertyControl, configurer.getFillBlacklist(), context);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TableMapper extends BaseControlsMapper<Table> {
super(dictionaryControlLoader);
}

public void processControl(Table table, List<String> fillBlacklist, Object context) {
public void processControl(Table table, PropertyPageMapperConfigurer configurer, Object context) {
Class<?> tableRowClass = getTableRowClass(table, context.getClass());

table.getControls().stream()
Expand All @@ -61,16 +61,28 @@ public void processControl(Table table, List<String> fillBlacklist, Object conte
comboRadio.setOptions(dictionaryControlLoader.loadDictionary(comboRadio, tableRowClass));
});

// table controls except for checkboxes are always required
// mark table as readonly if property page is in readonly mode
if (configurer.isReadonly()) {
table.setReadonly(true);
}

table.getControls()
.forEach(control -> control.setRequired(!(control instanceof Checkbox)));
.forEach(control -> {
// table controls except for checkboxes are always required
control.setRequired(!(control instanceof Checkbox));

// always add row id
Hidden hidden = new Hidden();
hidden.setProperty("id");
table.getControls().add(hidden);
// rewrite readonly value to the whole table
control.setReadonly(table.isReadonly());
});

// add row id when table is not readonly
if (!table.isReadonly()) {
Hidden hidden = new Hidden();
hidden.setProperty("id");
table.getControls().add(hidden);
}

fillPropertyValue(table, fillBlacklist, context);
fillPropertyValue(table, configurer.getFillBlacklist(), context);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/json-schemas/control/checkbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"required": {
"type": "boolean"
},
"readonly": {
"type": "boolean"
},
"constraints": {
"$ref": "../property/constraints.json#"
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/json-schemas/control/combo.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"required": {
"type": "boolean"
},
"readonly": {
"type": "boolean"
},
"constraints": {
"$ref": "../property/constraints.json#"
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/json-schemas/control/date.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"required": {
"type": "boolean"
},
"readonly": {
"type": "boolean"
},
"constraints": {
"$ref": "../property/constraints.json#"
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/json-schemas/control/datetime.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"required": {
"type": "boolean"
},
"readonly": {
"type": "boolean"
},
"constraints": {
"$ref": "../property/constraints.json#"
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/json-schemas/control/money.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"required": {
"type": "boolean"
},
"readonly": {
"type": "boolean"
},
"constraints": {
"$ref": "../property/constraints.json#"
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/json-schemas/control/number.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"required": {
"type": "boolean"
},
"readonly": {
"type": "boolean"
},
"constraints": {
"$ref": "../property/constraints.json#"
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/json-schemas/control/radio.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"required": {
"type": "boolean"
},
"readonly": {
"type": "boolean"
},
"loadSettings": {
"type": "object",
"oneOf": [
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/json-schemas/control/table.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
"label": {
"type": "string"
},
"readonly": {
"type": "boolean"
},
"property": {
"type": "string",
"pattern": "^[a-zA-Z](?:[a-zA-Z\\d.]*[a-zA-Z\\d])?+$"
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/json-schemas/control/text.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"required": {
"type": "boolean"
},
"readonly": {
"type": "boolean"
},
"constraints": {
"$ref": "../property/constraints.json#"
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/json-schemas/control/textarea.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"required": {
"type": "boolean"
},
"readonly": {
"type": "boolean"
},
"rows": {
"type": "integer",
"minimum": 0
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/json-schemas/property/constraints.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
},
"requirement": {
"$ref": "#/$defs/constraint"
},
"modifiable": {
"$ref": "#/$defs/constraint"
}
},
"$defs": {
Expand Down
Loading

0 comments on commit 57fb891

Please sign in to comment.