cgeo/c-geo-opensource — Issue #1079: Better models and ORM
Author: SammysHP
Created: 2012-02-01 22:04:31+00:00
State: open
Labels: refactoring

Please look at the following code and give feedback. I summarized a few of my ideas for a better data-handling from the last weeks.

```
// Cache "GC23456" is already present int the database. This will load it.
Cache cache1 = new Cache("GC23456").load();

// Now we can access it in the normal way.
cache1.getName();
cache1.getCoordinates();

// We don't need it any more, so delete it.
cache1.delete();

// Please note: cache1 still has all data (we might store it agein later)
// If we don't need the object anymore, give it to the garbage collector.

// Well, now we'll load a cache, that was never opened before.
Cache cache2 = new Cache("GC2AB45").load();

// This will load the cache automatically with the connector/adapter
// (internally selected in the ORM-driver).
// It is automatically added to an internal cache (not the one you're searching)

// Storing the cache is easy. Simply call
cache2.save();

// Now it's stored in the default list. But what, if we want another list?
cache2.setListId(22);
cache2.save();

// A big problem before was storing of whole lists of caches. This took ages
// because we stored every cache with it's own request. Each request is very
// expensive (remember: it's sqlite!).
CacheList list = new CacheList();
list.add(cache1);
list.add(cache2);
list.save();

// That's all! CacheList.save() uses e.g. prepared statements in the ORM-backend
// to merge all contained caches into a single request.

// Also mass-operations are possible. Image you want to add a list of caches to
// a specific list.
list.setListId(10); // calls the method for every contained cache
list.save();

// Please note: CacheList is _not_ the type of list that the user selects in
// c:geo (it's only a data model), but it can be used for one.

// Maybe we can also add lazy evaluation, so that only the first 20 caches are
// loaded at the beginning and the remaining ones when they're used.
```