use of org.apache.ofbiz.entity.GenericNotImplementedException in project ofbiz-framework by apache.
the class GenericDAO method singleUpdateView.
/* ====================================================================== */
/* ====================================================================== */
/**
* Try to update the given ModelViewEntity by trying to insert/update on the entities of which the view is composed.
*
* Works fine with standard O/R mapped models, but has some restrictions meeting more complicated view entities.
* <li>A direct link is required, which means that one of the ModelViewLink field entries must have a value found
* in the given view entity, for each ModelViewLink</li>
* <li>For now, each member entity is updated iteratively, so if eg. the second member entity fails to update,
* the first is written although. See code for details. Try to use "clean" views, until code is more robust ...</li>
* <li>For now, aliased field names in views are not processed correctly, I guess. To be honest, I did not
* find out how to construct such a view - so view fieldnames must have same named fields in member entities.</li>
* <li>A new exception, e.g. GenericViewNotUpdatable, should be defined and thrown if the update fails</li>
*/
private int singleUpdateView(GenericEntity entity, ModelViewEntity modelViewEntity, List<ModelField> fieldsToSave, SQLProcessor sqlP) throws GenericEntityException {
Delegator delegator = entity.getDelegator();
int retVal = 0;
ModelEntity memberModelEntity = null;
// Construct insert/update for each model entity
for (ModelViewEntity.ModelMemberEntity modelMemberEntity : modelViewEntity.getMemberModelMemberEntities().values()) {
String meName = modelMemberEntity.getEntityName();
String meAlias = modelMemberEntity.getEntityAlias();
if (Debug.verboseOn())
Debug.logVerbose("[singleUpdateView]: Processing MemberEntity " + meName + " with Alias " + meAlias, module);
try {
memberModelEntity = delegator.getModelReader().getModelEntity(meName);
} catch (GenericEntityException e) {
throw new GenericEntityException("Failed to get model entity for " + meName, e);
}
Map<String, Object> findByMap = new HashMap<String, Object>();
// Now iterate the ModelViewLinks to construct the "WHERE" part for update/insert
Iterator<ModelViewEntity.ModelViewLink> linkIter = modelViewEntity.getViewLinksIterator();
while (linkIter != null && linkIter.hasNext()) {
ModelViewEntity.ModelViewLink modelViewLink = linkIter.next();
if (modelViewLink.getEntityAlias().equals(meAlias) || modelViewLink.getRelEntityAlias().equals(meAlias)) {
Iterator<ModelKeyMap> kmIter = modelViewLink.getKeyMapsIterator();
while (kmIter != null && kmIter.hasNext()) {
ModelKeyMap keyMap = kmIter.next();
String fieldName = "";
if (modelViewLink.getEntityAlias().equals(meAlias)) {
fieldName = keyMap.getFieldName();
} else {
fieldName = keyMap.getRelFieldName();
}
if (Debug.verboseOn())
Debug.logVerbose("[singleUpdateView]: --- Found field to set: " + meAlias + "." + fieldName, module);
Object value = null;
if (modelViewEntity.isField(keyMap.getFieldName())) {
value = entity.get(keyMap.getFieldName());
if (Debug.verboseOn())
Debug.logVerbose("[singleUpdateView]: --- Found map value: " + value.toString(), module);
} else if (modelViewEntity.isField(keyMap.getRelFieldName())) {
value = entity.get(keyMap.getRelFieldName());
if (Debug.verboseOn())
Debug.logVerbose("[singleUpdateView]: --- Found map value: " + value.toString(), module);
} else {
throw new GenericNotImplementedException("Update on view entities: no direct link found, unable to update");
}
findByMap.put(fieldName, value);
}
}
}
// Look what there already is in the database
List<GenericValue> meResult = null;
try {
meResult = EntityQuery.use(delegator).from(meName).where(findByMap).queryList();
} catch (GenericEntityException e) {
throw new GenericEntityException("Error while retrieving partial results for entity member: " + meName, e);
}
if (Debug.verboseOn())
Debug.logVerbose("[singleUpdateView]: --- Found " + meResult.size() + " results for entity member " + meName, module);
// Got results 0 -> INSERT, 1 -> UPDATE, >1 -> View is nor updatable
GenericValue meGenericValue = null;
if (meResult.size() == 0) {
// Create new value to insert
try {
// Create new value to store
meGenericValue = delegator.makeValue(meName, findByMap);
} catch (Exception e) {
throw new GenericEntityException("Could not create new value for member entity" + meName + " of view " + modelViewEntity.getEntityName(), e);
}
} else if (meResult.size() == 1) {
// Update existing value
meGenericValue = meResult.iterator().next();
} else {
throw new GenericEntityException("Found more than one result for member entity " + meName + " in view " + modelViewEntity.getEntityName() + " - this is no updatable view");
}
// Construct fieldsToSave list for this member entity
List<ModelField> meFieldsToSave = new LinkedList<ModelField>();
for (ModelField modelField : fieldsToSave) {
if (memberModelEntity.isField(modelField.getName())) {
ModelField meModelField = memberModelEntity.getField(modelField.getName());
if (meModelField != null) {
meGenericValue.set(meModelField.getName(), entity.get(modelField.getName()));
meFieldsToSave.add(meModelField);
if (Debug.verboseOn())
Debug.logVerbose("[singleUpdateView]: --- Added field to save: " + meModelField.getName() + " with value " + meGenericValue.get(meModelField.getName()), module);
} else {
throw new GenericEntityException("Could not get field " + modelField.getName() + " from model entity " + memberModelEntity.getEntityName());
}
}
}
/*
* Finally, do the insert/update
* TODO:
* Do the real inserts/updates outside the memberEntity-loop,
* only if all of the found member entities are updatable.
* This avoids partial creation of member entities, which would mean data inconsistency:
* If not all member entities can be updated, then none should be updated
*/
if (meResult.size() == 0) {
retVal += singleInsert(meGenericValue, memberModelEntity, memberModelEntity.getFieldsUnmodifiable(), sqlP);
} else {
if (meFieldsToSave.size() > 0) {
retVal += singleUpdate(meGenericValue, memberModelEntity, meFieldsToSave, sqlP);
} else {
if (Debug.verboseOn())
Debug.logVerbose("[singleUpdateView]: No update on member entity " + memberModelEntity.getEntityName() + " needed", module);
}
}
}
return retVal;
}
use of org.apache.ofbiz.entity.GenericNotImplementedException in project ofbiz-framework by apache.
the class SqlJdbcUtil method setValue.
public static <T> void setValue(SQLProcessor sqlP, ModelField modelField, String entityName, Object fieldValue, ModelFieldTypeReader modelFieldTypeReader) throws GenericEntityException {
ModelFieldType mft = modelFieldTypeReader.getModelFieldType(modelField.getType());
if (mft == null) {
throw new GenericModelException("GenericDAO.getValue: definition fieldType " + modelField.getType() + " not found, cannot setValue for field " + entityName + "." + modelField.getName() + ".");
}
// if the value is the GenericEntity.NullField, treat as null
if (fieldValue == GenericEntity.NULL_FIELD) {
fieldValue = null;
}
// ----- Try out the new handler code -----
ModelField.EncryptMethod encryptMethod = modelField.getEncryptMethod();
if (encryptMethod.isEncrypted()) {
fieldValue = sqlP.getDelegator().encryptFieldValue(entityName, encryptMethod, fieldValue);
}
JdbcValueHandler<T> handler = UtilGenerics.cast(mft.getJdbcValueHandler());
if (handler != null) {
try {
sqlP.setValue(handler, handler.getJavaClass().cast(fieldValue));
return;
} catch (SQLException e) {
throw new GenericDataSourceException("SQL Exception while setting value on field [" + modelField.getName() + "] of entity " + entityName + ": ", e);
}
} else {
Debug.logWarning("JdbcValueHandler not found for java-type " + mft.getJavaType() + ", falling back on switch statement. Entity = " + modelField.getModelEntity().getEntityName() + ", field = " + modelField.getName() + ".", module);
}
// ------------------------------------------
String fieldType = mft.getJavaType();
if (fieldValue != null) {
if (!ObjectType.instanceOf(fieldValue, fieldType)) {
// this is only an info level message because under normal operation for most JDBC
// drivers this will be okay, but if not then the JDBC driver will throw an exception
// and when lower debug levels are on this should help give more info on what happened
String fieldClassName = fieldValue.getClass().getName();
if (fieldValue instanceof byte[]) {
fieldClassName = "byte[]";
}
if (Debug.verboseOn())
Debug.logVerbose("type of field " + entityName + "." + modelField.getName() + " is " + fieldClassName + ", was expecting " + mft.getJavaType() + "; this may " + "indicate an error in the configuration or in the class, and may result " + "in an SQL-Java data conversion error. Will use the real field type: " + fieldClassName + ", not the definition.", module);
fieldType = fieldClassName;
}
}
try {
int typeValue = getType(fieldType);
switch(typeValue) {
case 1:
sqlP.setValue((String) fieldValue);
break;
case 2:
sqlP.setValue((java.sql.Timestamp) fieldValue);
break;
case 3:
sqlP.setValue((java.sql.Time) fieldValue);
break;
case 4:
sqlP.setValue((java.sql.Date) fieldValue);
break;
case 5:
sqlP.setValue((java.lang.Integer) fieldValue);
break;
case 6:
sqlP.setValue((java.lang.Long) fieldValue);
break;
case 7:
sqlP.setValue((java.lang.Float) fieldValue);
break;
case 8:
sqlP.setValue((java.lang.Double) fieldValue);
break;
case 9:
sqlP.setValue((java.math.BigDecimal) fieldValue);
break;
case 10:
sqlP.setValue((java.lang.Boolean) fieldValue);
break;
case 11:
sqlP.setBinaryStream(fieldValue);
break;
case 12:
if (fieldValue instanceof byte[]) {
sqlP.setBytes((byte[]) fieldValue);
} else if (fieldValue instanceof ByteBuffer) {
sqlP.setBytes(((ByteBuffer) fieldValue).array());
} else {
sqlP.setValue((java.sql.Blob) fieldValue);
}
break;
case 13:
sqlP.setValue((java.sql.Clob) fieldValue);
break;
case 14:
if (fieldValue != null) {
sqlP.setValue(new java.sql.Date(((java.util.Date) fieldValue).getTime()));
} else {
sqlP.setValue((java.sql.Date) null);
}
break;
case 15:
sqlP.setValue(UtilGenerics.<Collection<?>>cast(fieldValue));
break;
}
} catch (GenericNotImplementedException e) {
throw new GenericNotImplementedException("Not Implemented Exception while setting value on field [" + modelField.getName() + "] of entity " + entityName + ": " + e.toString(), e);
} catch (SQLException sqle) {
throw new GenericDataSourceException("SQL Exception while setting value on field [" + modelField.getName() + "] of entity " + entityName + ": ", sqle);
}
}
Aggregations