Skip to content

Commit

Permalink
Improved: Allow to define a view link condition when creating a dve (…
Browse files Browse the repository at this point in the history
…OFBIZ-13125)


this PR adds the possibility to use the ViewEntityCondition definition
when creating a DynamicView
  • Loading branch information
MkLeila authored Aug 30, 2024
1 parent 693da97 commit 31eb051
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.ofbiz.entity.model.ModelViewEntity.ModelAliasAll;
import org.apache.ofbiz.entity.model.ModelViewEntity.ModelMemberEntity;
import org.apache.ofbiz.entity.model.ModelViewEntity.ModelViewLink;
import org.apache.ofbiz.entity.model.ModelViewEntity.ViewEntityCondition;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
Expand Down Expand Up @@ -351,10 +352,78 @@ public void addAllAliasesToList(List<ModelAlias> addList) {
* @param modelKeyMaps the model key maps
*/
public void addViewLink(String entityAlias, String relEntityAlias, Boolean relOptional, List<ModelKeyMap> modelKeyMaps) {
ModelViewLink modelViewLink = new ModelViewLink(entityAlias, relEntityAlias, relOptional, null, modelKeyMaps);
addViewLink(entityAlias, relEntityAlias, relOptional, modelKeyMaps, null);
}

/**
* Add view link.
* @param entityAlias the entity alias
* @param relEntityAlias the rel entity alias
* @param relOptional the rel optional
* @param viewCond the condition of the view link
*/
public void addViewLink(String entityAlias, String relEntityAlias, Boolean relOptional, ViewEntityCondition viewCond) {
addViewLink(entityAlias, relEntityAlias, relOptional, new ArrayList<ModelKeyMap>(), viewCond);
}

/**
* Add view link.
* @param entityAlias the entity alias
* @param relEntityAlias the rel entity alias
* @param relOptional the rel optional
* @param modelKeyMaps the model key maps
* @param viewCond the condition of the view link
*/
public void addViewLink(String entityAlias, String relEntityAlias, Boolean relOptional,
List<ModelKeyMap> modelKeyMaps, ViewEntityCondition viewCond) {
ModelViewLink modelViewLink = new ModelViewLink(entityAlias, relEntityAlias, relOptional, viewCond, modelKeyMaps);
this.viewLinks.add(modelViewLink);
}

/**
* Prepare a ViewEntityCondition to use in view link
* @param delegator
* @param entityAlias
* @param fieldName
* @param operator
* @param value
* @return
*/
public ViewEntityCondition makeViewEntityCondition(Delegator delegator, String entityAlias, String fieldName, String operator, String value) {
Element entityConditionExpr = ViewEntityCondition.makeViewEntityConditionExpr(entityAlias, fieldName, operator, value, "", "");
return makeViewEntityCondition(delegator, entityConditionExpr);
}

/**
* Prepare a ViewEntityCondition to use in view link
* @param delegator
* @param entityAlias
* @param fieldName
* @param operator
* @param relEntityAlias
* @param relFieldName
* @return
*/
public ViewEntityCondition makeViewEntityCondition(Delegator delegator, String entityAlias, String fieldName, String operator,
String relEntityAlias, String relFieldName) {
Element entityConditionExpr = ViewEntityCondition
.makeViewEntityConditionExpr(entityAlias, fieldName, operator, "", relEntityAlias, relFieldName);
return makeViewEntityCondition(delegator, entityConditionExpr);
}

/**
* Prepare a ViewEntityCondition to use in view link
* @param delegator
* @param entityConditionElement
* @return
*/
public ViewEntityCondition makeViewEntityCondition(Delegator delegator, Element entityConditionElement) {
if (!"entity-condition".equals(entityConditionElement.getNodeName())) {
entityConditionElement = ViewEntityCondition.makeViewEntityCondition(entityConditionElement);
}
return new ViewEntityCondition(this.makeModelViewEntity(delegator), null, entityConditionElement);
}

/**
* Add all view links to list.
* @param addList the add list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.apache.ofbiz.entity.condition.EntityOperator;
import org.apache.ofbiz.entity.jdbc.SqlJdbcUtil;
import org.apache.ofbiz.entity.util.EntityUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

Expand Down Expand Up @@ -1470,6 +1471,71 @@ public EntityCondition getHavingCondition(ModelFieldTypeReader modelFieldTypeRea
return null;
}
}

public static Element makeViewEntityCondition(Element child) {
return makeViewEntityCondition(List.of(child));
}

public static Element makeViewEntityCondition(List<Element> children) {
Document doc = children.get(0).getOwnerDocument();
Element entityConditionElement = doc.createElement("entity-condition");
for (Element child : children) {
if (child.getOwnerDocument() != doc) {
child = (Element) doc.importNode(child, true);
}
entityConditionElement.appendChild(child);
}
return entityConditionElement;
}

public static Element makeViewEntityConditionList(String combine, List<Element> children) {
Document doc = children.get(0).getOwnerDocument();
Element conditionListElement = doc.createElement("condition-list");
if (UtilValidate.isNotEmpty(combine)) {
conditionListElement.setAttribute("combine", combine);
}
for (Element child : children) {
if (child.getOwnerDocument() != doc) {
child = (Element) doc.importNode(child, true);
}
conditionListElement.appendChild(child);
}
return conditionListElement;
}

public static Element makeViewEntityConditionExpr(String entityAlias, String fieldName, String operator, String value) {
return makeViewEntityConditionExpr(entityAlias, fieldName, operator, value, null, null);
}

public static Element makeViewEntityConditionExpr(String entityAlias, String fieldName, String operator,
String relEntityAlias, String relFieldName) {
return makeViewEntityConditionExpr(entityAlias, fieldName, operator, null, relEntityAlias, relFieldName);
}

public static Element makeViewEntityConditionExpr(String entityAlias, String fieldName, String operator,
String value, String relEntityAlias, String relFieldName) {
Document doc = UtilXml.makeEmptyXmlDocument();
Element conditionExprElement = doc.createElement("condition-expr");
if (UtilValidate.isNotEmpty(entityAlias)) {
conditionExprElement.setAttribute("entity-alias", entityAlias);
}
if (UtilValidate.isNotEmpty(fieldName)) {
conditionExprElement.setAttribute("field-name", fieldName);
}
if (UtilValidate.isNotEmpty(operator)) {
conditionExprElement.setAttribute("operator", operator);
}
if (UtilValidate.isNotEmpty(value)) {
conditionExprElement.setAttribute("value", value);
}
if (UtilValidate.isNotEmpty(relEntityAlias)) {
conditionExprElement.setAttribute("rel-entity-alias", relEntityAlias);
}
if (UtilValidate.isNotEmpty(relFieldName)) {
conditionExprElement.setAttribute("rel-field-name", relFieldName);
}
return conditionExprElement;
}
}

public interface ViewCondition extends Serializable {
Expand Down

0 comments on commit 31eb051

Please sign in to comment.