Search in sources :

Example 1 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class EntityIdCoder method toObjectId.

public ObjectId toObjectId(String stringId) {
    if (stringId.startsWith(TEMP_ID_PREFIX)) {
        String idValues = stringId.substring(entityName.length() + 1 + TEMP_PREFIX_LENGTH);
        return new ObjectId(entityName, decodeTemp(idValues));
    }
    String idValues = stringId.substring(entityName.length() + 1);
    if (converters.size() == 1) {
        Entry<String, Converter> entry = converters.entrySet().iterator().next();
        String decoded;
        try {
            decoded = URLDecoder.decode(idValues, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // unexpected
            throw new CayenneRuntimeException("Unsupported encoding", e);
        }
        return new ObjectId(entityName, entry.getKey(), entry.getValue().fromStringId(decoded));
    }
    Map<String, Object> idMap = new HashMap<>(idSize);
    StringTokenizer toks = new StringTokenizer(idValues, ID_SEPARATOR);
    if (toks.countTokens() != converters.size()) {
        throw new IllegalArgumentException("Invalid String ID for entity " + entityName + ": " + idValues);
    }
    for (Entry<String, Converter> entry : converters.entrySet()) {
        String value = toks.nextToken();
        String decoded;
        try {
            decoded = URLDecoder.decode(value, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // unexpected
            throw new CayenneRuntimeException("Unsupported encoding", e);
        }
        idMap.put(entry.getKey(), entry.getValue().fromStringId(decoded));
    }
    return new ObjectId(entityName, idMap);
}
Also used : StringTokenizer(java.util.StringTokenizer) ObjectId(org.apache.cayenne.ObjectId) HashMap(java.util.HashMap) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class EntityIdCoderTest method testSingleLongPk.

@Test
public void testSingleLongPk() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.BIGINT);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    ObjectId id = new ObjectId("x", "ID", 3L);
    EntityIdCoder coder = new EntityIdCoder(entity);
    assertEquals("x:3", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:3");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 3 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class EntityIdCoderTest method testSingleIntPk.

@Test
public void testSingleIntPk() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.INTEGER);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    ObjectId id = new ObjectId("x", "ID", 3);
    EntityIdCoder coder = new EntityIdCoder(entity);
    assertEquals("x:3", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:3");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 4 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class EntityIdCoderTest method testSingleStringPk.

@Test
public void testSingleStringPk() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.VARCHAR);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    EntityIdCoder coder = new EntityIdCoder(entity);
    ObjectId id = new ObjectId("x", "ID", "AbC");
    assertEquals("x:AbC", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:AbC");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Example 5 with ObjectId

use of org.apache.cayenne.ObjectId in project cayenne by apache.

the class EntityIdCoderTest method testIdEncoding.

@Test
public void testIdEncoding() {
    DbEntity dbEntity = new DbEntity("X");
    DbAttribute pk = new DbAttribute("ID");
    pk.setType(Types.VARCHAR);
    pk.setPrimaryKey(true);
    dbEntity.addAttribute(pk);
    ObjEntity entity = mock(ObjEntity.class);
    when(entity.getName()).thenReturn("x");
    when(entity.getDbEntityName()).thenReturn(dbEntity.getName());
    when(entity.getDbEntity()).thenReturn(dbEntity);
    EntityIdCoder coder = new EntityIdCoder(entity);
    ObjectId id = new ObjectId("x", "ID", "Ab:C");
    assertEquals("x:Ab%3AC", coder.toStringId(id));
    ObjectId parsedId = coder.toObjectId("x:Ab%3AC");
    assertEquals(id, parsedId);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) Test(org.junit.Test)

Aggregations

ObjectId (org.apache.cayenne.ObjectId)156 Test (org.junit.Test)104 Persistent (org.apache.cayenne.Persistent)38 DataObject (org.apache.cayenne.DataObject)20 DataRow (org.apache.cayenne.DataRow)20 ObjectContext (org.apache.cayenne.ObjectContext)20 ObjEntity (org.apache.cayenne.map.ObjEntity)20 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)18 HashMap (java.util.HashMap)17 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)16 ArrayList (java.util.ArrayList)14 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)14 Artist (org.apache.cayenne.testdo.testmap.Artist)14 Map (java.util.Map)13 EntityResolver (org.apache.cayenne.map.EntityResolver)13 InvocationOnMock (org.mockito.invocation.InvocationOnMock)13 ChangeMap (org.apache.cayenne.commitlog.model.ChangeMap)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)10 SelectQuery (org.apache.cayenne.query.SelectQuery)10 ObjectChange (org.apache.cayenne.commitlog.model.ObjectChange)9