Skip to content

Commit

Permalink
fix: Dependency fields. (#41)
Browse files Browse the repository at this point in the history
* fix: Dependency fields.

* add comments.

* fix npe.
  • Loading branch information
EdwinBetanc0urt authored Aug 14, 2024
1 parent 0f36ce2 commit dd97551
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private Map<String, Object> parseField(MBrowseField field) {
detail.put("is_read_only", field.isReadOnly());
detail.put("read_only_logic", field.getReadOnlyLogic());
detail.put("is_info_only", field.isInfoOnly());

// Mandatory Properties
detail.put("is_mandatory", field.isMandatory());

Expand Down Expand Up @@ -248,7 +248,8 @@ private Map<String, Object> parseField(MBrowseField field) {
+ Optional.ofNullable(field.getDefaultValue2()).orElse("")
)
);
detail.put("dependent_fields", DependenceUtil.generateDependentBrowseFields(field));
List<Map<String, Object>> dependentFieldsList = DependenceUtil.generateDependentBrowseFields(field);
detail.put("dependent_fields", dependentFieldsList);
return detail;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@
import org.compiere.model.MTab;
import org.compiere.model.MValRule;
import org.compiere.model.MWindow;
import org.compiere.util.Env;
import org.compiere.util.Util;

/**
* The util class for all documents
* @author Yamel Senih, [email protected], ERPCyA http://www.erpya.com
*/
public class DependenceUtil {

/**
* Determinate if columnName is used on context values
* @param columnName
Expand All @@ -61,7 +60,7 @@ public static boolean isUseParentColumnOnContext(String columnName, String conte
// @ColumnName@ , @#ColumnName@ , @$ColumnName@
StringBuffer patternValue = new StringBuffer()
.append("@")
.append("($|#){0,1}")
.append("($|#|\\d\\|){0,1}")
.append(columnName)
.append("(@)")
;
Expand All @@ -88,24 +87,25 @@ public static boolean isUseParentColumnOnContext(String columnName, String conte

return isUsedParentColumn;
}



public static List<Map<String, Object>> generateDependentProcessParameters(MProcessPara processParameter) {
List<Map<String, Object>> depenentFieldsList = new ArrayList<>();
if (processParameter == null) {
return depenentFieldsList;
}

String parentColumnName = processParameter.getColumnName();
final String parentColumnName = processParameter.getColumnName();

MProcess process = MProcess.get(processParameter.getCtx(), processParameter.getAD_Process_ID());
List<MProcessPara> parametersList = process.getParametersAsList();
if (parametersList == null || parametersList.isEmpty()) {
return depenentFieldsList;
}

parametersList.parallelStream()
parametersList.stream()
.filter(currentParameter -> {
if (!currentParameter.isActive()) {
if (currentParameter == null || !currentParameter.isActive()) {
return false;
}
// Display Logic
Expand All @@ -116,25 +116,33 @@ public static List<Map<String, Object>> generateDependentProcessParameters(MProc
if (isUseParentColumnOnContext(parentColumnName, currentParameter.getDefaultValue())) {
return true;
}
// TODO: Validate range with `_To` suffix
if (isUseParentColumnOnContext(parentColumnName, currentParameter.getDefaultValue2())) {
return true;
}
// ReadOnly Logic
if (isUseParentColumnOnContext(parentColumnName, currentParameter.getReadOnlyLogic())) {
return true;
}
// Dynamic Validation
if (currentParameter.getAD_Val_Rule_ID() > 0) {
MValRule validationRule = MValRule.get(Env.getCtx(), currentParameter.getAD_Val_Rule_ID());
MValRule validationRule = MValRule.get(
currentParameter.getCtx(),
currentParameter.getAD_Val_Rule_ID()
);
if (isUseParentColumnOnContext(parentColumnName, validationRule.getCode())) {
return true;
}
}
return false;
})
.forEach(currentParameter -> {
final String currentColumnName = currentParameter.getColumnName();
Map<String, Object> detail = new HashMap<>();
detail.put("internal_id", currentParameter.getAD_Process_Para_ID());
detail.put("id", currentParameter.getUUID());
detail.put("uuid", currentParameter.getUUID());
detail.put("column_name", currentParameter.getColumnName());
detail.put("column_name", currentColumnName);
// Process
detail.put("parent_id", process.getAD_Process_ID());
detail.put("parent_uuid", process.getUUID());
Expand All @@ -144,19 +152,21 @@ public static List<Map<String, Object>> generateDependentProcessParameters(MProc

return depenentFieldsList;
}



public static List<Map<String, Object>> generateDependentWindowFields(MField field) {
List<Map<String, Object>> depenentFieldsList = new ArrayList<>();
if (field == null) {
return depenentFieldsList;
}
String parentColumnName = MColumn.getColumnName(field.getCtx(), field.getAD_Column_ID());
MColumn column = MColumn.get(field.getCtx(), field.getAD_Column_ID());
final String parentColumnName = column.getColumnName();
MTab parentTab = MTab.get(field.getCtx(), field.getAD_Tab_ID());
List<MTab> tabsList = Arrays.asList(MWindow.get(field.getCtx(), parentTab.getAD_Window_ID()).getTabs(false, null));
if (tabsList == null || tabsList.isEmpty()) {
return depenentFieldsList;
}
tabsList.parallelStream()
tabsList.stream()
.filter(currentTab -> {
// transaltion tab is not rendering on client
return currentTab.isActive() && !currentTab.isTranslationTab() && !currentTab.isSortTab();
Expand All @@ -167,9 +177,9 @@ public static List<Map<String, Object>> generateDependentWindowFields(MField fie
return;
}

fieldsList.parallelStream()
fieldsList.stream()
.filter(currentField -> {
if (!currentField.isActive()) {
if (currentField == null || !currentField.isActive()) {
return false;
}
// Display Logic
Expand All @@ -182,13 +192,19 @@ public static List<Map<String, Object>> generateDependentWindowFields(MField fie
}
// Dynamic Validation
if (currentField.getAD_Val_Rule_ID() > 0) {
MValRule validationRule = MValRule.get(Env.getCtx(), currentField.getAD_Val_Rule_ID());
MValRule validationRule = MValRule.get(
currentField.getCtx(),
currentField.getAD_Val_Rule_ID()
);
if (isUseParentColumnOnContext(parentColumnName, validationRule.getCode())) {
return true;
}
}

MColumn currentColumn = MColumn.get(Env.getCtx(), currentField.getAD_Column_ID());
MColumn currentColumn = MColumn.get(
currentField.getCtx(),
currentField.getAD_Column_ID()
);
// Default Value of Column
if (isUseParentColumnOnContext(parentColumnName, currentColumn.getDefaultValue())) {
return true;
Expand All @@ -203,15 +219,21 @@ public static List<Map<String, Object>> generateDependentWindowFields(MField fie
}
// Dynamic Validation
if (currentColumn.getAD_Val_Rule_ID() > 0) {
MValRule validationRule = MValRule.get(Env.getCtx(), currentColumn.getAD_Val_Rule_ID());
MValRule validationRule = MValRule.get(
currentColumn.getCtx(),
currentColumn.getAD_Val_Rule_ID()
);
if (isUseParentColumnOnContext(parentColumnName, validationRule.getCode())) {
return true;
}
}
return false;
})
.forEach(currentField -> {
String currentColumnName = MColumn.getColumnName(Env.getCtx(), currentField.getAD_Column_ID());
final String currentColumnName = MColumn.getColumnName(
currentField.getCtx(),
currentField.getAD_Column_ID()
);
Map<String, Object> detail = new HashMap<>();
detail.put("internal_id", currentField.getAD_Field_ID());
detail.put("id", currentField.getUUID());
Expand All @@ -226,19 +248,27 @@ public static List<Map<String, Object>> generateDependentWindowFields(MField fie
});
return depenentFieldsList;
}



public static List<Map<String, Object>> generateDependentBrowseFields(MBrowseField browseField) {
List<Map<String, Object>> depenentFieldsList = new ArrayList<>();
if (browseField == null) {
return depenentFieldsList;
}

MViewColumn viewColumn = MViewColumn.getById(Env.getCtx(), browseField.getAD_View_Column_ID(), null);
MViewColumn viewColumn = MViewColumn.getById(
browseField.getCtx(),
browseField.getAD_View_Column_ID(),
null
);
String parentColumnName = viewColumn.getColumnName();

String elementName = null;
if(viewColumn.getAD_Column_ID() != 0) {
MColumn column = MColumn.get(Env.getCtx(), viewColumn.getAD_Column_ID());
MColumn column = MColumn.get(
browseField.getCtx(),
viewColumn.getAD_Column_ID()
);
elementName = column.getColumnName();
}
if(Util.isEmpty(elementName, true)) {
Expand All @@ -252,9 +282,9 @@ public static List<Map<String, Object>> generateDependentBrowseFields(MBrowseFie
return depenentFieldsList;
}

browseFieldsList.parallelStream()
browseFieldsList.stream()
.filter(currentBrowseField -> {
if(!currentBrowseField.isActive()) {
if(currentBrowseField == null || !currentBrowseField.isActive()) {
return false;
}
// Display Logic
Expand All @@ -267,7 +297,8 @@ public static List<Map<String, Object>> generateDependentBrowseFields(MBrowseFie
|| isUseParentColumnOnContext(parentElementName, currentBrowseField.getDefaultValue())) {
return true;
}
// Default Value 2
// Default Value 2 (range)
// TODO: Validate range with `_To` suffix
if (isUseParentColumnOnContext(parentColumnName, currentBrowseField.getDefaultValue2())
|| isUseParentColumnOnContext(parentElementName, currentBrowseField.getDefaultValue2())) {
return true;
Expand All @@ -279,7 +310,10 @@ public static List<Map<String, Object>> generateDependentBrowseFields(MBrowseFie
}
// Dynamic Validation
if (currentBrowseField.getAD_Val_Rule_ID() > 0) {
MValRule validationRule = MValRule.get(Env.getCtx(), currentBrowseField.getAD_Val_Rule_ID());
MValRule validationRule = MValRule.get(
currentBrowseField.getCtx(),
currentBrowseField.getAD_Val_Rule_ID()
);
if (isUseParentColumnOnContext(parentColumnName, validationRule.getCode())
|| isUseParentColumnOnContext(parentElementName, validationRule.getCode())) {
return true;
Expand All @@ -288,12 +322,17 @@ public static List<Map<String, Object>> generateDependentBrowseFields(MBrowseFie
return false;
})
.forEach(currentBrowseField -> {
MViewColumn currentViewColumn = MViewColumn.getById(Env.getCtx(), currentBrowseField.getAD_View_Column_ID(), null);
MViewColumn currentViewColumn = MViewColumn.getById(
currentBrowseField.getCtx(),
currentBrowseField.getAD_View_Column_ID(),
null
);
final String currentColumnName = currentViewColumn.getColumnName();
Map<String, Object> detail = new HashMap<>();
detail.put("id", currentBrowseField.getAD_Browse_Field_ID());
detail.put("id", currentBrowseField.getUUID());
detail.put("uuid", currentBrowseField.getUUID());
detail.put("column_name", currentViewColumn.getColumnName());
detail.put("column_name", currentColumnName);
// Browse
detail.put("parent_id", browse.getAD_Browse_ID());
detail.put("parent_uuid", browse.getUUID());
Expand All @@ -303,4 +342,5 @@ public static List<Map<String, Object>> generateDependentBrowseFields(MBrowseFie

return depenentFieldsList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ Map<String, Object> parseProcessParameter(MProcessPara parameter) {
+ Optional.ofNullable(parameter.getDefaultValue2()).orElse("")
)
);
detail.put("dependent_fields", DependenceUtil.generateDependentProcessParameters(parameter));
List<Map<String, Object>> dependentFieldsList = DependenceUtil.generateDependentProcessParameters(parameter);
detail.put("dependent_fields", dependentFieldsList);
return detail;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ private Map<String, Object> parseField(MField field) {
detail.put("is_always_updateable", column.isAlwaysUpdateable());

// Mandatory Properties
detail.put("is_mandatory", (field.getIsMandatory() != null && field.getIsMandatory().equals("Y")? true: column.isMandatory()));
boolean isMandatory = (field.getIsMandatory() != null && field.getIsMandatory().equals("Y") ? true: column.isMandatory());
detail.put("is_mandatory", isMandatory);
detail.put("mandatory_logic", column.getMandatoryLogic());

// External Info
Expand Down Expand Up @@ -451,7 +452,8 @@ private Map<String, Object> parseField(MField field) {
Optional.ofNullable(field.getDefaultValue()).orElse(column.getDefaultValue())
)
);
detail.put("dependent_fields", DependenceUtil.generateDependentWindowFields(field));
List<Map<String, Object>> dependentFieldsList = DependenceUtil.generateDependentWindowFields(field);
detail.put("dependent_fields", dependentFieldsList);
detail.put("process_id", column.getAD_Process_ID());
if (column.getAD_Process_ID() > 0) {
detail.put("process", parseProcess(
Expand Down

0 comments on commit dd97551

Please sign in to comment.