Search in sources :

Example 41 with GenericValue

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

the class ProductEvents method setDefaultStoreSettings.

/**
 * Simple event to set the users initial locale and currency Uom based on website product store
 */
public static String setDefaultStoreSettings(HttpServletRequest request, HttpServletResponse response) {
    GenericValue productStore = ProductStoreWorker.getProductStore(request);
    if (productStore != null) {
        String currencyStr = null;
        String localeStr = null;
        String timeZoneStr = null;
        HttpSession session = request.getSession();
        GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
        if (userLogin != null) {
            // user login currency
            currencyStr = userLogin.getString("lastCurrencyUom");
            // user login locale
            localeStr = userLogin.getString("lastLocale");
            // user login timezone
            timeZoneStr = userLogin.getString("lastTimeZone");
        }
        // if currency is not set, the store's default currency is used
        if (currencyStr == null && productStore.get("defaultCurrencyUomId") != null) {
            currencyStr = productStore.getString("defaultCurrencyUomId");
        }
        // if locale is not set, the store's default locale is used
        if (localeStr == null && productStore.get("defaultLocaleString") != null) {
            localeStr = productStore.getString("defaultLocaleString");
        }
        // if timezone is not set, the store's default timezone is used
        if (timeZoneStr == null && productStore.get("defaultTimeZoneString") != null) {
            timeZoneStr = productStore.getString("defaultTimeZoneString");
        }
        UtilHttp.setCurrencyUom(session, currencyStr);
        UtilHttp.setLocale(request, localeStr);
        UtilHttp.setTimeZone(request, timeZoneStr);
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HttpSession(javax.servlet.http.HttpSession)

Example 42 with GenericValue

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

the class ProductEvents method updateProductQuickAdminSelFeat.

public static String updateProductQuickAdminSelFeat(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    String productId = request.getParameter("productId");
    String variantProductId = request.getParameter("productId0");
    String useImagesProdId = request.getParameter("useImages");
    String productFeatureTypeId = request.getParameter("productFeatureTypeId");
    if (UtilValidate.isEmpty(productFeatureTypeId)) {
        String errMsg = "Error: please select a ProductFeature Type to add or update variant features.";
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    try {
        boolean beganTransaction = TransactionUtil.begin();
        try {
            GenericValue productFeatureType = EntityQuery.use(delegator).from("ProductFeatureType").where("productFeatureTypeId", productFeatureTypeId).queryOne();
            if (productFeatureType == null) {
                String errMsg = "Error: the ProductFeature Type specified was not valid and one is require to add or update variant features.";
                request.setAttribute("_ERROR_MESSAGE_", errMsg);
                return "error";
            }
            // check for variantProductId - this will mean that we have multiple variants to update
            if (variantProductId != null) {
                // multiple products, so use a numeric suffix to get them all
                int attribIdx = 0;
                GenericValue product = EntityQuery.use(delegator).from("Product").where("productId", productId).queryOne();
                do {
                    GenericValue variantProduct = EntityQuery.use(delegator).from("Product").where("productId", variantProductId).queryOne();
                    String description = request.getParameter("description" + attribIdx);
                    // blank means null, which means delete the feature application
                    if ((description != null) && (description.trim().length() < 1)) {
                        description = null;
                    }
                    Set<String> variantDescRemoveToRemoveOnVirtual = new HashSet<>();
                    checkUpdateFeatureApplByDescription(variantProductId, variantProduct, description, productFeatureTypeId, productFeatureType, "STANDARD_FEATURE", nowTimestamp, delegator, null, variantDescRemoveToRemoveOnVirtual);
                    checkUpdateFeatureApplByDescription(productId, product, description, productFeatureTypeId, productFeatureType, "SELECTABLE_FEATURE", nowTimestamp, delegator, variantDescRemoveToRemoveOnVirtual, null);
                    // update image urls
                    if ((useImagesProdId != null) && (useImagesProdId.equals(variantProductId))) {
                        product.set("smallImageUrl", variantProduct.getString("smallImageUrl"));
                        product.set("mediumImageUrl", variantProduct.getString("mediumImageUrl"));
                        product.set("largeImageUrl", null);
                        product.set("detailImageUrl", null);
                        product.store();
                    }
                    attribIdx++;
                    variantProductId = request.getParameter("productId" + attribIdx);
                } while (variantProductId != null);
            }
            TransactionUtil.commit(beganTransaction);
        } catch (GenericEntityException e) {
            String errMsg = "Error updating quick admin selectable feature settings: " + e.toString();
            Debug.logError(e, errMsg, module);
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            TransactionUtil.rollback(beganTransaction, errMsg, e);
            return "error";
        }
    } catch (GenericTransactionException gte) {
        String errMsg = "Error updating quick admin selectable feature settings: " + gte.toString();
        Debug.logError(gte, errMsg, module);
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) Timestamp(java.sql.Timestamp) HashSet(java.util.HashSet)

Example 43 with GenericValue

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

the class ProductEvents method checkStoreCustomerRole.

/**
 * If ProductStore.requireCustomerRole == Y then the loggedin user must be associated with the store in the customer role.
 * This event method is called from the ProductEvents.storeCheckLogin and ProductEvents.storeLogin
 *
 * @param request
 * @param response
 * @return String with response, maybe "success" or "error" if logged in user is not associated with the ProductStore in the CUSTOMER role.
 */
public static String checkStoreCustomerRole(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");
    GenericValue productStore = ProductStoreWorker.getProductStore(request);
    if (productStore != null && userLogin != null) {
        if ("Y".equals(productStore.getString("requireCustomerRole"))) {
            List<GenericValue> productStoreRoleList = null;
            try {
                productStoreRoleList = EntityQuery.use(delegator).from("ProductStoreRole").where("productStoreId", productStore.get("productStoreId"), "partyId", userLogin.get("partyId"), "roleTypeId", "CUSTOMER").filterByDate().queryList();
            } catch (GenericEntityException e) {
                Debug.logError(e, "Database error finding CUSTOMER ProductStoreRole records, required by the ProductStore with ID [" + productStore.getString("productStoreId") + "]", module);
            }
            if (UtilValidate.isEmpty(productStoreRoleList)) {
                // uh-oh, this user isn't associated...
                String errorMsg = "The " + productStore.getString("storeName") + " [" + productStore.getString("productStoreId") + "] ProductStore requires that customers be associated with it, and the logged in user is NOT associated with it in the CUSTOMER role; userLoginId=[" + userLogin.getString("userLoginId") + "], partyId=[" + userLogin.getString("partyId") + "]";
                Debug.logWarning(errorMsg, module);
                request.setAttribute("_ERROR_MESSAGE_", errorMsg);
                session.removeAttribute("userLogin");
                session.removeAttribute("autoUserLogin");
                return "error";
            }
        }
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) HttpSession(javax.servlet.http.HttpSession) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 44 with GenericValue

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

the class ProductEvents method getProductCompareList.

public static List<GenericValue> getProductCompareList(HttpServletRequest request) {
    HttpSession session = request.getSession();
    Object compareListObj = session.getAttribute("productCompareList");
    List<GenericValue> compareList = null;
    if (compareListObj == null) {
        compareList = new LinkedList<>();
    } else if (!(compareListObj instanceof List<?>)) {
        Debug.logWarning("Session attribute productCompareList contains something other than the expected product list, overwriting.", module);
        compareList = new LinkedList<>();
    } else {
        compareList = UtilGenerics.cast(compareListObj);
    }
    return compareList;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HttpSession(javax.servlet.http.HttpSession) LinkedList(java.util.LinkedList)

Example 45 with GenericValue

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

the class ProductPromoContentWrapper method getProductPromoContentAsText.

public static void getProductPromoContentAsText(String productPromoId, GenericValue productPromo, String productPromoContentTypeId, Locale locale, String mimeTypeId, String partyId, String roleTypeId, Delegator delegator, LocalDispatcher dispatcher, Writer outWriter, boolean cache) throws GeneralException, IOException {
    if (UtilValidate.isEmpty(productPromoId) && productPromo != null) {
        productPromoId = productPromo.getString("productPromoId");
    }
    if (UtilValidate.isEmpty(delegator) && productPromo != null) {
        delegator = productPromo.getDelegator();
    }
    if (UtilValidate.isEmpty(mimeTypeId)) {
        mimeTypeId = EntityUtilProperties.getPropertyValue("content", "defaultMimeType", "text/html; charset=utf-8", delegator);
    }
    if (UtilValidate.isEmpty(delegator)) {
        throw new GeneralRuntimeException("Unable to find a delegator to use!");
    }
    List<EntityExpr> exprs = new ArrayList<>();
    exprs.add(EntityCondition.makeCondition("productPromoId", EntityOperator.EQUALS, productPromoId));
    exprs.add(EntityCondition.makeCondition("productPromoContentTypeId", EntityOperator.EQUALS, productPromoContentTypeId));
    List<GenericValue> productPromoContentList = EntityQuery.use(delegator).from("ProductPromoContent").where(EntityCondition.makeCondition(exprs, EntityOperator.AND)).orderBy("-fromDate").cache(cache).queryList();
    GenericValue productPromoContent = null;
    if (UtilValidate.isNotEmpty(productPromoContentList)) {
        productPromoContent = EntityUtil.getFirst(EntityUtil.filterByDate(productPromoContentList));
    }
    if (productPromoContent != null) {
        // when rendering the product promo content, always include the ProductPromo and ProductPromoContent records that this comes from
        Map<String, Object> inContext = new HashMap<>();
        inContext.put("productPromo", productPromo);
        inContext.put("productPromoContent", productPromoContent);
        ContentWorker.renderContentAsText(dispatcher, productPromoContent.getString("contentId"), outWriter, inContext, locale, mimeTypeId, partyId, roleTypeId, cache);
        return;
    }
    String candidateFieldName = ModelUtil.dbNameToVarName(productPromoContentTypeId);
    ModelEntity productModel = delegator.getModelEntity("ProductPromo");
    if (productModel.isField(candidateFieldName)) {
        if (UtilValidate.isEmpty(productPromo)) {
            productPromo = EntityQuery.use(delegator).from("ProductPromo").where("productPromoId", productPromoId).cache().queryOne();
        }
        if (productPromo != null) {
            String candidateValue = productPromo.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) ArrayList(java.util.ArrayList) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) EntityExpr(org.apache.ofbiz.entity.condition.EntityExpr)

Aggregations

GenericValue (org.apache.ofbiz.entity.GenericValue)1422 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)871 Delegator (org.apache.ofbiz.entity.Delegator)721 Locale (java.util.Locale)505 HashMap (java.util.HashMap)463 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)370 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)356 BigDecimal (java.math.BigDecimal)338 LinkedList (java.util.LinkedList)312 Timestamp (java.sql.Timestamp)202 GeneralException (org.apache.ofbiz.base.util.GeneralException)168 Map (java.util.Map)155 IOException (java.io.IOException)116 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)97 HttpSession (javax.servlet.http.HttpSession)89 ArrayList (java.util.ArrayList)69 Security (org.apache.ofbiz.security.Security)69 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)59 List (java.util.List)56 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)52