package ca.uhn.fhir.jpa.interceptor;

import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;

import ca.uhn.fhir.context.RuntimeResourceDefinition;
import ca.uhn.fhir.jpa.dao.BaseHapiFhirDao;
import ca.uhn.fhir.jpa.dao.IFhirResourceDao;
import ca.uhn.fhir.jpa.dao.SearchParameterMap;
import ca.uhn.fhir.jpa.provider.ServletSubRequestDetails;
import ca.uhn.fhir.rest.method.RequestDetails;
import ca.uhn.fhir.rest.server.IBundleProvider;
import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.interceptor.ServerOperationInterceptorAdapter;

public abstract class BaseRestHookSubscriptionInterceptor extends ServerOperationInterceptorAdapter {
	private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseRestHookSubscriptionInterceptor.class);

	protected static final Integer MAX_SUBSCRIPTION_RESULTS = 10000;

	protected abstract IFhirResourceDao<?> getSubscriptionDao();

	protected void checkSubscriptionCriterias(String theCriteria) {
		try {
			IBundleProvider results = executeSubscriptionCriteria(theCriteria, null);
		} catch (Exception e) {
			ourLog.warn("Invalid criteria when creating subscription", e);
			throw new InvalidRequestException("Invalid criteria: " + e.getMessage());
		}
	}

	private IBundleProvider executeSubscriptionCriteria(String theCriteria, IIdType idType) {
		String criteria = theCriteria;

		/*
		 * Run the subscriptions query and look for matches, add the id as part of the criteria 
		 * to avoid getting matches of previous resources rather than the recent resource
		 */
		if (idType != null) {
			criteria += "&_id=" + idType.getResourceType() + "/" + idType.getIdPart();
		}

		IBundleProvider results = getBundleProvider(criteria, true);
		return results;
	}

	/**
	 * Search based on a query criteria
	 * 
	 * @param theCheckOnly Is this just a test that the search works
	 */
	protected IBundleProvider getBundleProvider(String theCriteria, boolean theCheckOnly) {
		RuntimeResourceDefinition responseResourceDef = getSubscriptionDao().validateCriteriaAndReturnResourceDefinition(theCriteria);
		SearchParameterMap responseCriteriaUrl = BaseHapiFhirDao.translateMatchUrl(getSubscriptionDao(), getSubscriptionDao().getContext(), theCriteria, responseResourceDef);

		RequestDetails req = new ServletSubRequestDetails();
		req.setSubRequest(true);

		IFhirResourceDao<? extends IBaseResource> responseDao = getSubscriptionDao().getDao(responseResourceDef.getImplementingClass());
		
		if (theCheckOnly) {
			responseCriteriaUrl.setLoadSynchronousUpTo(1);
		} else {
			responseCriteriaUrl.setLoadSynchronousUpTo(MAX_SUBSCRIPTION_RESULTS);
		}
		
		IBundleProvider responseResults = responseDao.search(responseCriteriaUrl, req);
		return responseResults;
	}

}

@@ -31,16 +31,688 @@
 eptor;%0A%0A
+/*-%0A * #%25L%0A * HAPI FHIR JPA Server%0A * %25%25%0A * Copyright (C) 2014 - 2017 University Health Network%0A * %25%25%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A * %0A *      http://www.apache.org/licenses/LICENSE-2.0%0A * %0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A * #L%25%0A */%0A%0A
 import o

Add license header