/** * Determine whether this instance is a normal instance or is an in-memory "snapshot" instance associated * with a {@link SnapshotJTransaction}. * * <p> * Equvialent to {@code getTransaction().isSnapshot()}. * * @return true if instance is a snapshot instance */ default boolean isSnapshot() { return this.getTransaction().isSnapshot(); }
/** * Recreate a deleted instance, if it does not exist, in its associated transaction. * The fields of a recreated object are set to their initial values. If the object already exists, nothing changes. * * @return true if instance was recreated, false if it already existed * @throws io.permazen.core.StaleTransactionException * if the transaction {@linkplain #getTransaction associated with this instance} is no longer usable */ default boolean recreate() { return this.getTransaction().recreate(this); }
/** * Ensure the given {@link JObject} is registered in its associated transaction's object cache. * * <p> * This method is used internally, to handle mutations in model class superclass constructors, which will occur * before the newly created {@link JObject} is fully constructed and associated with its {@link JTransaction}. * * @param jobj object to register * @throws NullPointerException if {@code jobj} is null */ public static void registerJObject(JObject jobj) { jobj.getTransaction().jobjectCache.register(jobj); }
/** * Apply this change to the specified object. * * <p> * This is a convenience method, equivalent to: * <blockquote><code> * apply(obj.getTransaction(), jobj); * </code></blockquote> * * @param jobj object to which to apply this change * @throws IllegalStateException if there is no {@link JTransaction} associated with {@code jobj} * @throws IllegalArgumentException if {@code jobj} is null */ public void apply(JObject jobj) { Preconditions.checkArgument(jobj != null, "null jobj"); this.apply(jobj.getTransaction(), jobj); }
/** * Update the schema version of this instance, if necessary, so that it matches the schema version * of its associated transaction. * * <p> * If a version change occurs, matching {@link io.permazen.annotation.OnVersionChange @OnVersionChange} * methods will be invoked prior to this method returning. * * @return true if the object's schema version was changed, false if it was already updated * @throws io.permazen.core.DeletedObjectException * if this object does not exist in the {@link JTransaction} associated with this instance * @throws io.permazen.core.StaleTransactionException * if the transaction {@linkplain #getTransaction associated with this instance} is no longer usable */ default boolean upgrade() { return this.getTransaction().updateSchemaVersion(this); }
/** * Delete this instance, if it exists, in this instance's associated transaction. * * <p> * See {@link io.permazen.core.Transaction#delete Transaction.delete()} for details on secondary deletions from * {@link io.permazen.core.DeleteAction#DELETE} and {@link io.permazen.annotation.JField#cascadeDelete}. * * @return true if instance was deleted, false if it did not exist * @throws io.permazen.core.StaleTransactionException * if the transaction associated with the current thread is no longer usable * @throws io.permazen.core.ReferencedObjectException if the object is referenced by some other object * through a reference field configured for {@link io.permazen.core.DeleteAction#EXCEPTION} */ default boolean delete() { return this.getTransaction().delete(this); }
/** * Set the Java value of this field in the given object. * Does not alter the schema version of the object. * * @param jobj object containing this field * @param value new value * @throws io.permazen.core.DeletedObjectException if {@code jobj} does not exist in its associated {@link JTransaction} * @throws io.permazen.core.StaleTransactionException if the {@link JTransaction} associated with {@code jobj} * is no longer usable * @throws IllegalArgumentException if this field is a sub-field of a complex field * @throws IllegalArgumentException if {@code value} is not an appropriate value for this field * @throws IllegalArgumentException if {@code jobj} is null */ public void setValue(JObject jobj, Object value) { Preconditions.checkArgument(jobj != null, "null jobj"); Preconditions.checkArgument(!(this.parent instanceof JComplexField), "field is a complex sub-field"); jobj.getTransaction().writeSimpleField(jobj, this.storageId, value, false); }
@Override public Object getValue(JObject jobj) { Preconditions.checkArgument(jobj != null, "null jobj"); Preconditions.checkArgument(!(this.parent instanceof JComplexField), "field is a complex sub-field"); return jobj.getTransaction().readSimpleField(jobj.getObjId(), this.storageId, false); }
@Override public List<?> getValue(JObject jobj) { Preconditions.checkArgument(jobj != null, "null jobj"); return jobj.getTransaction().readListField(jobj.getObjId(), this.storageId, false); }
@Override public NavigableSet<?> getValue(JObject jobj) { Preconditions.checkArgument(jobj != null, "null jobj"); return jobj.getTransaction().readSetField(jobj.getObjId(), this.storageId, false); }
@Override public NavigableMap<?, ?> getValue(JObject jobj) { Preconditions.checkArgument(jobj != null, "null jobj"); return jobj.getTransaction().readMapField(jobj.getObjId(), this.storageId, false); }
@Override public Counter getValue(JObject jobj) { Preconditions.checkArgument(jobj != null, "null jobj"); return jobj.getTransaction().readCounterField(jobj.getObjId(), this.storageId, false); }
/** * Determine whether this instance still exists in its associated transaction. * * @return true if instance exists, otherwise false * @throws io.permazen.core.StaleTransactionException * if the transaction {@linkplain #getTransaction associated with this instance} is no longer usable */ default boolean exists() { return this.getTransaction().exists(this.getObjId()); }
/** * Get this instance's current schema version. Does not change this instance's schema version. * * @return the schema version of this instance * @throws io.permazen.core.DeletedObjectException * if this object does not exist in the {@link JTransaction} associated with this instance * @throws io.permazen.core.StaleTransactionException * if the transaction {@linkplain #getTransaction associated with this instance} is no longer usable */ default int getSchemaVersion() { return this.getTransaction().getSchemaVersion(this.getObjId()); }
/** * Get the {@link JClass} of which this {@link JObject} is an instance. * * @return associated {@link JClass} * @throws io.permazen.core.TypeNotInSchemaVersionException if this instance has a type that does not exist * in this instance's schema version, i.e., this instance is an {@link UntypedJObject} */ default JClass<?> getJClass() { return this.getTransaction().getPermazen().getJClass(this.getObjId()); }
/** * Reattach the given {@link JObject} to the current transaction if it's associated with a stale transaction. */ public static JObject refresh(JObject jobj) { return !jobj.getTransaction().isValid() ? JTransaction.getCurrent().get(jobj) : jobj; }
@Override protected Long getContentLength(JObject jobj, MediaType contentType) { return KVStoreHttpMessageConverter.getKVStoreContentLength(jobj.getTransaction().getTransaction().getKVTransaction()); }
@Override protected void writeInternal(JObject jobj, HttpOutputMessage output) throws IOException { output.getHeaders().setContentType(this.getDefaultContentType(jobj)); KVStoreHttpMessageConverter.writeKVStore(jobj.getTransaction().getTransaction().getKVTransaction(), output); } }
@Override public Field<?> caseJSimpleField(JSimpleField jfield) { final boolean allowNull = jfield.getGetter().getAnnotation(NotNull.class) == null && !jfield.getTypeToken().isPrimitive(); return new SimpleFieldFieldBuilder(JObjectEditorWindow.this.jobj.getTransaction(), jfield, JObjectEditorWindow.this.session, allowNull).buildField(); } @Override
private String getTypeName(JObject jobj) { return jobj.getTransaction().getTransaction().getSchemas() .getVersion(jobj.getSchemaVersion()).getObjType(jobj.getObjId().getStorageId()).getName(); } }