Search in sources :

Example 1 with EntityLockedException

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

the class GenericDAO method singleUpdate.

private int singleUpdate(GenericEntity entity, ModelEntity modelEntity, List<ModelField> fieldsToSave, SQLProcessor sqlP) throws GenericEntityException {
    if (modelEntity instanceof ModelViewEntity) {
        return singleUpdateView(entity, (ModelViewEntity) modelEntity, fieldsToSave, sqlP);
    }
    // no non-primaryKey fields, update doesn't make sense, so don't do it
    if (fieldsToSave.size() <= 0) {
        if (Debug.verboseOn())
            Debug.logVerbose("Trying to do an update on an entity with no non-PK fields, returning having done nothing; entity=" + entity, module);
        // returning one because it was effectively updated, ie the same thing, so don't trigger any errors elsewhere
        return 1;
    }
    if (modelEntity.lock()) {
        GenericEntity entityCopy = GenericEntity.createGenericEntity(entity);
        select(entityCopy, sqlP);
        Object stampField = entity.get(ModelEntity.STAMP_FIELD);
        if ((stampField != null) && (!stampField.equals(entityCopy.get(ModelEntity.STAMP_FIELD)))) {
            String lockedTime = entityCopy.getTimestamp(ModelEntity.STAMP_FIELD).toString();
            throw new EntityLockedException("You tried to update an old version of this data. Version locked: (" + lockedTime + ")");
        }
    }
    // 2. don't set the stamp values if it is from an EntitySync (ie maintain original values), unless the stamps are null then set it anyway, ie even if it was from an EntitySync (also used for imports and such)
    if (modelEntity.isField(ModelEntity.STAMP_TX_FIELD) && (!entity.getIsFromEntitySync() || entity.get(ModelEntity.STAMP_TX_FIELD) == null)) {
        entity.set(ModelEntity.STAMP_TX_FIELD, TransactionUtil.getTransactionStartStamp());
        addFieldIfMissing(fieldsToSave, ModelEntity.STAMP_TX_FIELD, modelEntity);
    }
    // if we have a STAMP_FIELD then update it with NOW.
    if (modelEntity.isField(ModelEntity.STAMP_FIELD) && (!entity.getIsFromEntitySync() || entity.get(ModelEntity.STAMP_FIELD) == null)) {
        entity.set(ModelEntity.STAMP_FIELD, TransactionUtil.getTransactionUniqueNowStamp());
        addFieldIfMissing(fieldsToSave, ModelEntity.STAMP_FIELD, modelEntity);
    }
    StringBuilder sql = new StringBuilder().append("UPDATE ").append(modelEntity.getTableName(datasource)).append(" SET ");
    modelEntity.colNameString(fieldsToSave, sql, "", "=?, ", "=?", false);
    sql.append(" WHERE ");
    SqlJdbcUtil.makeWhereStringFromFields(sql, modelEntity.getPkFieldsUnmodifiable(), entity, "AND");
    int retVal = 0;
    try {
        sqlP.prepareStatement(sql.toString());
        SqlJdbcUtil.setValues(sqlP, fieldsToSave, entity, modelFieldTypeReader);
        SqlJdbcUtil.setPkValues(sqlP, modelEntity, entity, modelFieldTypeReader);
        retVal = sqlP.executeUpdate();
        entity.synchronizedWithDatasource();
    } catch (GenericEntityException e) {
        throw new GenericEntityException("Error while updating: " + entity.toString(), e);
    }
    if (retVal == 0) {
        throw new GenericEntityNotFoundException("Tried to update an entity that does not exist, entity: " + entity.toString());
    }
    return retVal;
}
Also used : GenericEntity(org.apache.ofbiz.entity.GenericEntity) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) ModelViewEntity(org.apache.ofbiz.entity.model.ModelViewEntity) GenericEntityNotFoundException(org.apache.ofbiz.entity.GenericEntityNotFoundException) EntityLockedException(org.apache.ofbiz.entity.EntityLockedException)

Aggregations

EntityLockedException (org.apache.ofbiz.entity.EntityLockedException)1 GenericEntity (org.apache.ofbiz.entity.GenericEntity)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericEntityNotFoundException (org.apache.ofbiz.entity.GenericEntityNotFoundException)1 ModelViewEntity (org.apache.ofbiz.entity.model.ModelViewEntity)1