Search in sources :

Example 1 with ModelEntity

use of org.apache.ofbiz.entity.model.ModelEntity 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)

Example 2 with ModelEntity

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

the class ProductUtilServices method duplicateRelated.

protected static void duplicateRelated(GenericValue product, String title, String relatedEntityName, String productIdField, String variantProductId, Timestamp nowTimestamp, boolean removeOld, Delegator delegator, boolean test) throws GenericEntityException {
    List<GenericValue> relatedList = EntityUtil.filterByDate(product.getRelated(title + relatedEntityName, null, null, false), nowTimestamp);
    for (GenericValue relatedValue : relatedList) {
        GenericValue newRelatedValue = (GenericValue) relatedValue.clone();
        newRelatedValue.set(productIdField, variantProductId);
        // create a new one? see if one already exists with different from/thru dates
        ModelEntity modelEntity = relatedValue.getModelEntity();
        if (modelEntity.isField("fromDate")) {
            GenericPK findValue = newRelatedValue.getPrimaryKey();
            // can't just set to null, need to remove the value so it isn't a constraint in the query
            findValue.remove("fromDate");
            List<GenericValue> existingValueList = EntityQuery.use(delegator).from(relatedEntityName).where(findValue).filterByDate(nowTimestamp).queryList();
            if (existingValueList.size() > 0) {
                if (test) {
                    Debug.logInfo("Found " + existingValueList.size() + " existing values for related entity name: " + relatedEntityName + ", not copying, findValue is: " + findValue, module);
                }
                continue;
            }
            newRelatedValue.set("fromDate", nowTimestamp);
        }
        if (EntityQuery.use(delegator).from(relatedEntityName).where(EntityCondition.makeCondition(newRelatedValue.getPrimaryKey(), EntityOperator.AND)).queryCount() == 0) {
            if (test) {
                Debug.logInfo("Test mode, would create: " + newRelatedValue, module);
            } else {
                newRelatedValue.create();
            }
        }
    }
    if (removeOld) {
        if (test) {
            Debug.logInfo("Test mode, would remove related " + title + relatedEntityName + " with dummy key: " + product.getRelatedDummyPK(title + relatedEntityName), module);
        } else {
            product.removeRelated(title + relatedEntityName);
        }
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericPK(org.apache.ofbiz.entity.GenericPK) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 3 with ModelEntity

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

the class ModelForm method addAutoFieldsFromService.

private void addAutoFieldsFromService(AutoFieldsService autoFieldsService, ModelReader entityModelReader, DispatchContext dispatchContext, Set<String> useWhenFields, List<ModelFormFieldBuilder> fieldBuilderList, Map<String, ModelFormFieldBuilder> fieldBuilderMap) {
    // read service def and auto-create fields
    ModelService modelService = null;
    try {
        modelService = dispatchContext.getModelService(autoFieldsService.serviceName);
    } catch (GenericServiceException e) {
        String errmsg = "Error finding Service with name " + autoFieldsService.serviceName + " for auto-fields-service in a form widget";
        Debug.logError(e, errmsg, module);
        throw new IllegalArgumentException(errmsg);
    }
    for (ModelParam modelParam : modelService.getInModelParamList()) {
        if (modelParam.internal) {
            // skip auto params that the service engine populates...
            continue;
        }
        if (modelParam.formDisplay) {
            if (UtilValidate.isNotEmpty(modelParam.entityName) && UtilValidate.isNotEmpty(modelParam.fieldName)) {
                ModelEntity modelEntity;
                try {
                    modelEntity = entityModelReader.getModelEntity(modelParam.entityName);
                    ModelField modelField = modelEntity.getField(modelParam.fieldName);
                    if (modelField != null) {
                        // okay, populate using the entity field info...
                        ModelFormFieldBuilder builder = new ModelFormFieldBuilder();
                        builder.setModelForm(this);
                        builder.setName(modelField.getName());
                        builder.setEntityName(modelEntity.getEntityName());
                        builder.setFieldName(modelField.getName());
                        builder.induceFieldInfoFromEntityField(modelEntity, modelField, autoFieldsService.defaultFieldType);
                        if (UtilValidate.isNotEmpty(autoFieldsService.mapName)) {
                            builder.setMapName(autoFieldsService.mapName);
                        }
                        builder.setRequiredField(!modelParam.optional);
                        addUpdateField(builder, useWhenFields, fieldBuilderList, fieldBuilderMap);
                        // continue to skip creating based on service param
                        continue;
                    }
                } catch (GenericEntityException e) {
                    Debug.logError(e, module);
                }
            }
            ModelFormFieldBuilder builder = new ModelFormFieldBuilder();
            builder.setModelForm(this);
            builder.setName(modelParam.name);
            builder.setServiceName(modelService.name);
            builder.setAttributeName(modelParam.name);
            builder.setTitle(modelParam.formLabel);
            builder.setRequiredField(!modelParam.optional);
            builder.induceFieldInfoFromServiceParam(modelService, modelParam, autoFieldsService.defaultFieldType);
            builder.setPosition(autoFieldsService.defaultPosition);
            if (UtilValidate.isNotEmpty(autoFieldsService.mapName)) {
                builder.setMapName(autoFieldsService.mapName);
            }
            addUpdateField(builder, useWhenFields, fieldBuilderList, fieldBuilderMap);
        }
    }
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) ModelParam(org.apache.ofbiz.service.ModelParam) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity) ModelService(org.apache.ofbiz.service.ModelService)

Example 4 with ModelEntity

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

the class ModelForm method addAutoFieldsFromEntity.

private void addAutoFieldsFromEntity(AutoFieldsEntity autoFieldsEntity, ModelReader entityModelReader, Set<String> useWhenFields, List<ModelFormFieldBuilder> fieldBuilderList, Map<String, ModelFormFieldBuilder> fieldBuilderMap) {
    // read entity def and auto-create fields
    ModelEntity modelEntity = null;
    try {
        modelEntity = entityModelReader.getModelEntity(autoFieldsEntity.entityName);
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    if (modelEntity == null) {
        throw new IllegalArgumentException("Error finding Entity with name " + autoFieldsEntity.entityName + " for auto-fields-entity in a form widget");
    }
    Iterator<ModelField> modelFieldIter = modelEntity.getFieldsIterator();
    while (modelFieldIter.hasNext()) {
        ModelField modelField = modelFieldIter.next();
        if (modelField.getIsAutoCreatedInternal()) {
            // don't ever auto-add these, should only be added if explicitly referenced
            continue;
        }
        ModelFormFieldBuilder builder = new ModelFormFieldBuilder();
        builder.setModelForm(this);
        builder.setName(modelField.getName());
        builder.setEntityName(modelEntity.getEntityName());
        builder.setFieldName(modelField.getName());
        builder.induceFieldInfoFromEntityField(modelEntity, modelField, autoFieldsEntity.defaultFieldType);
        builder.setPosition(autoFieldsEntity.defaultPosition);
        if (UtilValidate.isNotEmpty(autoFieldsEntity.mapName)) {
            builder.setMapName(autoFieldsEntity.mapName);
        }
        addUpdateField(builder, useWhenFields, fieldBuilderList, fieldBuilderMap);
    }
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 5 with ModelEntity

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

the class GenericDelegator method createSingle.

/* (non-Javadoc)
     * @see org.apache.ofbiz.entity.Delegator#createSingle(java.lang.String, java.lang.Object)
     */
@Override
public GenericValue createSingle(String entityName, Object singlePkValue) throws GenericEntityException {
    if (entityName == null || singlePkValue == null) {
        return null;
    }
    ModelEntity entity = this.getModelReader().getModelEntity(entityName);
    GenericValue genericValue = GenericValue.create(this, entity, singlePkValue);
    return this.create(genericValue);
}
Also used : ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Aggregations

ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)102 GenericValue (org.apache.ofbiz.entity.GenericValue)37 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)29 ModelField (org.apache.ofbiz.entity.model.ModelField)28 HashMap (java.util.HashMap)22 Delegator (org.apache.ofbiz.entity.Delegator)17 ModelViewEntity (org.apache.ofbiz.entity.model.ModelViewEntity)16 LinkedList (java.util.LinkedList)14 Locale (java.util.Locale)12 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)11 ArrayList (java.util.ArrayList)10 ModelRelation (org.apache.ofbiz.entity.model.ModelRelation)10 IOException (java.io.IOException)8 TreeSet (java.util.TreeSet)8 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)8 Map (java.util.Map)7 GeneralRuntimeException (org.apache.ofbiz.base.util.GeneralRuntimeException)7 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)7 ModelFieldType (org.apache.ofbiz.entity.model.ModelFieldType)7 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)7