Description
The Db4o plugin enables lightweight access to database functionality using db4o. This plugin does NOT provide domain classes nor dynamic finders like GORM does.
Installation
The current version of griffon-db4o is 0.5
To install just issue the following command
| Code Block |
|---|
griffon install-plugin db4o |
Usage
Upon installation the plugin will generate the following artifacts at $appdir/griffon-app/conf:
- Db4oConfig.groovy - contains the datasource definition.
- BootstrapDb4o.groovy - defines init/destroy hooks for data to be manipulated during app startup/shutdown.
A new dynamic method named withDb4o will be injected into all controllers, giving you access to a com.db4o.ObjectContainer object, with which you'll be able to make calls to the database. Remember to make all calls to the database off the EDT otherwise your application may appear unresponsive when doing long computations inside the EDT.
This method is aware of multiple db4os. If no db4oName is specified when calling it then the default dataSource will be selected. Here are two example usages, the first queries against the default db4o while the second queries a db4o whose name has been configured as 'internal'
| Code Block |
|---|
package sample
class SampleController {
def queryAllDataSources = {
withDb4o { dsName, db -> ... }
withDb4o('internal') { dsName, db -> ... }
}
}
|
This method is also accessible to any component through the singleton griffon.plugins.db4o.Db4oConnector. You can inject these methods to non-artifacts via metaclasses. Simply grab hold of a particular metaclass and call Db4oConnector.enhance(metaClassInstance).
Configuration
Dynamic method injection
The withDb4o() dynamic method will be added to controllers by default. You can change this setting by adding a configuration flag in Config.groovy
| Code Block |
|---|
griffon.db4o.injectInto = ["controller", "service"] |
DataSource
- dataSource.name - defines the name of the db file to use
- dataSource.delete - will delete the db file upon application shutdown if true
If the default configuration settings prove to be inadequate for your needs you still have a chance to tweak the configuration programmatically by implementing
| Code Block |
|---|
def configure(EmbeddedConfiguration configuration) {
// empty
}
|
Events
The following events will be triggered by this addon
- Db4oConnectStart[config, dataSourceName] - triggered before connecting to the objectContainer
- Db4oConnectEnd[dataSourceName, objectContainer] - triggered after connecting to the objectContainer
- Db4oDisconnectStart[config, dataSourceName, objectContainer] - triggered before disconnecting from the objectContainer
- Db4oDisconnectEnd[confing, dataSourceName] - triggered after disconnecting from the objectContainer
Multiple DataSources
The config file DataSource.groovy defines a default dataSource block. As the name implies this is the dataSource used by default, however you can configure named dataSources by adding a new config block. For example connecting to a dataSource whose name is 'internal' can be done in this way
| Code Block |
|---|
dataSources {
internal {
name = "internal-db"
}
}
|
This block can be used inside the environments() block in the same way as the default dataSources block is used.
Example
Follow these steps to create a simple application that displays data stored inside a database.
1. Create a new application named 'sample'
| Code Block |
|---|
griffon create-app sample |
| Info |
|---|
Following steps must be executed inside the application's directory |
2. Install the Db4o plugin
| Code Block |
|---|
griffon install-plugin db4o |
3. Create a bean class
| Code Block | ||
|---|---|---|
| ||
class Person {
int id
String name
String lastname
}
|
4. Setup bootstrapping data. Edit griffon-app/conf/BootstrapDb4o.groovy (We'll use the Griffon's team current roster, feel free to input your own data)
| Code Block |
|---|
class BootstrapDb4o {
def init = { dsName, db4o ->
db4o.store(new Person(id: 1, name: "Danno", lastname: "Ferrin"))
db4o.store(new Person(id: 2, name: "Andres", lastname: "Almiray"))
db4o.store(new Person(id: 3, name: "James", lastname: "Williams"))
db4o.store(new Person(id: 4, name: "Guillaume", lastname: "Laforge"))
db4o.store(new Person(id: 5, name: "Jim", lastname: "Shingler"))
db4o.store(new Person(id: 6, name: "Josh", lastname: "Reed"))
db4o.store(new Person(id: 7, name: "Hamlet", lastname: "D'Arcy"))
}
def destroy = { dsName, db4o ->
}
}
|
5. Download a copy of GlazedLists' latest stable release. Place it under $appdir/lib. Or install the Glazedlists Plugin.
6. Edit griffon-app/models/SampleModel.groovy by adding a personList property.
| Code Block |
|---|
import groovy.beans.Bindable
import ca.odell.glazedlists.EventList
import ca.odell.glazedlists.BasicEventList
import ca.odell.glazedlists.SortedList
class SampleModel {
EventList personsList = new SortedList(new BasicEventList(),
{a, b -> a.id <=> b.id} as Comparator)
}
|
GlazedLists' EventList simplifies working with JList, JTable and their models as you will soon find out.
7. Edit griffon-app/views/SampleView.groovy. We'll add a scrollPane and a table. The Table's model will be tied to model.personsList.
| Code Block |
|---|
import ca.odell.glazedlists.*
import ca.odell.glazedlists.gui.*
import ca.odell.glazedlists.swing.*
def createTableModel() {
def columnNames = ["Id", "Name", "Lastname"]
new EventTableModel(model.personsList, [
getColumnCount: {columnNames.size()},
getColumnName: {index -> columnNames[index]},
getColumnValue: {object, index ->
object."${columnNames[index].toLowerCase()}"
}] as TableFormat)
}
application(title: 'Db4o - sample',
size: [320,240],
locationByPlatform: true,
iconImage: imageIcon('/griffon-icon-48x48.png').image,
iconImages: [imageIcon('/griffon-icon-48x48.png').image,
imageIcon('/griffon-icon-32x32.png').image,
imageIcon('/griffon-icon-16x16.png').image]) {
scrollPane {
table(id: "personsTable", model: createTableModel())
new TableComparatorChooser(personsTable,
model.personsList, AbstractTableComparatorChooser.SINGLE_COLUMN)
}
}
|
If you installed the Glazedlists plugin then the view code can be simplified further
| Code Block |
|---|
application(title: 'Db4o',
size: [320, 240],
locationByPlatform: true,
iconImage: imageIcon('/griffon-icon-48x48.png').image,
iconImages: [imageIcon('/griffon-icon-48x48.png').image,
imageIcon('/griffon-icon-32x32.png').image,
imageIcon('/griffon-icon-16x16.png').image]) {
scrollPane {
table(id: 'personsTable') {
tableFormat = defaultTableFormat(columnNames: ['Id', 'Name', 'Lastname'])
eventTableModel(source: model.personsList, format: tableFormat)
installTableComparatorChooser(source: model.personsList)
}
}
}
|
8. Finally edit griffon-app/controllers/SampleController.groovy. The controller will react to an application event, load the data into a temporal List then update model.personsList inside the EDT. Application event handlers are guaranteed to run outside of the EDT.
| Code Block |
|---|
class SampleController {
def model
def onStartupEnd = { app ->
withDb4o { dsName, db4o ->
def tmpList = db4o.query(Person)
edt { model.personsList.addAll(tmpList) }
}
}
}
|
9. Run the application by typing
| Code Block |
|---|
griffon run-app |
Full source code for this sample application can be found at https://github.com/aalmiray/griffon_sample_apps/tree/master/persistence/db4o
History
Version | Date | Notes |
|---|---|---|
0.5 | 10-21-11 | Release sync with Griffon 0.9.4 |
0.4 | 07-27-10 | GRIFFON-385 Add configuration flag for optionally deleting db file |
0.3 | 11-08-10 | GRIFFON-258 Add pre/post trigger events |
0.2 | 07-22-10 | Release sync with Griffon 0.9 |
0.1 | 03-01-10 | Initial release |