Search in sources :

Example 21 with GenericPK

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

the class ShoppingCartItem method getPromoQuantityCandidateUseActionAndAllConds.

public BigDecimal getPromoQuantityCandidateUseActionAndAllConds(GenericValue productPromoAction) {
    BigDecimal totalUse = BigDecimal.ZERO;
    String productPromoId = productPromoAction.getString("productPromoId");
    String productPromoRuleId = productPromoAction.getString("productPromoRuleId");
    GenericPK productPromoActionPK = productPromoAction.getPrimaryKey();
    BigDecimal existingValue = this.quantityUsedPerPromoCandidate.get(productPromoActionPK);
    if (existingValue != null) {
        totalUse = existingValue;
    }
    for (Map.Entry<GenericPK, BigDecimal> entry : this.quantityUsedPerPromoCandidate.entrySet()) {
        GenericPK productPromoCondActionPK = entry.getKey();
        BigDecimal quantityUsed = entry.getValue();
        if (quantityUsed != null) {
            // must be in the same rule and be a condition
            if (productPromoId.equals(productPromoCondActionPK.getString("productPromoId")) && productPromoRuleId.equals(productPromoCondActionPK.getString("productPromoRuleId")) && productPromoCondActionPK.containsKey("productPromoCondSeqId")) {
                totalUse = totalUse.add(quantityUsed);
            }
        }
    }
    return totalUse;
}
Also used : GenericPK(org.apache.ofbiz.entity.GenericPK) HashMap(java.util.HashMap) Map(java.util.Map) BigDecimal(java.math.BigDecimal)

Example 22 with GenericPK

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

the class ShoppingCartItem method resetPromoRuleUse.

public synchronized void resetPromoRuleUse(String productPromoId, String productPromoRuleId) {
    Iterator<Map.Entry<GenericPK, BigDecimal>> entryIter = this.quantityUsedPerPromoCandidate.entrySet().iterator();
    while (entryIter.hasNext()) {
        Map.Entry<GenericPK, BigDecimal> entry = entryIter.next();
        GenericPK productPromoCondActionPK = entry.getKey();
        BigDecimal quantityUsed = entry.getValue();
        if (productPromoId.equals(productPromoCondActionPK.getString("productPromoId")) && productPromoRuleId.equals(productPromoCondActionPK.getString("productPromoRuleId"))) {
            entryIter.remove();
            BigDecimal existingValue = this.quantityUsedPerPromoFailed.get(productPromoCondActionPK);
            if (existingValue == null) {
                this.quantityUsedPerPromoFailed.put(productPromoCondActionPK, quantityUsed);
            } else {
                this.quantityUsedPerPromoFailed.put(productPromoCondActionPK, quantityUsed.add(existingValue));
            }
            this.promoQuantityUsed = this.promoQuantityUsed.subtract(quantityUsed);
        }
    }
}
Also used : GenericPK(org.apache.ofbiz.entity.GenericPK) HashMap(java.util.HashMap) Map(java.util.Map) BigDecimal(java.math.BigDecimal)

Example 23 with GenericPK

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

the class ScriptHelperImpl method runFindByPrimaryKey.

private static GenericValue runFindByPrimaryKey(ModelEntity modelEntity, ContextHelper ctxHelper, boolean useCache, boolean autoFieldMap, Map<String, ? extends Object> fieldMap, List<String> selectFieldList) throws ScriptException {
    Map<String, Object> entityContext = new HashMap<String, Object>();
    Delegator delegator = ctxHelper.getDelegator();
    Map<String, Object> context = ctxHelper.getBindings();
    if (autoFieldMap) {
        GenericValue tempVal = delegator.makeValue(modelEntity.getEntityName());
        Object parametersObj = context.get("parameters");
        if (parametersObj != null && parametersObj instanceof Map<?, ?>) {
            tempVal.setAllFields(UtilGenerics.checkMap(parametersObj), true, null, Boolean.TRUE);
        }
        tempVal.setAllFields(context, true, null, Boolean.TRUE);
        entityContext.putAll(tempVal);
    }
    if (fieldMap != null) {
        entityContext.putAll(fieldMap);
    }
    entityContext.put("locale", context.get("locale"));
    entityContext.put("timeZone", context.get("timeZone"));
    modelEntity.convertFieldMapInPlace(entityContext, delegator);
    entityContext.remove("locale");
    entityContext.remove("timeZone");
    Set<String> fieldsToSelect = null;
    if (selectFieldList != null) {
        fieldsToSelect = new HashSet<String>(selectFieldList);
    }
    if (fieldsToSelect != null && useCache) {
        String errMsg = "Error running script " + ctxHelper.getScriptName() + ": Problem invoking the findOne method: Cannot specify selectFieldList argument when useCache is set to true ";
        Debug.logWarning(errMsg, module);
        throw new ScriptException(errMsg);
    }
    GenericValue valueOut = null;
    GenericPK entityPK = delegator.makePK(modelEntity.getEntityName(), entityContext);
    if (entityPK.containsPrimaryKey(true)) {
        try {
            if (useCache) {
                valueOut = EntityQuery.use(delegator).from(entityPK.getEntityName()).where(entityPK).cache(true).queryOne();
            } else {
                if (fieldsToSelect != null) {
                    valueOut = delegator.findByPrimaryKeyPartial(entityPK, fieldsToSelect);
                } else {
                    valueOut = EntityQuery.use(delegator).from(entityPK.getEntityName()).where(entityPK).queryOne();
                }
            }
        } catch (GenericEntityException e) {
            String errMsg = "Error running script " + ctxHelper.getScriptName() + ": Problem invoking the findOne method: " + e.getMessage();
            Debug.logWarning(e, errMsg, module);
            throw new ScriptException(errMsg);
        }
    } else {
        if (Debug.warningOn()) {
            Debug.logWarning("Error running script " + ctxHelper.getScriptName() + ": Returning null because found incomplete primary key in find: " + entityPK, module);
        }
    }
    return valueOut;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ScriptException(javax.script.ScriptException) GenericPK(org.apache.ofbiz.entity.GenericPK) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 24 with GenericPK

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

the class EntityCache method remove.

public GenericValue remove(GenericPK pk) {
    UtilCache<GenericPK, GenericValue> entityCache = getCache(pk.getEntityName());
    if (Debug.verboseOn()) {
        Debug.logVerbose("Removing from EntityCache with PK [" + pk + "], will remove from this cache: " + (entityCache == null ? "[No cache found to remove from]" : entityCache.getName()), module);
    }
    if (entityCache == null) {
        return null;
    }
    GenericValue retVal = entityCache.remove(pk);
    ModelEntity model = pk.getModelEntity();
    if (model != null) {
        Iterator<String> it = model.getViewConvertorsIterator();
        while (it.hasNext()) {
            String targetEntityName = it.next();
            UtilCache.clearCache(getCacheName(targetEntityName));
        }
    }
    if (Debug.verboseOn()) {
        Debug.logVerbose("Removing from EntityCache with PK [" + pk + "], found this in the cache: " + retVal, module);
    }
    return retVal;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericPK(org.apache.ofbiz.entity.GenericPK) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

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