forked from playframework/play1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[playframework#265] Adding the possibility to define an own ObjectType
by Annotation.
- Loading branch information
1 parent
c156923
commit 377ea40
Showing
16 changed files
with
618 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
samples-and-tests/just-test-cases/app/controllers/MyBookApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package controllers; | ||
|
||
import java.util.List; | ||
|
||
import models.MyBook; | ||
import play.mvc.Controller; | ||
|
||
public class MyBookApplication extends Controller { | ||
|
||
public static void index() { | ||
List<MyBook> testEntries = MyBook.all().fetch(); | ||
render(testEntries); | ||
} | ||
|
||
public static void edit(Long id) { | ||
MyBook testObj = MyBook.findById(id); | ||
notFoundIfNull(testObj); | ||
render(testObj); | ||
} | ||
|
||
|
||
public static void save(long id, MyBook testObj) { | ||
if (!testObj.isPersistent()) { | ||
notFound("The object Test with id "+ id + " wasn't found anymore!"); | ||
} | ||
if (testObj.validateAndSave()) { | ||
index(); | ||
} else { | ||
render("Application/edit.html", testObj); | ||
} | ||
} | ||
|
||
|
||
} |
16 changes: 16 additions & 0 deletions
16
samples-and-tests/just-test-cases/app/controllers/MyBookCustomCrud.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package controllers; | ||
|
||
import play.db.Model; | ||
import controllers.CRUD.ObjectType; | ||
import controllers.OptimisticLockingCRUD.CustomizableObjectType; | ||
import models.MyBook; | ||
|
||
@CRUD.For(MyBook.class) | ||
public class MyBookCustomCrud extends OptimisticLockingCRUD { | ||
public static CustomizableObjectType createObjectType(Class<? extends Model> entityClass) { | ||
final CustomizableObjectType type = OptimisticLockingCRUD.createObjectType(entityClass); | ||
type.defineBlankFields("excludedProperty", "text", "version"); | ||
type.defineShowFields("excludedProperty", "text", "version"); | ||
return type; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
samples-and-tests/just-test-cases/app/controllers/MyBooks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package controllers; | ||
|
||
import play.db.Model; | ||
import controllers.CRUD.ObjectType; | ||
import controllers.OptimisticLockingCRUD.CustomizableObjectType; | ||
import models.MyBook; | ||
|
||
public class MyBooks extends OptimisticLockingCRUD { | ||
public static CustomizableObjectType createObjectType(Class<? extends Model> entityClass) { | ||
final CustomizableObjectType type = OptimisticLockingCRUD.createObjectType(entityClass); | ||
type.addExcludedFields("excludedProperty", "notFound"); | ||
return type; | ||
} | ||
} |
138 changes: 138 additions & 0 deletions
138
samples-and-tests/just-test-cases/app/controllers/OptimisticLockingCRUD.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package controllers; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import play.Logger; | ||
import play.db.Model; | ||
import play.mvc.Http.Request; | ||
|
||
public class OptimisticLockingCRUD extends CRUD { | ||
|
||
protected static CustomizableObjectType createObjectType(Class<? extends Model> entityClass) { | ||
return new CustomizableObjectType(entityClass); | ||
} | ||
|
||
|
||
public static class CustomizableObjectType extends ObjectType { | ||
|
||
private Set<String> hiddenFieldNames = new HashSet<String>(); | ||
private Set<String> excludedFieldNames = new HashSet<String>(); | ||
|
||
private String[] showFieldNames = null; | ||
private String[] blankFieldNames = null; | ||
|
||
private Map<String, ObjectField> fieldMap = new HashMap<String, ObjectField>(); | ||
|
||
|
||
public CustomizableObjectType(Class<? extends Model> modelClass) { | ||
super(modelClass); | ||
hiddenFieldNames.add("version"); | ||
} | ||
|
||
public CustomizableObjectType(String modelClass) | ||
throws ClassNotFoundException { | ||
super(modelClass); | ||
} | ||
|
||
public void addHiddenFields(String... hiddenFields) { | ||
for (String hiddenField : hiddenFields) { | ||
hiddenFieldNames.add(hiddenField); | ||
} | ||
} | ||
|
||
public void addExcludedFields(String... excludedFields) { | ||
for (String excludedField : excludedFields) { | ||
excludedFieldNames.add(excludedField); | ||
} | ||
} | ||
|
||
public void defineShowFields(String... showFields) { | ||
showFieldNames = showFields; | ||
} | ||
|
||
public void defineBlankFields(String... blankFields) { | ||
blankFieldNames = blankFields; | ||
} | ||
|
||
@Override | ||
public List<ObjectField> getFields() { | ||
String methodName = Request.current().actionMethod; | ||
if ("blank".equals(methodName) && blankFieldNames != null) { | ||
return getFields(blankFieldNames); | ||
} else if ("show".equals(methodName) && showFieldNames != null) { | ||
return getFields(showFieldNames); | ||
} else { | ||
return getFieldsCommon(); | ||
} | ||
} | ||
|
||
|
||
|
||
private List <ObjectField> getFields(String[] fieldnameList) { | ||
if (fieldMap.isEmpty()) { | ||
final List <ObjectField> fields = getFieldsCommon(); | ||
for (ObjectField objectField : fields) { | ||
fieldMap.put(objectField.name, objectField); | ||
} | ||
} | ||
final List<ObjectField> result = new ArrayList<ObjectField>(fieldnameList.length); | ||
for (String fieldname : fieldnameList) { | ||
if (fieldMap.containsKey(fieldname)) { | ||
result.add(fieldMap.get(fieldname)); | ||
} else { | ||
Logger.warn("Unknown field with name >%s<", fieldname); | ||
} | ||
|
||
} | ||
return result; | ||
} | ||
|
||
private List<ObjectField> getFieldsCommon() { | ||
final List <ObjectField> fields = super.getFields(); | ||
final Set<String> hiddenFieldsNotChanged = new HashSet<String>(hiddenFieldNames); | ||
final Set<String> excludedFieldsNotChanged = new HashSet<String>(excludedFieldNames); | ||
final List<ObjectField> hiddenFields = new ArrayList<ObjectField>(); | ||
final List<ObjectField> normalFields = new ArrayList<ObjectField>(); | ||
for (Iterator iterator = fields.iterator(); iterator.hasNext() && | ||
(!hiddenFieldsNotChanged.isEmpty() || !excludedFieldsNotChanged.isEmpty());) { | ||
final ObjectField field = (ObjectField) iterator.next(); | ||
if (excludedFieldsNotChanged.remove(field.name)) { | ||
Logger.debug("ignore " + field.name); | ||
//Ignore this field. | ||
} else if (hiddenFieldsNotChanged.remove(field.name)) { | ||
Logger.debug("hidden " + field.name); | ||
field.type = "hidden"; | ||
hiddenFields.add(field); | ||
} else { | ||
Logger.debug("normal " + field.name); | ||
normalFields.add(field); | ||
} | ||
|
||
} | ||
hiddenFields.addAll(normalFields); | ||
if (!hiddenFieldsNotChanged.isEmpty()) { | ||
final StringBuilder message = new StringBuilder( | ||
"Not all hidden fields was found in model: "); | ||
for (String hiddenFieldName : hiddenFieldsNotChanged) { | ||
message.append(hiddenFieldName).append(", "); | ||
} | ||
Logger.warn(message.toString()); | ||
} | ||
if (!excludedFieldsNotChanged.isEmpty()) { | ||
final StringBuilder message = new StringBuilder( | ||
"Not all excluded fields was found in model: "); | ||
for (String excludedField : excludedFieldsNotChanged) { | ||
message.append(excludedField).append(", "); | ||
} | ||
Logger.warn(message.toString()); | ||
} | ||
return hiddenFields; | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
samples-and-tests/just-test-cases/app/controllers/admin/CustomAdminCompany.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package controllers.admin; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
|
||
import javax.persistence.Version; | ||
|
||
import models.Company; | ||
import play.db.Model; | ||
import play.exceptions.UnexpectedException; | ||
import controllers.CRUD; | ||
|
||
@CRUD.For(Company.class) | ||
public class CustomAdminCompany extends CRUD { | ||
protected static ObjectType createObjectType(Class<? extends Model> entityClass) { | ||
return new VersionObjectType(entityClass); | ||
} | ||
|
||
public static class VersionObjectType extends ObjectType { | ||
|
||
private final String versionColumn; | ||
|
||
public VersionObjectType(Class<? extends Model> modelClass) { | ||
super(modelClass); | ||
versionColumn = getVersionColumnName(modelClass); | ||
} | ||
|
||
private String getVersionColumnName(Class modelClass) { | ||
Class c = modelClass; | ||
try { | ||
while (!c.equals(Object.class)) { | ||
for (Field field : c.getDeclaredFields()) { | ||
if (field.isAnnotationPresent(Version.class)) { | ||
return field.getName(); | ||
} | ||
} | ||
c = c.getSuperclass(); | ||
} | ||
} catch (Exception e) { | ||
throw new UnexpectedException("Error while determining the object @Version for an object of type " + modelClass); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public List<ObjectField> getFields() { | ||
List<ObjectField> result = super.getFields(); | ||
for (ObjectField objectField : result) { | ||
if (objectField.name.equals(versionColumn)) { | ||
objectField.type = "hidden"; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
} | ||
|
||
} | ||
|
Oops, something went wrong.