Skip to content

Commit

Permalink
Fixed: Convert delegator.find to EntityQuery (OFBIZ-9189)
Browse files Browse the repository at this point in the history
The code came from a project under ofbiz 13.07 and is still using deprecated delegator.find calls, so I converted them to EntityQuery

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1780800 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
nmalin committed Jan 29, 2017
1 parent 6347527 commit 5d6a1b5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.entity.condition.EntityCondition;
import org.apache.ofbiz.entity.util.EntityQuery;
import org.apache.ofbiz.service.GenericServiceException;
import org.apache.ofbiz.service.LocalDispatcher;
import org.apache.ofbiz.service.ServiceUtil;
Expand Down Expand Up @@ -240,7 +241,7 @@ public static String recordReportContent(Delegator delegator, LocalDispatcher di
EntityCondition entityConditionOnName = EntityCondition.makeCondition("drObjectInfo", templateFileLocation);
ecl = EntityCondition.makeCondition(UtilMisc.toList(entityConditionRpt, entityConditionOnName));
i++;
} while (delegator.findCountByCondition("ContentDataResourceView", ecl, null, null) > 0);
} while (EntityQuery.use(delegator).from("ContentDataResourceView").where(ecl).queryCount() > 0);

//resolve the initial form structure from master content
Map<String, Object> resultElectronicText = dispatcher.runSync("getElectronicText", UtilMisc.toMap("contentId", masterContentId, "locale", locale, "userLogin", userLogin));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.ofbiz.entity.condition.EntityCondition;
import org.apache.ofbiz.entity.condition.EntityExpr;
import org.apache.ofbiz.entity.condition.EntityOperator;
import org.apache.ofbiz.entity.util.EntityQuery;
import org.apache.ofbiz.entity.util.EntityUtil;
import org.apache.ofbiz.party.party.PartyHelper;
import org.apache.ofbiz.service.DispatchContext;
Expand Down Expand Up @@ -75,7 +76,7 @@ public static Map<String, Object> workEffortPerPerson(DispatchContext dctx, Map<
listConditions.add(conditionLastName);
}
EntityCondition ecl = EntityCondition.makeCondition(listConditions, EntityOperator.AND);
listPersons = delegator.findList("Person", ecl, UtilMisc.toSet("partyId", "firstName", "lastName"), null, null, true);
listPersons = EntityQuery.use(delegator).from("Person").where(ecl).select("partyId", "firstName", "lastName").queryList();
GenericValue person = null;
if (listPersons.size() > 1) {
return ServiceUtil.returnError("Your criteria match with several people");
Expand Down Expand Up @@ -106,7 +107,7 @@ public static Map<String, Object> workEffortPerPerson(DispatchContext dctx, Map<
EntityExpr conditionParty = EntityCondition.makeCondition("partyId", partyId);
listConditionsWorkEffort.add(conditionParty);
ecl = EntityCondition.makeCondition(listConditionsWorkEffort, EntityOperator.AND);
listWorkEffortTime = delegator.findList("WorkEffortAndTimeEntry", ecl, UtilMisc.toSet("hours", "fromDate", "thruDate"), null, null, true);
listWorkEffortTime = EntityQuery.use(delegator).from("WorkEffortAndTimeEntry").where(ecl).select("hours", "fromDate", "thruDate").queryList();
} catch (GenericEntityException e) {
e.printStackTrace();
ServiceUtil.returnError("Error getting party from person name.");
Expand Down Expand Up @@ -184,7 +185,9 @@ public static Map<String, Object> turnOver(DispatchContext dctx, Map<String, Obj
EntityExpr conditionBeforeDate = EntityCondition.makeCondition("thruDate", EntityOperator.LESS_THAN_EQUAL_TO, UtilDateTime.nowTimestamp());
EntityExpr conditionNull = EntityCondition.makeCondition("thruDate", null);
EntityCondition conditionThroughDate = EntityCondition.makeCondition(EntityOperator.OR, UtilMisc.toList(conditionBeforeDate, conditionNull));
List<GenericValue> listProductIds = delegator.findList("ProductCategoryMember", EntityCondition.makeCondition(UtilMisc.toList(conditionProductCategory, conditionFromDate, conditionThroughDate)), UtilMisc.toSet("productId"), null, null, true);
List<GenericValue> listProductIds = EntityQuery.use(delegator).from("ProductCategoryMember")
.where(EntityCondition.makeCondition(UtilMisc.toList(conditionProductCategory, conditionFromDate, conditionThroughDate)))
.select("productId").queryList();
List<String> listProductIdsString = EntityUtil.getFieldListFromEntityList(listProductIds, "productId", true);

EntityExpr conditionProductCat = EntityCondition.makeCondition("productId", EntityOperator.IN, listProductIdsString);
Expand All @@ -202,10 +205,10 @@ public static Map<String, Object> turnOver(DispatchContext dctx, Map<String, Obj
}
// getting list of invoice Ids linked to these productStore
EntityExpr conditionProductStoreId = EntityCondition.makeCondition("productStoreId", EntityOperator.IN, productStoreList);
List<GenericValue> listOrderAndProductStores = delegator.findList("OrderAndProductStore", conditionProductStoreId, UtilMisc.toSet("orderId"), null, null, true);
List<GenericValue> listOrderAndProductStores = EntityQuery.use(delegator).from("OrderAndProductStore").where(conditionProductStoreId).select("orderId").queryList();
List<String> listOrderIds = EntityUtil.getFieldListFromEntityList(listOrderAndProductStores, "orderId", true);
EntityExpr conditionOrderId = EntityCondition.makeCondition("orderId", EntityOperator.IN, listOrderIds);
List<GenericValue> listInvoices = delegator.findList("OrderItemBilling", conditionOrderId, UtilMisc.toSet("invoiceId"), null, null, false);
List<GenericValue> listInvoices = EntityQuery.use(delegator).from("OrderItemBilling").where(conditionOrderId).select("invoiceId").queryList();
List<String> listInvoiceString = EntityUtil.getFieldListFromEntityList(listInvoices, "invoiceId", true);

EntityExpr conditionInvoiceIdProductStore = EntityCondition.makeCondition("invoiceId", EntityOperator.IN, listInvoiceString);
Expand Down Expand Up @@ -252,7 +255,7 @@ public static Map<String, Object> turnOver(DispatchContext dctx, Map<String, Obj
fieldsToSelect.add("productId");
fieldsToSelect.add("partyId");
fieldsToSelect.add("primaryProductCategoryId");
listTurnOver = delegator.findList("InvoiceItemProductAndParty", EntityCondition.makeCondition(listAllConditions), fieldsToSelect, null, null, true);
listTurnOver = EntityQuery.use(delegator).from("InvoiceItemProductAndParty").where(EntityCondition.makeCondition(listAllConditions)).select(fieldsToSelect).queryList();

// adding missing fields
for (GenericValue invoice : listTurnOver) {
Expand All @@ -266,12 +269,10 @@ public static Map<String, Object> turnOver(DispatchContext dctx, Map<String, Obj
EntityExpr conditionInvoiceId = EntityCondition.makeCondition("invoiceId", invoice.getString("invoiceId"));
// EntityExpr conditionInvoiceItemSeqId = EntityCondition.makeCondition("invoiceItemSeqId", invoice.getString("invoiceItemSeqId"));
// List<GenericValue> listOrderBilling = delegator.findList("OrderItemBilling", EntityCondition.makeCondition(UtilMisc.toList(conditionInvoiceId, conditionInvoiceItemSeqId)), UtilMisc.toSet("orderId"), null, null, false);
List<GenericValue> listOrderBilling = delegator.findList("OrderItemBilling", conditionInvoiceId, UtilMisc.toSet("orderId"), null, null, false);
if (UtilValidate.isNotEmpty(listOrderBilling)) {
GenericValue orderBilling = EntityUtil.getFirst(listOrderBilling);
GenericValue orderBilling = EntityQuery.use(delegator).from("OrderItemBilling").where(conditionInvoiceId).select("orderId").queryFirst();
if (orderBilling != null) {
EntityExpr conditionOrderId = EntityCondition.makeCondition("orderId", orderBilling.getString("orderId"));
List<GenericValue> listProductStore = delegator.findList("OrderAndProductStore", conditionOrderId, null, null, null, true);
GenericValue productStore = EntityUtil.getFirst(listProductStore);
GenericValue productStore = EntityQuery.use(delegator).from("OrderAndProductStore").where(conditionOrderId).cache().queryFirst();
if (UtilValidate.isNotEmpty(productStoreList) && ! productStoreList.contains(productStore.getString("productStoreId"))) {
continue; // pretty ugly... but had problems with the rare case where an invoice matches with several orders with more than one productStore
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import org.apache.ofbiz.entity.util.EntityQuery;
import org.xml.sax.SAXException;
import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.GeneralException;
Expand Down Expand Up @@ -221,7 +222,7 @@ public static Map<String, Object> createFlexibleReportFromMaster(DispatchContext
GenericValue masterContentAttribute = null;
try {
EntityCondition entityCondition = EntityCondition.makeCondition("contentId", masterContentId);
masterContentAttribute = EntityUtil.getFirst(delegator.findList("ContentAttribute", entityCondition, null, null, null, false));
masterContentAttribute = EntityQuery.use(delegator).from("ContentAttribute").where(entityCondition).queryFirst();
} catch (GenericEntityException e) {
e.printStackTrace();
return ServiceUtil.returnError(e.getMessage());
Expand Down Expand Up @@ -302,7 +303,7 @@ public static Map<String, Object> overrideReportForm(DispatchContext dctx, Map<S
}

try {
GenericValue content = delegator.findOne("Content", true, UtilMisc.toMap("contentId", reportContentId));
GenericValue content = EntityQuery.use(delegator).from("Content").where("contentId", reportContentId).queryOne();
String dataResourceId = content.getString("dataResourceId");
StringBuffer newForm = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?> <forms xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"http://ofbiz.apache.org/dtds/widget-form.xsd\">");
newForm.append(overrideFilters);
Expand Down Expand Up @@ -351,10 +352,10 @@ public static Map<String, Object> createFlexibleReportFromMasterEntityWorkflow(D
contentId = BirtWorker.recordReportContent(delegator, dispatcher, context);
// callPerformFindFromBirt is the customMethod for Entity workflow
String rptDesignFileName = BirtUtil.resolveRptDesignFilePathFromContent(delegator, contentId);
GenericValue content = delegator.findOne("Content", true, "contentId", contentId);
GenericValue content = EntityQuery.use(delegator).from("Content").where("contentId", contentId).queryOne();
String customMethodId = content.getString("customMethodId");
if (UtilValidate.isEmpty(customMethodId)) customMethodId = "CM_FB_PERFORM_FIND";
GenericValue customMethod = delegator.findOne("CustomMethod", true, "customMethodId", customMethodId);
GenericValue customMethod = EntityQuery.use(delegator).from("CustomMethod").where("customMethodId", customMethodId).cache().queryOne();
if (customMethod == null) {
return ServiceUtil.returnError("CustomMethod not exist : " + customMethodId); //todo labelise
}
Expand Down Expand Up @@ -394,12 +395,12 @@ public static Map<String, Object> createFlexibleReportFromMasterServiceWorkflow(
Map<String, Object> result = ServiceUtil.returnSuccess();

try {
GenericValue masterContent = delegator.findOne("Content", true, "contentId", masterContentId);
GenericValue masterContent = EntityQuery.use(delegator).from("Content").where("contentId", masterContentId).cache().queryOne();
String customMethodId = masterContent.getString("customMethodId");
if (UtilValidate.isEmpty(customMethodId)) {
throw new GeneralException("The master content " + masterContentId + " haven't a customMethod");
}
GenericValue customMethod = delegator.findOne("CustomMethod", true, "customMethodId", customMethodId);
GenericValue customMethod = EntityQuery.use(delegator).from("CustomMethod").where("customMethodId", customMethodId).cache().queryOne();
if (customMethod == null) {
return ServiceUtil.returnError("CustomMethod not exist : " + customMethodId); //todo labelise
}
Expand Down Expand Up @@ -518,9 +519,9 @@ public static Map<String, Object> createFormForDisplay(DispatchContext dctx, Map

String textData = null;
try {
GenericValue content = delegator.findOne("Content", true, "contentId", reportContentId);
GenericValue content = EntityQuery.use(delegator).from("Content").where("contentId", reportContentId).cache().queryOne();
String dataResourceId = content.getString("dataResourceId");
GenericValue electronicText = delegator.findOne("ElectronicText", true, "dataResourceId", dataResourceId);
GenericValue electronicText = EntityQuery.use(delegator).from("ElectronicText").where("dataResourceId", dataResourceId).cache().queryOne();
textData = electronicText.getString("textData");
} catch (GenericEntityException e) {
return ServiceUtil.returnError(e.getMessage());
Expand Down Expand Up @@ -551,7 +552,7 @@ public static Map<String, Object> deleteAllReports(DispatchContext dctx, Map<Str
List<GenericValue> listContent = null;
EntityCondition entityConditionContent = EntityCondition.makeCondition("contentTypeId", "FLEXIBLE_REPORT");
try {
listContent = delegator.findList("Content", entityConditionContent, UtilMisc.toSet("contentId"), null, null, false);
listContent = EntityQuery.use(delegator).from("Content").where(entityConditionContent).select("contentId").queryList();
} catch (GenericEntityException e) {
e.printStackTrace();
return ServiceUtil.returnError(e.getMessage());
Expand Down Expand Up @@ -587,11 +588,11 @@ public static Map<String, Object> deleteFlexibleReport(DispatchContext dctx, Map
List<GenericValue> listRptDesignFileGV = null;
String contentIdRpt;
try {
listContentRpt = delegator.findList("ContentAssoc", EntityCondition.makeCondition("contentId", EntityOperator.EQUALS, contentId), UtilMisc.toSet("contentIdTo"), null, null, false);
listContentRpt = EntityQuery.use(delegator).from("ContentAssoc").where("contentId", contentId).select("contentIdTo").queryList();
contentIdRpt = listContentRpt.get(0).getString("contentIdTo");
List<EntityExpr> listConditions = UtilMisc.toList(EntityCondition.makeCondition("contentTypeId", EntityOperator.EQUALS, "RPTDESIGN"), EntityCondition.makeCondition("contentId", EntityOperator.EQUALS, contentIdRpt));
EntityConditionList<EntityExpr> ecl = EntityCondition.makeCondition(listConditions, EntityOperator.AND);
listRptDesignFileGV = delegator.findList("ContentDataResourceView", ecl, UtilMisc.toSet("drObjectInfo"), null, null, false);
listRptDesignFileGV = EntityQuery.use(delegator).from("ContentDataResourceView").where(ecl).select("drObjectInfo").queryList();
} catch (GenericEntityException e1) {
e1.printStackTrace();
return ServiceUtil.returnError(e1.getMessage());
Expand Down Expand Up @@ -656,7 +657,7 @@ public static Map<String, Object> uploadRptDesign(DispatchContext dctx, Map<Stri

GenericValue dataResource = null;
try {
dataResource = delegator.findOne("DataResource", false, "dataResourceId", dataResourceId);
dataResource = EntityQuery.use(delegator).from("DataResource").where("dataResourceId", dataResourceId).queryOne();
} catch (GenericEntityException e1) {
e1.printStackTrace();
return ServiceUtil.returnError(e1.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.apache.ofbiz.entity.condition.EntityCondition;
import org.apache.ofbiz.entity.condition.EntityConditionList;
import org.apache.ofbiz.entity.condition.EntityExpr;
import org.apache.ofbiz.entity.util.EntityQuery;
import org.apache.ofbiz.entity.util.EntityUtil;
import org.apache.ofbiz.security.Security;
import org.apache.ofbiz.service.GenericServiceException;
Expand Down Expand Up @@ -234,16 +235,16 @@ public static String resolveTemplatePathLocation() {
* @throws GenericEntityException
*/
public static String resolveRptDesignFilePathFromContent(Delegator delegator, String contentId) throws GenericEntityException {
List<GenericValue> listContentRpt = delegator.findList("ContentAssoc", EntityCondition.makeCondition("contentId", contentId), UtilMisc.toSet("contentIdTo"), null, null, true);
if (UtilValidate.isNotEmpty(listContentRpt)) {
String contentIdRpt = EntityUtil.getFirst(listContentRpt).getString("contentIdTo");
GenericValue contentRpt = EntityQuery.use(delegator).from("ContentAssoc").where("contentId", contentId).select("contentIdTo").cache().queryFirst();
if (contentRpt != null) {
String contentIdRpt = contentRpt.getString("contentIdTo");
List<EntityExpr> listConditions = UtilMisc.toList(
EntityCondition.makeCondition("contentTypeId", "RPTDESIGN"),
EntityCondition.makeCondition("contentId", contentIdRpt));
EntityConditionList<EntityExpr> ecl = EntityCondition.makeCondition(listConditions);
List<GenericValue> listDataRessouceRptDesignFile = delegator.findList("ContentDataResourceView", ecl, UtilMisc.toSet("drObjectInfo"), null, null, true);
if (UtilValidate.isNotEmpty(listDataRessouceRptDesignFile)) {
return EntityUtil.getFirst(listDataRessouceRptDesignFile).getString("drObjectInfo");
GenericValue dataRessouceRptDesignFile = EntityQuery.use(delegator).from("ContentDataResourceView").where(ecl).select("drObjectInfo").cache().queryFirst();
if (dataRessouceRptDesignFile != null) {
return dataRessouceRptDesignFile.getString("drObjectInfo");
}
}
return "";
Expand Down

0 comments on commit 5d6a1b5

Please sign in to comment.