001package ca.uhn.fhir.jpa.interceptor.validation;
002
003/*-
004 * #%L
005 * HAPI FHIR Storage api
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.rest.api.server.RequestDetails;
024import org.apache.commons.lang3.Validate;
025import org.hl7.fhir.instance.model.api.IBaseOperationOutcome;
026import org.hl7.fhir.instance.model.api.IBaseResource;
027
028import javax.annotation.Nonnull;
029
030/**
031 * This is an internal API for HAPI FHIR. It is subject to change without warning.
032 */
033public interface IRepositoryValidatingRule {
034
035        @Nonnull
036        String getResourceType();
037
038        @Nonnull
039        RuleEvaluation evaluate(RequestDetails theRequestDetails, @Nonnull IBaseResource theResource);
040
041        class RuleEvaluation {
042
043                private final IBaseOperationOutcome myOperationOutcome;
044                private IRepositoryValidatingRule myRule;
045                private boolean myPasses;
046                private String myFailureDescription;
047
048                private RuleEvaluation(IRepositoryValidatingRule theRule, boolean thePasses, String theFailureDescription, IBaseOperationOutcome theOperationOutcome) {
049                        myRule = theRule;
050                        myPasses = thePasses;
051                        myFailureDescription = theFailureDescription;
052                        myOperationOutcome = theOperationOutcome;
053                }
054
055                static RuleEvaluation forSuccess(IRepositoryValidatingRule theRule) {
056                        Validate.notNull(theRule);
057                        return new RuleEvaluation(theRule, true, null, null);
058                }
059
060                static RuleEvaluation forFailure(IRepositoryValidatingRule theRule, String theFailureDescription) {
061                        Validate.notNull(theRule);
062                        Validate.notNull(theFailureDescription);
063                        return new RuleEvaluation(theRule, false, theFailureDescription, null);
064                }
065
066                static RuleEvaluation forFailure(IRepositoryValidatingRule theRule, IBaseOperationOutcome theOperationOutcome) {
067                        Validate.notNull(theRule);
068                        Validate.notNull(theOperationOutcome);
069                        return new RuleEvaluation(theRule, false, null, theOperationOutcome);
070                }
071
072                public IBaseOperationOutcome getOperationOutcome() {
073                        return myOperationOutcome;
074                }
075
076                public IRepositoryValidatingRule getRule() {
077                        return myRule;
078                }
079
080                public boolean isPasses() {
081                        return myPasses;
082                }
083
084                public String getFailureDescription() {
085                        return myFailureDescription;
086                }
087
088        }
089}