001package ca.uhn.fhir.rest.server.interceptor.validation.helpers;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2023 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.context.FhirContext;
024import ca.uhn.fhir.util.PropertyModifyingHelper;
025import org.apache.commons.lang3.StringUtils;
026import org.hl7.fhir.instance.model.api.IBase;
027
028import java.util.Arrays;
029import java.util.List;
030import java.util.stream.Collectors;
031
032/**
033 * Helper class for working with FHIR Address element
034 */
035public class AddressHelper extends PropertyModifyingHelper {
036
037        public static final String FIELD_LINE = "line";
038        public static final String FIELD_CITY = "city";
039        public static final String FIELD_TEXT = "text";
040        public static final String FIELD_DISTRICT = "district";
041        public static final String FIELD_STATE = "state";
042        public static final String FIELD_POSTAL = "postalCode";
043        public static final String FIELD_COUNTRY = "country";
044
045        public static final String[] FIELD_NAMES = {FIELD_TEXT, FIELD_LINE, FIELD_CITY, FIELD_DISTRICT, FIELD_STATE,
046                FIELD_POSTAL, FIELD_COUNTRY};
047
048        public static final String[] ADDRESS_PARTS = {FIELD_CITY, FIELD_DISTRICT, FIELD_STATE, FIELD_POSTAL};
049
050        public AddressHelper(FhirContext theFhirContext, IBase theBase) {
051                super(theFhirContext, theBase);
052        }
053
054        public String getCountry() {
055                return get(FIELD_COUNTRY);
056        }
057
058        public String getCity() {
059                return get(FIELD_CITY);
060        }
061
062        public String getState() {
063                return get(FIELD_STATE);
064        }
065
066        public String getPostalCode() {
067                return get(FIELD_POSTAL);
068        }
069
070        public String getText() {
071                return get(FIELD_TEXT);
072        }
073
074        public void setCountry(String theCountry) {
075                set(FIELD_COUNTRY, theCountry);
076        }
077
078        public void setCity(String theCity) {
079                set(FIELD_CITY, theCity);
080        }
081
082        public void setState(String theState) {
083                set(FIELD_STATE, theState);
084        }
085
086        public void setPostalCode(String thePostal) {
087                set(FIELD_POSTAL, thePostal);
088        }
089
090        public void setText(String theText) {
091                set(FIELD_TEXT, theText);
092        }
093
094        public String getParts() {
095                return Arrays.stream(ADDRESS_PARTS)
096                        .map(this::get)
097                        .filter(s -> !StringUtils.isBlank(s))
098                        .collect(Collectors.joining(getDelimiter()));
099        }
100
101        public String getLine() {
102                return get(FIELD_LINE);
103        }
104
105        public List<String> getLines() {
106                return getMultiple(FIELD_LINE);
107        }
108
109        public AddressHelper addLine(String theLine) {
110                set(FIELD_LINE, theLine);
111                return this;
112        }
113
114        public <T> T getAddress() {
115                return (T) getBase();
116        }
117
118        @Override
119        public String toString() {
120                return getFields(FIELD_NAMES);
121        }
122
123}