List of usage examples for org.apache.commons.collections4.iterators ReverseListIterator ReverseListIterator
public ReverseListIterator(final List<E> list)
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 2 s . c o 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...)//ww 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()); } }
From source file:tpt.dbweb.cat.Compare.java
/** * Generate "(chainidx" or "chainidx" or "chainidx)" strings *//from w w w . j a va 2 s.c o m * @param docIdx * @param pair * @param chains * @return */ private String chainAnnotationAttr(int docIdx, ComparePair pair, PosType posType, List<MentionChains> chains) { if (pair == null || pair.getPos(docIdx) == null) { return null; } List<EntityMention> mentions = pair.getMentions(docIdx); if (mentions == null || mentions.size() == 0) { return null; } StringBuilder sb = new StringBuilder(); boolean printIntermediates = !pair.emps.get(docIdx).stream() .filter(emp -> (emp.posType == PosType.START || emp.posType == PosType.END)).findAny().isPresent(); for (EntityMentionPos emp : Utility.iterable(new ReverseListIterator<>(pair.emps.get(docIdx)))) { if (emp == null) { continue; } if (emp.posType != posType) { continue; } PosType pt = emp.posType; Chain c0 = chains.get(docIdx).mentionToChain.get(emp.em); if (c0 == null) { return null; } String chainStr = "" /*+ c0.idx*/; switch (pt) { case START: chainStr = "(" + chainStr; break; case END: chainStr = chainStr + ")"; break; case INTERMEDIATE: if (!printIntermediates) { chainStr = null; } break; default: log.warn("chainToStr cannot deal with pos type {} for compare pair", pt, pair); } if (chainStr != null) { sb.append(chainStr); } } return sb.toString(); }