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.offer.server.graphql; 018 019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject; 020import com.echothree.model.control.graphql.server.graphql.count.Connections; 021import com.echothree.model.control.graphql.server.graphql.count.CountedObjects; 022import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher; 023import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData; 024import com.echothree.model.control.graphql.server.util.count.ObjectLimiter; 025import com.echothree.model.control.offer.server.control.OfferUseControl; 026import com.echothree.model.control.offer.server.control.UseControl; 027import com.echothree.model.control.graphql.server.util.BaseGraphQl; 028import com.echothree.model.control.user.server.control.UserControl; 029import com.echothree.model.data.offer.common.OfferUseConstants; 030import com.echothree.model.data.offer.server.entity.Use; 031import com.echothree.model.data.offer.server.entity.UseDetail; 032import com.echothree.util.server.persistence.Session; 033import graphql.annotations.annotationTypes.GraphQLDescription; 034import graphql.annotations.annotationTypes.GraphQLField; 035import graphql.annotations.annotationTypes.GraphQLName; 036import graphql.annotations.annotationTypes.GraphQLNonNull; 037import graphql.annotations.connection.GraphQLConnection; 038import graphql.schema.DataFetchingEnvironment; 039import java.util.ArrayList; 040import java.util.stream.Collectors; 041 042@GraphQLDescription("use object") 043@GraphQLName("Use") 044public class UseObject 045 extends BaseEntityInstanceObject { 046 047 private final Use use; // Always Present 048 049 public UseObject(Use use) { 050 super(use.getPrimaryKey()); 051 052 this.use = use; 053 } 054 055 private UseDetail useDetail; // Optional, use getUseDetail() 056 057 private UseDetail getUseDetail() { 058 if(useDetail == null) { 059 useDetail = use.getLastDetail(); 060 } 061 062 return useDetail; 063 } 064 065 @GraphQLField 066 @GraphQLDescription("use type name") 067 @GraphQLNonNull 068 public String getUseName() { 069 return getUseDetail().getUseName(); 070 } 071 072 @GraphQLField 073 @GraphQLDescription("use type") 074 public UseTypeObject getUseType(final DataFetchingEnvironment env) { 075 return OfferSecurityUtils.getHasUseTypeAccess(env) ? new UseTypeObject(getUseDetail().getUseType()) : null; 076 } 077 078 @GraphQLField 079 @GraphQLDescription("is default") 080 @GraphQLNonNull 081 public boolean getIsDefault() { 082 return getUseDetail().getIsDefault(); 083 } 084 085 @GraphQLField 086 @GraphQLDescription("sort order") 087 @GraphQLNonNull 088 public int getSortOrder() { 089 return getUseDetail().getSortOrder(); 090 } 091 092 @GraphQLField 093 @GraphQLDescription("description") 094 @GraphQLNonNull 095 public String getDescription(final DataFetchingEnvironment env) { 096 var useControl = Session.getModelController(UseControl.class); 097 var userControl = Session.getModelController(UserControl.class); 098 099 return useControl.getBestUseDescription(use, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 100 } 101 102 @GraphQLField 103 @GraphQLDescription("offer uses") 104 @GraphQLNonNull 105 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 106 public CountingPaginatedData<OfferUseObject> getOfferUses(final DataFetchingEnvironment env) { 107 if(OfferSecurityUtils.getHasOfferUsesAccess(env)) { 108 var offerUseControl = Session.getModelController(OfferUseControl.class); 109 var totalCount = offerUseControl.countOfferUsesByUse(use); 110 111 try(var objectLimiter = new ObjectLimiter(env, OfferUseConstants.COMPONENT_VENDOR_NAME, OfferUseConstants.ENTITY_TYPE_NAME, totalCount)) { 112 var entities = offerUseControl.getOfferUsesByUse(use); 113 var offerUses = entities.stream().map(OfferUseObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 114 115 return new CountedObjects<>(objectLimiter, offerUses); 116 } 117 } else { 118 return Connections.emptyConnection(); 119 } 120 } 121 122}