use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.
the class ContentWorker method getAssociatedContentView.
public static List<GenericValue> getAssociatedContentView(GenericValue currentContent, String linkDir, List<String> assocTypes, List<String> contentTypes, String fromDate, String thruDate) throws GenericEntityException {
List<EntityExpr> exprListAnd = new LinkedList<EntityExpr>();
String origContentId = (String) currentContent.get("contentId");
String contentIdName = "contentId";
String contentAssocViewName = "contentAssocView";
if (linkDir != null && "TO".equalsIgnoreCase(linkDir)) {
contentIdName = contentIdName.concat("To");
contentAssocViewName = contentAssocViewName.concat("To");
}
EntityExpr expr = EntityCondition.makeCondition(contentIdName, EntityOperator.EQUALS, origContentId);
exprListAnd.add(expr);
if (contentTypes.size() > 0) {
exprListAnd.add(EntityCondition.makeCondition("contentTypeId", EntityOperator.IN, contentTypes));
}
if (assocTypes.size() > 0) {
exprListAnd.add(EntityCondition.makeCondition("contentAssocTypeId", EntityOperator.IN, assocTypes));
}
if (fromDate != null) {
Timestamp tsFrom = UtilDateTime.toTimestamp(fromDate);
expr = EntityCondition.makeCondition("fromDate", EntityOperator.GREATER_THAN_EQUAL_TO, tsFrom);
exprListAnd.add(expr);
}
if (thruDate != null) {
Timestamp tsThru = UtilDateTime.toTimestamp(thruDate);
expr = EntityCondition.makeCondition("thruDate", EntityOperator.LESS_THAN, tsThru);
exprListAnd.add(expr);
}
Delegator delegator = currentContent.getDelegator();
return EntityQuery.use(delegator).from(contentAssocViewName).where(exprListAnd).queryList();
}
use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.
the class ContentServicesComplex method getAssocAndContentAndDataResourceCacheMethod.
public static Map<String, Object> getAssocAndContentAndDataResourceCacheMethod(Delegator delegator, String contentId, String mapKey, String direction, Timestamp fromDate, String fromDateStr, List<String> assocTypes, List<String> contentTypes, Boolean nullThruDatesOnly, String contentAssocPredicateId, String orderBy) throws GenericEntityException, MiniLangException {
EntityExpr joinExpr = null;
String viewName = null;
String contentFieldName = null;
if (direction != null && "From".equalsIgnoreCase(direction)) {
contentFieldName = "caContentIdTo";
joinExpr = EntityCondition.makeCondition("caContentIdTo", EntityOperator.EQUALS, contentId);
} else {
contentFieldName = "caContentId";
joinExpr = EntityCondition.makeCondition("contentId", EntityOperator.EQUALS, contentId);
}
if (direction != null && "From".equalsIgnoreCase(direction)) {
viewName = "ContentAssocDataResourceViewFrom";
} else {
viewName = "ContentAssocDataResourceViewTo";
}
List<EntityCondition> conditionList = new ArrayList<EntityCondition>();
conditionList.add(joinExpr);
if (UtilValidate.isNotEmpty(mapKey)) {
String mapKeyValue = "is null".equalsIgnoreCase(mapKey) ? null : mapKey;
conditionList.add(EntityCondition.makeCondition("caMapKey", mapKeyValue));
}
if (UtilValidate.isNotEmpty(contentAssocPredicateId)) {
String contentAssocPredicateIdValue = "is null".equalsIgnoreCase(contentAssocPredicateId) ? null : contentAssocPredicateId;
conditionList.add(EntityCondition.makeCondition("caMapKey", contentAssocPredicateIdValue));
}
if (nullThruDatesOnly != null && nullThruDatesOnly) {
conditionList.add(EntityCondition.makeCondition("caThruDate", null));
}
if (UtilValidate.isNotEmpty(assocTypes)) {
conditionList.add(EntityCondition.makeCondition("caContentAssocTypeId", EntityOperator.IN, assocTypes));
}
List<GenericValue> contentAssocsTypeFiltered = EntityQuery.use(delegator).from(viewName).where(conditionList).orderBy("caSequenceNum", "-caFromDate").cache().queryList();
String assocRelationName = null;
if (direction != null && "To".equalsIgnoreCase(direction)) {
assocRelationName = "ToContent";
} else {
assocRelationName = "FromContent";
}
GenericValue contentAssocDataResourceView = null;
GenericValue content = null;
GenericValue dataResource = null;
List<GenericValue> contentAssocDataResourceList = new LinkedList<GenericValue>();
// TODO: this needs to be passed in
Locale locale = Locale.getDefault();
try {
for (GenericValue contentAssocView : contentAssocsTypeFiltered) {
GenericValue contentAssoc = EntityQuery.use(delegator).from("ContentAssoc").where(UtilMisc.toMap("contentId", contentAssocView.getString("contentId"), "contentIdTo", contentAssocView.getString(contentFieldName), "contentAssocTypeId", contentAssocView.getString("caContentAssocTypeId"), "fromDate", contentAssocView.getTimestamp("caFromDate"))).queryOne();
content = contentAssoc.getRelatedOne(assocRelationName, true);
if (UtilValidate.isNotEmpty(contentTypes)) {
String contentTypeId = (String) content.get("contentTypeId");
if (contentTypes.contains(contentTypeId)) {
contentAssocDataResourceView = delegator.makeValue(viewName);
contentAssocDataResourceView.setAllFields(content, true, null, null);
}
} else {
contentAssocDataResourceView = delegator.makeValue(viewName);
contentAssocDataResourceView.setAllFields(content, true, null, null);
}
SimpleMapProcessor.runSimpleMapProcessor("component://content/minilang/ContentManagementMapProcessors.xml", "contentAssocOut", contentAssoc, contentAssocDataResourceView, new LinkedList<Object>(), locale);
String dataResourceId = content.getString("dataResourceId");
if (UtilValidate.isNotEmpty(dataResourceId))
dataResource = content.getRelatedOne("DataResource", true);
if (dataResource != null) {
SimpleMapProcessor.runSimpleMapProcessor("component://content/minilang/ContentManagementMapProcessors.xml", "dataResourceOut", dataResource, contentAssocDataResourceView, new LinkedList<Object>(), locale);
}
contentAssocDataResourceList.add(contentAssocDataResourceView);
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
if (UtilValidate.isNotEmpty(orderBy)) {
List<String> orderByList = StringUtil.split(orderBy, "|");
contentAssocDataResourceList = EntityUtil.orderBy(contentAssocDataResourceList, orderByList);
}
Map<String, Object> results = new HashMap<String, Object>();
results.put("entityList", contentAssocDataResourceList);
if (UtilValidate.isNotEmpty(contentAssocDataResourceList)) {
results.put("view", contentAssocDataResourceList.get(0));
}
return results;
}
use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.
the class CommonWorkers method getCountryList.
public static List<GenericValue> getCountryList(Delegator delegator) {
List<GenericValue> geoList = new LinkedList<>();
String defaultCountry = EntityUtilProperties.getPropertyValue("general", "country.geo.id.default", delegator);
GenericValue defaultGeo = null;
if (UtilValidate.isNotEmpty(defaultCountry)) {
try {
defaultGeo = EntityQuery.use(delegator).from("Geo").where("geoId", defaultCountry).cache().queryOne();
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot lookup Geo", module);
}
}
List<EntityExpr> exprs = UtilMisc.toList(EntityCondition.makeCondition("geoTypeId", EntityOperator.EQUALS, "COUNTRY"));
List<String> countriesAvailable = StringUtil.split(EntityUtilProperties.getPropertyValue("general", "countries.geo.id.available", delegator), ",");
if (UtilValidate.isNotEmpty(countriesAvailable)) {
// only available countries (we don't verify the list of geoId in countries.geo.id.available)
exprs.add(EntityCondition.makeCondition("geoId", EntityOperator.IN, countriesAvailable));
}
List<GenericValue> countriesList = new LinkedList<>();
try {
countriesList = EntityQuery.use(delegator).from("Geo").where(exprs).orderBy("geoName").cache(true).queryList();
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot lookup Geo", module);
}
if (defaultGeo != null) {
geoList.add(defaultGeo);
boolean removeDefaultGeo = UtilValidate.isEmpty(countriesList);
if (!removeDefaultGeo) {
for (GenericValue country : countriesList) {
if (country.get("geoId").equals(defaultGeo.get("geoId"))) {
removeDefaultGeo = true;
}
}
}
if (removeDefaultGeo) {
// Remove default country to avoid double rows in drop-down, from 1st place to keep alphabetical order
geoList.remove(0);
}
geoList.addAll(countriesList);
} else {
geoList = countriesList;
}
return geoList;
}
use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.
the class EntityDataLoadContainer method getTenantList.
private List<GenericValue> getTenantList(Property delegatorNameProp) throws ContainerException {
if (!EntityUtil.isMultiTenantEnabled()) {
throw new ContainerException("Multitenant is disabled, must be enabled in general.properties -> multitenant=Y");
}
Delegator delegator = getDelegator(delegatorNameProp, null);
List<EntityExpr> expr = new ArrayList<EntityExpr>();
expr.add(EntityCondition.makeCondition("disabled", EntityOperator.EQUALS, "N"));
expr.add(EntityCondition.makeCondition("disabled", EntityOperator.EQUALS, null));
try {
return EntityQuery.use(delegator).from("Tenant").where(expr, EntityOperator.OR).queryList();
} catch (GenericEntityException e) {
throw new ContainerException(e);
}
}
use of org.apache.ofbiz.entity.condition.EntityExpr in project ofbiz-framework by apache.
the class FinAccountServices method refundFinAccount.
public static Map<String, Object> refundFinAccount(DispatchContext dctx, Map<String, Object> context) {
LocalDispatcher dispatcher = dctx.getDispatcher();
Delegator delegator = dctx.getDelegator();
Locale locale = (Locale) context.get("locale");
GenericValue userLogin = (GenericValue) context.get("userLogin");
String finAccountId = (String) context.get("finAccountId");
Map<String, Object> result = null;
GenericValue finAccount;
try {
finAccount = EntityQuery.use(delegator).from("FinAccount").where("finAccountId", finAccountId).queryOne();
} catch (GenericEntityException e) {
return ServiceUtil.returnError(e.getMessage());
}
if (finAccount != null) {
// check to make sure the account is refundable
if (!"Y".equals(finAccount.getString("isRefundable"))) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingFinAccountIsNotRefundable", locale));
}
// get the actual and available balance
BigDecimal availableBalance = finAccount.getBigDecimal("availableBalance");
BigDecimal actualBalance = finAccount.getBigDecimal("actualBalance");
// be settled first
if (actualBalance.compareTo(availableBalance) != 0) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "AccountingFinAccountCannotBeRefunded", locale));
}
// now we make sure there is something to refund
if (actualBalance.compareTo(BigDecimal.ZERO) > 0) {
BigDecimal remainingBalance = new BigDecimal(actualBalance.toString());
BigDecimal refundAmount = BigDecimal.ZERO;
List<EntityExpr> exprs = UtilMisc.toList(EntityCondition.makeCondition("finAccountTransTypeId", EntityOperator.EQUALS, "DEPOSIT"), EntityCondition.makeCondition("finAccountId", EntityOperator.EQUALS, finAccountId));
EntityCondition condition = EntityCondition.makeCondition(exprs, EntityOperator.AND);
try (EntityListIterator eli = EntityQuery.use(delegator).from("FinAccountTrans").where(condition).orderBy("-transactionDate").queryIterator()) {
GenericValue trans;
while (remainingBalance.compareTo(FinAccountHelper.ZERO) < 0 && (trans = eli.next()) != null) {
String orderId = trans.getString("orderId");
String orderItemSeqId = trans.getString("orderItemSeqId");
// make sure there is an order available to refund
if (orderId != null && orderItemSeqId != null) {
GenericValue orderHeader = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne();
GenericValue productStore = orderHeader.getRelatedOne("ProductStore", false);
GenericValue orderItem = EntityQuery.use(delegator).from("OrderItem").where("orderId", orderId, "orderItemSeqId", orderItemSeqId).queryOne();
if (!"ITEM_CANCELLED".equals(orderItem.getString("statusId"))) {
// make sure the item hasn't already been returned
List<GenericValue> returnItems = orderItem.getRelated("ReturnItem", null, null, false);
if (UtilValidate.isEmpty(returnItems)) {
BigDecimal txAmt = trans.getBigDecimal("amount");
BigDecimal refAmt = txAmt;
if (remainingBalance.compareTo(txAmt) == -1) {
refAmt = remainingBalance;
}
remainingBalance = remainingBalance.subtract(refAmt);
refundAmount = refundAmount.add(refAmt);
// create the return header
Map<String, Object> rhCtx = UtilMisc.toMap("returnHeaderTypeId", "CUSTOMER_RETURN", "fromPartyId", finAccount.getString("ownerPartyId"), "toPartyId", productStore.getString("payToPartyId"), "userLogin", userLogin);
Map<String, Object> rhResp = dispatcher.runSync("createReturnHeader", rhCtx);
if (ServiceUtil.isError(rhResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(rhResp));
}
String returnId = (String) rhResp.get("returnId");
// create the return item
Map<String, Object> returnItemCtx = new HashMap<>();
returnItemCtx.put("returnId", returnId);
returnItemCtx.put("orderId", orderId);
returnItemCtx.put("description", orderItem.getString("itemDescription"));
returnItemCtx.put("orderItemSeqId", orderItemSeqId);
returnItemCtx.put("returnQuantity", BigDecimal.ONE);
returnItemCtx.put("receivedQuantity", BigDecimal.ONE);
returnItemCtx.put("returnPrice", refAmt);
returnItemCtx.put("returnReasonId", "RTN_NOT_WANT");
// refund return
returnItemCtx.put("returnTypeId", "RTN_REFUND");
returnItemCtx.put("returnItemTypeId", "RET_NPROD_ITEM");
returnItemCtx.put("userLogin", userLogin);
Map<String, Object> retItResp = dispatcher.runSync("createReturnItem", returnItemCtx);
if (ServiceUtil.isError(retItResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(retItResp));
}
String returnItemSeqId = (String) retItResp.get("returnItemSeqId");
// approve the return
Map<String, Object> appRet = UtilMisc.toMap("statusId", "RETURN_ACCEPTED", "returnId", returnId, "userLogin", userLogin);
Map<String, Object> appResp = dispatcher.runSync("updateReturnHeader", appRet);
if (ServiceUtil.isError(appResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(appResp));
}
// "receive" the return - should trigger the refund
Map<String, Object> recRet = UtilMisc.toMap("statusId", "RETURN_RECEIVED", "returnId", returnId, "userLogin", userLogin);
Map<String, Object> recResp = dispatcher.runSync("updateReturnHeader", recRet);
if (ServiceUtil.isError(recResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(recResp));
}
// get the return item
GenericValue returnItem = EntityQuery.use(delegator).from("ReturnItem").where("returnId", returnId, "returnItemSeqId", returnItemSeqId).queryOne();
GenericValue response = returnItem.getRelatedOne("ReturnItemResponse", false);
if (response == null) {
throw new GeneralException("No return response found for: " + returnItem.getPrimaryKey());
}
String paymentId = response.getString("paymentId");
// create the adjustment transaction
Map<String, Object> txCtx = new HashMap<>();
txCtx.put("finAccountTransTypeId", "ADJUSTMENT");
txCtx.put("finAccountId", finAccountId);
txCtx.put("orderId", orderId);
txCtx.put("orderItemSeqId", orderItemSeqId);
txCtx.put("paymentId", paymentId);
txCtx.put("amount", refAmt.negate());
txCtx.put("partyId", finAccount.getString("ownerPartyId"));
txCtx.put("userLogin", userLogin);
Map<String, Object> txResp = dispatcher.runSync("createFinAccountTrans", txCtx);
if (ServiceUtil.isError(txResp)) {
throw new GeneralException(ServiceUtil.getErrorMessage(txResp));
}
}
}
}
}
} catch (GeneralException e) {
Debug.logError(e, module);
return ServiceUtil.returnError(e.getMessage());
}
// check to make sure we balanced out
if (remainingBalance.compareTo(FinAccountHelper.ZERO) == 1) {
result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resourceError, "AccountingFinAccountPartiallyRefunded", locale));
}
}
}
if (result == null) {
result = ServiceUtil.returnSuccess();
}
return result;
}
Aggregations