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.inventory.server.graphql;
018
019import java.util.stream.Collectors;
020import java.util.ArrayList;
021import graphql.annotations.connection.GraphQLConnection;
022import graphql.annotations.annotationTypes.GraphQLNonNull;
023import com.echothree.util.server.persistence.Session;
024import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
025import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
026import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
027import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
028import com.echothree.model.control.graphql.server.graphql.count.Connections;
029import com.echothree.model.data.inventory.common.InventoryLayerConstants;
030import com.echothree.model.control.inventory.server.control.InventoryLayerControl;
031import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
032import com.echothree.model.control.item.server.graphql.ItemObject;
033import com.echothree.model.control.item.server.graphql.ItemSecurityUtils;
034import com.echothree.model.control.party.server.graphql.CompanyObject;
035import com.echothree.model.control.party.server.graphql.PartySecurityUtils;
036import com.echothree.model.data.inventory.server.entity.InventoryCostingPool;
037import com.echothree.model.data.inventory.server.entity.InventoryCostingPoolDetail;
038import graphql.annotations.annotationTypes.GraphQLDescription;
039import graphql.annotations.annotationTypes.GraphQLField;
040import graphql.annotations.annotationTypes.GraphQLName;
041import graphql.schema.DataFetchingEnvironment;
042
043@GraphQLDescription("inventory costing pool object")
044@GraphQLName("InventoryCostingPool")
045public class InventoryCostingPoolObject
046        extends BaseEntityInstanceObject {
047
048    private final InventoryCostingPool inventoryCostingPool; // Always Present
049
050    public InventoryCostingPoolObject(final InventoryCostingPool inventoryCostingPool) {
051        super(inventoryCostingPool.getPrimaryKey());
052
053        this.inventoryCostingPool = inventoryCostingPool;
054    }
055
056    private InventoryCostingPoolDetail inventoryCostingPoolDetail; // Optional, use getInventoryCostingPoolDetail()
057
058    private InventoryCostingPoolDetail getInventoryCostingPoolDetail() {
059        if(inventoryCostingPoolDetail == null) {
060            inventoryCostingPoolDetail = inventoryCostingPool.getLastDetail();
061        }
062
063        return inventoryCostingPoolDetail;
064    }
065
066    @GraphQLField
067    @GraphQLDescription("company")
068    public CompanyObject getCompany(final DataFetchingEnvironment env) {
069        var party = getInventoryCostingPoolDetail().getCompanyParty();
070
071        return PartySecurityUtils.getHasPartyAccess(env, party) ? new CompanyObject(party) : null;
072    }
073
074    @GraphQLField
075    @GraphQLDescription("item")
076    public ItemObject getItem(final DataFetchingEnvironment env) {
077        return ItemSecurityUtils.getHasItemAccess(env) ? new ItemObject(getInventoryCostingPoolDetail().getItem()) : null;
078    }
079
080    @GraphQLField
081    @GraphQLDescription("inventory condition")
082    public InventoryConditionObject getInventoryCondition(final DataFetchingEnvironment env) {
083        return InventorySecurityUtils.getHasInventoryConditionAccess(env)
084                ? new InventoryConditionObject(getInventoryCostingPoolDetail().getInventoryCondition()) : null;
085    }
086
087    @GraphQLField
088    @GraphQLDescription("inventory layers")
089    @GraphQLNonNull
090    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
091    public CountingPaginatedData<InventoryLayerObject> getInventoryLayers(final DataFetchingEnvironment env) {
092        if(InventorySecurityUtils.getHasInventoryLayersAccess(env)) {
093            var inventoryLayerControl = Session.getModelController(InventoryLayerControl.class);
094            var totalCount = inventoryLayerControl.countInventoryLayersByInventoryCostingPool(inventoryCostingPool);
095
096            try(var objectLimiter = new ObjectLimiter(env, InventoryLayerConstants.COMPONENT_VENDOR_NAME, InventoryLayerConstants.ENTITY_TYPE_NAME, totalCount)) {
097                var entities = inventoryLayerControl.getInventoryLayersByInventoryCostingPool(inventoryCostingPool);
098                var inventoryLayers = entities.stream()
099                        .map(InventoryLayerObject::new)
100                        .collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
101
102                return new CountedObjects<>(objectLimiter, inventoryLayers);
103            }
104        } else {
105            return Connections.emptyConnection();
106        }
107    }
108
109}