/** * 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()); }
@Override public int sort(JObject jobj1, JObject jobj2) { return Integer.compare(jobj1.getSchemaVersion(), jobj2.getSchemaVersion()); } }
/** * Get the Java model object with the same object ID as the given {@link JObject} and whose state derives from this transaction. * * <p> * This method can be thought of as a "refresh" operation for objects being imported from other transactions into this one. * * <p> * <b>A non-null object is always returned, but the corresponding object may not actually exist in this transaction.</b> * In that case, attempts to access its fields will throw {@link io.permazen.core.DeletedObjectException}. * Use {@link JObject#exists JObject.exists()} to check. * * <p> * This method is equivalent to {@code get(jobj.getObjId())} followed by an appropriate cast to type {@code T}. * * @param jobj Java model object * @param <T> expected Java type * @return Java model object in this transaction with the same object ID (possibly {@code jobj} itself) * @throws IllegalArgumentException if {@code jobj} is null, or not a {@link Permazen} database object * @throws ClassCastException if the Java model object in this transaction somehow does not have the same type as {@code jobj} * @see #get(ObjId) * @see #get(ObjId, Class) */ @SuppressWarnings("unchecked") public <T extends JObject> T get(T jobj) { return (T)jobj.getModelClass().cast(this.get(jobj.getObjId())); }
return this.cascadeCopyTo(this.getTransaction().getSnapshotTransaction(), cascadeName, clone);
@Override public ObjId convertToPresentation(JObject jobj, Class<? extends ObjId> targetType, Locale locale) { if (jobj == null) return null; return jobj.getObjId(); }
/** * 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(); }
/** * Update a single item in this container by updating its backing object. * * <p> * This updates the backing object with the same object ID as {@code jobj}, if any, and then fires * {@link com.vaadin.data.Property.ValueChangeEvent}s for all properties of the corresponding * {@link com.vaadin.data.Item}. * * @param jobj updated database object */ public void updateItem(JObject jobj) { Preconditions.checkArgument(jobj != null, "null jobj"); final SimpleItem<JObject> item = (SimpleItem<JObject>)this.getItem(jobj.getObjId()); if (item != null) { jobj.copyTo(item.getObject().getTransaction(), new CopyState()); item.fireValueChange(); } }
/** * Copy the given {@link JObject} into the destination transaction. * * <p> * The implementation in {@link ChangeCopier} copies {@code jobj} and objects reachable via the configured copy cascade, * unless {@code jobj} does not exist, in which case it is not copied (but the * {@linkplain JTransaction#get(ObjId) corresponding} {@link JObject} is still returned). * Subclasses may override as needed. * * @param jobj original object * @return copied object in {@link #dest} * @throws IllegalArgumentException if {@code jobj} is null */ @SuppressWarnings("unchecked") protected JObject copy(JObject jobj) { Preconditions.checkArgument(jobj != null, "null jobj"); final ObjId id = jobj.getObjId(); if (jobj.exists()) { final JTransaction jtx = jobj.getTransaction(); jtx.copyTo(this.dest, this.copyState, jtx.cascadeFindAll(id, this.cascadeName, this.recursionLimit)); } return dest.get(this.copyState.getDestinationId(id)); } }
private String getTypeName(JObject jobj) { return jobj.getTransaction().getTransaction().getSchemas() .getVersion(jobj.getSchemaVersion()).getObjType(jobj.getObjId().getStorageId()).getName(); } }
final ObjId id = this.jobj.getObjId(); final JObject target = this.create ? (JObject)jtx.create(this.jclass) : jtx.get(id); if (!this.create && !target.exists()) { Notification.show("Object " + id + " no longer exists", null, Notification.Type.WARNING_MESSAGE); return true; this.jobj.copyTo(jtx, new CopyState(new ObjIdMap<>(Collections.singletonMap(id, target.getObjId()))));
/** * Copy this instance and other instances it references through the specified reference paths (if any) * into the associated in-memory snapshot transaction. * * <p> * Normally this method would only be invoked on a regular database {@link JObject}. * The returned object will always be a snapshot {@link JObject}. * * <p> * This is a convenience method, and is equivalent to invoking: * <blockquote><code> * this.copyTo(this.getTransaction().getSnapshotTransaction(), new CopyState(), refPaths); * </code></blockquote> * * @param refPaths zero or more reference paths that refer to additional objects to be copied * @return the snapshot {@link JObject} copy of this instance * @throws io.permazen.core.DeletedObjectException if any copied object ends up with a reference to an object * that does not exist in {@code dest} through a reference field * {@linkplain io.permazen.annotation.JField#allowDeletedSnapshot configured} to disallow deleted assignment * in snapshot transactions * @throws IllegalArgumentException if this instance is a {@linkplain #isSnapshot snapshot instance} * @throws IllegalArgumentException if any path in {@code refPaths} is invalid * @see #copyIn copyIn() */ default JObject copyOut(String... refPaths) { return this.copyTo(this.getTransaction().getSnapshotTransaction(), new CopyState(), refPaths); }
@Override protected Value apply(ParseSession session, Value[] params) { // Get object Object obj = params[0].checkNotNull(session, "upgrade()"); if (obj instanceof JObject) obj = ((JObject)obj).getObjId(); else if (!(obj instanceof ObjId)) throw new EvalException("invalid upgrade() operation on non-database object of type " + obj.getClass().getName()); final ObjId id = (ObjId)obj; // Upgrade object try { return new ConstValue(session.getMode().hasPermazen() ? JTransaction.getCurrent().get(id).upgrade() : session.getTransaction().updateSchemaVersion(id)); } catch (DeletedObjectException e) { throw new EvalException("invalid upgrade() operation on non-existent object " + id); } } }
final ObjId id = jobj.getObjId(); final boolean deleted = this.tx.delete(id); jobj.resetCachedFieldValues();
@Override @RetryTransaction @org.springframework.transaction.annotation.Transactional("permazenGuiTransactionManager") protected boolean execute() { final ObjId id = this.objectChooser.getObjId(); if (id == null) return true; final JObject jobj = JTransaction.getCurrent().get(id); if (!jobj.exists()) { Notification.show("Object " + id + " no longer exists", null, Notification.Type.WARNING_MESSAGE); return false; } try { ReferenceFieldField.this.setValue(jobj.copyTo(ReferenceFieldField.this.dest, new CopyState())); } catch (Exception e) { Notification.show("Error: " + e, null, Notification.Type.ERROR_MESSAGE); } return true; } }
@RetryTransaction @Transactional("permazenGuiTransactionManager") private boolean canUpgrade(ObjId id) { final JObject jobj = JTransaction.getCurrent().get(id); return jobj.exists() && jobj.getSchemaVersion() != this.jdb.getActualVersion(); }
@RetryTransaction @Transactional("permazenGuiTransactionManager") private int doUpgrade(ObjId id) { final JObject jobj = JTransaction.getCurrent().get(id); final int oldVersion; try { oldVersion = jobj.getSchemaVersion(); } catch (DeletedObjectException e) { return -1; } final boolean upgraded = jobj.upgrade(); if (upgraded) this.objectChooser.getJObjectContainer().reloadAfterCommit(); return upgraded ? oldVersion : 0; }
@RetryTransaction @Transactional("permazenGuiTransactionManager") private JObject doCopyForEdit(ObjId id) { // Find object final JTransaction jtx = JTransaction.getCurrent(); final JObject jobj = jtx.get(id); if (!jobj.exists()) return null; // Copy out object and its dependencies return this.objectChooser.getJObjectContainer().copyWithRelated(jobj, jtx.getSnapshotTransaction(), new CopyState()); }
/** * Copy this instance, and other instances it references through the specified {@code refPaths} (if any), * into the transaction associated with the current thread. * * <p> * Normally this method would only be invoked on a snapshot {@link JObject}. * The returned object will be a {@link JObject} in the currently open transaction. * * <p> * This is a convenience method, and is equivalent to invoking: * <blockquote><code> * this.copyTo(JTransaction.getCurrent(), new CopyState(), refPaths) * </code></blockquote> * * @param refPaths zero or more reference paths that refer to additional objects to be copied * @return the regular database copy of this instance * @throws io.permazen.core.DeletedObjectException if any copied object ends up with a reference to an object * that does not exist in {@code dest} through a reference field * {@linkplain io.permazen.annotation.JField#allowDeletedSnapshot configured} to disallow deleted assignment * @throws io.permazen.core.SchemaMismatchException * if the schema corresponding to this object's version is not identical in both transactions * @throws IllegalArgumentException if any path in {@code refPaths} is invalid * @see #copyOut copyOut() */ default JObject copyIn(String... refPaths) { return this.copyTo(JTransaction.getCurrent(), new CopyState(), refPaths); }
@Override protected ObjId doForward(T jobj) { if (jobj == null) return null; return ((JObject)jobj).getObjId(); }
/** * 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); }