use of org.apache.cayenne.Persistent in project cayenne by apache.
the class DataContextEJBQLOrderByIT method testOrderByPath.
@Test
public void testOrderByPath() throws Exception {
createTwoArtistsTwoPaintings();
String ejbql1 = "SELECT p FROM Painting p ORDER BY p.toArtist.artistName ASC";
EJBQLQuery query1 = new EJBQLQuery(ejbql1);
List<?> results1 = context.performQuery(query1);
assertEquals(2, results1.size());
assertEquals(33005, Cayenne.intPKForObject((Persistent) results1.get(0)));
assertEquals(33006, Cayenne.intPKForObject((Persistent) results1.get(1)));
String ejbql2 = "SELECT p FROM Painting p ORDER BY p.toArtist.artistName DESC";
EJBQLQuery query2 = new EJBQLQuery(ejbql2);
List<?> results2 = context.performQuery(query2);
assertEquals(2, results2.size());
assertEquals(33006, Cayenne.intPKForObject((Persistent) results2.get(0)));
assertEquals(33005, Cayenne.intPKForObject((Persistent) results2.get(1)));
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class DeepMergeOperation method merge.
private <T extends Persistent> T merge(final T peerInParentContext, ClassDescriptor descriptor, final Map<ObjectId, Persistent> seen) {
ObjectId id = peerInParentContext.getObjectId();
// sanity check
if (id == null) {
throw new CayenneRuntimeException("Server returned an object without an id: %s", peerInParentContext);
}
Persistent seenTarget = seen.get(id);
if (seenTarget != null) {
return (T) seenTarget;
}
final T target = shallowMergeOperation.merge(peerInParentContext);
seen.put(id, target);
descriptor = descriptor.getSubclassDescriptor(peerInParentContext.getClass());
descriptor.visitProperties(new PropertyVisitor() {
public boolean visitToOne(ToOneProperty property) {
if (!property.isFault(peerInParentContext)) {
Persistent destinationSource = (Persistent) property.readProperty(peerInParentContext);
Object destinationTarget = destinationSource != null ? merge(destinationSource, property.getTargetDescriptor(), seen) : null;
Object oldTarget = property.isFault(target) ? null : property.readProperty(target);
property.writePropertyDirectly(target, oldTarget, destinationTarget);
}
return true;
}
public boolean visitToMany(ToManyProperty property) {
if (!property.isFault(peerInParentContext)) {
Object value = property.readProperty(peerInParentContext);
Object targetValue;
if (property instanceof ToManyMapProperty) {
Map<?, ?> map = (Map) value;
Map targetMap = new HashMap();
for (Entry entry : map.entrySet()) {
Object destinationSource = entry.getValue();
Object destinationTarget = destinationSource != null ? merge((Persistent) destinationSource, property.getTargetDescriptor(), seen) : null;
targetMap.put(entry.getKey(), destinationTarget);
}
targetValue = targetMap;
} else {
Collection collection = (Collection) value;
Collection targetCollection = new ArrayList(collection.size());
for (Object destinationSource : collection) {
Object destinationTarget = destinationSource != null ? merge((Persistent) destinationSource, property.getTargetDescriptor(), seen) : null;
targetCollection.add(destinationTarget);
}
targetValue = targetCollection;
}
property.writePropertyDirectly(target, null, targetValue);
}
return true;
}
public boolean visitAttribute(AttributeProperty property) {
return true;
}
});
return target;
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class ChildDiffLoader method nodeRemoved.
public void nodeRemoved(Object nodeId) {
setExternalChange(Boolean.TRUE);
Persistent object = findObject(nodeId);
if (object != null) {
try {
context.deleteObjects(object);
} finally {
setExternalChange(Boolean.FALSE);
}
} else {
setExternalChange(Boolean.FALSE);
}
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class ChildDiffLoader method findObject.
protected Persistent findObject(Object nodeId) {
// first do a lookup in ObjectStore; if even a hollow object is found,
// return it;
// if not - fetch.
Persistent object = (Persistent) context.getGraphManager().getNode(nodeId);
if (object != null) {
return object;
}
ObjectId id = (ObjectId) nodeId;
// modified
if (id.isTemporary()) {
return null;
}
// skip context cache lookup, go directly to its channel
Query query = new ObjectIdQuery((ObjectId) nodeId);
QueryResponse response = context.getChannel().onQuery(context, query);
List<?> objects = response.firstList();
if (objects.size() == 0) {
throw new CayenneRuntimeException("No object for ID exists: %s", nodeId);
} else if (objects.size() > 1) {
throw new CayenneRuntimeException("Expected zero or one object, instead query matched: %d", objects.size());
}
return (Persistent) objects.get(0);
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class DataDomainProviderTest method testGet.
@Test
public void testGet() {
// create dependencies
final String testConfigName = "testConfig";
final DataChannelDescriptor testDescriptor = new DataChannelDescriptor();
DataMap map1 = new DataMap("map1");
testDescriptor.getDataMaps().add(map1);
DataMap map2 = new DataMap("map2");
testDescriptor.getDataMaps().add(map2);
DataNodeDescriptor nodeDescriptor1 = new DataNodeDescriptor();
nodeDescriptor1.setName("node1");
nodeDescriptor1.getDataMapNames().add("map1");
nodeDescriptor1.setAdapterType(OracleAdapter.class.getName());
nodeDescriptor1.setDataSourceFactoryType(MockDataSourceFactory.class.getName());
nodeDescriptor1.setParameters("jdbc/testDataNode1");
nodeDescriptor1.setSchemaUpdateStrategyType(ThrowOnPartialOrCreateSchemaStrategy.class.getName());
testDescriptor.getNodeDescriptors().add(nodeDescriptor1);
DataNodeDescriptor nodeDescriptor2 = new DataNodeDescriptor();
nodeDescriptor2.setName("node2");
nodeDescriptor2.getDataMapNames().add("map2");
nodeDescriptor2.setParameters("testDataNode2.driver.xml");
testDescriptor.getNodeDescriptors().add(nodeDescriptor2);
final DataChannelDescriptorLoader testLoader = new DataChannelDescriptorLoader() {
@Override
public ConfigurationTree<DataChannelDescriptor> load(Resource configurationResource) throws ConfigurationException {
return new ConfigurationTree<>(testDescriptor, null);
}
};
final EventManager eventManager = new MockEventManager();
final TestListener mockListener = new TestListener();
Module testModule = binder -> {
final ClassLoaderManager classLoaderManager = new DefaultClassLoaderManager();
binder.bind(ClassLoaderManager.class).toInstance(classLoaderManager);
binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
ServerModule.contributeProperties(binder);
ServerModule.contributeAdapterDetectors(binder).add(FirebirdSniffer.class).add(OpenBaseSniffer.class).add(FrontBaseSniffer.class).add(IngresSniffer.class).add(SQLiteSniffer.class).add(DB2Sniffer.class).add(H2Sniffer.class).add(HSQLDBSniffer.class).add(SybaseSniffer.class).add(DerbySniffer.class).add(SQLServerSniffer.class).add(OracleSniffer.class).add(PostgresSniffer.class).add(MySQLSniffer.class);
ServerModule.contributeDomainFilters(binder);
ServerModule.contributeDomainListeners(binder).add(mockListener);
ServerModule.contributeProjectLocations(binder).add(testConfigName);
// configure extended types
ServerModule.contributeDefaultTypes(binder);
ServerModule.contributeUserTypes(binder);
ServerModule.contributeTypeFactories(binder);
binder.bind(EventManager.class).toInstance(eventManager);
binder.bind(EntitySorter.class).toInstance(new AshwoodEntitySorter());
binder.bind(SchemaUpdateStrategyFactory.class).to(DefaultSchemaUpdateStrategyFactory.class);
final ResourceLocator locator = new ClassLoaderResourceLocator(classLoaderManager) {
public Collection<Resource> findResources(String name) {
// if this is the request we are getting, just let it go through..
if (name.endsWith("types.xml")) {
return super.findResources(name);
}
assertEquals(testConfigName, name);
return Collections.<Resource>singleton(new MockResource());
}
};
binder.bind(ResourceLocator.class).toInstance(locator);
binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR)).toInstance(locator);
binder.bind(ConfigurationNameMapper.class).to(DefaultConfigurationNameMapper.class);
binder.bind(DataChannelDescriptorMerger.class).to(DefaultDataChannelDescriptorMerger.class);
binder.bind(DataChannelDescriptorLoader.class).toInstance(testLoader);
binder.bind(DbAdapterFactory.class).to(DefaultDbAdapterFactory.class);
binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
binder.bind(BatchTranslatorFactory.class).to(DefaultBatchTranslatorFactory.class);
binder.bind(SelectTranslatorFactory.class).to(DefaultSelectTranslatorFactory.class);
binder.bind(DataSourceFactory.class).toInstance(new MockDataSourceFactory());
binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
binder.bind(QueryCache.class).toInstance(mock(QueryCache.class));
binder.bind(RowReaderFactory.class).toInstance(mock(RowReaderFactory.class));
binder.bind(DataNodeFactory.class).to(DefaultDataNodeFactory.class);
binder.bind(SQLTemplateProcessor.class).toInstance(mock(SQLTemplateProcessor.class));
binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
ServerModule.contributeValueObjectTypes(binder);
binder.bind(ValueObjectTypeRegistry.class).to(DefaultValueObjectTypeRegistry.class);
};
Injector injector = DIBootstrap.createInjector(testModule);
// create and initialize provide instance to test
DataDomainProvider provider = new DataDomainProvider();
injector.injectMembers(provider);
DataChannel channel = provider.get();
assertNotNull(channel);
assertTrue(channel instanceof DataDomain);
DataDomain domain = (DataDomain) channel;
assertSame(eventManager, domain.getEventManager());
assertEquals(2, domain.getDataMaps().size());
assertTrue(domain.getDataMaps().contains(map1));
assertTrue(domain.getDataMaps().contains(map2));
assertEquals(2, domain.getDataNodes().size());
DataNode node1 = domain.getDataNode("node1");
assertNotNull(node1);
assertEquals(1, node1.getDataMaps().size());
assertSame(map1, node1.getDataMaps().iterator().next());
assertSame(node1, domain.lookupDataNode(map1));
assertEquals(nodeDescriptor1.getDataSourceFactoryType(), node1.getDataSourceFactory());
assertNotNull(node1.getDataSource());
assertNotNull(node1.getSchemaUpdateStrategy());
assertEquals(nodeDescriptor1.getSchemaUpdateStrategyType(), node1.getSchemaUpdateStrategy().getClass().getName());
assertNotNull(node1.getAdapter());
assertEquals(OracleAdapter.class, node1.getAdapter().getClass());
DataNode node2 = domain.getDataNode("node2");
assertNotNull(node2);
assertEquals(1, node2.getDataMaps().size());
assertSame(map2, node2.getDataMaps().iterator().next());
assertSame(node2, domain.lookupDataNode(map2));
assertNull(node2.getDataSourceFactory());
assertNotNull(node2.getDataSource());
assertNotNull(node2.getSchemaUpdateStrategy());
assertEquals(SkipSchemaUpdateStrategy.class.getName(), node2.getSchemaUpdateStrategy().getClass().getName());
assertNotNull(node2.getAdapter());
// check that we have mock listener passed correctly
Persistent mockPersistent = mock(Persistent.class);
ObjectId mockObjectId = mock(ObjectId.class);
when(mockObjectId.getEntityName()).thenReturn("mock-entity-name");
when(mockPersistent.getObjectId()).thenReturn(mockObjectId);
domain.getEntityResolver().getCallbackRegistry().performCallbacks(LifecycleEvent.POST_LOAD, mockPersistent);
assertEquals("Should call postLoadCallback() method", 1, TestListener.counter.get());
}
Aggregations