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.shipping.server.graphql; 018 019import com.echothree.model.control.customer.server.control.CustomerControl; 020import com.echothree.model.control.customer.server.graphql.CustomerSecurityUtils; 021import com.echothree.model.control.customer.server.graphql.CustomerTypeShippingMethodObject; 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.selector.server.graphql.SelectorObject; 030import com.echothree.model.control.selector.server.graphql.SelectorSecurityUtils; 031import com.echothree.model.control.shipping.server.control.ShippingControl; 032import com.echothree.model.control.user.server.control.UserControl; 033import com.echothree.model.data.customer.common.CustomerTypeShippingMethodConstants; 034import com.echothree.model.data.shipping.server.entity.ShippingMethod; 035import com.echothree.model.data.shipping.server.entity.ShippingMethodDetail; 036import com.echothree.util.server.persistence.Session; 037import graphql.annotations.annotationTypes.GraphQLDescription; 038import graphql.annotations.annotationTypes.GraphQLField; 039import graphql.annotations.annotationTypes.GraphQLName; 040import graphql.annotations.annotationTypes.GraphQLNonNull; 041import graphql.annotations.connection.GraphQLConnection; 042import graphql.schema.DataFetchingEnvironment; 043import java.util.ArrayList; 044import java.util.stream.Collectors; 045 046@GraphQLDescription("shipping method object") 047@GraphQLName("ShippingMethod") 048public class ShippingMethodObject 049 extends BaseEntityInstanceObject { 050 051 private final ShippingMethod shippingMethod; // Always Present 052 053 public ShippingMethodObject(ShippingMethod shippingMethod) { 054 super(shippingMethod.getPrimaryKey()); 055 056 this.shippingMethod = shippingMethod; 057 } 058 059 private ShippingMethodDetail shippingMethodDetail; // Optional, use getShippingMethodDetail() 060 061 private ShippingMethodDetail getShippingMethodDetail() { 062 if(shippingMethodDetail == null) { 063 shippingMethodDetail = shippingMethod.getLastDetail(); 064 } 065 066 return shippingMethodDetail; 067 } 068 069 @GraphQLField 070 @GraphQLDescription("shipping method name") 071 @GraphQLNonNull 072 public String getShippingMethodName() { 073 return getShippingMethodDetail().getShippingMethodName(); 074 } 075 076 @GraphQLField 077 @GraphQLDescription("geo code selector") 078 public SelectorObject getGeoCodeSelector(final DataFetchingEnvironment env) { 079 SelectorObject result; 080 081 if(SelectorSecurityUtils.getHasSelectorAccess(env)) { 082 var selector = getShippingMethodDetail().getGeoCodeSelector(); 083 084 result = selector == null ? null : new SelectorObject(selector); 085 } else { 086 result = null; 087 } 088 089 return result; 090 } 091 092 @GraphQLField 093 @GraphQLDescription("item selector") 094 public SelectorObject getItemSelector(final DataFetchingEnvironment env) { 095 SelectorObject result; 096 097 if(SelectorSecurityUtils.getHasSelectorAccess(env)) { 098 var selector = getShippingMethodDetail().getItemSelector(); 099 100 result = selector == null ? null : new SelectorObject(selector); 101 } else { 102 result = null; 103 } 104 105 return result; 106 } 107 108 @GraphQLField 109 @GraphQLDescription("sort order") 110 @GraphQLNonNull 111 public int getSortOrder() { 112 return getShippingMethodDetail().getSortOrder(); 113 } 114 115 @GraphQLField 116 @GraphQLDescription("description") 117 @GraphQLNonNull 118 public String getDescription(final DataFetchingEnvironment env) { 119 var shippingControl = Session.getModelController(ShippingControl.class); 120 var userControl = Session.getModelController(UserControl.class); 121 122 return shippingControl.getBestShippingMethodDescription(shippingMethod, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 123 } 124 125 @GraphQLField 126 @GraphQLDescription("customer type shipping methods") 127 @GraphQLNonNull 128 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 129 public CountingPaginatedData<CustomerTypeShippingMethodObject> getCustomerTypeShippingMethods(final DataFetchingEnvironment env) { 130 if(CustomerSecurityUtils.getHasCustomerTypeShippingMethodsAccess(env)) { 131 var customerControl = Session.getModelController(CustomerControl.class); 132 var totalCount = customerControl.countCustomerTypeShippingMethodsByShippingMethod(shippingMethod); 133 134 try(var objectLimiter = new ObjectLimiter(env, CustomerTypeShippingMethodConstants.COMPONENT_VENDOR_NAME, CustomerTypeShippingMethodConstants.ENTITY_TYPE_NAME, totalCount)) { 135 var entities = customerControl.getCustomerTypeShippingMethodsByShippingMethod(shippingMethod); 136 var objects = entities.stream().map(CustomerTypeShippingMethodObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 137 138 return new CountedObjects<>(objectLimiter, objects); 139 } 140 } else { 141 return Connections.emptyConnection(); 142 } 143 } 144 145}