001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.contact.server.graphql;
018
019import com.echothree.model.control.contact.common.workflow.PostalAddressStatusConstants;
020import com.echothree.model.control.geo.server.control.GeoControl;
021import com.echothree.model.control.geo.server.graphql.GeoCodeObject;
022import com.echothree.model.control.geo.server.graphql.GeoSecurityUtils;
023import com.echothree.model.control.graphql.server.graphql.BaseObject;
024import com.echothree.model.control.graphql.server.util.BaseGraphQl;
025import com.echothree.model.control.party.server.graphql.NameSuffixObject;
026import com.echothree.model.control.party.server.graphql.PersonalTitleObject;
027import com.echothree.model.control.user.server.control.UserControl;
028import com.echothree.model.control.workflow.server.graphql.WorkflowEntityStatusObject;
029import com.echothree.model.data.contact.server.entity.ContactPostalAddress;
030import com.echothree.model.data.geo.server.entity.GeoCode;
031import com.echothree.util.common.persistence.BasePK;
032import com.echothree.util.server.persistence.Session;
033import graphql.annotations.annotationTypes.GraphQLDescription;
034import graphql.annotations.annotationTypes.GraphQLField;
035import graphql.annotations.annotationTypes.GraphQLName;
036import graphql.annotations.annotationTypes.GraphQLNonNull;
037import graphql.schema.DataFetchingEnvironment;
038
039@GraphQLDescription("contact postal address object")
040@GraphQLName("ContactPostalAddress")
041public class ContactPostalAddressObject
042        extends BaseObject
043        implements ContactMechanismInterface {
044
045    private final BasePK basePrimaryKey; // Always Present
046    private final ContactPostalAddress contactPostalAddress; // Always Present
047
048    public ContactPostalAddressObject(BasePK basePrimaryKey, ContactPostalAddress contactPostalAddress) {
049        this.basePrimaryKey = basePrimaryKey;
050        this.contactPostalAddress = contactPostalAddress;
051    }
052
053    @GraphQLField
054    @GraphQLDescription("personal title")
055    public PersonalTitleObject getPersonalTitle() {
056        var personalTitle = contactPostalAddress.getPersonalTitle();
057
058        return personalTitle == null ? null : new PersonalTitleObject(personalTitle);
059    }
060
061    @GraphQLField
062    @GraphQLDescription("first name")
063    public String getFirstName() {
064        return contactPostalAddress.getFirstName();
065    }
066
067    @GraphQLField
068    @GraphQLDescription("middle name")
069    public String getMiddleName() {
070        return contactPostalAddress.getMiddleName();
071    }
072
073    @GraphQLField
074    @GraphQLDescription("last name")
075    public String getLastName() {
076        return contactPostalAddress.getLastName();
077    }
078
079    @GraphQLField
080    @GraphQLDescription("name suffix")
081    public NameSuffixObject getNameSuffix() {
082        var nameSuffix = contactPostalAddress.getNameSuffix();
083
084        return nameSuffix == null ? null : new NameSuffixObject(nameSuffix);
085    }
086
087    @GraphQLField
088    @GraphQLDescription("company name")
089    public String getCompanyName() {
090        return contactPostalAddress.getCompanyName();
091    }
092
093    @GraphQLField
094    @GraphQLDescription("attention")
095    public String getAttention() {
096        return contactPostalAddress.getAttention();
097    }
098
099    @GraphQLField
100    @GraphQLDescription("address 1")
101    @GraphQLNonNull
102    public String getAddress1() {
103        return contactPostalAddress.getAddress1();
104    }
105
106    @GraphQLField
107    @GraphQLDescription("address 2")
108    public String getAddress2() {
109        return contactPostalAddress.getAddress2();
110    }
111
112    @GraphQLField
113    @GraphQLDescription("address 3")
114    public String getAddress3() {
115        return contactPostalAddress.getAddress3();
116    }
117
118    private GeoCode getCityGeoCode() {
119        return contactPostalAddress.getCityGeoCode();
120    }
121
122    @GraphQLField
123    @GraphQLDescription("city")
124    public String getCity(final DataFetchingEnvironment env) {
125        var description = contactPostalAddress.getCity();
126        var cityGeoCode = getCityGeoCode();
127
128        if(description == null) {
129            var geoControl = Session.getModelController(GeoControl.class);
130            var userControl = Session.getModelController(UserControl.class);
131
132            description = geoControl.getBestGeoCodeDescription(cityGeoCode, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
133
134            if(description == null) {
135                description = geoControl.getAliasForCity(cityGeoCode);
136            }
137        }
138
139        return description;
140    }
141
142    @GraphQLField
143    @GraphQLDescription("city geo code")
144    public GeoCodeObject getCityGeoCode(final DataFetchingEnvironment env) {
145        if(GeoSecurityUtils.getHasGeoCodeAccess(env)) {
146            var cityGeoCode = getCityGeoCode();
147
148            return cityGeoCode == null ? null : new GeoCodeObject(cityGeoCode);
149        } else {
150            return null;
151        }
152    }
153
154    @GraphQLField
155    @GraphQLDescription("county geo code")
156    public GeoCodeObject getCountyGeoCode(final DataFetchingEnvironment env) {
157        if(GeoSecurityUtils.getHasGeoCodeAccess(env)) {
158            var countyGeoCode = contactPostalAddress.getCountyGeoCode();
159
160            return countyGeoCode == null ? null : new GeoCodeObject(countyGeoCode);
161        } else {
162            return null;
163        }
164    }
165
166    private GeoCode getStateGeoCode() {
167        return contactPostalAddress.getStateGeoCode();
168    }
169
170    @GraphQLField
171    @GraphQLDescription("state")
172    public String getState(final DataFetchingEnvironment env) {
173        var description = contactPostalAddress.getState();
174        var stateGeoCode = getStateGeoCode();
175
176        if(description == null) {
177            var geoControl = Session.getModelController(GeoControl.class);
178            var userControl = Session.getModelController(UserControl.class);
179
180            description = geoControl.getBestGeoCodeDescription(stateGeoCode, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
181
182            if(description == null) {
183                description = geoControl.getAliasForState(stateGeoCode);
184            }
185        }
186
187        return description;
188    }
189
190    @GraphQLField
191    @GraphQLDescription("state geo code")
192    public GeoCodeObject getStateGeoCode(final DataFetchingEnvironment env) {
193        if(GeoSecurityUtils.getHasGeoCodeAccess(env)) {
194            var stateGeoCode = getStateGeoCode();
195
196            return stateGeoCode == null ? null : new GeoCodeObject(stateGeoCode);
197        } else {
198            return null;
199        }
200    }
201
202    private GeoCode getPostalCodeGeoCode() {
203        return contactPostalAddress.getPostalCodeGeoCode();
204    }
205
206    @GraphQLField
207    @GraphQLDescription("postal code")
208    public String getPostalCode(final DataFetchingEnvironment env) {
209        var description = contactPostalAddress.getPostalCode();
210        var postalCodeGeoCode = getPostalCodeGeoCode();
211
212        if(description == null) {
213            var geoControl = Session.getModelController(GeoControl.class);
214            var userControl = Session.getModelController(UserControl.class);
215
216            description = geoControl.getBestGeoCodeDescription(postalCodeGeoCode, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
217
218            if(description == null) {
219                description = geoControl.getAliasForPostalCode(postalCodeGeoCode);
220            }
221        }
222
223        return description;
224    }
225
226    @GraphQLField
227    @GraphQLDescription("postal code geo code")
228    public GeoCodeObject getPostalCodeGeoCode(final DataFetchingEnvironment env) {
229        if(GeoSecurityUtils.getHasGeoCodeAccess(env)) {
230            var postalCodeGeoCode = getPostalCodeGeoCode();
231
232            return postalCodeGeoCode == null ? null : new GeoCodeObject(postalCodeGeoCode);
233        } else {
234            return null;
235        }
236    }
237
238    @GraphQLField
239    @GraphQLDescription("country geo code")
240    @GraphQLNonNull
241    public GeoCodeObject getCountryGeoCode(final DataFetchingEnvironment env) {
242        if(GeoSecurityUtils.getHasGeoCodeAccess(env)) {
243            return new GeoCodeObject(contactPostalAddress.getCountryGeoCode());
244        } else {
245            return null;
246        }
247    }
248
249    @GraphQLField
250    @GraphQLDescription("is commercial")
251    @GraphQLNonNull
252    public boolean getIsCommercial() {
253        return contactPostalAddress.getIsCommercial();
254    }
255
256    @GraphQLField
257    @GraphQLDescription("postal address status")
258    public WorkflowEntityStatusObject getPostalAddressStatus(final DataFetchingEnvironment env) {
259        return getWorkflowEntityStatusObject(env, basePrimaryKey, PostalAddressStatusConstants.Workflow_POSTAL_ADDRESS_STATUS);
260    }
261
262}