Search in sources :

Example 46 with EntityListIterator

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

the class ProductSearchEvents method searchRemoveFeature.

/**
 * Removes a feature from search results
 *@param request The HTTPRequest object for the current request
 *@param response The HTTPResponse object for the current request
 *@return String specifying the exit status of this event
 */
public static String searchRemoveFeature(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    Locale locale = UtilHttp.getLocale(request);
    String productFeatureId = request.getParameter("productFeatureId");
    try {
        boolean beganTransaction = TransactionUtil.begin(DEFAULT_TX_TIMEOUT);
        try (EntityListIterator eli = getProductSearchResults(request)) {
            if (eli == null) {
                String errMsg = UtilProperties.getMessage(resource, "productsearchevents.no_results_found_probably_error_constraints", UtilHttp.getLocale(request));
                request.setAttribute("_ERROR_MESSAGE_", errMsg);
                return "error";
            }
            GenericValue searchResultView = null;
            int numRemoved = 0;
            while ((searchResultView = eli.next()) != null) {
                String productId = searchResultView.getString("mainProductId");
                numRemoved += delegator.removeByAnd("ProductFeatureAppl", UtilMisc.toMap("productId", productId, "productFeatureId", productFeatureId));
            }
            Map<String, Object> messageMap = UtilMisc.toMap("numRemoved", Integer.valueOf(numRemoved), "productFeatureId", productFeatureId);
            String eventMsg = UtilProperties.getMessage(resource, "productSearchEvents.removed_param_features", messageMap, locale) + ".";
            request.setAttribute("_EVENT_MESSAGE_", eventMsg);
        } catch (GenericEntityException e) {
            String errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_getting_results", locale) + " : " + e.toString();
            request.setAttribute("_ERROR_MESSAGE_", errorMsg);
            Debug.logError(e, errorMsg, module);
            TransactionUtil.rollback(beganTransaction, errorMsg, e);
            return "error";
        } finally {
            TransactionUtil.commit(beganTransaction);
        }
    } catch (GenericTransactionException e) {
        String errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_getting_results", locale) + " : " + e.toString();
        request.setAttribute("_ERROR_MESSAGE_", errorMsg);
        Debug.logError(e, errorMsg, module);
        return "error";
    }
    return "success";
}
Also used : Locale(java.util.Locale) 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) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator)

Example 47 with EntityListIterator

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

the class ProductSearchEvents method searchRemoveFromCategory.

/**
 * Removes the results of a search from the specified category
 *@param request The HTTPRequest object for the current request
 *@param response The HTTPResponse object for the current request
 *@return String specifying the exit status of this event
 */
public static String searchRemoveFromCategory(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String productCategoryId = request.getParameter("SE_SEARCH_CATEGORY_ID");
    String errMsg = null;
    try {
        boolean beganTransaction = TransactionUtil.begin(DEFAULT_TX_TIMEOUT);
        try (EntityListIterator eli = getProductSearchResults(request)) {
            if (eli == null) {
                errMsg = UtilProperties.getMessage(resource, "productsearchevents.no_results_found_probably_error_constraints", UtilHttp.getLocale(request));
                request.setAttribute("_ERROR_MESSAGE_", errMsg);
                return "error";
            }
            int numRemoved = 0;
            GenericValue searchResultView = null;
            while ((searchResultView = eli.next()) != null) {
                String productId = searchResultView.getString("mainProductId");
                numRemoved += delegator.removeByAnd("ProductCategoryMember", UtilMisc.toMap("productCategoryId", productCategoryId, "productId", productId));
            }
            Map<String, String> messageMap = UtilMisc.toMap("numRemoved", Integer.toString(numRemoved));
            errMsg = UtilProperties.getMessage(resource, "productsearchevents.removed_x_items", messageMap, UtilHttp.getLocale(request));
            request.setAttribute("_EVENT_MESSAGE_", errMsg);
        } catch (GenericEntityException e) {
            Map<String, String> messageMap = UtilMisc.toMap("errSearchResult", e.toString());
            errMsg = UtilProperties.getMessage(resource, "productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
            Debug.logError(e, errMsg, module);
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            TransactionUtil.rollback(beganTransaction, errMsg, e);
            return "error";
        } finally {
            TransactionUtil.commit(beganTransaction);
        }
    } catch (GenericTransactionException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errSearchResult", e.toString());
        errMsg = UtilProperties.getMessage(resource, "productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
        Debug.logError(e, 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) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 48 with EntityListIterator

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

the class ProductSearchEvents method searchExpireFromCategory.

/**
 * Sets the thru date of the results of a search to the specified date for the specified catogory
 *@param request The HTTPRequest object for the current request
 *@param response The HTTPResponse object for the current request
 *@return String specifying the exit status of this event
 */
public static String searchExpireFromCategory(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String productCategoryId = request.getParameter("SE_SEARCH_CATEGORY_ID");
    String thruDateStr = request.getParameter("thruDate");
    String errMsg = null;
    Timestamp thruDate;
    try {
        thruDate = Timestamp.valueOf(thruDateStr);
    } catch (RuntimeException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errDateFormat", e.toString());
        errMsg = UtilProperties.getMessage(resource, "productsearchevents.thruDate_not_formatted_properly", messageMap, UtilHttp.getLocale(request));
        Debug.logError(e, errMsg, module);
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    try {
        boolean beganTransaction = TransactionUtil.begin(DEFAULT_TX_TIMEOUT);
        try (EntityListIterator eli = getProductSearchResults(request)) {
            if (eli == null) {
                errMsg = UtilProperties.getMessage(resource, "productsearchevents.no_results_found_probably_error_constraints", UtilHttp.getLocale(request));
                request.setAttribute("_ERROR_MESSAGE_", errMsg);
                return "error";
            }
            GenericValue searchResultView = null;
            int numExpired = 0;
            while ((searchResultView = eli.next()) != null) {
                String productId = searchResultView.getString("mainProductId");
                // get all tuples that match product and category
                List<GenericValue> pcmList = EntityQuery.use(delegator).from("ProductCategoryMember").where("productCategoryId", productCategoryId, "productId", productId).queryList();
                // set those thrudate to that specificed maybe remove then add new one
                for (GenericValue pcm : pcmList) {
                    if (pcm.get("thruDate") == null) {
                        pcm.set("thruDate", thruDate);
                        pcm.store();
                        numExpired++;
                    }
                }
            }
            Map<String, String> messageMap = UtilMisc.toMap("numExpired", Integer.toString(numExpired));
            errMsg = UtilProperties.getMessage(resource, "productsearchevents.expired_x_items", messageMap, UtilHttp.getLocale(request));
            request.setAttribute("_EVENT_MESSAGE_", errMsg);
        } catch (GenericEntityException e) {
            Map<String, String> messageMap = UtilMisc.toMap("errSearchResult", e.toString());
            errMsg = UtilProperties.getMessage(resource, "productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
            Debug.logError(e, errMsg, module);
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            TransactionUtil.rollback(beganTransaction, errMsg, e);
            return "error";
        } finally {
            TransactionUtil.commit(beganTransaction);
        }
    } catch (GenericTransactionException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errSearchResult", e.toString());
        errMsg = UtilProperties.getMessage(resource, "productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
        Debug.logError(e, errMsg, module);
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) Timestamp(java.sql.Timestamp) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 49 with EntityListIterator

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

the class ProductSearchEvents method searchExportProductList.

/**
 *  Formats the results of a search to the screen as a tab-delimited output
 *@param request The HTTPRequest object for the current request
 *@param response The HTTPResponse object for the current request
 *@return String specifying the exit status of this event
 */
public static String searchExportProductList(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    String errMsg = null;
    List<Map<String, Object>> productExportList = new LinkedList<>();
    try {
        boolean beganTransaction = TransactionUtil.begin(DEFAULT_TX_TIMEOUT);
        try (EntityListIterator eli = getProductSearchResults(request)) {
            if (eli == null) {
                errMsg = UtilProperties.getMessage(resource, "productsearchevents.no_results_found_probably_error_constraints", UtilHttp.getLocale(request));
                request.setAttribute("_ERROR_MESSAGE_", errMsg);
                return "error";
            }
            GenericValue searchResultView = null;
            while ((searchResultView = eli.next()) != null) {
                Map<String, Object> productMap = new HashMap<>();
                String productId = searchResultView.getString("mainProductId");
                productMap.put("productId", productId);
                productMap.put("productFeatureCustom", EntityQuery.use(delegator).from("ProductFeatureAndAppl").where("productId", productId, "productFeatureTypeId", "HAZMAT").filterByDate().queryFirst());
                List<GenericValue> productCategories = EntityQuery.use(delegator).from("ProductCategoryAndMember").where("productId", productId).filterByDate().queryList();
                productMap.put("productCategories", productCategories);
                List<GenericValue> productFeatures = EntityQuery.use(delegator).from("ProductFeatureAndAppl").where("productId", productId).filterByDate().queryList();
                productMap.put("productFeatures", productFeatures);
                productExportList.add(productMap);
            }
        } catch (GenericEntityException e) {
            Map<String, String> messageMap = UtilMisc.toMap("errSearchResult", e.toString());
            errMsg = UtilProperties.getMessage(resource, "productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
            Debug.logError(e, errMsg, module);
            request.setAttribute("_ERROR_MESSAGE_", errMsg);
            TransactionUtil.rollback(beganTransaction, errMsg, e);
            return "error";
        } finally {
            TransactionUtil.commit(beganTransaction);
        }
    } catch (GenericTransactionException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errSearchResult", e.toString());
        errMsg = UtilProperties.getMessage(resource, "productsearchevents.error_getting_search_results", messageMap, UtilHttp.getLocale(request));
        Debug.logError(e, errMsg, module);
        request.setAttribute("_ERROR_MESSAGE_", errMsg);
        return "error";
    }
    request.setAttribute("productExportList", productExportList);
    return "success";
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 50 with EntityListIterator

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

the class ProductSearchEvents method searchAddFeature.

/**
 * Adds a feature to search results
 *@param request The HTTPRequest object for the current request
 *@param response The HTTPResponse object for the current request
 *@return String specifying the exit status of this event
 */
public static String searchAddFeature(HttpServletRequest request, HttpServletResponse response) {
    Delegator delegator = (Delegator) request.getAttribute("delegator");
    Locale locale = UtilHttp.getLocale(request);
    String productFeatureId = request.getParameter("productFeatureId");
    String fromDateStr = request.getParameter("fromDate");
    String thruDateStr = request.getParameter("thruDate");
    String amountStr = request.getParameter("amount");
    String sequenceNumStr = request.getParameter("sequenceNum");
    String productFeatureApplTypeId = request.getParameter("productFeatureApplTypeId");
    Timestamp thruDate = null;
    Timestamp fromDate = null;
    BigDecimal amount = null;
    Long sequenceNum = null;
    try {
        if (UtilValidate.isNotEmpty(fromDateStr)) {
            fromDate = Timestamp.valueOf(fromDateStr);
        }
        if (UtilValidate.isNotEmpty(thruDateStr)) {
            thruDate = Timestamp.valueOf(thruDateStr);
        }
        if (UtilValidate.isNotEmpty(amountStr)) {
            amount = new BigDecimal(amountStr);
        }
        if (UtilValidate.isNotEmpty(sequenceNumStr)) {
            sequenceNum = Long.valueOf(sequenceNumStr);
        }
    } catch (RuntimeException e) {
        String errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_casting_types", locale) + " : " + e.toString();
        request.setAttribute("_ERROR_MESSAGE_", errorMsg);
        Debug.logError(e, errorMsg, module);
        return "error";
    }
    try {
        boolean beganTransaction = TransactionUtil.begin(DEFAULT_TX_TIMEOUT);
        try (EntityListIterator eli = getProductSearchResults(request)) {
            if (eli == null) {
                String errMsg = UtilProperties.getMessage(resource, "productsearchevents.no_results_found_probably_error_constraints", UtilHttp.getLocale(request));
                request.setAttribute("_ERROR_MESSAGE_", errMsg);
                return "error";
            }
            GenericValue searchResultView = null;
            int numAdded = 0;
            while ((searchResultView = eli.next()) != null) {
                String productId = searchResultView.getString("mainProductId");
                GenericValue pfa = delegator.makeValue("ProductFeatureAppl");
                pfa.set("productId", productId);
                pfa.set("productFeatureId", productFeatureId);
                pfa.set("fromDate", fromDate);
                pfa.set("thruDate", thruDate);
                pfa.set("productFeatureApplTypeId", productFeatureApplTypeId);
                pfa.set("amount", amount);
                pfa.set("sequenceNum", sequenceNum);
                pfa.create();
                numAdded++;
            }
            Map<String, Object> messageMap = UtilMisc.toMap("numAdded", Integer.valueOf(numAdded), "productFeatureId", productFeatureId);
            String eventMsg = UtilProperties.getMessage(resource, "productSearchEvents.added_param_features", messageMap, locale) + ".";
            request.setAttribute("_EVENT_MESSAGE_", eventMsg);
        } catch (GenericEntityException e) {
            String errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_getting_results", locale) + " : " + e.toString();
            request.setAttribute("_ERROR_MESSAGE_", errorMsg);
            Debug.logError(e, errorMsg, module);
            TransactionUtil.rollback(beganTransaction, errorMsg, e);
            return "error";
        } finally {
            TransactionUtil.commit(beganTransaction);
        }
    } catch (GenericTransactionException e) {
        String errorMsg = UtilProperties.getMessage(resource, "productSearchEvents.error_getting_results", locale) + " : " + e.toString();
        request.setAttribute("_ERROR_MESSAGE_", errorMsg);
        Debug.logError(e, errorMsg, module);
        return "error";
    }
    return "success";
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator)

Aggregations

EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)65 GenericValue (org.apache.ofbiz.entity.GenericValue)59 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)57 Delegator (org.apache.ofbiz.entity.Delegator)33 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)29 LinkedList (java.util.LinkedList)25 HashMap (java.util.HashMap)24 Timestamp (java.sql.Timestamp)23 Locale (java.util.Locale)21 EntityQuery (org.apache.ofbiz.entity.util.EntityQuery)19 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)18 Map (java.util.Map)16 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)13 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)12 DynamicViewEntity (org.apache.ofbiz.entity.model.DynamicViewEntity)8 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)8 GeneralException (org.apache.ofbiz.base.util.GeneralException)7 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)7 ArrayList (java.util.ArrayList)6 EntityExpr (org.apache.ofbiz.entity.condition.EntityExpr)6