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.graphql.server.util;
018
019import com.echothree.model.control.user.server.control.UserControl;
020import com.echothree.model.data.accounting.server.entity.Currency;
021import com.echothree.model.data.party.server.entity.DateTimeFormat;
022import com.echothree.model.data.party.server.entity.Language;
023import com.echothree.model.data.party.server.entity.TimeZone;
024import com.echothree.model.data.user.common.pk.UserVisitPK;
025import com.echothree.model.data.user.server.entity.UserSession;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import com.echothree.util.server.persistence.Session;
028import graphql.schema.DataFetchingEnvironment;
029
030public interface BaseGraphQl {
031
032    String GRAPHQL_EXECUTION_CONTEXT = "com.echothree.model.control.graphql.server.util.GraphQlExecutionContext";
033
034    static GraphQlExecutionContext getGraphQlExecutionContext(final DataFetchingEnvironment env) {
035        return env.getGraphQlContext().get(GRAPHQL_EXECUTION_CONTEXT);
036    }
037
038    static UserVisitPK getUserVisitPK(final DataFetchingEnvironment env) {
039        return getGraphQlExecutionContext(env).getUserVisitPK();
040    }
041
042    static UserVisit getUserVisit(final DataFetchingEnvironment env) {
043        return getGraphQlExecutionContext(env).getUserVisit();
044    }
045
046    static UserSession getUserSession(final DataFetchingEnvironment env) {
047        return getGraphQlExecutionContext(env).getUserSession();
048    }
049
050    static String getRemoteInet4Address(final DataFetchingEnvironment env) {
051        return getGraphQlExecutionContext(env).getRemoteInet4Address();
052    }
053
054    static Language getLanguageEntity(final DataFetchingEnvironment env) {
055        var userControl = Session.getModelController(UserControl.class);
056
057        return userControl.getPreferredLanguageFromUserVisit(getUserVisit(env));
058    }
059
060    static Currency getCurrencyEntity(final DataFetchingEnvironment env) {
061        var userControl = Session.getModelController(UserControl.class);
062
063        return userControl.getPreferredCurrencyFromUserVisit(getUserVisit(env));
064    }
065
066    static TimeZone getTimeZoneEntity(final DataFetchingEnvironment env) {
067        var userControl = Session.getModelController(UserControl.class);
068
069        return userControl.getPreferredTimeZoneFromUserVisit(getUserVisit(env));
070    }
071
072    static java.util.TimeZone getJavaTimeZone(final DataFetchingEnvironment env) {
073        return java.util.TimeZone.getTimeZone(getTimeZoneEntity(env).getLastDetail().getJavaTimeZoneName());
074    }
075
076    static DateTimeFormat getDateTimeFormatEntity(final DataFetchingEnvironment env) {
077        var userControl = Session.getModelController(UserControl.class);
078
079        return userControl.getPreferredDateTimeFormatFromUserVisit(getUserVisit(env));
080    }
081
082}