From 379cc39e385c4f9e20b55e18db521a47daa2266d Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Fri, 28 Aug 2015 07:52:46 +1200 Subject: [PATCH] Initial add of Finder --- src/main/java/com/avaje/ebean/Finder.java | 185 ++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 src/main/java/com/avaje/ebean/Finder.java diff --git a/src/main/java/com/avaje/ebean/Finder.java b/src/main/java/com/avaje/ebean/Finder.java new file mode 100644 index 000000000..52dcfa86b --- /dev/null +++ b/src/main/java/com/avaje/ebean/Finder.java @@ -0,0 +1,185 @@ +package com.avaje.ebean; + +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +/** + * Intended to be used as a base class for 'Finder' implementations that can then + * be injected or used as public static fields on the associated entity bean. + *

+ * These 'finders' are a place to organise all the finder methods for that bean type + * and specific finder methods are expected to be added (find by unique properties etc). + *

+ * + *

Testing

+ *

+ * For testing the mocki-ebean project has the ability to replace the finder implementation + * + *

+ *
{@code
+ *
+ * public class CustomerFinder extends Finder {
+ *
+ *   public CustomerFinder() {
+ *     super(Customer.class);
+ *   }
+ *
+ *   // Add your customer finder methods ...
+ *
+ *   public Customer byName(String name) {
+ *     return query().eq("name", name).findUnique();
+ *   }
+ *
+ *   public List findNew() {
+ *     return query()
+ *       .eq("status", Customer.Status.NEW)
+ *       .orderBy("name")
+ *       .findList()
+ *   }
+ * }
+ *
+ * @Entity
+ * public class Customer extends BaseModel {
+ *
+ *   public static final CustomerFinder find = new CustomerFinder();
+ *   ...
+ *
+ * }
+ */ +public class Finder { + + /** + * The entity bean type. + */ + private final Class type; + + /** + * The name of the EbeanServer, null for the default server. + */ + private final String serverName; + + /** + * Create with the type of the entity bean. + * + *
{@code
+   *
+   * public class CustomerFinder extends Finder {
+   *
+   *   public CustomerFinder() {
+   *     super(Customer.class);
+   *   }
+   *
+   *   // ... add extra customer specific finder methods
+   * }
+   *
+   * @Entity
+   * public class Customer extends BaseModel {
+   *
+   *   public static final CustomerFinder find = new CustomerFinder();
+   *   ...
+   *
+   * }
+ */ + public Finder(Class type) { + this.serverName = null; + this.type = type; + } + + /** + * Create with the type of the entity bean and specific server name. + */ + public Finder(String serverName, Class type) { + this.serverName = serverName; + this.type = type; + } + + /** + * Return the underlying 'default' EbeanServer. + * + *

+ * This provides full access to the API such as explicit transaction demarcation etc. + * + */ + public EbeanServer db() { + return Ebean.getServer(serverName); + } + + /** + * Return typically a different EbeanServer to the default. + *

+ * This is equivalent to {@link Ebean#getServer(String)} + * + * @param server + * The name of the EbeanServer. If this is null then the default EbeanServer is + * returned. + */ + public EbeanServer db(String server) { + return Ebean.getServer(server); + } + + /** + * Creates an entity reference for this ID. + * + *

+ * Equivalent to {@link EbeanServer#getReference(Class, Object)} + */ + public T ref(I id) { + return db().getReference(type, id); + } + + /** + * Retrieves an entity by ID. + * + *

+ * Equivalent to {@link EbeanServer#find(Class, Object)} + */ + @Nullable + public T byId(I id) { + return db().find(type, id); + } + + /** + * Delete a bean by Id. + *

+ * Equivalent to {@link EbeanServer#delete(Class, Object)} + */ + public void deleteById(I id) { + db().delete(type, id); + } + + /** + * Retrieves all entities of the given type. + */ + public List all() { + return query().findList(); + } + + /** + * Creates a query. + *

+ * Equivalent to {@link EbeanServer#find(Class)} + */ + public Query query() { + return db().find(type); + } + +// /** +// * Creates a Finder for the named EbeanServer. +// * +// *

+// * Create and return a new Finder for a different server. +// */ +// public Finder on(String server) { +// return new Finder(server, type); +// } + +// /** +// * Create and return a new query using the OQL. +// *

+// * Equivalent to {@link EbeanServer#createQuery(Class, String)} +// */ +// public Query createQuery(String oql) { +// return db().createQuery(type, oql); +// } +}