To complement previous post, some more "generic" finders we have defined. The 
first one, "executeQuery", would be just enough for passing the JDO query :

public abstract class AbstractXMSDomainObjectRepositoryAndFactory<DO extends 
AbstractXMSDomainObject> extends AbstractFactoryAndRepository {

    @Deprecated
    @Programmatic
    public Object executeQuery(final String query) {

        // Ensure any newly persisted objects are saved to the object store
        // before executing the query.
        this.getContainer().flush();

        final Query jdoQuery = 
this.isisJdoSupport.getJdoPersistenceManager().newQuery(query);

        return jdoQuery.execute();

        // TODO Added without testing.
        // jdoQuery.closeAll();
    }



    @Programmatic
    public DO findByDatabaseId(final Class<T> entityClass, final String 
databaseId) {

        // Ensure any newly persisted objects are saved to the object store
        // before executing the query.
        this.getContainer().flush();

        return 
this.isisJdoSupport.getJdoPersistenceManager().getObjectById(entityClass, 
databaseId);

    }



   

    @Override
    protected <T> T persist(final T transientDomainObject) {

        // Ensure the Domain Object is persisted in a valid state.
        this.validate(transientDomainObject);

        return super.persist(transientDomainObject);
    }


   ...

}


And with the last method, "persist", we are overriding the Isis implementation 
to "force" that all objects are validated before persisting.


HTH,

Oscar






El 27/08/2013, a las 19:14, GESCONSULTOR - Óscar Bou <[email protected]> 
escribió:

> Hi Jeremy,
> 
> If you want to execute queries by means of JDO, you can inject the 
> IsisJdoSupport service, as of:
> 
> 
>    // {{ injected: IsisJdoSupport
>    protected IsisJdoSupport isisJdoSupport;
> 
>    public void injectIsisJdoSupport(final IsisJdoSupport isisJdoSupport) {
>        this.isisJdoSupport = isisJdoSupport;
>    }
> 
>    // }}
> 
> 
> After that, you can use the service to execute queries against the database. 
> As an example, one "generic" finder method is this one:
> 
>  @SuppressWarnings("unchecked")
>    @Programmatic
>    private <S extends AbstractXMSDomainObject> List<S> doFindByProp(final 
> Class<S> clazz, final String whereClause, final Integer firstResult, final 
> Integer maxResults, final String orderClause) {
> 
>        // Ensure any newly persisted objects are saved to the object store
>        // before executing the query.
>        this.getContainer().flush();
> 
>        // See this for examples:
>        // 
> http://www.datanucleus.org/products/accessplatform_2_1/jdo/query_api.html
>        final Query query = 
> this.isisJdoSupport.getJdoPersistenceManager().newQuery(clazz);
> 
>        // Always add the Tenant filter.
>        String filter = String.format("tenantId == '%s'", 
> this.userAccount.authenticatedUserTenantId());
>        if ((whereClause != null) && (whereClause.trim().length() != 0)) {
>            filter = filter.concat(" && ").concat(whereClause);
>        }
>        query.setFilter(filter);
> 
>        query.setRange(this.firstResultToLong(firstResult), 
> this.maxResultsToLong(maxResults));
>        final List<S> result = (List<S>) query.execute();
>        return result;
>    }
> 
> 
> HTH,
> 
> Oscar
> 
> 
> 
> 
> El 27/08/2013, a las 18:50, Jeremy Gurr <[email protected]> escribió:
> 
>> Hi Oscar,
>> 
>> That is nice to know, but I'm looking more for a direct sql lookup of a 
>> given entity. I don't want to have to pull the list of all objects from my 
>> database and iterate through them, since that will be much slower than a 
>> simple sql statement with a where clause. I can't just do it with a prepared 
>> statement since I will be searching on an arbitrary number of fields. I 
>> could just compose a sql statement myself directly, but I'd prefer to use 
>> the JDO framework and am wondering how to best access it through the isis 
>> utilities (I'm guessing the DomainObjectContainer?)
>> 
>> thanks,
>> Jeremy
>> 
>>> Hi, Jeremy.
>>> 
>>> In my case, the equivalent code in my domain would be:
>>> 
>>>  @Then("^.*the \"([^\"]*)\" risk register's risks collection should 
>>> contain:$")
>>>  public void 
>>> then_riskregisters_risks_collection_should_contain(@Transform(RiskSpecTransformer.RiskRegister.class)
>>>  final RiskRegister riskRegister, final List<RiskDesc> listOfExpecteds) 
>>> throws Throwable {
>>> 
>>>      this.nextTransaction();
>>> 
>>>      final SortedSet<Risk> risks = riskRegister.getRisks();
>>>      final ArrayList<Risk> risksList = Lists.newArrayList(risks);
>>>      assertTableEquals(listOfExpecteds, risksList);
>>> 
>>>  }
>>> 
>>> 
>>> Where the "assertTableEquals(xxx)" method is implemented on the 
>>> "CukeGlueAbstract" class and compares by name each field on the spec, with 
>>> the proper one on the class. Is this what you want to achieve?
>>> 
>>> 
>>> Regards,
>>> 
>>> Oscar
>>> 
>> 
> 

Reply via email to