@Override public <E extends T> E refreshAll(E entity) { EntityProxy<E> proxy = context.proxyOf(entity, false); synchronized (proxy.syncObject()) { return context.read(proxy.type().getClassType()).refreshAll(entity, proxy); } }
@Test public void testUpdate() { Person person = randomPerson(); data.insert(person); assertTrue(person.getId() > 0); person.setName("Bob Smith"); Calendar calendar = Calendar.getInstance(); calendar.set(1983, Calendar.NOVEMBER, 11); person.setBirthday(calendar.getTime()); EntityProxy<Person> proxy = Person.$TYPE.proxyProvider().apply(person); int count = 0; for (Property ignored : proxy.filterProperties( new Predicate<Property<Person, ?>>() { @Override public boolean test(Property value) { return value.state() == PropertyState.MODIFIED; } })) { count++; } assertEquals(2, count); data.update(person); for (Property ignored : proxy.filterProperties( new Predicate<Property<Person, ?>>() { @Override public boolean test(Property value) { return value.state() == PropertyState.MODIFIED; } })) { fail(); } }
private void writeObject(ObjectOutputStream stream) throws IOException { stream.defaultWriteObject(); Type<E> type = SerializationContext.getType(entityClass); EntityProxy<E> proxy = type.proxyProvider().apply(entity); for (Property<E, ?> property : proxy.filterProperties(getPropertyFilter())) { Object value = property.get(); stream.writeObject(value); } }
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { stream.defaultReadObject(); Type<E> type = SerializationContext.getType(entityClass); entity = type.factory().get(); EntityProxy<E> proxy = type.proxyProvider().apply(entity); for (Property<E, ?> property : proxy.filterProperties(getPropertyFilter())) { Object value = stream.readObject(); property.setObject(value, PropertyState.LOADED); } }
@SuppressWarnings("unchecked") @Override public <E extends T> E refresh(E entity, Attribute<?, ?>... attributes) { EntityProxy<E> proxy = context.proxyOf(entity, false); synchronized (proxy.syncObject()) { return context .read(proxy.type().getClassType()) .refresh(entity, proxy, (Attribute<E, ?>[]) attributes); } }
@Override public <E extends T> Void delete(E entity) { try (TransactionScope transaction = new TransactionScope(transactionProvider)) { EntityProxy<E> proxy = context.proxyOf(entity, true); synchronized (proxy.syncObject()) { context.write(proxy.type().getClassType()).delete(entity, proxy); transaction.commit(); } } return null; }
@SuppressWarnings("unchecked") @Override public <E extends T> Iterable<E> refresh(Iterable<E> entities, Attribute<?, ?>... attributes) { Iterator<E> iterator = entities.iterator(); if (iterator.hasNext()) { E entity = iterator.next(); EntityProxy<E> proxy = context.proxyOf(entity, false); EntityReader<E, T> reader = context.read(proxy.type().getClassType()); return reader.batchRefresh(entities, (Attribute<E, ?>[]) attributes); } return entities; }
@Override public <E extends T> E upsert(E entity) { try (TransactionScope transaction = new TransactionScope(transactionProvider)) { EntityProxy<E> proxy = context.proxyOf(entity, true); synchronized (proxy.syncObject()) { EntityWriter<E, T> writer = context.write(proxy.type().getClassType()); writer.upsert(entity, proxy); transaction.commit(); return entity; } } }
@Override public <E extends T> Void delete(Iterable<E> entities) { Iterator<E> iterator = entities.iterator(); if (iterator.hasNext()) { try (TransactionScope transaction = new TransactionScope(transactionProvider)) { E entity = iterator.next(); EntityProxy<E> proxy = context.proxyOf(entity, false); EntityWriter<E, T> writer = context.write(proxy.type().getClassType()); writer.delete(entities); transaction.commit(); } } return null; }
@SuppressWarnings("unchecked") @Override public <E extends T> E update(E entity, Attribute<?, ?>... attributes) { try (TransactionScope transaction = new TransactionScope(transactionProvider)) { EntityProxy<E> proxy = context.proxyOf(entity, true); synchronized (proxy.syncObject()) { context .write(proxy.type().getClassType()) .update(entity, proxy, (Attribute<E, ?>[]) attributes); transaction.commit(); return entity; } } }
@Override public void setLong(Attribute<E, Long> attribute, long value, PropertyState state) { if (proxy != null) { proxy.setLong(attribute, value, state); } add(value); }
@Override public void setInt(Attribute<E, Integer> attribute, int value, PropertyState state) { if (proxy != null) { proxy.setInt(attribute, value, state); } add(value); }
@Override public void setByte(Attribute<E, Byte> attribute, byte value, PropertyState state) { if (proxy != null) { proxy.setByte(attribute, value, state); } add(value); }
@Override public void setFloat(Attribute<E, Float> attribute, float value, PropertyState state) { if (proxy != null) { proxy.setFloat(attribute, value, state); } add(value); }
@Override public void setDouble(Attribute<E, Double> attribute, double value, PropertyState state) { if (proxy != null) { proxy.setDouble(attribute, value, state); } add(value); }
@Override public void setBoolean(Attribute<E, Boolean> attribute, boolean value, PropertyState state) { if (proxy != null) { proxy.setBoolean(attribute, value, state); } add(value); }
@Override public void setObject(Attribute<E, ?> attribute, Object value, PropertyState state) { if (proxy != null) { proxy.setObject(attribute, value, state); } add(value); }
@Override public <V> void set(Attribute<E, V> attribute, V value, PropertyState state) { if (proxy != null) { proxy.set(attribute, value, state); } add(value); }
@Override public void setShort(Attribute<E, Short> attribute, short value, PropertyState state) { if (proxy != null) { proxy.setShort(attribute, value, state); } add(value); }
@Override public <K, E extends T> Iterable<K> insert(Iterable<E> entities, @Nullable Class<K> keyClass) { Iterator<E> iterator = entities.iterator(); if (iterator.hasNext()) { try (TransactionScope transaction = new TransactionScope(transactionProvider)) { E entity = iterator.next(); EntityProxy<E> proxy = context.proxyOf(entity, true); EntityWriter<E, T> writer = context.write(proxy.type().getClassType()); GeneratedKeys<E> keys = writer.batchInsert(entities, keyClass != null); transaction.commit(); @SuppressWarnings("unchecked") Iterable<K> result = (Iterable<K>) keys; return result; } } return null; }
@Override public <K, E extends T> K insert(E entity, @Nullable Class<K> keyClass) { try (TransactionScope transaction = new TransactionScope(transactionProvider)) { EntityProxy<E> proxy = context.proxyOf(entity, true); synchronized (proxy.syncObject()) { EntityWriter<E, T> writer = context.write(proxy.type().getClassType()); GeneratedKeys<E> key = null; if (keyClass != null) { key = new GeneratedKeys<>(proxy.type().isImmutable() ? null : proxy); } writer.insert(entity, proxy, key); transaction.commit(); if (key != null && key.size() > 0) { return keyClass.cast(key.get(0)); } } } return null; }