Search in sources :

Example 11 with GeneralRuntimeException

use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.

the class EntityListIterator method getCompleteList.

/**
 * Gets all the elements of the {@link EntityListIterator} as a list.
 *
 * @return a list of the elements.
 * @throws GenericEntityException
 *             if there is a problem with the database.
 */
public List<GenericValue> getCompleteList() throws GenericEntityException {
    try {
        // if the resultSet has been moved forward at all, move back to the beginning
        if (haveMadeValue && !resultSet.isBeforeFirst()) {
            // do a quick check to see if the ResultSet is empty
            resultSet.beforeFirst();
        }
        List<GenericValue> list = new ArrayList<>();
        GenericValue nextValue;
        while ((nextValue = this.next()) != null) {
            list.add(nextValue);
        }
        return list;
    } catch (SQLException e) {
        closeWithWarning("Warning: auto-closed EntityListIterator because of exception: " + e.toString());
        throw new GeneralRuntimeException("Error getting results", e);
    } catch (GeneralRuntimeException e) {
        closeWithWarning("Warning: auto-closed EntityListIterator because of exception: " + e.toString());
        throw new GenericEntityException(e.getNonNestedMessage(), e.getNested());
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) SQLException(java.sql.SQLException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ArrayList(java.util.ArrayList)

Example 12 with GeneralRuntimeException

use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.

the class EntityListIterator method getPartialList.

/**
 * Gets a partial list of results starting at start and containing at most number elements.
 * Start is a one based value, i.e. 1 is the first element.
 *
 * @param start
 *            the index from which on the elements should be retrieved. Is one based.
 * @param number
 *            the maximum number of elements to get after the start index.
 * @return A list with the retrieved elements, with the size of number or less if the result set does not contain enough values.
 *            Empty list in case of no values or an invalid start index.
 * @throws GenericEntityException
 *            if there is an issue with the database.
 */
public List<GenericValue> getPartialList(int start, int number) throws GenericEntityException {
    try {
        if (number == 0)
            return new ArrayList<>(0);
        // just in case the caller missed the 1 based thingy
        if (start == 0)
            start = 1;
        // if can't reposition to desired index, throw exception
        if (!this.absolute(start - 1)) {
            // maybe better to just return an empty list here...
            return new ArrayList<>(0);
        }
        List<GenericValue> list = new ArrayList<>();
        GenericValue nextValue = null;
        // number > 0 comparison goes first to avoid the unwanted call to next
        while (number > 0 && (nextValue = this.next()) != null) {
            list.add(nextValue);
            number--;
        }
        return list;
    } catch (GeneralRuntimeException e) {
        closeWithWarning("Warning: auto-closed EntityListIterator because of exception: " + e.toString());
        throw new GenericEntityException(e.getNonNestedMessage(), e.getNested());
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ArrayList(java.util.ArrayList)

Example 13 with GeneralRuntimeException

use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.

the class ProductContentWrapper method getProductContentAsText.

public static void getProductContentAsText(String productId, GenericValue product, String productContentTypeId, Locale locale, String mimeTypeId, String partyId, String roleTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer outWriter, boolean cache) throws GeneralException, IOException {
    if (productId == null && product != null) {
        productId = product.getString("productId");
    }
    if (delegator == null && product != null) {
        delegator = product.getDelegator();
    }
    if (UtilValidate.isEmpty(mimeTypeId)) {
        mimeTypeId = EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator);
    }
    if (delegator == null) {
        throw new GeneralRuntimeException("Unable to find a delegator to use!");
    }
    List<GenericValue> productContentList = EntityQuery.use(delegator).from("ProductContent").where("productId", productId, "productContentTypeId", productContentTypeId).orderBy("-fromDate").cache(cache).filterByDate().queryList();
    if (UtilValidate.isEmpty(productContentList) && ("Y".equals(product.getString("isVariant")))) {
        GenericValue parent = ProductWorker.getParentProduct(productId, delegator);
        if (parent != null) {
            productContentList = EntityQuery.use(delegator).from("ProductContent").where("productId", parent.get("productId"), "productContentTypeId", productContentTypeId).orderBy("-fromDate").cache(cache).filterByDate().queryList();
        }
    }
    GenericValue productContent = EntityUtil.getFirst(productContentList);
    if (productContent != null) {
        // when rendering the product content, always include the Product and ProductContent records that this comes from
        Map<String, Object> inContext = new HashMap<>();
        inContext.put("product", product);
        inContext.put("productContent", productContent);
        ContentWorker.renderContentAsText(dispatcher, productContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, partyId, roleTypeId, cache);
        return;
    }
    String candidateFieldName = ModelUtil.dbNameToVarName(productContentTypeId);
    ModelEntity productModel = delegator.getModelEntity("Product");
    if (product == null) {
        product = EntityQuery.use(delegator).from("Product").where("productId", productId).cache().queryOne();
    }
    if (UtilValidate.isEmpty(product)) {
        Debug.logWarning("No Product entity found for productId: " + productId, module);
        return;
    }
    if (productModel.isField(candidateFieldName)) {
        String candidateValue = product.getString(candidateFieldName);
        if (UtilValidate.isNotEmpty(candidateValue)) {
            outWriter.write(candidateValue);
            return;
        } else if ("Y".equals(product.getString("isVariant"))) {
            // look up the virtual product
            GenericValue parent = ProductWorker.getParentProduct(productId, delegator);
            if (parent != null) {
                candidateValue = parent.getString(candidateFieldName);
                if (UtilValidate.isNotEmpty(candidateValue)) {
                    outWriter.write(candidateValue);
                    return;
                }
            }
        }
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) HashMap(java.util.HashMap) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 14 with GeneralRuntimeException

use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.

the class WorkEffortContentWrapper method getWorkEffortContentAsText.

public static void getWorkEffortContentAsText(String contentId, String workEffortId, GenericValue workEffort, String workEffortContentTypeId, Locale locale, String mimeTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer outWriter, boolean cache) throws GeneralException, IOException {
    if (workEffortId == null && workEffort != null) {
        workEffortId = workEffort.getString("workEffortId");
    }
    if (delegator == null && workEffort != null) {
        delegator = workEffort.getDelegator();
    }
    if (UtilValidate.isEmpty(mimeTypeId)) {
        mimeTypeId = EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator);
    }
    if (delegator == null) {
        throw new GeneralRuntimeException("Unable to find a delegator to use!");
    }
    // Honor work effort content over WorkEffort entity fields.
    GenericValue workEffortContent;
    if (contentId != null) {
        workEffortContent = EntityQuery.use(delegator).from("WorkEffortContent").where("workEffortId", workEffortId, "contentId", contentId).cache(cache).queryOne();
    } else {
        workEffortContent = getFirstWorkEffortContentByType(workEffortId, workEffort, workEffortContentTypeId, delegator, cache);
    }
    if (workEffortContent != null) {
        // when rendering the product content, always include the Product and ProductContent records that this comes from
        Map<String, Object> inContext = new HashMap<String, Object>();
        inContext.put("workEffort", workEffort);
        inContext.put("workEffortContent", workEffortContent);
        ContentWorker.renderContentAsText(dispatcher, workEffortContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, null, null, false);
        return;
    }
    // check for workeffort field
    String candidateFieldName = ModelUtil.dbNameToVarName(workEffortContentTypeId);
    ModelEntity workEffortModel = delegator.getModelEntity("WorkEffort");
    if (workEffortModel != null && workEffortModel.isField(candidateFieldName)) {
        if (workEffort == null) {
            workEffort = EntityQuery.use(delegator).from("WorkEffort").where("workEffortId", workEffortId).cache().queryOne();
        }
        if (workEffort != null) {
            String candidateValue = workEffort.getString(candidateFieldName);
            if (UtilValidate.isNotEmpty(candidateValue)) {
                outWriter.write(candidateValue);
                return;
            }
        }
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) HashMap(java.util.HashMap) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 15 with GeneralRuntimeException

use of org.apache.ofbiz.base.util.GeneralRuntimeException in project ofbiz-framework by apache.

the class OrderReturnServices method createReturnAdjustment.

public static Map<String, Object> createReturnAdjustment(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    String orderAdjustmentId = (String) context.get("orderAdjustmentId");
    String returnAdjustmentTypeId = (String) context.get("returnAdjustmentTypeId");
    String returnId = (String) context.get("returnId");
    String returnItemSeqId = (String) context.get("returnItemSeqId");
    String description = (String) context.get("description");
    BigDecimal amount = (BigDecimal) context.get("amount");
    Locale locale = (Locale) context.get("locale");
    GenericValue returnItemTypeMap = null;
    GenericValue orderAdjustment = null;
    GenericValue returnAdjustmentType = null;
    GenericValue orderItem = null;
    GenericValue returnItem = null;
    GenericValue returnHeader = null;
    // if orderAdjustment is not empty, then copy most return adjustment information from orderAdjustment's
    if (orderAdjustmentId != null) {
        try {
            orderAdjustment = EntityQuery.use(delegator).from("OrderAdjustment").where("orderAdjustmentId", orderAdjustmentId).queryOne();
            if (orderAdjustment == null) {
                return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderCreateReturnAdjustmentNotFoundOrderAdjustment", UtilMisc.toMap("orderAdjustmentId", orderAdjustmentId), locale));
            }
            // get returnHeaderTypeId from ReturnHeader and then use it to figure out return item type mapping
            returnHeader = EntityQuery.use(delegator).from("ReturnHeader").where("returnId", returnId).queryOne();
            String returnHeaderTypeId = ((returnHeader != null) && (returnHeader.getString("returnHeaderTypeId") != null)) ? returnHeader.getString("returnHeaderTypeId") : "CUSTOMER_RETURN";
            returnItemTypeMap = EntityQuery.use(delegator).from("ReturnItemTypeMap").where("returnHeaderTypeId", returnHeaderTypeId, "returnItemMapKey", orderAdjustment.get("orderAdjustmentTypeId")).queryOne();
            returnAdjustmentType = returnItemTypeMap.getRelatedOne("ReturnAdjustmentType", false);
            if (returnAdjustmentType != null && UtilValidate.isEmpty(description)) {
                description = returnAdjustmentType.getString("description");
            }
            if ((returnItemSeqId != null) && !("_NA_".equals(returnItemSeqId))) {
                returnItem = EntityQuery.use(delegator).from("ReturnItem").where("returnId", returnId, "returnItemSeqId", returnItemSeqId).queryOne();
                Debug.logInfo("returnId:" + returnId + ",returnItemSeqId:" + returnItemSeqId, module);
                orderItem = returnItem.getRelatedOne("OrderItem", false);
            } else {
                // associated to the same order item to which the adjustments refers (if any)
                if (UtilValidate.isNotEmpty(orderAdjustment.getString("orderItemSeqId")) && !"_NA_".equals(orderAdjustment.getString("orderItemSeqId"))) {
                    returnItem = EntityQuery.use(delegator).from("ReturnItem").where("returnId", returnId, "orderId", orderAdjustment.getString("orderId"), "orderItemSeqId", orderAdjustment.getString("orderItemSeqId")).queryFirst();
                    if (returnItem != null) {
                        orderItem = returnItem.getRelatedOne("OrderItem", false);
                    }
                }
            }
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
            throw new GeneralRuntimeException(e.getMessage());
        }
        context.putAll(orderAdjustment.getAllFields());
        if (UtilValidate.isNotEmpty(amount)) {
            context.put("amount", amount);
        }
    }
    // if still empty, use default RET_MAN_ADJ
    if (returnAdjustmentTypeId == null) {
        String mappingTypeId = returnItemTypeMap != null ? returnItemTypeMap.get("returnItemTypeId").toString() : null;
        returnAdjustmentTypeId = mappingTypeId != null ? mappingTypeId : "RET_MAN_ADJ";
    }
    // calculate the returnAdjustment amount
    if (returnItem != null) {
        // returnAdjustment for returnItem
        if (needRecalculate(returnAdjustmentTypeId)) {
            Debug.logInfo("returnPrice:" + returnItem.getBigDecimal("returnPrice") + ",returnQuantity:" + returnItem.getBigDecimal("returnQuantity") + ",sourcePercentage:" + orderAdjustment.getBigDecimal("sourcePercentage"), module);
            BigDecimal returnTotal = returnItem.getBigDecimal("returnPrice").multiply(returnItem.getBigDecimal("returnQuantity"));
            BigDecimal orderTotal = orderItem.getBigDecimal("quantity").multiply(orderItem.getBigDecimal("unitPrice"));
            amount = getAdjustmentAmount("RET_SALES_TAX_ADJ".equals(returnAdjustmentTypeId), returnTotal, orderTotal, orderAdjustment.getBigDecimal("amount"));
        } else {
            amount = (BigDecimal) context.get("amount");
        }
    } else {
        // returnAdjustment for returnHeader
        amount = (BigDecimal) context.get("amount");
    }
    // store the return adjustment
    String seqId = delegator.getNextSeqId("ReturnAdjustment");
    GenericValue newReturnAdjustment = delegator.makeValue("ReturnAdjustment", UtilMisc.toMap("returnAdjustmentId", seqId));
    try {
        newReturnAdjustment.setNonPKFields(context);
        if (orderAdjustment != null && orderAdjustment.get("taxAuthorityRateSeqId") != null) {
            newReturnAdjustment.set("taxAuthorityRateSeqId", orderAdjustment.getString("taxAuthorityRateSeqId"));
        }
        newReturnAdjustment.set("amount", amount == null ? BigDecimal.ZERO : amount);
        newReturnAdjustment.set("returnAdjustmentTypeId", returnAdjustmentTypeId);
        newReturnAdjustment.set("description", description);
        newReturnAdjustment.set("returnItemSeqId", UtilValidate.isEmpty(returnItemSeqId) ? "_NA_" : returnItemSeqId);
        delegator.create(newReturnAdjustment);
        Map<String, Object> result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, "OrderCreateReturnAdjustment", UtilMisc.toMap("seqId", seqId), locale));
        result.put("returnAdjustmentId", seqId);
        return result;
    } catch (GenericEntityException e) {
        Debug.logError(e, "Failed to store returnAdjustment", module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource, "OrderCreateReturnAdjustmentFailed", locale));
    }
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) BigDecimal(java.math.BigDecimal)

Aggregations

GeneralRuntimeException (org.apache.ofbiz.base.util.GeneralRuntimeException)20 GenericValue (org.apache.ofbiz.entity.GenericValue)11 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)6 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)6 HashMap (java.util.HashMap)5 MessageDigest (java.security.MessageDigest)3 ArrayList (java.util.ArrayList)3 Locale (java.util.Locale)3 Delegator (org.apache.ofbiz.entity.Delegator)3 IOException (java.io.IOException)2 BigDecimal (java.math.BigDecimal)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 SQLException (java.sql.SQLException)2 SecretKeyFactory (javax.crypto.SecretKeyFactory)2 PBEKeySpec (javax.crypto.spec.PBEKeySpec)2 GeneralException (org.apache.ofbiz.base.util.GeneralException)2 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)2 TemplateException (freemarker.template.TemplateException)1 Writer (java.io.Writer)1