void provideField(int field) { if (_pc.pcGetStateManager() != null) throw new InternalException(_loc.get("detach-val-mismatch", _pc)); _pc.pcReplaceStateManager(this); _pc.pcProvideField(field); _pc.pcReplaceStateManager(null); }
/** * Return the object id for <code>ob</code> if it has one, or * <code>null</code> otherwise. */ private static Object getObjectId(Object ob) { if (!ImplHelper.isManageable(ob)) return null; PersistenceCapable pc = ImplHelper.toPersistenceCapable(ob, null); if (pc == null || pc.pcIsNew()) return null; else return pc.pcFetchObjectId(); }
/** * Extracts the broker from the given persistence capable instance. * @param pc a persistence capable instance * @return null if a Broker can notbe extracted */ protected Broker getBroker(PersistenceCapable pc) { if (pc == null) return null; Object ctx = pc.pcGetGenericContext(); return ctx instanceof Broker ? (Broker)ctx : null; }
/** * Store field-level information from the given state manager. */ protected void storeField(OpenJPAStateManager sm, FieldMetaData fmd) { if (fmd.getManagement() != fmd.MANAGE_PERSISTENT) return; int index = fmd.getIndex(); OpenJPAStateManager dsm = null; if (sm.getPersistenceCapable().pcIsDetached()) { dsm = (DetachedStateManager) sm.getPersistenceCapable(). pcGetStateManager(); sm.getPersistenceCapable().pcReplaceStateManager(sm); } Object val = toData(fmd, sm.fetchField(index, false), sm.getContext()); if (dsm != null) sm.getPersistenceCapable().pcReplaceStateManager(dsm); if (val != NULL) setData(index, val); else // unable to store field value; clear out any old values clearData(index); }
private static OpenJPAStateManager getStateManager(Object obj) { if (obj == null || !(obj instanceof PersistenceCapable)) { return null; } PersistenceCapable pc = (PersistenceCapable)obj; StateManager sm = pc.pcGetStateManager(); if (sm == null || !(sm instanceof OpenJPAStateManager)) { return null; } return (OpenJPAStateManager)sm; } }
/** * Calculate proper attach strategy for instance. */ private AttachStrategy getStrategy(Object toAttach) { PersistenceCapable pc = ImplHelper.toPersistenceCapable(toAttach, getBroker().getConfiguration()); if (pc.pcGetStateManager() instanceof AttachStrategy) return (AttachStrategy) pc.pcGetStateManager(); Object obj = pc.pcGetDetachedState(); if (obj instanceof AttachStrategy) return (AttachStrategy) obj; if (obj == null || obj == PersistenceCapable.DESERIALIZED) { // new or detached without state if (_version == null) _version = new VersionAttachStrategy(); return _version; } // detached state if (_detach == null) _detach = new DetachedStateAttachStrategy(); return _detach; }
/** * This method makes a best effort to determine if the provided object is detached. * * @param find * - If true, as a last resort this method will check whether or not the provided object exists in the * DB. If it is in the DB, the provided object is detached. * @return - True if the provided obj is detached, false otherwise. */ public boolean isDetached(Object obj, boolean find) { if (!(ImplHelper.isManageable(obj))) return false; PersistenceCapable pc = ImplHelper.toPersistenceCapable(obj, _conf); if (pc.pcGetStateManager() instanceof DetachedStateManager) return true; Boolean detached = pc.pcIsDetached(); if (detached != null) return detached.booleanValue(); if(!find){ return false; } // last resort: instance is detached if it has a store record ClassMetaData meta = _repo.getMetaData(ImplHelper.getManagedInstance(pc).getClass(), _loader, true); Object oid = ApplicationIds.create(pc, meta); if (oid == null) return false; return find(oid, null, EXCLUDE_ALL, null, 0) != null; }
/** * Return the state manager for the given instance, or null. * * @param assertThisContext if true, thow an exception if the given * object is managed by another broker */ protected StateManagerImpl getStateManagerImpl(Object obj, boolean assertThisContext) { if (ImplHelper.isManageable(obj)) { PersistenceCapable pc = ImplHelper.toPersistenceCapable(obj, _conf); if (pc.pcGetGenericContext() == this) return (StateManagerImpl) pc.pcGetStateManager(); if (assertThisContext && pc.pcGetGenericContext() != null) throw new UserException(_loc.get("not-managed", Exceptions.toString(obj))).setFailedObject(obj); } return null; }
/** * Generate an application id based on the current primary key field state * of the given instance. */ public static Object create(PersistenceCapable pc, ClassMetaData meta) { if (pc == null) return null; Object oid = pc.pcNewObjectIdInstance(); if (oid == null) return null; if (!meta.isOpenJPAIdentity()) { pc.pcCopyKeyFieldsToObjectId(oid); return oid; } FieldMetaData pk = meta.getPrimaryKeyFields()[0]; if (pk.getDeclaredTypeCode() != JavaTypes.OID) return oid; // always copy oid object in case field value mutates or becomes // managed ObjectId objid = (ObjectId) oid; ClassMetaData embed = pk.getEmbeddedMetaData(); objid.setId(copy(objid.getId(), embed, embed.getFields())); return objid; }
/** * This method will detach all provided StateManagers in place. * * @param states * The StateManagers to be detached. */ public void detachAll(Collection<StateManagerImpl> states) { for (StateManagerImpl sm : states) { ClassMetaData cmd = sm.getMetaData(); if (sm.isPersistent() && cmd.isDetachable()) { PersistenceCapable pc = sm.getPersistenceCapable(); if (pc.pcIsDetached() == false) { // Detach proxy fields. BitSet loaded = sm.getLoaded(); for (FieldMetaData fmd : cmd.getProxyFields()) { if (loaded.get(fmd.getIndex())) { detachProxyField(fmd, pc, sm, _tsm); } } pc.pcReplaceStateManager(null); } } } }
/** * Helper method to provide the given field number to the given * field manager. */ void provideField(PersistenceCapable pc, FieldManager store, int field) { FieldManager beforeFM = _fm; _fm = store; pc.pcProvideField(field); // Retaining original FM because of the possibility of reentrant calls _fm = beforeFM; }
public Object getObjectId(Object obj) { assertOpen(); if (ImplHelper.isManageable(obj)) return (ImplHelper.toPersistenceCapable(obj, _conf)) .pcFetchObjectId(); return null; }
/** * Restore the given field. If this method returns true, then you need * to use this field manager to replace the given field in the state * manager's instance. */ public boolean restoreField(int field) { if (!_loaded.get(field)) return false; if (_mutable != null && _mutable.get(field)) return true; // copy the saved field over if (_copyField == null) _copyField = new int[1]; _copyField[0] = field; _sm.getPersistenceCapable().pcCopyFields(_copy, _copyField); return false; }
public boolean writeDetached(ObjectOutput out) throws IOException { out.writeObject(_pc.pcGetDetachedState()); out.writeObject(this); return false; }
public Object getVersion(Object obj) { assertOpen(); if (ImplHelper.isManageable(obj)) return (ImplHelper.toPersistenceCapable(obj, _conf)).pcGetVersion(); return null; }
/** * Copy fields from an outside source to the key fields in the identity * object. */ public static void copyKeyFieldsToObjectId(Class<?> pcClass, FieldSupplier fm, Object oid) { Meta meta = getMeta(pcClass); if (meta.pc == null) throw new UserException(_loc.get("copy-no-id", pcClass)); meta.pc.pcCopyKeyFieldsToObjectId(fm, oid); }
/** * Copy fields to an outside source from the key fields in the identity * object. */ public static void copyKeyFieldsFromObjectId(Class<?> pcClass, FieldConsumer fm, Object oid) { Meta meta = getMeta(pcClass); if (meta.pc == null) throw new UserException(_loc.get("copy-no-id", pcClass)); meta.pc.pcCopyKeyFieldsFromObjectId(fm, oid); }
public boolean isDetached(Object obj) { if (!(ImplHelper.isManageable(obj))) return false; PersistenceCapable pc = ImplHelper.toPersistenceCapable(obj, _conf); Boolean detached = pc.pcIsDetached(); if (detached != null) return detached.booleanValue(); // last resort: instance is detached if it has a store record ClassMetaData meta = _conf.getMetaDataRepositoryInstance(). getMetaData(ImplHelper.getManagedInstance(pc).getClass(), _loader, true); Object oid = ApplicationIds.create(pc, meta); if (oid == null) return false; return find(oid, null, EXCLUDE_ALL, null, 0) != null; }
/** * Store field-level information from the given state manager. */ protected void storeField(OpenJPAStateManager sm, FieldMetaData fmd) { if (fmd.getManagement() != fmd.MANAGE_PERSISTENT) return; int index = fmd.getIndex(); OpenJPAStateManager dsm = null; if (sm.getPersistenceCapable().pcIsDetached()) { dsm = (DetachedStateManager) sm.getPersistenceCapable(). pcGetStateManager(); sm.getPersistenceCapable().pcReplaceStateManager(sm); } Object val = toData(fmd, sm.fetchField(index, false), sm.getContext()); if (dsm != null) sm.getPersistenceCapable().pcReplaceStateManager(dsm); if (val != NULL) setData(index, val); else // unable to store field value; clear out any old values clearData(index); }
private static boolean isManagedByAnotherPCtx(PersistenceCapable pc, BrokerImpl broker) { StateManager sm = pc.pcGetStateManager(); if (sm != null && sm instanceof StateManagerImpl) { StateManagerImpl smi = (StateManagerImpl) sm; Broker associatedBroker = smi.getBroker(); if (broker != associatedBroker) { return true; } } return false; } }