001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.server.control.GeoControl;
020import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
021import com.echothree.model.control.graphql.server.util.BaseGraphQl;
022import com.echothree.model.control.user.server.control.UserControl;
023import com.echothree.model.data.geo.server.entity.GeoCode;
024import com.echothree.model.data.geo.server.entity.GeoCodeDetail;
025import com.echothree.util.server.persistence.Session;
026import graphql.annotations.annotationTypes.GraphQLDescription;
027import graphql.annotations.annotationTypes.GraphQLField;
028import graphql.annotations.annotationTypes.GraphQLName;
029import graphql.annotations.annotationTypes.GraphQLNonNull;
030import graphql.schema.DataFetchingEnvironment;
031
032@GraphQLDescription("geo code object")
033@GraphQLName("GeoCode")
034public class GeoCodeObject
035        extends BaseEntityInstanceObject {
036    
037    private final GeoCode geoCode; // Always Present
038    
039    public GeoCodeObject(GeoCode geoCode) {
040        super(geoCode.getPrimaryKey());
041        
042        this.geoCode = geoCode;
043    }
044
045    private GeoCodeDetail geoCodeDetail; // Optional, use getGeoCodeDetail()
046    
047    private GeoCodeDetail getGeoCodeDetail() {
048        if(geoCodeDetail == null) {
049            geoCodeDetail = geoCode.getLastDetail();
050        }
051        
052        return geoCodeDetail;
053    }
054    
055    @GraphQLField
056    @GraphQLDescription("geo code type name")
057    @GraphQLNonNull
058    public String getGeoCodeName() {
059        return getGeoCodeDetail().getGeoCodeName();
060    }
061
062    @GraphQLField
063    @GraphQLDescription("geo code type")
064    public GeoCodeTypeObject getGeoCodeType(final DataFetchingEnvironment env) {
065        return GeoSecurityUtils.getHasGeoCodeTypeAccess(env) ? new GeoCodeTypeObject(getGeoCodeDetail().getGeoCodeType()) : null;
066    }
067
068    @GraphQLField
069    @GraphQLDescription("geo code scope")
070    public GeoCodeScopeObject getGeoCodeScope(final DataFetchingEnvironment env) {
071        return GeoSecurityUtils.getHasGeoCodeScopeAccess(env) ? new GeoCodeScopeObject(getGeoCodeDetail().getGeoCodeScope()) : null;
072    }
073
074    @GraphQLField
075    @GraphQLDescription("is default")
076    @GraphQLNonNull
077    public boolean getIsDefault() {
078        return getGeoCodeDetail().getIsDefault();
079    }
080    
081    @GraphQLField
082    @GraphQLDescription("sort order")
083    @GraphQLNonNull
084    public int getSortOrder() {
085        return getGeoCodeDetail().getSortOrder();
086    }
087    
088    @GraphQLField
089    @GraphQLDescription("description")
090    @GraphQLNonNull
091    public String getDescription(final DataFetchingEnvironment env) {
092        var geoControl = Session.getModelController(GeoControl.class);
093        var userControl = Session.getModelController(UserControl.class);
094
095        return geoControl.getBestGeoCodeDescription(geoCode, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
096    }
097
098}