001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.party.server.graphql;
018
019import com.echothree.model.control.geo.server.control.GeoControl;
020import com.echothree.model.control.geo.server.graphql.GeoCodeTimeZoneObject;
021import com.echothree.model.control.geo.server.graphql.GeoSecurityUtils;
022import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
023import com.echothree.model.control.graphql.server.graphql.count.Connections;
024import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
025import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
026import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
027import com.echothree.model.control.graphql.server.util.BaseGraphQl;
028import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
029import com.echothree.model.control.party.server.control.PartyControl;
030import com.echothree.model.control.user.server.control.UserControl;
031import com.echothree.model.data.geo.common.GeoCodeTimeZoneConstants;
032import com.echothree.model.data.party.server.entity.TimeZone;
033import com.echothree.model.data.party.server.entity.TimeZoneDetail;
034import com.echothree.util.server.persistence.Session;
035import graphql.annotations.annotationTypes.GraphQLDescription;
036import graphql.annotations.annotationTypes.GraphQLField;
037import graphql.annotations.annotationTypes.GraphQLName;
038import graphql.annotations.annotationTypes.GraphQLNonNull;
039import graphql.annotations.connection.GraphQLConnection;
040import graphql.schema.DataFetchingEnvironment;
041import java.util.ArrayList;
042import java.util.stream.Collectors;
043
044@GraphQLDescription("time zone object")
045@GraphQLName("TimeZone")
046public class TimeZoneObject
047        extends BaseEntityInstanceObject {
048    
049    private final TimeZone timeZone; // Always Present
050    
051    public TimeZoneObject(TimeZone timeZone) {
052        super(timeZone.getPrimaryKey());
053        
054        this.timeZone = timeZone;
055    }
056
057    private TimeZoneDetail timeZoneDetail; // Optional, use getTimeZoneDetail()
058    
059    private TimeZoneDetail getTimeZoneDetail() {
060        if(timeZoneDetail == null) {
061            timeZoneDetail = timeZone.getLastDetail();
062        }
063        
064        return timeZoneDetail;
065    }
066    
067    @GraphQLField
068    @GraphQLDescription("java time zone name")
069    @GraphQLNonNull
070    public String getJavaTimeZoneName() {
071        return getTimeZoneDetail().getJavaTimeZoneName();
072    }
073
074    @GraphQLField
075    @GraphQLDescription("unix time zone name")
076    @GraphQLNonNull
077    public String getUnixTimeZoneName() {
078        return getTimeZoneDetail().getUnixTimeZoneName();
079    }
080
081    @GraphQLField
082    @GraphQLDescription("is default")
083    @GraphQLNonNull
084    public boolean getIsDefault() {
085        return getTimeZoneDetail().getIsDefault();
086    }
087    
088    @GraphQLField
089    @GraphQLDescription("sort order")
090    @GraphQLNonNull
091    public int getSortOrder() {
092        return getTimeZoneDetail().getSortOrder();
093    }
094    
095    @GraphQLField
096    @GraphQLDescription("description")
097    @GraphQLNonNull
098    public String getDescription(final DataFetchingEnvironment env) {
099        var partyControl = Session.getModelController(PartyControl.class);
100        var userControl = Session.getModelController(UserControl.class);
101
102        return partyControl.getBestTimeZoneDescription(timeZone, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
103    }
104
105    @GraphQLField
106    @GraphQLDescription("geo code date time formats")
107    @GraphQLNonNull
108    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
109    public CountingPaginatedData<GeoCodeTimeZoneObject> getGeoCodeTimeZones(final DataFetchingEnvironment env) {
110        if(GeoSecurityUtils.getHasGeoCodeTimeZonesAccess(env)) {
111            var geoControl = Session.getModelController(GeoControl.class);
112            var totalCount = geoControl.countGeoCodeTimeZonesByTimeZone(timeZone);
113
114            try(var objectLimiter = new ObjectLimiter(env, GeoCodeTimeZoneConstants.COMPONENT_VENDOR_NAME, GeoCodeTimeZoneConstants.ENTITY_TYPE_NAME, totalCount)) {
115                var entities = geoControl.getGeoCodeTimeZonesByTimeZone(timeZone);
116                var items = entities.stream().map(GeoCodeTimeZoneObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
117
118                return new CountedObjects<>(objectLimiter, items);
119            }
120        } else {
121            return Connections.emptyConnection();
122        }
123    }
124
125}