Search in sources :

Example 6 with EntityListIterator

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

the class ProductUtilServices method makeStandAloneFromSingleVariantVirtuals.

public static Map<String, Object> makeStandAloneFromSingleVariantVirtuals(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    Locale locale = (Locale) context.get("locale");
    String errMsg = null;
    Debug.logInfo("Starting makeStandAloneFromSingleVariantVirtuals", module);
    DynamicViewEntity dve = new DynamicViewEntity();
    dve.addMemberEntity("PVIRT", "Product");
    dve.addMemberEntity("PVA", "ProductAssoc");
    dve.addViewLink("PVIRT", "PVA", Boolean.FALSE, UtilMisc.toList(new ModelKeyMap("productId", "productId")));
    dve.addAlias("PVIRT", "productId", null, null, null, Boolean.TRUE, null);
    dve.addAlias("PVIRT", "salesDiscontinuationDate", null, null, null, null, null);
    dve.addAlias("PVA", "productAssocTypeId", null, null, null, null, null);
    dve.addAlias("PVA", "fromDate", null, null, null, null, null);
    dve.addAlias("PVA", "thruDate", null, null, null, null, null);
    dve.addAlias("PVA", "productIdToCount", "productIdTo", null, null, null, "count-distinct");
    EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("productAssocTypeId", EntityOperator.EQUALS, "PRODUCT_VARIANT"), EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN, nowTimestamp))), EntityOperator.AND);
    EntityCondition havingCond = EntityCondition.makeCondition("productIdToCount", EntityOperator.EQUALS, Long.valueOf(1));
    EntityQuery eq = EntityQuery.use(delegator).select("productId", "productIdToCount").from(dve).where(condition).having(havingCond);
    try (EntityListIterator eliOne = eq.queryIterator()) {
        List<GenericValue> valueList = eliOne.getCompleteList();
        Debug.logInfo("Found " + valueList.size() + " virtual products with one variant to turn into a stand alone product.", module);
        int numWithOneOnly = 0;
        for (GenericValue value : valueList) {
            // has only one variant period, is it valid? should already be discontinued if not
            String productId = value.getString("productId");
            List<GenericValue> paList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
            // verify the query; tested on a bunch, looks good
            if (paList.size() != 1) {
                Debug.logInfo("Virtual product with ID " + productId + " should have 1 assoc, has " + paList.size(), module);
            } else {
                // for all virtuals with one variant move all info from virtual to variant and remove virtual, make variant as not a variant
                dispatcher.runSync("mergeVirtualWithSingleVariant", UtilMisc.<String, Object>toMap("productId", productId, "removeOld", Boolean.TRUE, "userLogin", userLogin));
                numWithOneOnly++;
                if (numWithOneOnly % 100 == 0) {
                    Debug.logInfo("Made " + numWithOneOnly + " virtual products with only one valid variant stand-alone products.", module);
                }
            }
        }
        EntityCondition conditionWithDates = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("productAssocTypeId", EntityOperator.EQUALS, "PRODUCT_VARIANT"), EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN, nowTimestamp)), EntityCondition.makeCondition("fromDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp), EntityCondition.makeCondition(EntityCondition.makeCondition("thruDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("thruDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp))), EntityOperator.AND);
        eq = EntityQuery.use(delegator).select("productId", "productIdToCount").from(dve).where(conditionWithDates).having(havingCond);
        try (EntityListIterator eliMulti = eq.queryIterator()) {
            List<GenericValue> valueMultiList = eliMulti.getCompleteList();
            Debug.logInfo("Found " + valueMultiList.size() + " virtual products with one VALID variant to pull the variant from to make a stand alone product.", module);
            int numWithOneValid = 0;
            for (GenericValue value : valueMultiList) {
                // has only one valid variant
                String productId = value.getString("productId");
                List<GenericValue> paList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
                // verify the query; tested on a bunch, looks good
                if (paList.size() != 1) {
                    Debug.logInfo("Virtual product with ID " + productId + " should have 1 assoc, has " + paList.size(), module);
                } else {
                    // for all virtuals with one valid variant move info from virtual to variant, put variant in categories from virtual, remove virtual from all categories but leave "family" otherwise intact, mark variant as not a variant
                    dispatcher.runSync("mergeVirtualWithSingleVariant", UtilMisc.<String, Object>toMap("productId", productId, "removeOld", Boolean.FALSE, "userLogin", userLogin));
                    numWithOneValid++;
                    if (numWithOneValid % 100 == 0) {
                        Debug.logInfo("Made " + numWithOneValid + " virtual products with one valid variant stand-alone products.", module);
                    }
                }
            }
            Debug.logInfo("Found virtual products with one valid variant: " + numWithOneValid + ", with one variant only: " + numWithOneOnly, module);
        } catch (GenericEntityException e) {
            Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
            errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_makeStandAloneFromSingleVariantVirtuals", messageMap, locale);
            Debug.logError(e, errMsg, module);
            return ServiceUtil.returnError(errMsg);
        }
    } catch (GenericEntityException | GenericServiceException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
        errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_makeStandAloneFromSingleVariantVirtuals", messageMap, locale);
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(errMsg);
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) LocalDispatcher(org.apache.ofbiz.service.LocalDispatcher) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) EntityQuery(org.apache.ofbiz.entity.util.EntityQuery) Timestamp(java.sql.Timestamp) DynamicViewEntity(org.apache.ofbiz.entity.model.DynamicViewEntity) ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) HashMap(java.util.HashMap) Map(java.util.Map) ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap)

Example 7 with EntityListIterator

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

the class ProductUtilServices method setAllProductImageNames.

/**
 * reset all product image names with a certain pattern, ex: /images/products/${size}/${productId}.jpg
 * NOTE: only works on fields of Product right now
 */
public static Map<String, Object> setAllProductImageNames(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    String pattern = (String) context.get("pattern");
    Locale locale = (Locale) context.get("locale");
    String errMsg = null;
    if (UtilValidate.isEmpty(pattern)) {
        Map<String, Object> imageContext = new HashMap<>();
        imageContext.putAll(context);
        imageContext.put("tenantId", delegator.getDelegatorTenantId());
        String imageFilenameFormat = EntityUtilProperties.getPropertyValue("catalog", "image.filename.format", delegator);
        String imageUrlPrefix = FlexibleStringExpander.expandString(EntityUtilProperties.getPropertyValue("catalog", "image.url.prefix", delegator), imageContext);
        imageUrlPrefix = imageUrlPrefix.endsWith("/") ? imageUrlPrefix.substring(0, imageUrlPrefix.length() - 1) : imageUrlPrefix;
        pattern = imageUrlPrefix + "/" + imageFilenameFormat;
    }
    try (EntityListIterator eli = EntityQuery.use(delegator).from("Product").queryIterator()) {
        GenericValue product = null;
        int numSoFar = 0;
        while ((product = eli.next()) != null) {
            String productId = (String) product.get("productId");
            Map<String, String> smallMap = UtilMisc.toMap("size", "small", "productId", productId);
            Map<String, String> mediumMap = UtilMisc.toMap("size", "medium", "productId", productId);
            Map<String, String> largeMap = UtilMisc.toMap("size", "large", "productId", productId);
            Map<String, String> detailMap = UtilMisc.toMap("size", "detail", "productId", productId);
            if ("Y".equals(product.getString("isVirtual"))) {
                // find the first variant, use it's ID for the names...
                List<GenericValue> productAssocList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", productId, "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
                if (productAssocList.size() > 0) {
                    GenericValue productAssoc = EntityUtil.getFirst(productAssocList);
                    smallMap.put("productId", productAssoc.getString("productIdTo"));
                    mediumMap.put("productId", productAssoc.getString("productIdTo"));
                    product.set("smallImageUrl", FlexibleStringExpander.expandString(pattern, smallMap));
                    product.set("mediumImageUrl", FlexibleStringExpander.expandString(pattern, mediumMap));
                } else {
                    product.set("smallImageUrl", null);
                    product.set("mediumImageUrl", null);
                }
                product.set("largeImageUrl", null);
                product.set("detailImageUrl", null);
            } else {
                product.set("smallImageUrl", FlexibleStringExpander.expandString(pattern, smallMap));
                product.set("mediumImageUrl", FlexibleStringExpander.expandString(pattern, mediumMap));
                product.set("largeImageUrl", FlexibleStringExpander.expandString(pattern, largeMap));
                product.set("detailImageUrl", FlexibleStringExpander.expandString(pattern, detailMap));
            }
            product.store();
            numSoFar++;
            if (numSoFar % 500 == 0) {
                Debug.logInfo("Image URLs set for " + numSoFar + " products.", module);
            }
        }
        Debug.logInfo("Completed - Image URLs set for " + numSoFar + " products.", module);
    } catch (GenericEntityException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
        errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_setAllProductImageNames", messageMap, locale);
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(errMsg);
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) HashMap(java.util.HashMap) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) HashMap(java.util.HashMap) Map(java.util.Map) ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap)

Example 8 with EntityListIterator

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

the class ProductUtilServices method discVirtualsWithDiscVariants.

/**
 * First expire all ProductAssocs for all disc variants, then disc all virtuals that have all expired variant ProductAssocs
 */
public static Map<String, Object> discVirtualsWithDiscVariants(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    Timestamp nowTimestamp = UtilDateTime.nowTimestamp();
    Locale locale = (Locale) context.get("locale");
    String errMsg = null;
    EntityCondition conditionOne = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("isVariant", EntityOperator.EQUALS, "Y"), EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.NOT_EQUAL, null), EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.LESS_THAN_EQUAL_TO, nowTimestamp)), EntityOperator.AND);
    try (EntityListIterator eliOne = EntityQuery.use(delegator).from("Product").where(conditionOne).queryIterator()) {
        GenericValue productOne = null;
        int numSoFarOne = 0;
        while ((productOne = eliOne.next()) != null) {
            String virtualProductId = ProductWorker.getVariantVirtualId(productOne);
            GenericValue virtualProduct = EntityQuery.use(delegator).from("Product").where("productId", virtualProductId).queryOne();
            if (virtualProduct == null) {
                continue;
            }
            List<GenericValue> passocList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", virtualProductId, "productIdTo", productOne.get("productId"), "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
            if (passocList.size() > 0) {
                for (GenericValue passoc : passocList) {
                    passoc.set("thruDate", nowTimestamp);
                    passoc.store();
                }
                numSoFarOne++;
                if (numSoFarOne % 500 == 0) {
                    Debug.logInfo("Expired variant ProductAssocs for " + numSoFarOne + " sales discontinued variant products.", module);
                }
            }
        }
        // get all non-discontinued virtuals, see if all variant ProductAssocs are expired, if discontinue
        EntityCondition condition = EntityCondition.makeCondition(UtilMisc.toList(EntityCondition.makeCondition("isVirtual", EntityOperator.EQUALS, "Y"), EntityCondition.makeCondition(EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.EQUALS, null), EntityOperator.OR, EntityCondition.makeCondition("salesDiscontinuationDate", EntityOperator.GREATER_THAN_EQUAL_TO, nowTimestamp))), EntityOperator.AND);
        try (EntityListIterator eli = EntityQuery.use(delegator).from("Product").where(condition).queryIterator()) {
            GenericValue product = null;
            int numSoFar = 0;
            while ((product = eli.next()) != null) {
                List<GenericValue> passocList = EntityQuery.use(delegator).from("ProductAssoc").where("productId", product.get("productId"), "productAssocTypeId", "PRODUCT_VARIANT").filterByDate().queryList();
                if (passocList.size() == 0) {
                    product.set("salesDiscontinuationDate", nowTimestamp);
                    delegator.store(product);
                    numSoFar++;
                    if (numSoFar % 500 == 0) {
                        Debug.logInfo("Sales discontinued " + numSoFar + " virtual products that have no valid variants.", module);
                    }
                }
            }
        } catch (GenericEntityException e) {
            Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
            errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_discVirtualsWithDiscVariants", messageMap, locale);
            Debug.logError(e, errMsg, module);
            return ServiceUtil.returnError(errMsg);
        }
    } catch (GenericEntityException e) {
        Map<String, String> messageMap = UtilMisc.toMap("errMessage", e.toString());
        errMsg = UtilProperties.getMessage(resourceError, "productutilservices.entity_error_running_discVirtualsWithDiscVariants", messageMap, locale);
        Debug.logError(e, errMsg, module);
        return ServiceUtil.returnError(errMsg);
    }
    return ServiceUtil.returnSuccess();
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Delegator(org.apache.ofbiz.entity.Delegator) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) Timestamp(java.sql.Timestamp) HashMap(java.util.HashMap) Map(java.util.Map) ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap)

Example 9 with EntityListIterator

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

the class GenericDelegator method findListIteratorByCondition.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#findListIteratorByCondition(org.apache.ofbiz.entity.model.DynamicViewEntity, org.apache.ofbiz.entity.condition.EntityCondition, org.apache.ofbiz.entity.condition.EntityCondition, java.util.Collection, java.util.List, org.apache.ofbiz.entity.util.EntityFindOptions)
     */
@Override
public EntityListIterator findListIteratorByCondition(DynamicViewEntity dynamicViewEntity, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, Collection<String> fieldsToSelect, List<String> orderBy, EntityFindOptions findOptions) throws GenericEntityException {
    // if there is no transaction throw an exception, we don't want to create a transaction here since closing it would mess up the ELI
    if (!TransactionUtil.isTransactionInPlace()) {
        // throw new GenericEntityException("ERROR: Cannot do a find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.");
        // throwing an exception is a little harsh for now, just display a really big error message since we want to get all of these fixed...
        Exception newE = new Exception("Stack Trace");
        Debug.logError(newE, "ERROR: Cannot do a find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module);
    }
    ModelViewEntity modelViewEntity = dynamicViewEntity.makeModelViewEntity(this);
    if (whereEntityCondition != null) {
        whereEntityCondition.checkCondition(modelViewEntity);
    }
    if (havingEntityCondition != null) {
        havingEntityCondition.checkCondition(modelViewEntity);
    }
    GenericHelper helper = getEntityHelper(dynamicViewEntity.getOneRealEntityName());
    EntityListIterator eli = helper.findListIteratorByCondition(this, modelViewEntity, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderBy, findOptions);
    eli.setDelegator(this);
    // TODO: add decrypt fields
    return eli;
}
Also used : ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) GenericHelper(org.apache.ofbiz.entity.datasource.GenericHelper) SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) SAXException(org.xml.sax.SAXException) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 10 with EntityListIterator

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

the class GenericDelegator method find.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#find(java.lang.String, org.apache.ofbiz.entity.condition.EntityCondition, org.apache.ofbiz.entity.condition.EntityCondition, java.util.Set, java.util.List, org.apache.ofbiz.entity.util.EntityFindOptions)
     */
@Override
public EntityListIterator find(String entityName, EntityCondition whereEntityCondition, EntityCondition havingEntityCondition, Set<String> fieldsToSelect, List<String> orderBy, EntityFindOptions findOptions) throws GenericEntityException {
    // if there is no transaction throw an exception, we don't want to create a transaction here since closing it would mess up the ELI
    if (!TransactionUtil.isTransactionInPlace()) {
        // throw new GenericEntityException("ERROR: Cannot do a find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.");
        // throwing an exception is a little harsh for now, just display a really big error message since we want to get all of these fixed...
        Exception newE = new Exception("Stack Trace");
        Debug.logError(newE, "ERROR: Cannot do a find that returns an EntityListIterator with no transaction in place. Wrap this call in a transaction.", module);
    }
    ModelEntity modelEntity = getModelReader().getModelEntity(entityName);
    GenericValue dummyValue = GenericValue.create(modelEntity);
    EntityEcaRuleRunner<?> ecaRunner = this.getEcaRuleRunner(modelEntity.getEntityName());
    ecaRunner.evalRules(EntityEcaHandler.EV_VALIDATE, EntityEcaHandler.OP_FIND, dummyValue, false);
    if (whereEntityCondition != null) {
        whereEntityCondition.checkCondition(modelEntity);
    }
    if (havingEntityCondition != null) {
        havingEntityCondition.checkCondition(modelEntity);
    }
    ecaRunner.evalRules(EntityEcaHandler.EV_RUN, EntityEcaHandler.OP_FIND, dummyValue, false);
    GenericHelper helper = getEntityHelper(modelEntity.getEntityName());
    EntityListIterator eli = helper.findListIteratorByCondition(this, modelEntity, whereEntityCondition, havingEntityCondition, fieldsToSelect, orderBy, findOptions);
    eli.setDelegator(this);
    ecaRunner.evalRules(EntityEcaHandler.EV_RETURN, EntityEcaHandler.OP_FIND, dummyValue, false);
    return eli;
}
Also used : ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) GenericHelper(org.apache.ofbiz.entity.datasource.GenericHelper) SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) SAXException(org.xml.sax.SAXException) GeneralRuntimeException(org.apache.ofbiz.base.util.GeneralRuntimeException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

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