001/*-
002 * #%L
003 * HAPI FHIR - Master Data Management
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.mdm.provider;
021
022import ca.uhn.fhir.i18n.Msg;
023import ca.uhn.fhir.mdm.api.MdmLinkSourceEnum;
024import ca.uhn.fhir.mdm.api.MdmMatchResultEnum;
025import ca.uhn.fhir.model.primitive.IdDt;
026import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
027import org.hl7.fhir.instance.model.api.IIdType;
028
029public class MdmControllerUtil {
030        public static MdmMatchResultEnum extractMatchResultOrNull(String theMatchResult) {
031                if (theMatchResult == null) {
032                        return null;
033                }
034                return MdmMatchResultEnum.valueOf(theMatchResult);
035        }
036
037        public static MdmLinkSourceEnum extractLinkSourceOrNull(String theLinkSource) {
038                if (theLinkSource == null) {
039                        return null;
040                }
041                return MdmLinkSourceEnum.valueOf(theLinkSource);
042        }
043
044        public static IIdType extractGoldenResourceIdDtOrNull(String theName, String theGoldenResourceId) {
045                if (theGoldenResourceId == null) {
046                        return null;
047                }
048                return getGoldenIdDtOrThrowException(theName, theGoldenResourceId);
049        }
050
051        public static IIdType extractSourceIdDtOrNull(String theName, String theSourceId) {
052                if (theSourceId == null) {
053                        return null;
054                }
055                return getSourceIdDtOrThrowException(theName, theSourceId);
056        }
057
058        static IdDt getGoldenIdDtOrThrowException(String theParamName, String theId) {
059                IdDt goldenResourceId = new IdDt(theId);
060                if (goldenResourceId.getIdPart() == null) {
061                        throw new InvalidRequestException(Msg.code(1505) + theParamName + " is '" + theId
062                                        + "'.  must have form <resourceType>/<id> where <id> is the id of the resource");
063                }
064                return goldenResourceId;
065        }
066
067        public static IIdType getSourceIdDtOrThrowException(String theParamName, String theSourceId) {
068                IdDt sourceId = new IdDt(theSourceId);
069                if (sourceId.getIdPart() == null) {
070                        throw new InvalidRequestException(
071                                        Msg.code(1506) + theParamName + " is '" + theSourceId
072                                                        + "'.  must have form <resourceType>/<id>  where <id> is the id of the resource and <resourceType> is the type of the resource");
073                }
074                return sourceId;
075        }
076}