@Override public JObject convertToModel(ObjId id, Class<? extends JObject> targetType, Locale locale) { if (id == null) return null; return this.jtx.get(id); } }
@Override public Value evaluate(ParseSession session) { return new ConstValue(JTransaction.getCurrent().get(id)); }
@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()); }
final JObject target = this.create ? (JObject)jtx.create(this.jclass) : jtx.get(id);
/** * 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 T doBackward(ObjId id) { if (id == null) return null; final JObject jobj = this.jtx.get(id); return this.type.cast(jobj); }
@RetryTransaction @Transactional("permazenGuiTransactionManager") private boolean doDelete(ObjId id) { final boolean deleted = JTransaction.getCurrent().get(id).delete(); if (deleted) this.objectChooser.getJObjectContainer().reloadAfterCommit(); return deleted; }
/** * Get the Java model object that is associated with this transaction and has the given ID, cast to the given type. * * <p> * This method guarantees that for any particular {@code id}, the same Java instance will always be returned. * * <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 just invoke {@link #get(ObjId)} and then casts the result. * * @param id object ID * @param type expected type * @param <T> expected Java model type * @return Java model object * @throws ClassCastException if the Java model object does not have type {@code type} * @throws IllegalArgumentException if {@code type} is null * @see #get(ObjId) * @see #get(JObject) */ public <T> T get(ObjId id, Class<T> type) { Preconditions.checkArgument(type != null, "null type"); return type.cast(this.get(id)); }
@RetryTransaction @Transactional("permazenGuiTransactionManager") private boolean canUpgrade(ObjId id) { final JObject jobj = JTransaction.getCurrent().get(id); return jobj.exists() && jobj.getSchemaVersion() != this.jdb.getActualVersion(); }
/** * Create a new instance of the given type in this transaction. * * @param jclass object type * @param <T> Java model type * @return newly created instance * @throws IllegalArgumentException if {@code jclass} is not valid for this instance * @throws StaleTransactionException if this transaction is no longer usable */ public <T> T create(JClass<T> jclass) { final ObjId id = this.tx.create(jclass.storageId); return jclass.getType().cast(this.get(id)); }
@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; }
/** * Import a plain Java object (POJO), along with all other objects reachable from it via copied reference fields. * * <p> * If {@code obj} has already been imported, the previously assigned {@link JObject} is returned. * * @param obj object to import; must not be null * @return imported object, or null if the {@code storageIdMapper} returned null for {@code obj} * @throws io.permazen.core.DeletedObjectException if {@code storageIdMapper} returns the object ID of a non-existent object * @throws io.permazen.core.TypeNotInSchemaVersionException if {@code storageIdMapper} returns an object ID that does not * corresponding to any Permazen model class * @throws IllegalArgumentException if {@code obj} is null */ public JObject importPlain(Object obj) { // Sanity check Preconditions.checkArgument(obj != null, "null obj"); // Import object (if not already imported) final ObjId id = this.doImportPlain(obj); // Recursively copy any fields needing to be copied this.recurseOnFields(); // Done return this.jtx.get(id); }
@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; } }
/** * 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())); }
@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); } } }
/** * 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)); } }
@Override public Value apply(ParseSession session, Object params) { final ParamInfo info = (ParamInfo)params; final int storageId = info.getStorageId(); final Node version = info.getVersion(); // Create object final Transaction tx = session.getTransaction(); final ObjId id = version != null ? tx.create(storageId, version.evaluate(session).checkIntegral(session, "create()")) : tx.create(storageId); return new ConstValue(session.getMode().hasPermazen() ? JTransaction.getCurrent().get(id) : id); }
private void doOnCreate(ObjId id) { // Get JClass, if known final JClass<?> jclass; try { jclass = this.jdb.getJClass(id); } catch (TypeNotInSchemaVersionException e) { return; // object type does not exist in our schema } // Enqueue for revalidation if (this.validationMode == ValidationMode.AUTOMATIC && jclass.requiresDefaultValidation) this.revalidate(Collections.singleton(id)); // Notify @OnCreate methods Object jobj = null; for (OnCreateScanner<?>.MethodInfo info : jclass.onCreateMethods) { if (this.isSnapshot() && !info.getAnnotation().snapshotTransactions()) continue; if (jobj == null) jobj = this.get(id); Util.invoke(info.getMethod(), jobj); } }
private void doOnDelete(ObjId id) { // Get JClass, if known final JClass<?> jclass; try { jclass = this.jdb.getJClass(id); } catch (TypeNotInSchemaVersionException e) { return; // object type does not exist in our schema } // Notify @OnDelete methods Object jobj = null; for (OnDeleteScanner<?>.MethodInfo info : jclass.onDeleteMethods) { if (this.isSnapshot() && !info.getAnnotation().snapshotTransactions()) continue; if (jobj == null) jobj = this.get(id); Util.invoke(info.getMethod(), jobj); } }
final CopyState copyState = clone ? new CopyState(dest.createClones(ids)) : new CopyState(); jtx.copyTo(dest, copyState, ids); return dest.get(copyState.getDestinationId(id));