org.ektorp.support
Class CouchDbRepositorySupport<T>

java.lang.Object
  extended by org.ektorp.support.CouchDbRepositorySupport<T>
Type Parameters:
T -
All Implemented Interfaces:
GenericRepository<T>

public class CouchDbRepositorySupport<T>
extends Object
implements GenericRepository<T>

Provides "out of the box" CRUD functionality for sub classes. Note that this class will try to access the standard design document named according to this convention: _design/[repository type simple name] e.g. _design/Sofa if this repository's handled type is foo.bar.Sofa It is preferable that this design document must define a view named "all". The "all"-view should only return document id's that refer to documents that can be loaded as this repository's handled type.

Author:
henrik lundgren

Field Summary
static String AUTO_UPDATE_VIEW_ON_CHANGE
          System property key: org.ektorp.support.AutoUpdateViewOnChange
protected  CouchDbConnector db
           
protected static org.slf4j.Logger log
           
protected  String stdDesignDocumentId
           
protected  Class<T> type
           
 
Constructor Summary
protected CouchDbRepositorySupport(Class<T> type, CouchDbConnector db)
           
protected CouchDbRepositorySupport(Class<T> type, CouchDbConnector db, boolean createIfNotExists)
           
protected CouchDbRepositorySupport(Class<T> type, CouchDbConnector db, String designDocName)
          Alternative constructor allowing a custom design document name (not linked to the type class name)
 
Method Summary
 void add(T entity)
           
 boolean contains(String docId)
           
protected  ViewQuery createQuery(String viewName)
          Creates a ViewQuery pre-configured with correct dbPath, design document id and view name.
protected  void debugDesignDoc(DesignDocument generated)
           
 T get(String id)
           
 T get(String id, Options options)
           
 T get(String id, String rev)
          Deprecated. use get(String id, Options options)
 List<T> getAll()
          If the repository's design document has a view named "all" it will be used to fetch all documents of this repository's handled type.
protected  DesignDocumentFactory getDesignDocumentFactory()
           
 void initStandardDesignDocument()
           Will create the standard design document if it does not exists in the database.
protected  List<T> queryView(String viewName)
          Allows subclasses to query a view and load the result as the repository's handled type.
protected  List<T> queryView(String viewName, ComplexKey key)
          Allows subclasses to query views with simple String value keys and load the result as the repository's handled type.
protected  List<T> queryView(String viewName, int key)
          Allows subclasses to query views with simple String value keys and load the result as the repository's handled type.
protected  List<T> queryView(String viewName, String key)
          Allows subclasses to query views with simple String value keys and load the result as the repository's handled type.
 void remove(T entity)
           
 void setDesignDocumentFactory(DesignDocumentFactory df)
           
 void update(T entity)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AUTO_UPDATE_VIEW_ON_CHANGE

public static final String AUTO_UPDATE_VIEW_ON_CHANGE
System property key: org.ektorp.support.AutoUpdateViewOnChange

See Also:
Constant Field Values

log

protected static final org.slf4j.Logger log

db

protected final CouchDbConnector db

type

protected final Class<T> type

stdDesignDocumentId

protected final String stdDesignDocumentId
Constructor Detail

CouchDbRepositorySupport

protected CouchDbRepositorySupport(Class<T> type,
                                   CouchDbConnector db)

CouchDbRepositorySupport

protected CouchDbRepositorySupport(Class<T> type,
                                   CouchDbConnector db,
                                   boolean createIfNotExists)

CouchDbRepositorySupport

protected CouchDbRepositorySupport(Class<T> type,
                                   CouchDbConnector db,
                                   String designDocName)
Alternative constructor allowing a custom design document name (not linked to the type class name)

Parameters:
type -
db -
designDocName -
Method Detail

add

public void add(T entity)
Specified by:
add in interface GenericRepository<T>
Throws:
UpdateConflictException - if there was an update conflict.

getAll

public List<T> getAll()
If the repository's design document has a view named "all" it will be used to fetch all documents of this repository's handled type. "all" must return document ids that refers documents that are readable by this repository. If the "all"-view is not defined, all documents in the database (except design documents) will be fetched. In this case the database must only contain documents that are readable by this repository.

Specified by:
getAll in interface GenericRepository<T>
Returns:
all objects of this repository's handled type in the db.

get

public T get(String id)
Specified by:
get in interface GenericRepository<T>
Parameters:
id -
Returns:
Throws:
DocumentNotFoundException - if the document was not found.

get

public T get(String id,
             Options options)
Parameters:
id -
options -
Returns:
Throws:
DocumentNotFoundException - if the document was not found.

get

public T get(String id,
             String rev)
Deprecated. use get(String id, Options options)

Specified by:
get in interface GenericRepository<T>
Parameters:
id -
rev -
Returns:
Throws:
DocumentNotFoundException - if the document was not found.

remove

public void remove(T entity)
Specified by:
remove in interface GenericRepository<T>

update

public void update(T entity)
Specified by:
update in interface GenericRepository<T>
Throws:
UpdateConflictException - if there was an update conflict.

createQuery

protected ViewQuery createQuery(String viewName)
Creates a ViewQuery pre-configured with correct dbPath, design document id and view name.

Parameters:
viewName -
Returns:

queryView

protected List<T> queryView(String viewName,
                            String key)
Allows subclasses to query views with simple String value keys and load the result as the repository's handled type. The viewName must be defined in this repository's design document.

Parameters:
viewName -
key -
Returns:

queryView

protected List<T> queryView(String viewName,
                            int key)
Allows subclasses to query views with simple String value keys and load the result as the repository's handled type. The viewName must be defined in this repository's design document.

Parameters:
viewName -
keyValue -
Returns:

queryView

protected List<T> queryView(String viewName,
                            ComplexKey key)
Allows subclasses to query views with simple String value keys and load the result as the repository's handled type. The viewName must be defined in this repository's design document.

Parameters:
viewName -
key -
Returns:

queryView

protected List<T> queryView(String viewName)
Allows subclasses to query a view and load the result as the repository's handled type. The viewName must be defined in this repository's design document.

Parameters:
viewName -
Returns:

initStandardDesignDocument

public void initStandardDesignDocument()

Will create the standard design document if it does not exists in the database.

Will also generate view definitions for finder methods defined in this class and annotated by the @GenerateView annotation. The method name must adhere to the name convention of findBy[Property].

The method:


debugDesignDoc

protected void debugDesignDoc(DesignDocument generated)

contains

public boolean contains(String docId)
Specified by:
contains in interface GenericRepository<T>
Returns:
true if a document with the specified id exists in the database.

setDesignDocumentFactory

public void setDesignDocumentFactory(DesignDocumentFactory df)

getDesignDocumentFactory

protected DesignDocumentFactory getDesignDocumentFactory()


Copyright © 2011. All Rights Reserved.