Search in sources :

Example 1 with ConfigItem

use of org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigItem in project ofbiz-framework by apache.

the class ProductConfigWorker method storeProductConfigWrapper.

/**
 * First search persisted configurations and update configWrapper.configId if found.
 * Otherwise store ProductConfigWrapper to ProductConfigConfig entity and updates configWrapper.configId with new configId
 * This method persists only the selected options, price data is lost.
 * @param configWrapper the ProductConfigWrapper object
 * @param delegator the delegator
 */
public static void storeProductConfigWrapper(ProductConfigWrapper configWrapper, Delegator delegator) {
    if (configWrapper == null || (!configWrapper.isCompleted())) {
        return;
    }
    String configId = null;
    List<ConfigItem> questions = configWrapper.getQuestions();
    List<GenericValue> configsToCheck = new LinkedList<>();
    int selectedOptionSize = 0;
    for (ConfigItem ci : questions) {
        String configItemId = null;
        Long sequenceNum = null;
        List<ProductConfigWrapper.ConfigOption> selectedOptions = new LinkedList<>();
        List<ConfigOption> options = ci.getOptions();
        if (ci.isStandard()) {
            selectedOptions.addAll(options);
        } else {
            for (ConfigOption oneOption : options) {
                if (oneOption.isSelected()) {
                    selectedOptions.add(oneOption);
                }
            }
        }
        if (selectedOptions.size() > 0) {
            selectedOptionSize += selectedOptions.size();
            configItemId = ci.getConfigItemAssoc().getString("configItemId");
            sequenceNum = ci.getConfigItemAssoc().getLong("sequenceNum");
            try {
                List<GenericValue> configs = EntityQuery.use(delegator).from("ProductConfigConfig").where("configItemId", configItemId, "sequenceNum", sequenceNum).queryList();
                for (GenericValue productConfigConfig : configs) {
                    for (ConfigOption oneOption : selectedOptions) {
                        String configOptionId = oneOption.configOption.getString("configOptionId");
                        if (productConfigConfig.getString("configOptionId").equals(configOptionId)) {
                            String comments = oneOption.getComments() != null ? oneOption.getComments() : "";
                            if ((UtilValidate.isEmpty(comments) && UtilValidate.isEmpty(productConfigConfig.getString("description"))) || comments.equals(productConfigConfig.getString("description"))) {
                                configsToCheck.add(productConfigConfig);
                            }
                        }
                    }
                }
            } catch (GenericEntityException e) {
                Debug.logError(e, module);
            }
        }
    }
    if (UtilValidate.isNotEmpty(configsToCheck)) {
        for (GenericValue productConfigConfig : configsToCheck) {
            String tempConfigId = productConfigConfig.getString("configId");
            try {
                List<GenericValue> tempResult = EntityQuery.use(delegator).from("ProductConfigConfig").where("configId", tempConfigId).queryList();
                if (tempResult.size() == selectedOptionSize && configsToCheck.containsAll(tempResult)) {
                    List<GenericValue> configOptionProductOptions = EntityQuery.use(delegator).from("ConfigOptionProductOption").where("configId", tempConfigId).queryList();
                    if (UtilValidate.isNotEmpty(configOptionProductOptions)) {
                        // check for variant product equality
                        for (ConfigItem ci : questions) {
                            String configItemId = null;
                            Long sequenceNum = null;
                            List<ProductConfigWrapper.ConfigOption> selectedOptions = new LinkedList<>();
                            List<ConfigOption> options = ci.getOptions();
                            if (ci.isStandard()) {
                                selectedOptions.addAll(options);
                            } else {
                                for (ConfigOption oneOption : options) {
                                    if (oneOption.isSelected()) {
                                        selectedOptions.add(oneOption);
                                    }
                                }
                            }
                            boolean match = true;
                            for (ProductConfigWrapper.ConfigOption anOption : selectedOptions) {
                                if (match && anOption.hasVirtualComponent()) {
                                    List<GenericValue> components = anOption.getComponents();
                                    for (GenericValue aComponent : components) {
                                        if (anOption.isVirtualComponent(aComponent)) {
                                            Map<String, String> componentOptions = anOption.getComponentOptions();
                                            String optionProductId = aComponent.getString("productId");
                                            String optionProductOptionId = null;
                                            if (UtilValidate.isNotEmpty(componentOptions)) {
                                                optionProductOptionId = componentOptions.get(optionProductId);
                                            }
                                            String configOptionId = anOption.configOption.getString("configOptionId");
                                            configItemId = ci.getConfigItemAssoc().getString("configItemId");
                                            sequenceNum = ci.getConfigItemAssoc().getLong("sequenceNum");
                                            GenericValue configOptionProductOption = delegator.makeValue("ConfigOptionProductOption");
                                            configOptionProductOption.set("configId", tempConfigId);
                                            configOptionProductOption.set("configItemId", configItemId);
                                            configOptionProductOption.set("sequenceNum", sequenceNum);
                                            configOptionProductOption.set("configOptionId", configOptionId);
                                            configOptionProductOption.set("productId", optionProductId);
                                            configOptionProductOption.set("productOptionId", optionProductOptionId);
                                            if (!configOptionProductOptions.remove(configOptionProductOption)) {
                                                match = false;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                            if (match && (UtilValidate.isEmpty(configOptionProductOptions))) {
                                configWrapper.configId = tempConfigId;
                                Debug.logInfo("Existing configuration found with configId:" + tempConfigId, module);
                                return;
                            }
                        }
                    } else {
                        configWrapper.configId = tempConfigId;
                        Debug.logInfo("Existing configuration found with configId:" + tempConfigId, module);
                        return;
                    }
                }
            } catch (GenericEntityException e) {
                Debug.logError(e, module);
            }
        }
    }
    // Current configuration is not found in ProductConfigConfig entity. So lets store this one
    boolean nextId = true;
    for (ConfigItem ci : questions) {
        String configItemId = null;
        Long sequenceNum = null;
        List<ProductConfigWrapper.ConfigOption> selectedOptions = new LinkedList<>();
        List<ConfigOption> options = ci.getOptions();
        if (ci.isStandard()) {
            selectedOptions.addAll(options);
        } else {
            for (ConfigOption oneOption : options) {
                if (oneOption.isSelected()) {
                    selectedOptions.add(oneOption);
                }
            }
        }
        if (selectedOptions.size() > 0) {
            if (nextId) {
                configId = delegator.getNextSeqId("ProductConfigConfig");
                // get next configId only once and only if there are selectedOptions
                nextId = false;
            }
            configItemId = ci.getConfigItemAssoc().getString("configItemId");
            sequenceNum = ci.getConfigItemAssoc().getLong("sequenceNum");
            for (ConfigOption oneOption : selectedOptions) {
                Map<String, String> componentOptions = oneOption.componentOptions;
                List<GenericValue> toBeStored = new LinkedList<>();
                String configOptionId = oneOption.configOption.getString("configOptionId");
                String description = oneOption.getComments();
                GenericValue productConfigConfig = delegator.makeValue("ProductConfigConfig");
                productConfigConfig.put("configId", configId);
                productConfigConfig.put("configItemId", configItemId);
                productConfigConfig.put("sequenceNum", sequenceNum);
                productConfigConfig.put("configOptionId", configOptionId);
                productConfigConfig.put("description", description);
                toBeStored.add(productConfigConfig);
                if (oneOption.hasVirtualComponent()) {
                    List<GenericValue> components = oneOption.getComponents();
                    for (GenericValue component : components) {
                        if (oneOption.isVirtualComponent(component) && UtilValidate.isNotEmpty(componentOptions)) {
                            String componentOption = componentOptions.get(component.getString("productId"));
                            GenericValue configOptionProductOption = delegator.makeValue("ConfigOptionProductOption");
                            configOptionProductOption.put("configId", configId);
                            configOptionProductOption.put("configItemId", configItemId);
                            configOptionProductOption.put("sequenceNum", sequenceNum);
                            configOptionProductOption.put("configOptionId", configOptionId);
                            configOptionProductOption.put("productId", component.getString("productId"));
                            configOptionProductOption.put("productOptionId", componentOption);
                            toBeStored.add(configOptionProductOption);
                        }
                    }
                }
                try {
                    delegator.storeAll(toBeStored);
                } catch (GenericEntityException e) {
                    configId = null;
                    Debug.logWarning(e.getMessage(), module);
                }
            }
        }
    }
    // save  configId to configWrapper, so we can use it in shopping cart operations
    configWrapper.configId = configId;
    Debug.logInfo("New configId created:" + configId, module);
    return;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) ConfigItem(org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigItem) LinkedList(java.util.LinkedList) ConfigOption(org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption) ConfigOption(org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Aggregations

LinkedList (java.util.LinkedList)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1 ConfigItem (org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigItem)1 ConfigOption (org.apache.ofbiz.product.config.ProductConfigWrapper.ConfigOption)1