All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.agrest.cayenne.processor.select.ViaQueryResolver Maven / Gradle / Ivy

There is a newer version: 5.0.M19
Show newest version
package io.agrest.cayenne.processor.select;

import io.agrest.AgException;
import io.agrest.cayenne.persister.ICayennePersister;
import io.agrest.cayenne.processor.CayenneProcessor;
import io.agrest.cayenne.processor.ICayenneQueryAssembler;
import io.agrest.meta.AgEntity;
import io.agrest.resolver.BaseRootDataResolver;
import io.agrest.runtime.processor.select.SelectContext;
import org.apache.cayenne.DataObject;
import org.apache.cayenne.query.SelectQuery;

import javax.ws.rs.core.Response;
import java.util.List;

/**
 * A root resolver that builds and executes a Cayenne DB query based on the request parameters provided by the client.
 *
 * @since 3.4
 */
public class ViaQueryResolver extends BaseRootDataResolver {

    protected final ICayenneQueryAssembler queryAssembler;
    protected final ICayennePersister persister;

    public ViaQueryResolver(ICayenneQueryAssembler queryAssembler, ICayennePersister persister) {
        this.queryAssembler = queryAssembler;
        this.persister = persister;
    }

    @Override
    protected void doAssembleQuery(SelectContext context) {
        CayenneProcessor.setQuery(context.getEntity(), queryAssembler.createRootQuery(context));
    }

    @Override
    protected List doFetchData(SelectContext context) {
        SelectQuery select = CayenneProcessor.getQuery(context.getEntity());
        List result = persister.sharedContext().select(select);
        checkObjectNotFound(context, result);
        return result;
    }

    protected void checkObjectNotFound(SelectContext context, List result) {
        if (context.isAtMostOneObject() && result.size() != 1) {

            AgEntity entity = context.getEntity().getAgEntity();

            if (result.isEmpty()) {
                throw new AgException(Response.Status.NOT_FOUND,
                        String.format("No object for ID '%s' and entity '%s'", context.getId(), entity.getName()));
            } else {
                throw new AgException(Response.Status.INTERNAL_SERVER_ERROR, String.format(
                        "Found more than one object for ID '%s' and entity '%s'", context.getId(), entity.getName()));
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy