@Override public boolean updateState(com.cloud.storage.Volume.State currentState, Event event, com.cloud.storage.Volume.State nextState, Volume vo, Object data) { Long oldUpdated = vo.getUpdatedCount(); Date oldUpdatedTime = vo.getUpdated(); SearchCriteria<VolumeVO> sc = AllFieldsSearch.create(); sc.setParameters("id", vo.getId()); sc.setParameters("state", currentState); sc.setParameters("updatedCount", vo.getUpdatedCount()); vo.incrUpdatedCount(); UpdateBuilder builder = getUpdateBuilder(vo); builder.set(vo, "state", nextState); builder.set(vo, "updated", new Date()); int rows = update((VolumeVO)vo, sc); if (rows == 0 && s_logger.isDebugEnabled()) { VolumeVO dbVol = findByIdIncludingRemoved(vo.getId()); if (dbVol != null) { StringBuilder str = new StringBuilder("Unable to update ").append(vo.toString()); str.append(": DB Data={id=").append(dbVol.getId()).append("; state=").append(dbVol.getState()).append("; updatecount=").append(dbVol.getUpdatedCount()).append(";updatedTime=") .append(dbVol.getUpdated()); str.append(": New Data={id=").append(vo.getId()).append("; state=").append(nextState).append("; event=").append(event).append("; updatecount=").append(vo.getUpdatedCount()) .append("; updatedTime=").append(vo.getUpdated()); str.append(": stale Data={id=").append(vo.getId()).append("; state=").append(currentState).append("; event=").append(event).append("; updatecount=").append(oldUpdated) .append("; updatedTime=").append(oldUpdatedTime); } else { s_logger.debug("Unable to update volume: id=" + vo.getId() + ", as there is no such volume exists in the database anymore"); } } return rows > 0; }
@Override @DB() @SuppressWarnings("unchecked") public T createForUpdate(final ID id) { final T entity = (T)_factory.newInstance(new Callback[] {NoOp.INSTANCE, new UpdateBuilder(this)}); if (id != null) { try { _idField.set(entity, id); } catch (final IllegalArgumentException e) { } catch (final IllegalAccessException e) { } } return entity; }
protected void makeIncrChange(String method, Object[] args) { String field = methodToField(method, 4); Attribute attr = _dao._allAttributes.get(field); assert (attr != null && attr.isUpdatable()) : "Updating an attribute that's not updatable: " + field; incr(attr, args == null || args.length == 0 ? 1 : args[0]); }
public void set(Object entity, String name, Object value) { Attribute attr = makeChange(name, value); set(entity, attr, value); }
@Override public Object intercept(Object object, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { String name = method.getName(); if (name.startsWith("set")) { String field = methodToField(name, 3); makeChange(field, args[0]); } else if (name.startsWith("incr")) { makeIncrChange(name, args); } else if (name.startsWith("decr")) { makeDecrChange(name, args); } return methodProxy.invokeSuper(object, args); }
protected void makeDecrChange(String method, Object[] args) { String field = methodToField(method, 4); Attribute attr = _dao._allAttributes.get(field); assert (attr != null && attr.isUpdatable()) : "Updating an attribute that's not updatable: " + field; decr(attr, args == null || args.length == 0 ? 1 : args[0]); }
final String searchClause = sc.getWhereClause(); sql = ub.toSql(_tables); if (sql == null) { return 0; pstmt = txn.prepareAutoCloseStatement(sql.toString()); Collection<Ternary<Attribute, Boolean, Object>> changes = ub.getChanges(); ub.clear(); return result; } catch (final SQLException e) {
@Override public void changeActiveNicsBy(long networkId, int count) { SearchCriteria<NetworkOpVO> sc = AllFieldsSearch.create(); sc.setParameters("network", networkId); NetworkOpVO vo = createForUpdate(); UpdateBuilder builder = getUpdateBuilder(vo); builder.incr(_activeNicsAttribute, count); update(builder, sc, null); }
protected int update(ID id, UpdateBuilder ub, T entity) { if (_cache != null) { _cache.remove(id); } SearchCriteria<T> sc = createSearchCriteria(); sc.addAnd(_idAttributes.get(_table)[0], SearchCriteria.Op.EQ, id); TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); try { if (ub.getCollectionChanges() != null) { insertElementCollection(entity, _idAttributes.get(_table)[0], id, ub.getCollectionChanges()); } } catch (SQLException e) { throw new CloudRuntimeException("Unable to persist element collection", e); } int rowsUpdated = update(ub, sc, null); txn.commit(); return rowsUpdated; }
@Override public Object intercept(final Object object, final Method method, final Object[] args, final MethodProxy methodProxy) throws Throwable { final String name = method.getName(); if (name.startsWith("set")) { final String field = methodToField(name, 3); makeChange(field, args[0]); } else if (name.startsWith("incr")) { makeIncrChange(name, args); } else if (name.startsWith("decr")) { makeDecrChange(name, args); } return methodProxy.invokeSuper(object, args); }
public void set(final Object entity, final String name, final Object value) { final Attribute attr = makeChange(name, value); set(entity, attr, value); }
final String searchClause = sc.getWhereClause(); sql = ub.toSql(_tables); if (sql == null) { return 0; pstmt = txn.prepareAutoCloseStatement(sql.toString()); final Collection<Ternary<Attribute, Boolean, Object>> changes = ub.getChanges(); ub.clear(); return result; } catch (final SQLException e) {
protected void makeDecrChange(final String method, final Object[] args) { final String field = methodToField(method, 4); final Attribute attr = _dao._allAttributes.get(field); assert (attr != null && attr.isUpdatable()) : "Updating an attribute that's not updatable: " + field; decr(attr, args == null || args.length == 0 ? 1 : args[0]); }
protected int update(final ID id, final UpdateBuilder ub, final T entity) { if (_cache != null) { _cache.remove(id); } final SearchCriteria<T> sc = createSearchCriteria(); sc.addAnd(_idAttributes.get(_table)[0], SearchCriteria.Op.EQ, id); final TransactionLegacy txn = TransactionLegacy.currentTxn(); txn.start(); try { if (ub.getCollectionChanges() != null) { insertElementCollection(entity, _idAttributes.get(_table)[0], id, ub.getCollectionChanges()); } } catch (final SQLException e) { throw new CloudRuntimeException("Unable to persist element collection", e); } final int rowsUpdated = update(ub, sc, null); txn.commit(); return rowsUpdated; }
@SuppressWarnings("unchecked") @DB() protected T toEntityBean(final ResultSet result, final boolean cache) throws SQLException { final T entity = (T)_factory.newInstance(new Callback[] {NoOp.INSTANCE, new UpdateBuilder(this)}); toEntityBean(result, entity); if (cache && _cache != null) { try { _cache.put(new Element(_idField.get(entity), entity)); } catch (final Exception e) { s_logger.debug("Can't put it in the cache", e); } } return entity; }
protected void makeIncrChange(final String method, final Object[] args) { final String field = methodToField(method, 4); final Attribute attr = _dao._allAttributes.get(field); assert (attr != null && attr.isUpdatable()) : "Updating an attribute that's not updatable: " + field; incr(attr, args == null || args.length == 0 ? 1 : args[0]); }
@Override public int remove(SearchCriteria<T> sc) { if (_removeSql == null) { return expunge(sc); } T vo = createForUpdate(); UpdateBuilder ub = getUpdateBuilder(vo); ub.set(vo, _removed.second(), new Date()); return update(ub, sc, null); }
Callback[] callbacks = new Callback[] {NoOp.INSTANCE, new UpdateBuilder(this)}; _searchEnhancer.setCallback(new UpdateBuilder(this));