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);
}
}
}
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);
}
}
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();
}
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());
}
}
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());
}
}
}
Aggregations