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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations