0
votes

I have many different Kinds in my app, they are unrelated in the Datastore, but they share a common Java base class which helps me process them generically. (By generically I mean without regard to their kind, not in the Java 'generics' sense.)

Now I want to perform some tests on one entity from each kind, and I can't figure out how to do it.

I want to do something like this:

Class<? extends MyBaseUnit> cl = getNextKind();
MyBaseUnit bu = (MyBaseUnit) ofy().load().type( cl ).filter( ?? ).first().now();

I don't think there is any such thing as a null filter, and if I just remove the filter() call then first() returns a Ref and I can't seem to do much with that.

I guess I could use a filter of ("id >", 0) for all the kinds with a long id, but what would a similar meaningless filter be for the ones with a string name?

Or maybe there is a better way of doing this? My ideal would be to retrieve a different entity every time I run the test.

1

1 Answers

0
votes

In the end I did it the ugly way that I contemplated at the end of my question:

for (KindInfo ki: kinds) {
    BaseUnit bu = null;
    List<? extends BaseUnit> lbu = null;

    if (ki.usesLongKey()) {
        lbu = ofy().load().type( cl ).filter( "id !=", 7).limit(1).list();
    } else {
        lbu = ofy().load().type( cl ).filter( "kn !=", "barf35" ).limit(1).list();
    }
    if ((null == lbu) || (0 == lbu.size())) {
        Log.i( "No entities for type=" + cl.getName() );
    } else {
        bu = (BaseUnit) lbu.get(0);
        runTestsOnSampleEntity( bu );
    }
}

The filters are just made up values ("kn" is the attribute name used by all of my Kinds that use a string key name).

I initially tried to use a filter of ("id !=", 0") since 0 is not a valid id, but this caused a ""java.lang.IllegalArgumentException".