DataModelScanner modelScanner = new DataModelScanner();
modelScanner.withCaffinitasPersistenceXml();
modelScanner.getFilterBuilder().includePackage(Demo.class.getPackage().getName());
DataModel model = modelScanner.scan();
PersistenceManager persistenceManager = PersistenceManagerBuilder.newBuilder().
withModel(model).
withCluster(cluster).
withDefaultKeyspace("default_ks").
withMappedKeyspace("lookup_KS_NAME", "demo").
build();
try
{
SchemaGenerator schemaGenerator = persistenceManager.createSchemaGenerator();
System.out.println("Executing CQL DDL statements...");
System.out.println();
for (String stmt : schemaGenerator.generateLiveAlterDDL().getStatements())
{
System.out.println(stmt);
}
CqlStatementList cqlDDL = schemaGenerator.generateLiveAlterDDL();
cqlDDL.execute(persistenceManager.driverSession());
System.out.println("Wait until all schema objects are available...");
System.out.println();
persistenceManager.refreshSchema(); // This is necessary until schema changes are propagated from the Java Driver to Caffinitas Mappe automatically!
schemaGenerator.forSchemaObjectsAvailable().awaitUninterruptibly();
PersistenceSession session = persistenceManager.createSession();
try
{
// create a user object and persist it
UserEntity snazy = new UserEntity();
snazy.setUsername("snazy");
AddressType mainAddress = new AddressType(null, null, "Koeln", "Germany");
snazy.setMainAddress(mainAddress);
snazy.getOtherAddresses().put(AddressKind.HOME, mainAddress);
session.insert(snazy);
// now load the user object
UserEntity loaded = session.loadOne(UserEntity.class, "snazy");
System.out.printf("got user record for %s %n", loaded.getUsername());
//
//
//
// Setup a project...
ProjectEntity project = new ProjectEntity();
project.setId(UUID.randomUUID());
project.setName("caffinitas");
project.setLicense(License.APACHE_V2);
project.getAuthors().add("snazy");
project.getContributors().add("snazy");
project.setProjectURL(new URL("http://caffinitas.org/"));
AggregatorModuleEntity parentModule = new AggregatorModuleEntity();
parentModule.setProjectId(project.getId());
parentModule.setName("caffinitas-parent");
CodeModuleEntity moduleCore = new CodeModuleEntity();
moduleCore.setProjectId(project.getId());
moduleCore.setName("caffinitas-core");
moduleCore.setLanguage(Language.JAVA);
CodeModuleEntity moduleDemo = new CodeModuleEntity();
moduleDemo.setProjectId(project.getId());
moduleDemo.setName("caffinitas-demo");
moduleDemo.setLanguage(Language.JAVA);
CodeModuleEntity moduleTest = new CodeModuleEntity();
moduleTest.setProjectId(project.getId());
moduleTest.setName("caffinitas-test");
moduleTest.setLanguage(Language.JAVA);
parentModule.getModules().add("caffinitas-core");
parentModule.getModules().add("caffinitas-demo");
parentModule.getModules().add("caffinitas-test");
ModifyFuture<ProjectEntity> projectPersistFuture = session.insertAsync(project);
ModifyFuture<AggregatorModuleEntity> parentModulePersistFuture = session.insertAsync(parentModule);
ModifyFuture<CodeModuleEntity> moduleCorePersistFuture = session.insertAsync(moduleCore);
ModifyFuture<CodeModuleEntity> moduleDemoPersistFuture = session.insertAsync(moduleDemo);
ModifyFuture<CodeModuleEntity> moduleTestPersistFuture = session.insertAsync(moduleTest);
ListenableFuture<List<Object>> combinedFuture =
Futures.allAsList(projectPersistFuture, parentModulePersistFuture,
moduleCorePersistFuture, moduleDemoPersistFuture, moduleTestPersistFuture);
Uninterruptibles.getUninterruptibly(combinedFuture);
//
project = session.loadOne(ProjectEntity.class, project.getId());
// load all modules (since projectId is the partition key in ModuleEntity we can load it with one SELECT)
List<ModuleEntity> modules = session.loadMultiple(ModuleEntity.class, project.getId());
for (ModuleEntity module : modules)
{
System.out.println(" " + module.getName());
}
}
finally
{
session.close();
}
}
finally
{
persistenceManager.close();
}
}