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