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.geo.server.graphql;
018
019import com.echothree.model.control.geo.common.GeoCodeTypes;
020import com.echothree.model.control.geo.server.control.GeoControl;
021import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
022import com.echothree.model.control.graphql.server.util.BaseGraphQl;
023import com.echothree.model.control.user.server.control.UserControl;
024import com.echothree.model.data.geo.server.entity.GeoCode;
025import com.echothree.model.data.geo.server.entity.GeoCodeDetail;
026import com.echothree.util.server.persistence.Session;
027import graphql.annotations.annotationTypes.GraphQLDescription;
028import graphql.annotations.annotationTypes.GraphQLField;
029import graphql.annotations.annotationTypes.GraphQLName;
030import graphql.annotations.annotationTypes.GraphQLNonNull;
031import graphql.schema.DataFetchingEnvironment;
032
033@GraphQLDescription("geo code object")
034@GraphQLName("GeoCode")
035public class GeoCodeObject
036        extends BaseEntityInstanceObject {
037
038    private final GeoCode geoCode; // Always Present
039
040    public GeoCodeObject(GeoCode geoCode) {
041        super(geoCode.getPrimaryKey());
042
043        this.geoCode = geoCode;
044    }
045
046    private GeoCodeDetail geoCodeDetail; // Optional, use getGeoCodeDetail()
047
048    private GeoCodeDetail getGeoCodeDetail() {
049        if(geoCodeDetail == null) {
050            geoCodeDetail = geoCode.getLastDetail();
051        }
052
053        return geoCodeDetail;
054    }
055
056    @GraphQLField
057    @GraphQLDescription("geo code type name")
058    @GraphQLNonNull
059    public String getGeoCodeName() {
060        return getGeoCodeDetail().getGeoCodeName();
061    }
062
063    @GraphQLField
064    @GraphQLDescription("geo code type")
065    public GeoCodeTypeObject getGeoCodeType(final DataFetchingEnvironment env) {
066        return GeoSecurityUtils.getHasGeoCodeTypeAccess(env) ? new GeoCodeTypeObject(getGeoCodeDetail().getGeoCodeType()) : null;
067    }
068
069    @GraphQLField
070    @GraphQLDescription("geo code scope")
071    public GeoCodeScopeObject getGeoCodeScope(final DataFetchingEnvironment env) {
072        return GeoSecurityUtils.getHasGeoCodeScopeAccess(env) ? new GeoCodeScopeObject(getGeoCodeDetail().getGeoCodeScope()) : null;
073    }
074
075    @GraphQLField
076    @GraphQLDescription("is default")
077    @GraphQLNonNull
078    public boolean getIsDefault() {
079        return getGeoCodeDetail().getIsDefault();
080    }
081
082    @GraphQLField
083    @GraphQLDescription("sort order")
084    @GraphQLNonNull
085    public int getSortOrder() {
086        return getGeoCodeDetail().getSortOrder();
087    }
088
089    @GraphQLField
090    @GraphQLDescription("description")
091    @GraphQLNonNull
092    public String getDescription(final DataFetchingEnvironment env) {
093        var geoControl = Session.getModelController(GeoControl.class);
094        var userControl = Session.getModelController(UserControl.class);
095
096        return geoControl.getBestGeoCodeDescription(geoCode, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
097    }
098
099    private GeoCodeTypes geoCodeTypeEnum = null; // Optional, use getGeoCodeTypeEnum()
100
101    protected GeoCodeTypes getGeoCodeTypeEnum() {
102        if(geoCodeTypeEnum == null) {
103            geoCodeTypeEnum = GeoCodeTypes.valueOf(getGeoCodeDetail().getGeoCodeType().getLastDetail().getGeoCodeTypeName());
104        }
105
106        return geoCodeTypeEnum;
107    }
108
109    @GraphQLField
110    @GraphQLDescription("geo code")
111    public GeoCodeInterface getGeoCode(final DataFetchingEnvironment env) {
112        var geoControl = Session.getModelController(GeoControl.class);
113
114        return switch(getGeoCodeTypeEnum()) {
115            case ROOT -> null;
116            case COUNTRY -> new GeoCodeCountryObject(geoControl.getGeoCodeCountry(geoCode));
117            case STATE -> null;
118            case COUNTY -> null;
119            case CITY -> null;
120            case ZIP_CODE -> null;
121        };
122    }
123
124}