*/
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 = FastMap.newInstance();
// 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 = delegator.findByAnd(meName, findByMap);
} 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 = FastList.newInstance();
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;