Example usage for org.apache.commons.collections4.iterators ReverseListIterator next

List of usage examples for org.apache.commons.collections4.iterators ReverseListIterator next

Introduction

In this page you can find the example usage for org.apache.commons.collections4.iterators ReverseListIterator next.

Prototype

public E next() 

Source Link

Document

Gets the next element.

Usage

From source file:org.efaps.db.Insert.java

/**
 * A new statement must be created an executed for one table. If the
 * parameter '_id' is set to <code>0</code>, a new id is generated. If the
 * JDBC driver supports method <code>getGeneratedKeys</code>, this method is
 * used, otherwise method {@link org.efaps.db.databases#getNewId} is used to
 * retrieve a new id value./*from ww  w.j  a  va 2s.co m*/
 *
 * @param _con      connection resource
 * @param _table    sql table used to insert
 * @param _values   values to be inserted
 * @param _id       new created id
 * @return new created id if parameter <i>_id</i> is set to <code>0</code>
 * @see #createOneStatement
 * @throws EFapsException on error
 */
private long executeOneStatement(final ConnectionResource _con, final SQLTable _table,
        final List<Value> _values, final long _id) throws EFapsException {
    Insert.LOG.debug("Preparing insert on table: {} for Values: {}", _table, _values);
    long ret = _id;
    try {
        final SQLInsert insert = Context.getDbType().newInsert(_table.getSqlTable(), _table.getSqlColId(),
                _id == 0);

        if (_id != 0) {
            insert.column(_table.getSqlColId(), _id);
        }
        if (_table.getSqlColType() != null) {
            insert.column(_table.getSqlColType(), getType().getId());
        }

        final ReverseListIterator<Value> iterator = new ReverseListIterator<Value>(_values);

        final Set<String> added = new HashSet<String>();
        while (iterator.hasNext()) {
            final Value value = iterator.next();
            final String colKey = value.getAttribute().getSqlColNames().toString();
            if (!added.contains(colKey)) {
                value.getAttribute().prepareDBInsert(insert, value.getValues());
                added.add(colKey);
            }
        }

        final Long bck = insert.execute(_con.getConnection());
        if (bck != null) {
            ret = bck;
        }

    } catch (final SQLException e) {
        Insert.LOG.error("executeOneStatement", e);
        throw new EFapsException(getClass(), "executeOneStatement.Exception", e, _table.getName());
    }
    return ret;
}

From source file:org.efaps.db.Update.java

/**
 * The update is done without calling triggers and check of access rights.
 *
 * @throws EFapsException if update not possible (unique key, object does
 *             not exists, etc...)/*from  w  w w. j a va 2  s . c o  m*/
 */
public void executeWithoutTrigger() throws EFapsException {
    if (Update.STATUSOK.getStati().isEmpty()) {
        final Context context = Context.getThreadContext();
        ConnectionResource con = null;
        try {
            con = context.getConnectionResource();
            for (final Entry<SQLTable, List<Value>> entry : this.table2values.entrySet()) {
                final SQLUpdate update = Context.getDbType().newUpdate(entry.getKey().getSqlTable(),
                        entry.getKey().getSqlColId(), getInstance().getId());
                // iterate in reverse order and only execute the ones that are not added yet, permitting
                // to overwrite the value for attributes by adding them later
                final ReverseListIterator<Value> iterator = new ReverseListIterator<Value>(entry.getValue());

                final Set<String> added = new HashSet<String>();
                while (iterator.hasNext()) {
                    final Value value = iterator.next();
                    final String colKey = value.getAttribute().getSqlColNames().toString();
                    if (!added.contains(colKey)) {
                        value.getAttribute().prepareDBUpdate(update, value.getValues());
                        added.add(colKey);
                    }
                }
                final Set<String> updatedColumns = update.execute(con.getConnection());
                final Iterator<Entry<Attribute, Value>> attrIter = this.trigRelevantAttr2values.entrySet()
                        .iterator();
                while (attrIter.hasNext()) {
                    final Entry<Attribute, Value> trigRelEntry = attrIter.next();
                    if (trigRelEntry.getKey().getTable().equals(entry.getKey())) {
                        boolean updated = false;
                        for (final String colName : trigRelEntry.getKey().getSqlColNames()) {
                            if (updatedColumns.contains(colName)) {
                                updated = true;
                                break;
                            }
                        }
                        if (!updated) {
                            attrIter.remove();
                        }
                    }
                }
            }
            con.commit();
            AccessCache.registerUpdate(getInstance());
            Queue.registerUpdate(getInstance());
        } catch (final SQLException e) {
            Update.LOG.error("Update of '" + this.instance + "' not possible", e);
            throw new EFapsException(getClass(), "executeWithoutTrigger.SQLException", e, this.instance);
        } finally {
            if (con != null && con.isOpened()) {
                con.abort();
            }
        }
    } else {
        throw new EFapsException(getClass(), "executeWithout.StatusInvalid", Update.STATUSOK.getStati());
    }
}