Skip to content

Commit

Permalink
remove findById method
Browse files Browse the repository at this point in the history
  • Loading branch information
fliptoo committed Jun 27, 2016
1 parent d0c2d1b commit 9e3b1dc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 48 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ lazy val core = project
),
scalaVersion := "2.11.8",
name := "playjpa",
version := "1.0.1",
version := "1.0.2",
organization := "com.fliptoo",
autoScalaLibrary := false,
crossPaths := false
Expand All @@ -30,7 +30,7 @@ lazy val plugin = project
.dependsOn(core)
.settings(
name := "sbt-playjpa",
version := "1.0.1",
version := "1.0.2",
organization := "com.fliptoo",
sbtPlugin := true,
autoScalaLibrary := false,
Expand Down
29 changes: 9 additions & 20 deletions playjpa/src/main/java/com/fliptoo/playjpa/Blob.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
package com.fliptoo.playjpa;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.StringType;
import org.hibernate.usertype.UserType;

import play.Application;

import javax.inject.Inject;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

public class Blob implements UserType {

@Inject
Application app;

private String UUID;
private String type;
private String folder;
Expand Down Expand Up @@ -171,15 +160,15 @@ public Object replace(Object o, Object o1, Object o2) throws HibernateException
}

public File getStore(String folder) {
String name = app.configuration().getString("playJPA.attachments.path");
String name = PlayJpa.app.configuration().getString("playJPA.attachments.path");
if (StringUtils.isEmpty(name)) name = "data/attachments";
if (folder != null && folder.length() > 0)
name += File.separator + folder;
File store;
if (new File(name).isAbsolute()) {
store = new File(name);
} else {
store = app.getFile(name);
store = PlayJpa.app.getFile(name);
}
if (!store.exists()) {
store.mkdirs();
Expand Down
24 changes: 12 additions & 12 deletions playjpa/src/main/java/com/fliptoo/playjpa/JPQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@ public static List findAll(String entity) {
return PlayJpa.jpaApi.em().createQuery("select e from " + entity + " e").getResultList();
}

public static Model findById(String entity, Object id) throws Exception {
return PlayJpa.jpaApi.em().find((Class<Model>) PlayJpa.app.classloader().loadClass(entity), id);
public static <T extends Model> T findById(String entity, Object id) throws Exception {
return (T) PlayJpa.jpaApi.em().find(PlayJpa.app.classloader().loadClass(entity), id);
}

public static <T extends Model> T findOneBy(String entity, String query, Object... params) {
Query q = PlayJpa.jpaApi.em().createQuery(
createFindByQuery(entity, entity, query, params));
List results = bindParameters(q, params).getResultList();
if (results.size() == 0) {
return null;
}
return (T) results.get(0);
}

public static List findBy(String entity, String query, Object[] params) {
Expand Down Expand Up @@ -62,16 +72,6 @@ public static int deleteAll(String entity) {
return bindParameters(q).executeUpdate();
}

public static Model findOneBy(String entity, String query, Object[] params) {
Query q = PlayJpa.jpaApi.em().createQuery(
createFindByQuery(entity, entity, query, params));
List results = bindParameters(q, params).getResultList();
if (results.size() == 0) {
return null;
}
return (Model) results.get(0);
}

public static String createFindByQuery(String entityName, String entityClass, String query, Object... params) {
if (query == null || query.trim().length() == 0) {
return "from " + entityName;
Expand Down
13 changes: 1 addition & 12 deletions playjpa/src/main/java/com/fliptoo/playjpa/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

public class Model {

public boolean deleted;

/**
* store (ie insert) the entity.
*/
Expand Down Expand Up @@ -77,22 +75,13 @@ public static <T extends Model> List<T> findAll() {
throw new UnsupportedOperationException("Please annotate your JPA model with @javax.persistence.Entity annotation.");
}

/**
* Find the entity with the corresponding id.
* @param id The entity id
* @return The entity
*/
public static <T extends Model> T findById(Object id) {
throw new UnsupportedOperationException("Please annotate your JPA model with @javax.persistence.Entity annotation.");
}

/**
* Prepare a query to find one entity.
* @param query HQL query or shortcut
* @param params Params to bind to the query
* @return The entity
*/
public static <T extends Model> T findOneBy(String query, Object[] params) {
public static <T extends Model> T findOneBy(String query, Object... params) {
throw new UnsupportedOperationException("Please annotate your JPA model with @javax.persistence.Entity annotation.");
}

Expand Down
4 changes: 2 additions & 2 deletions playjpa/src/main/java/com/fliptoo/playjpa/PlayJpa.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ public void enhance(CtClass cc, String Entity, String Model, String JPAQuery, St
ce.printStackTrace();
}
}

makeMethod("public static long count() { return " + JPQL + ".count(\"" + Entity + "\"); }", cc);
makeMethod("public static long count(String query, Object[] params) { return " + JPQL + ".count(\"" + Entity + "\", query, params); }", cc);
makeMethod("public static java.util.List findAll() { return " + JPQL + ".findAll(\"" + Entity + "\"); }", cc);
makeMethod("public static " + Model + " findById(Object id) { return " + JPQL + ".findById(\"" + Entity + "\", id); }", cc);
makeMethod("public static " + Model + " findOneBy(String query, Object[] params) { return " + JPQL + ".findOneBy(\"" + Entity + "\", query, params); }", cc);
makeMethod("public static " + JPAQuery + " find(String query, Object[] params) { return " + JPQL + ".find(\"" + Entity + "\", query, params); }", cc);
makeMethod("public static " + JPAQuery + " find() { return " + JPQL + ".find(\"" + Entity + "\"); }", cc);
makeMethod("public static " + JPAQuery + " all() { return " + JPQL + ".all(\"" + Entity + "\"); }", cc);
makeMethod("public static int delete(String query, Object[] params) { return " + JPQL + ".delete(\"" + Entity + "\", query, params); }", cc);
makeMethod("public static int deleteAll() { return " + JPQL + ".deleteAll(\"" + Entity + "\"); }", cc);
makeMethod("public static " + Model + " findOneBy(String query, Object[] params) { return " + JPQL + ".findOneBy(\"" + Entity + "\", query, params); }", cc);
}
});
}
Expand Down

0 comments on commit 9e3b1dc

Please sign in to comment.