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.offer.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
021import com.echothree.model.control.graphql.server.graphql.count.Connections;
022import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
023import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
024import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
025import com.echothree.model.control.item.server.graphql.ItemObject;
026import com.echothree.model.control.item.server.graphql.ItemSecurityUtils;
027import com.echothree.model.control.offer.server.control.OfferItemControl;
028import com.echothree.model.data.offer.common.OfferItemPriceConstants;
029import com.echothree.model.data.offer.server.entity.OfferItem;
030import com.echothree.util.server.persistence.Session;
031import graphql.annotations.annotationTypes.GraphQLDescription;
032import graphql.annotations.annotationTypes.GraphQLField;
033import graphql.annotations.annotationTypes.GraphQLName;
034import graphql.annotations.annotationTypes.GraphQLNonNull;
035import graphql.annotations.connection.GraphQLConnection;
036import graphql.schema.DataFetchingEnvironment;
037import java.util.ArrayList;
038import java.util.stream.Collectors;
039
040@GraphQLDescription("offer item price object")
041@GraphQLName("OfferItem")
042public class OfferItemObject
043        extends BaseEntityInstanceObject {
044    
045    private final OfferItem offerItem; // Always Present
046    
047    public OfferItemObject(OfferItem offerItem) {
048        super(offerItem.getPrimaryKey());
049        
050        this.offerItem = offerItem;
051    }
052
053    @GraphQLField
054    @GraphQLDescription("offer")
055    public OfferObject getOffer(final DataFetchingEnvironment env) {
056        return OfferSecurityUtils.getHasOfferAccess(env) ? new OfferObject(offerItem.getOffer()) : null;
057    }
058
059    @GraphQLField
060    @GraphQLDescription("item")
061    public ItemObject getItem(final DataFetchingEnvironment env) {
062        return ItemSecurityUtils.getHasItemAccess(env) ? new ItemObject(offerItem.getItem()) : null;
063    }
064
065    @GraphQLField
066    @GraphQLDescription("offer item prices")
067    @GraphQLNonNull
068    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
069    public CountingPaginatedData<OfferItemPriceObject> getOfferItemPrices(final DataFetchingEnvironment env) {
070        if(OfferSecurityUtils.getHasOfferItemPricesAccess(env)) {
071            var offerItemControl = Session.getModelController(OfferItemControl.class);
072            var totalCount = offerItemControl.countOfferItemPricesByOfferItem(offerItem);
073
074            try(var objectLimiter = new ObjectLimiter(env, OfferItemPriceConstants.COMPONENT_VENDOR_NAME, OfferItemPriceConstants.ENTITY_TYPE_NAME, totalCount)) {
075                var entities = offerItemControl.getOfferItemPricesByOfferItem(offerItem);
076                var offerItemPrices = entities.stream().map(OfferItemPriceObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
077
078                return new CountedObjects<>(objectLimiter, offerItemPrices);
079            }
080        } else {
081            return Connections.emptyConnection();
082        }
083    }
084
085}