Search in sources :

Example 16 with GenericPK

use of org.apache.ofbiz.entity.GenericPK in project ofbiz-framework by apache.

the class EntityCacheServices method clearCacheLine.

/**
 * Clear Cache Line Service: one of the following context parameters is required: value, dummyPK or primaryKey
 * @param dctx The DispatchContext that this service is operating in
 * @param context Map containing the input parameters
 * @return Map with the result of the service, the output parameters
 */
public static Map<String, Object> clearCacheLine(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    Boolean distributeBool = (Boolean) context.get("distribute");
    boolean distribute = false;
    if (distributeBool != null)
        distribute = distributeBool.booleanValue();
    if (context.containsKey("value")) {
        GenericValue value = (GenericValue) context.get("value");
        if (Debug.infoOn())
            Debug.logInfo("Got a clear cache line by value service call; entityName: " + value.getEntityName(), module);
        if (Debug.verboseOn())
            Debug.logVerbose("Got a clear cache line by value service call; value: " + value, module);
        delegator.clearCacheLine(value, distribute);
    } else if (context.containsKey("dummyPK")) {
        GenericEntity dummyPK = (GenericEntity) context.get("dummyPK");
        if (Debug.infoOn())
            Debug.logInfo("Got a clear cache line by dummyPK service call; entityName: " + dummyPK.getEntityName(), module);
        if (Debug.verboseOn())
            Debug.logVerbose("Got a clear cache line by dummyPK service call; dummyPK: " + dummyPK, module);
        delegator.clearCacheLineFlexible(dummyPK, distribute);
    } else if (context.containsKey("primaryKey")) {
        GenericPK primaryKey = (GenericPK) context.get("primaryKey");
        if (Debug.infoOn())
            Debug.logInfo("Got a clear cache line by primaryKey service call; entityName: " + primaryKey.getEntityName(), module);
        if (Debug.verboseOn())
            Debug.logVerbose("Got a clear cache line by primaryKey service call; primaryKey: " + primaryKey, module);
        delegator.clearCacheLine(primaryKey, distribute);
    } else if (context.containsKey("condition")) {
        String entityName = (String) context.get("entityName");
        EntityCondition condition = (EntityCondition) context.get("condition");
        if (Debug.infoOn())
            Debug.logInfo("Got a clear cache line by condition service call; entityName: " + entityName, module);
        if (Debug.verboseOn())
            Debug.logVerbose("Got a clear cache line by condition service call; condition: " + condition, module);
        delegator.clearCacheLineByCondition(entityName, condition, distribute);
    }
    return ServiceUtil.returnSuccess();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericPK(org.apache.ofbiz.entity.GenericPK) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntity(org.apache.ofbiz.entity.GenericEntity) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition)

Example 17 with GenericPK

use of org.apache.ofbiz.entity.GenericPK in project ofbiz-framework by apache.

the class EntityDataAssert method checkSingleValue.

public static void checkSingleValue(GenericValue checkValue, Delegator delegator, List<Object> errorMessages) throws GenericEntityException {
    if (checkValue == null) {
        errorMessages.add("Got a value to check was null");
        return;
    }
    // to check get the PK, find by that, compare all fields
    GenericPK checkPK = null;
    try {
        checkPK = checkValue.getPrimaryKey();
        GenericValue currentValue = EntityQuery.use(delegator).from(checkPK.getEntityName()).where(checkPK).queryOne();
        if (currentValue == null) {
            errorMessages.add("Entity [" + checkPK.getEntityName() + "] record not found for pk: " + checkPK);
            return;
        }
        ModelEntity modelEntity = checkValue.getModelEntity();
        for (String nonpkFieldName : modelEntity.getNoPkFieldNames()) {
            // skip the fields the entity engine maintains
            if (ModelEntity.CREATE_STAMP_FIELD.equals(nonpkFieldName) || ModelEntity.CREATE_STAMP_TX_FIELD.equals(nonpkFieldName) || ModelEntity.STAMP_FIELD.equals(nonpkFieldName) || ModelEntity.STAMP_TX_FIELD.equals(nonpkFieldName)) {
                continue;
            }
            Object checkField = checkValue.get(nonpkFieldName);
            Object currentField = currentValue.get(nonpkFieldName);
            if (checkField != null && !checkField.equals(currentField)) {
                errorMessages.add("Field [" + modelEntity.getEntityName() + "." + nonpkFieldName + "] did not match; file value [" + checkField + "], db value [" + currentField + "] pk [" + checkPK + "]");
            }
        }
    } catch (GenericEntityException e) {
        throw e;
    } catch (Throwable t) {
        String errMsg;
        if (checkPK == null) {
            errMsg = "Error checking value [" + checkValue + "]: " + t.toString();
        } else {
            errMsg = "Error checking entity [" + checkPK.getEntityName() + "] with pk [" + checkPK.getAllFields() + "]: " + t.toString();
        }
        errorMessages.add(errMsg);
        Debug.logError(t, errMsg, module);
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericPK(org.apache.ofbiz.entity.GenericPK) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 18 with GenericPK

use of org.apache.ofbiz.entity.GenericPK in project ofbiz-framework by apache.

the class EntityQuery method makeWhereCondition.

private EntityCondition makeWhereCondition(boolean usingCache) {
    if (whereEntityCondition == null && fieldMap != null) {
        if (this.searchPkOnly) {
            // Resolve if the map contains a sub map parameters, use a containsKeys to avoid error when a GenericValue is given as map
            Map<String, Object> parameters = fieldMap.containsKey("parameters") ? (Map<String, Object>) fieldMap.get("parameters") : null;
            GenericPK pk = GenericPK.create(delegator.getModelEntity(entityName));
            pk.setPKFields(parameters);
            pk.setPKFields(fieldMap);
            this.whereEntityCondition = EntityCondition.makeCondition(pk.getPrimaryKey());
        } else {
            this.whereEntityCondition = EntityCondition.makeCondition(fieldMap);
        }
    }
    // we don't use the useCache field here because not all queries will actually use the cache, e.g. findCountByCondition never uses the cache
    if (filterByDate && !usingCache) {
        if (whereEntityCondition != null) {
            return EntityCondition.makeCondition(whereEntityCondition, this.makeDateCondition());
        } else {
            return this.makeDateCondition();
        }
    }
    return whereEntityCondition;
}
Also used : GenericPK(org.apache.ofbiz.entity.GenericPK)

Example 19 with GenericPK

use of org.apache.ofbiz.entity.GenericPK in project ofbiz-framework by apache.

the class ProductEvents method removeFeatureApplsByFeatureTypeId.

public static String removeFeatureApplsByFeatureTypeId(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String productId = request.getParameter("productId");
    String productFeatureTypeId = request.getParameter("productFeatureTypeId");
    try {
        GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
        // get all the variants
        List<GenericValue> variantAssocs = product.getRelated("MainProductAssoc", UtilMisc.toMap("productAssocTypeId", "PRODUCT_VARIANT"), null, false);
        variantAssocs = EntityUtil.filterByDate(variantAssocs);
        List<GenericValue> variants = EntityUtil.getRelated("AssocProduct", null, variantAssocs, false);
        for (GenericValue variant : variants) {
            // get the selectable features for the variant
            List<GenericValue> productFeatureAndAppls = variant.getRelated("ProductFeatureAndAppl", UtilMisc.toMap("productFeatureTypeId", productFeatureTypeId, "productFeatureApplTypeId", "STANDARD_FEATURE"), null, false);
            for (GenericValue productFeatureAndAppl : productFeatureAndAppls) {
                GenericPK productFeatureApplPK = delegator.makePK("ProductFeatureAppl");
                productFeatureApplPK.setPKFields(productFeatureAndAppl);
                delegator.removeByPrimaryKey(productFeatureApplPK);
            }
        }
        List<GenericValue> productFeatureAndAppls = product.getRelated("ProductFeatureAndAppl", UtilMisc.toMap("productFeatureTypeId", productFeatureTypeId, "productFeatureApplTypeId", "SELECTABLE_FEATURE"), null, false);
        for (GenericValue productFeatureAndAppl : productFeatureAndAppls) {
            GenericPK productFeatureApplPK = delegator.makePK("ProductFeatureAppl");
            productFeatureApplPK.setPKFields(productFeatureAndAppl);
            delegator.removeByPrimaryKey(productFeatureApplPK);
        }
    } catch (GenericEntityException e) {
        String errMsg = "Error creating new virtual product from variant products: " + e.toString();
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericPK(org.apache.ofbiz.entity.GenericPK) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 20 with GenericPK

use of org.apache.ofbiz.entity.GenericPK in project ofbiz-framework by apache.

the class ShoppingCartItem method addPromoQuantityCandidateUse.

public synchronized BigDecimal addPromoQuantityCandidateUse(BigDecimal quantityDesired, GenericValue productPromoCondAction, boolean checkAvailableOnly) {
    if (quantityDesired.compareTo(BigDecimal.ZERO) == 0) {
        return BigDecimal.ZERO;
    }
    BigDecimal promoQuantityAvailable = this.getPromoQuantityAvailable();
    BigDecimal promoQuantityToUse = quantityDesired;
    if (promoQuantityAvailable.compareTo(BigDecimal.ZERO) > 0) {
        if (promoQuantityToUse.compareTo(promoQuantityAvailable) > 0) {
            promoQuantityToUse = promoQuantityAvailable;
        }
        if (!checkAvailableOnly) {
            // keep track of candidate promo uses on cartItem
            GenericPK productPromoCondActionPK = productPromoCondAction.getPrimaryKey();
            BigDecimal existingValue = this.quantityUsedPerPromoCandidate.get(productPromoCondActionPK);
            if (existingValue == null) {
                this.quantityUsedPerPromoCandidate.put(productPromoCondActionPK, promoQuantityToUse);
            } else {
                this.quantityUsedPerPromoCandidate.put(productPromoCondActionPK, promoQuantityToUse.add(existingValue));
            }
            this.promoQuantityUsed = this.promoQuantityUsed.add(promoQuantityToUse);
        }
        return promoQuantityToUse;
    }
    return BigDecimal.ZERO;
}
Also used : GenericPK(org.apache.ofbiz.entity.GenericPK) BigDecimal(java.math.BigDecimal)

Aggregations

GenericPK (org.apache.ofbiz.entity.GenericPK)24 GenericValue (org.apache.ofbiz.entity.GenericValue)17 HashMap (java.util.HashMap)11 BigDecimal (java.math.BigDecimal)9 Delegator (org.apache.ofbiz.entity.Delegator)8 Map (java.util.Map)7 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)7 Locale (java.util.Locale)5 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)4 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)4 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)3 Timestamp (java.sql.Timestamp)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 HttpSession (javax.servlet.http.HttpSession)2 GeneralException (org.apache.ofbiz.base.util.GeneralException)2 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)2 EntityConditionList (org.apache.ofbiz.entity.condition.EntityConditionList)2 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1