Created
December 19, 2011 12:50
-
-
Save hugithordarson/1497082 to your computer and use it in GitHub Desktop.
CayenneDataObjectEncoder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package is.landsvirkjun.admin.util; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.apache.cayenne.CayenneDataObject; | |
import org.apache.cayenne.DataObjectUtils; | |
import org.apache.cayenne.ObjectContext; | |
import org.apache.tapestry5.ValueEncoder; | |
public class CayenneDataObjectEncoder implements ValueEncoder<CayenneDataObject> { | |
private static Map<Integer,ObjectContext> _dcs = new HashMap<Integer,ObjectContext>(); | |
private static final String DIVIDER = "-"; | |
public String toClient( CayenneDataObject cayenneDataObject ) { | |
ObjectContext oc = cayenneDataObject.getObjectContext(); | |
Integer hash = null; | |
if( oc != null ) { | |
hash = oc.hashCode(); | |
} | |
else { | |
hash = -1; | |
} | |
_dcs.put( hash, oc ); | |
String entityName = cayenneDataObject.getObjEntity().getName(); | |
Integer id = DataObjectUtils.intPKForObject( cayenneDataObject ); | |
String identifier = hash + DIVIDER + entityName + DIVIDER + id; | |
return identifier; | |
} | |
public CayenneDataObject toValue( String string ) { | |
String[] parts = string.split( DIVIDER ); | |
ObjectContext oc = _dcs.get( Integer.valueOf( parts[0] ) ); | |
String entityName = parts[1]; | |
Integer id = Integer.valueOf( parts[2] ); | |
return (CayenneDataObject)DataObjectUtils.objectForPK( oc, entityName, id ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment