    protected QueryTask calculateMatches(Association as, PresentationContext pc,
            Attributes rq, Attributes keys) throws DicomServiceException {
        AttributesValidator validator = new AttributesValidator(keys);
        QueryRetrieveLevel level = QueryRetrieveLevel.valueOf(validator, qrLevels);
        String cuid = rq.getString(Tag.AffectedSOPClassUID);
        ExtendedNegotiation extNeg = as.getAAssociateAC().getExtNegotiationFor(cuid);
        EnumSet<QueryOption> queryOpts = QueryOption.toOptions(extNeg);
        boolean relational = queryOpts.contains(QueryOption.RELATIONAL);
        level.validateQueryKeys(validator, rootLevel, relational);
        ArchiveApplicationEntity ae = (ArchiveApplicationEntity) as.getApplicationEntity();
        QueryParam queryParam = ae.getQueryParam(codeManager, queryOpts, roles());
        try {
            ApplicationEntity sourceAE = aeCache.findApplicationEntity(as.getRemoteAET());
            if (sourceAE != null) {
                Device sourcDevice = sourceAE.getDevice();
                queryParam.setDefaultIssuerOfPatientID(
                        sourcDevice.getIssuerOfPatientID());
                queryParam.setDefaultIssuerOfAccessionNumber(
                        sourcDevice.getIssuerOfAccessionNumber());
            }
            IDWithIssuer pid = IDWithIssuer.pidWithIssuer(keys,
                    queryParam.getDefaultIssuerOfPatientID());
            IDWithIssuer[] pids = pid == null 
                    ? IDWithIssuer.EMPTY
                    : pixConsumer == null 
                            ? new IDWithIssuer[] { pid }
                            : pixConsumer.pixQuery(ae, pid);
            CompositeQuery query = (CompositeQuery) JNDIUtils.lookup(CompositeQuery.JNDI_NAME);
            switch (level) {
            case PATIENT:
                query.findPatients(pids, keys, queryParam);
                break;
            case STUDY:
                query.findStudies(pids, keys, queryParam);
                break;
            case SERIES:
                query.findSeries(pids, keys, queryParam);
                break;
            default: 
                query.findInstances(pids, keys, queryParam);
                break;
            }
            return new QueryTaskImpl(as, pc, rq, keys, query, queryParam, pids);
        } catch (Exception e) {
            throw new DicomServiceException(Status.UnableToProcess, e);
        }
    }

    protected QueryTask calculateMatches(Association as, PresentationContext pc,
            Attributes rq, Attributes keys) throws DicomServiceException {
        AttributesValidator validator = new AttributesValidator(keys);
        QueryRetrieveLevel level = QueryRetrieveLevel.valueOf(validator, qrLevels);
        String cuid = rq.getString(Tag.AffectedSOPClassUID);
        ExtendedNegotiation extNeg = as.getAAssociateAC().getExtNegotiationFor(cuid);
        EnumSet<QueryOption> queryOpts = QueryOption.toOptions(extNeg);
        boolean relational = queryOpts.contains(QueryOption.RELATIONAL);
        level.validateQueryKeys(validator, rootLevel, relational);
        ArchiveApplicationEntity ae = (ArchiveApplicationEntity) as.getApplicationEntity();
        QueryParam queryParam = ae.getQueryParam(codeManager, queryOpts, roles());
        try {
            ApplicationEntity sourceAE = aeCache.get(as.getRemoteAET());
            if (sourceAE != null) {
                Device sourcDevice = sourceAE.getDevice();
                queryParam.setDefaultIssuerOfPatientID(
                        sourcDevice.getIssuerOfPatientID());
                queryParam.setDefaultIssuerOfAccessionNumber(
                        sourcDevice.getIssuerOfAccessionNumber());
            }
            IDWithIssuer pid = IDWithIssuer.pidWithIssuer(keys,
                    queryParam.getDefaultIssuerOfPatientID());
            IDWithIssuer[] pids = pid == null 
                    ? IDWithIssuer.EMPTY
                    : pixConsumer == null 
                            ? new IDWithIssuer[] { pid }
                            : pixConsumer.pixQuery(ae, pid);
            CompositeQuery query = (CompositeQuery) JNDIUtils.lookup(CompositeQuery.JNDI_NAME);
            switch (level) {
            case PATIENT:
                query.findPatients(pids, keys, queryParam);
                break;
            case STUDY:
                query.findStudies(pids, keys, queryParam);
                break;
            case SERIES:
                query.findSeries(pids, keys, queryParam);
                break;
            default: 
                query.findInstances(pids, keys, queryParam);
                break;
            }
            return new QueryTaskImpl(as, pc, rq, keys, query, queryParam, pids);
        } catch (Exception e) {
            throw new DicomServiceException(Status.UnableToProcess, e);
        }
    }
