Created
November 28, 2011 11:00
-
-
Save ripper234/1399990 to your computer and use it in GitHub Desktop.
An enhancement of ObjectType that supports filtering
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
- com.google.guava -> guava 10.0.1 |
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
package controllers; | |
import com.google.common.base.Predicate; | |
import com.google.common.collect.Collections2; | |
import play.db.jpa.Model; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.HashSet; | |
import java.util.List; | |
import static com.google.common.collect.Lists.newArrayList; | |
public class FilteredObjectType extends CRUD.ObjectType{ | |
private final Collection<ObjectField> filteredFields; | |
public FilteredObjectType(Class<? extends Model> modelClass, String ... filteredFieldNames) { | |
this(new CRUD.ObjectType(modelClass), filteredFieldNames); | |
} | |
public FilteredObjectType(CRUD.ObjectType objectType, final String ... filteredFieldNames) { | |
super(objectType.entityClass); | |
final HashSet<String> filteredFieldNamesSet = new HashSet<String>(Arrays.asList(filteredFieldNames)); | |
filteredFields = Collections2.filter(super.getFields(), new Predicate<ObjectField>() { | |
public boolean apply(ObjectField objectField) { | |
return !filteredFieldNamesSet.contains(objectField.name); | |
} | |
}); | |
} | |
@Override | |
public List<ObjectField> getFields() { | |
// defensive copy | |
return newArrayList(filteredFields); | |
} | |
} |
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
public static void show(long id) { | |
MyModel object = MyModel.findById(id); | |
CRUD.ObjectType type = new FilteredObjectType(MyModel.class, "filteredField1", "filteredField2"); | |
render(type, object); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment