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 com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
023import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
024import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
025import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
026import com.echothree.model.control.graphql.server.graphql.count.Connections;
027import com.echothree.model.data.inventory.common.InventoryCostingPoolConstants;
028import com.echothree.model.control.inventory.server.control.InventoryCostingPoolControl;
029import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
030import com.echothree.model.control.graphql.server.util.BaseGraphQl;
031import com.echothree.model.control.inventory.server.control.InventoryConditionControl;
032import com.echothree.model.control.user.server.control.UserControl;
033import com.echothree.model.data.inventory.server.entity.InventoryCondition;
034import com.echothree.model.data.inventory.server.entity.InventoryConditionDetail;
035import com.echothree.util.server.persistence.Session;
036import graphql.annotations.annotationTypes.GraphQLDescription;
037import graphql.annotations.annotationTypes.GraphQLField;
038import graphql.annotations.annotationTypes.GraphQLName;
039import graphql.annotations.annotationTypes.GraphQLNonNull;
040import graphql.schema.DataFetchingEnvironment;
041
042@GraphQLDescription("inventory condition object")
043@GraphQLName("InventoryCondition")
044public class InventoryConditionObject
045        extends BaseEntityInstanceObject {
046    
047    private final InventoryCondition inventoryCondition; // Always Present
048    
049    public InventoryConditionObject(InventoryCondition inventoryCondition) {
050        super(inventoryCondition.getPrimaryKey());
051        
052        this.inventoryCondition = inventoryCondition;
053    }
054
055    private InventoryConditionDetail inventoryConditionDetail; // Optional, use getInventoryConditionDetail()
056    
057    private InventoryConditionDetail getInventoryConditionDetail() {
058        if(inventoryConditionDetail == null) {
059            inventoryConditionDetail = inventoryCondition.getLastDetail();
060        }
061        
062        return inventoryConditionDetail;
063    }
064    
065    @GraphQLField
066    @GraphQLDescription("inventory condition name")
067    @GraphQLNonNull
068    public String getInventoryConditionName() {
069        return getInventoryConditionDetail().getInventoryConditionName();
070    }
071
072    @GraphQLField
073    @GraphQLDescription("is default")
074    @GraphQLNonNull
075    public boolean getIsDefault() {
076        return getInventoryConditionDetail().getIsDefault();
077    }
078    
079    @GraphQLField
080    @GraphQLDescription("sort order")
081    @GraphQLNonNull
082    public int getSortOrder() {
083        return getInventoryConditionDetail().getSortOrder();
084    }
085    
086    @GraphQLField
087    @GraphQLDescription("description")
088    @GraphQLNonNull
089    public String getDescription(final DataFetchingEnvironment env) {
090        var inventoryConditionControl = Session.getModelController(InventoryConditionControl.class);
091        var userControl = Session.getModelController(UserControl.class);
092
093        return inventoryConditionControl.getBestInventoryConditionDescription(inventoryCondition, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
094    }
095    
096    @GraphQLField
097    @GraphQLDescription("inventory costing pools")
098    @GraphQLNonNull
099    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
100    public CountingPaginatedData<InventoryCostingPoolObject> getInventoryCostingPools(final DataFetchingEnvironment env) {
101        if(InventorySecurityUtils.getHasInventoryCostingPoolsAccess(env)) {
102            var inventoryCostingPoolControl = Session.getModelController(InventoryCostingPoolControl.class);
103            var totalCount = inventoryCostingPoolControl.countInventoryCostingPoolsByInventoryCondition(inventoryCondition);
104
105            try(var objectLimiter = new ObjectLimiter(env, InventoryCostingPoolConstants.COMPONENT_VENDOR_NAME, InventoryCostingPoolConstants.ENTITY_TYPE_NAME, totalCount)) {
106                var entities = inventoryCostingPoolControl.getInventoryCostingPoolsByInventoryCondition(inventoryCondition);
107                var inventoryCostingPools = entities.stream()
108                        .map(InventoryCostingPoolObject::new)
109                        .collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
110
111                return new CountedObjects<>(objectLimiter, inventoryCostingPools);
112            }
113        } else {
114            return Connections.emptyConnection();
115        }
116    }
117
118}