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.shipping.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.control.graphql.server.util.BaseGraphQl;
021import com.echothree.model.control.selector.server.graphql.SelectorObject;
022import com.echothree.model.control.selector.server.graphql.SelectorSecurityUtils;
023import com.echothree.model.control.shipping.server.control.ShippingControl;
024import com.echothree.model.control.user.server.control.UserControl;
025import com.echothree.model.data.shipping.server.entity.ShippingMethod;
026import com.echothree.model.data.shipping.server.entity.ShippingMethodDetail;
027import com.echothree.util.server.persistence.Session;
028import graphql.annotations.annotationTypes.GraphQLDescription;
029import graphql.annotations.annotationTypes.GraphQLField;
030import graphql.annotations.annotationTypes.GraphQLName;
031import graphql.annotations.annotationTypes.GraphQLNonNull;
032import graphql.schema.DataFetchingEnvironment;
033
034@GraphQLDescription("shipping method object")
035@GraphQLName("ShippingMethod")
036public class ShippingMethodObject
037        extends BaseEntityInstanceObject {
038    
039    private final ShippingMethod shippingMethod; // Always Present
040    
041    public ShippingMethodObject(ShippingMethod shippingMethod) {
042        super(shippingMethod.getPrimaryKey());
043        
044        this.shippingMethod = shippingMethod;
045    }
046
047    private ShippingMethodDetail shippingMethodDetail; // Optional, use getShippingMethodDetail()
048    
049    private ShippingMethodDetail getShippingMethodDetail() {
050        if(shippingMethodDetail == null) {
051            shippingMethodDetail = shippingMethod.getLastDetail();
052        }
053        
054        return shippingMethodDetail;
055    }
056    
057    @GraphQLField
058    @GraphQLDescription("shipping method name")
059    @GraphQLNonNull
060    public String getShippingMethodName() {
061        return getShippingMethodDetail().getShippingMethodName();
062    }
063
064    @GraphQLField
065    @GraphQLDescription("geo code selector")
066    public SelectorObject getGeoCodeSelector(final DataFetchingEnvironment env) {
067        SelectorObject result;
068
069        if(SelectorSecurityUtils.getHasSelectorAccess(env)) {
070            var selector = getShippingMethodDetail().getGeoCodeSelector();
071
072            result = selector == null ? null : new SelectorObject(selector);
073        } else {
074            result = null;
075        }
076
077        return result;
078    }
079
080    @GraphQLField
081    @GraphQLDescription("item selector")
082    public SelectorObject getItemSelector(final DataFetchingEnvironment env) {
083        SelectorObject result;
084
085        if(SelectorSecurityUtils.getHasSelectorAccess(env)) {
086            var selector = getShippingMethodDetail().getItemSelector();
087
088            result = selector == null ? null : new SelectorObject(selector);
089        } else {
090            result = null;
091        }
092
093        return result;
094    }
095
096    @GraphQLField
097    @GraphQLDescription("sort order")
098    @GraphQLNonNull
099    public int getSortOrder() {
100        return getShippingMethodDetail().getSortOrder();
101    }
102    
103    @GraphQLField
104    @GraphQLDescription("description")
105    @GraphQLNonNull
106    public String getDescription(final DataFetchingEnvironment env) {
107        var shippingControl = Session.getModelController(ShippingControl.class);
108        var userControl = Session.getModelController(UserControl.class);
109
110        return shippingControl.getBestShippingMethodDescription(shippingMethod, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
111    }
112
113}