/** * Encodes object to XML using provided encoder. * * @since 1.2 */ public void encodeAsXML(XMLEncoder encoder) { EntityResolver er = getDataContext().getEntityResolver(); ObjEntity object = er.lookupObjEntity(getClass()); String[] fields = this.getClass().getName().split("\\."); encoder.setRoot(fields[fields.length - 1], this.getClass().getName()); for (Iterator it = object.getDeclaredAttributes().iterator(); it.hasNext();) { ObjAttribute att = (ObjAttribute) it.next(); String name = att.getName(); encoder.encodeProperty(name, readNestedProperty(name)); } }
public void addToManyTarget(String relName, DataObject value, boolean setReverse) { if (value == null) { throw new NullPointerException("Attempt to add null target DataObject."); } willConnect(relName, value); // Now do the rest of the normal handling (regardless of whether it was // flattened or not) List list = (List) readProperty(relName); // call 'recordArcCreated' AFTER readProperty as readProperty ensures that this // object fault is resolved getDataContext().getObjectStore().recordArcCreated( this, value.getObjectId(), relName); list.add(value); if (value != null && setReverse) { setReverseRelationship(relName, value); } }
public void removeToManyTarget(String relName, DataObject value, boolean setReverse) { // Now do the rest of the normal handling (regardless of whether it was // flattened or not) List relList = (List) readProperty(relName); // call 'recordArcDeleted' AFTER readProperty as readProperty ensures that this // object fault is resolved getDataContext().getObjectStore().recordArcDeleted( this, value != null ? value.getObjectId() : null, relName); relList.remove(value); if (persistenceState == PersistenceState.COMMITTED) { persistenceState = PersistenceState.MODIFIED; } if (value != null && setReverse) { unsetReverseRelationship(relName, value); } }
ObjEntity objEntity = getDataContext().getEntityResolver().lookupObjEntity(this); if (objEntity == null) { throw new CayenneRuntimeException( DataNode node = getDataContext().getParentDataDomain().lookupDataNode( objEntity.getDataMap()); if (node == null) {
public void setToOneTarget( String relationshipName, DataObject value, boolean setReverse) { willConnect(relationshipName, value); Object oldTarget = readProperty(relationshipName); if (oldTarget == value) { return; } getDataContext().getObjectStore().recordArcCreated( this, value != null ? value.getObjectId() : null, relationshipName); if (setReverse) { // unset old reverse relationship if (oldTarget instanceof DataObject) { unsetReverseRelationship(relationshipName, (DataObject) oldTarget); } // set new reverse relationship if (value != null) { setReverseRelationship(relationshipName, value); } } objectContext.prepareForAccess(this, relationshipName); writePropertyDirectly(relationshipName, value); }
/** * Called before establishing a relationship with another object. Applies "persistence * by reachability" logic, pulling one of the two objects to a DataConext of another * object in case one of the objects is transient. If both objects are persistent, and * they don't have the same DataContext, CayenneRuntimeException is thrown. * * @since 1.2 */ protected void willConnect(String relationshipName, DataObject dataObject) { // first handle most common case - both objects are in the same // DataContext or target is null if (dataObject == null || this.getObjectContext() == dataObject.getObjectContext()) { return; } else if (this.getObjectContext() == null && dataObject.getObjectContext() != null) { dataObject.getDataContext().registerNewObject(this); } else if (this.getObjectContext() != null && dataObject.getObjectContext() == null) { this.getDataContext().registerNewObject(dataObject); } else { throw new CayenneRuntimeException( "Cannot set object as destination of relationship " + relationshipName + " because it is in a different DataContext"); } }