Search in sources :

Example 1 with ModelField

use of org.apache.ofbiz.entity.model.ModelField 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 2 with ModelField

use of org.apache.ofbiz.entity.model.ModelField 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 3 with ModelField

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

the class GenericDAO method updateByCondition.

public int updateByCondition(ModelEntity modelEntity, Map<String, ? extends Object> fieldsToSet, EntityCondition condition, SQLProcessor sqlP) throws GenericEntityException {
    if (modelEntity == null || fieldsToSet == null || condition == null)
        return 0;
    if (modelEntity instanceof ModelViewEntity) {
        throw new org.apache.ofbiz.entity.GenericNotImplementedException("Operation updateByCondition not supported yet for view entities");
    }
    StringBuilder sql = new StringBuilder("UPDATE ").append(modelEntity.getTableName(datasource));
    sql.append(" SET ");
    List<EntityConditionParam> params = new LinkedList<EntityConditionParam>();
    for (Map.Entry<String, ? extends Object> entry : fieldsToSet.entrySet()) {
        String name = entry.getKey();
        ModelField field = modelEntity.getField(name);
        if (field != null) {
            if (!params.isEmpty()) {
                sql.append(", ");
            }
            sql.append(field.getColName()).append(" = ?");
            params.add(new EntityConditionParam(field, entry.getValue()));
        }
    }
    sql.append(" WHERE ").append(condition.makeWhereString(modelEntity, params, this.datasource));
    sqlP.prepareStatement(sql.toString());
    for (EntityConditionParam param : params) {
        SqlJdbcUtil.setValue(sqlP, param.getModelField(), modelEntity.getEntityName(), param.getFieldValue(), modelFieldTypeReader);
    }
    return sqlP.executeUpdate();
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) GenericNotImplementedException(org.apache.ofbiz.entity.GenericNotImplementedException) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ModelKeyMap(org.apache.ofbiz.entity.model.ModelKeyMap) LinkedList(java.util.LinkedList) EntityConditionParam(org.apache.ofbiz.entity.condition.EntityConditionParam)

Example 4 with ModelField

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

the class GenericDAO method select.

public void select(GenericEntity entity, SQLProcessor sqlP) throws GenericEntityException {
    ModelEntity modelEntity = entity.getModelEntity();
    if (modelEntity.getPksSize() <= 0) {
        throw new GenericEntityException("Entity has no primary keys, cannot select by primary key");
    }
    StringBuilder sqlBuffer = new StringBuilder("SELECT ");
    if (modelEntity.getNopksSize() > 0) {
        modelEntity.colNameString(modelEntity.getNopksCopy(), sqlBuffer, "", ", ", "", datasource.getAliasViewColumns());
    } else {
        sqlBuffer.append("*");
    }
    sqlBuffer.append(SqlJdbcUtil.makeFromClause(modelEntity, modelFieldTypeReader, datasource));
    sqlBuffer.append(SqlJdbcUtil.makeWhereClause(modelEntity, modelEntity.getPkFieldsUnmodifiable(), entity, "AND", datasource.getJoinStyle()));
    sqlP.prepareStatement(sqlBuffer.toString(), true, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader);
    sqlP.executeQuery();
    if (sqlP.next()) {
        int idx = 1;
        Iterator<ModelField> nopkIter = modelEntity.getNopksIterator();
        while (nopkIter.hasNext()) {
            ModelField curField = nopkIter.next();
            SqlJdbcUtil.getValue(sqlP.getResultSet(), idx, curField, entity, modelFieldTypeReader);
            idx++;
        }
        entity.synchronizedWithDatasource();
    } else {
        // Debug.logWarning("[GenericDAO.select]: select failed, result set was empty for entity: " + entity.toString(), module);
        throw new GenericEntityNotFoundException("Result set was empty for entity: " + entity.toString());
    }
}
Also used : ModelField(org.apache.ofbiz.entity.model.ModelField) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericEntityNotFoundException(org.apache.ofbiz.entity.GenericEntityNotFoundException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Example 5 with ModelField

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

the class GenericDAO method partialSelect.

public void partialSelect(GenericEntity entity, Set<String> keys) throws GenericEntityException {
    ModelEntity modelEntity = entity.getModelEntity();
    if (modelEntity instanceof ModelViewEntity) {
        throw new org.apache.ofbiz.entity.GenericNotImplementedException("Operation partialSelect not supported yet for view entities");
    }
    // we don't want to select ALL fields, just the nonpk fields that are in the passed GenericEntity
    List<ModelField> partialFields = new LinkedList<ModelField>();
    Set<String> tempKeys = new TreeSet<String>(keys);
    Iterator<ModelField> entityFieldIter = modelEntity.getFieldsIterator();
    while (entityFieldIter.hasNext()) {
        ModelField curField = entityFieldIter.next();
        if (tempKeys.contains(curField.getName())) {
            partialFields.add(curField);
            tempKeys.remove(curField.getName());
        }
    }
    if (tempKeys.size() > 0) {
        throw new GenericModelException("In partialSelect invalid field names specified: " + tempKeys.toString());
    }
    StringBuilder sqlBuffer = new StringBuilder("SELECT ");
    if (partialFields.size() > 0) {
        modelEntity.colNameString(partialFields, sqlBuffer, "", ", ", "", datasource.getAliasViewColumns());
    } else {
        sqlBuffer.append("*");
    }
    sqlBuffer.append(SqlJdbcUtil.makeFromClause(modelEntity, modelFieldTypeReader, datasource));
    sqlBuffer.append(SqlJdbcUtil.makeWhereClause(modelEntity, modelEntity.getPkFieldsUnmodifiable(), entity, "AND", datasource.getJoinStyle()));
    try (SQLProcessor sqlP = new SQLProcessor(entity.getDelegator(), helperInfo)) {
        sqlP.prepareStatement(sqlBuffer.toString(), true, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader);
        sqlP.executeQuery();
        if (sqlP.next()) {
            for (int j = 0; j < partialFields.size(); j++) {
                ModelField curField = partialFields.get(j);
                SqlJdbcUtil.getValue(sqlP.getResultSet(), j + 1, curField, entity, modelFieldTypeReader);
            }
            entity.synchronizedWithDatasource();
        } else {
            throw new GenericEntityNotFoundException("Result set was empty for entity: " + entity.toString());
        }
    }
}
Also used : GenericModelException(org.apache.ofbiz.entity.GenericModelException) GenericNotImplementedException(org.apache.ofbiz.entity.GenericNotImplementedException) LinkedList(java.util.LinkedList) SQLProcessor(org.apache.ofbiz.entity.jdbc.SQLProcessor) ModelField(org.apache.ofbiz.entity.model.ModelField) TreeSet(java.util.TreeSet) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) GenericEntityNotFoundException(org.apache.ofbiz.entity.GenericEntityNotFoundException) ModelEntity(org.apache.ofbiz.entity.model.ModelEntity)

Aggregations

ModelField (org.apache.ofbiz.entity.model.ModelField)55 ModelEntity (org.apache.ofbiz.entity.model.ModelEntity)28 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)16 LinkedList (java.util.LinkedList)14 ModelViewEntity (org.apache.ofbiz.entity.model.ModelViewEntity)13 HashMap (java.util.HashMap)10 GenericValue (org.apache.ofbiz.entity.GenericValue)10 ModelFieldType (org.apache.ofbiz.entity.model.ModelFieldType)10 ModelKeyMap (org.apache.ofbiz.entity.model.ModelKeyMap)10 Map (java.util.Map)8 SQLException (java.sql.SQLException)7 Locale (java.util.Locale)7 GenericModelException (org.apache.ofbiz.entity.GenericModelException)7 TreeSet (java.util.TreeSet)6 Delegator (org.apache.ofbiz.entity.Delegator)6 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)6 GeneralException (org.apache.ofbiz.base.util.GeneralException)5 ModelParam (org.apache.ofbiz.service.ModelParam)5 SQLProcessor (org.apache.ofbiz.entity.jdbc.SQLProcessor)4 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)4