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 com.echothree.model.control.graphql.server.graphql.BaseObject;
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.inventory.server.control.BucketControl;
026import com.echothree.model.control.item.server.graphql.ItemObject;
027import com.echothree.model.control.item.server.graphql.ItemSecurityUtils;
028import com.echothree.model.control.party.server.graphql.PartyObject;
029import com.echothree.model.control.party.server.graphql.PartySecurityUtils;
030import com.echothree.model.control.uom.server.graphql.UnitOfMeasureTypeObject;
031import com.echothree.model.control.uom.server.graphql.UomSecurityUtils;
032import com.echothree.model.control.warehouse.server.graphql.LocationObject;
033import com.echothree.model.control.warehouse.server.graphql.WarehouseSecurityUtils;
034import com.echothree.model.data.inventory.server.entity.InventoryLocation;
035import com.echothree.model.data.inventory.common.InventoryLocationBucketConstants;
036import com.echothree.util.server.persistence.Session;
037import graphql.annotations.connection.GraphQLConnection;
038import graphql.annotations.annotationTypes.GraphQLDescription;
039import graphql.annotations.annotationTypes.GraphQLField;
040import graphql.annotations.annotationTypes.GraphQLName;
041import graphql.annotations.annotationTypes.GraphQLNonNull;
042import graphql.schema.DataFetchingEnvironment;
043import java.util.ArrayList;
044import java.util.stream.Collectors;
045
046@GraphQLDescription("inventory location object")
047@GraphQLName("InventoryLocation")
048public class InventoryLocationObject
049        extends BaseObject {
050
051    private final InventoryLocation inventoryLocation;
052
053    public InventoryLocationObject(final InventoryLocation inventoryLocation) {
054        this.inventoryLocation = inventoryLocation;
055    }
056
057    @GraphQLField
058    @GraphQLDescription("location")
059    public LocationObject getLocation(final DataFetchingEnvironment env) {
060        return WarehouseSecurityUtils.getHasLocationAccess(env)
061                ? new LocationObject(inventoryLocation.getLocation()) : null;
062    }
063
064    @GraphQLField
065    @GraphQLDescription("owner party")
066    public PartyObject getOwnerParty(final DataFetchingEnvironment env) {
067        var ownerParty = inventoryLocation.getOwnerParty();
068
069        return PartySecurityUtils.getHasPartyAccess(env, ownerParty) ? new PartyObject(ownerParty) : null;
070    }
071
072    @GraphQLField
073    @GraphQLDescription("item")
074    public ItemObject getItem(final DataFetchingEnvironment env) {
075        return ItemSecurityUtils.getHasItemAccess(env) ? new ItemObject(inventoryLocation.getItem()) : null;
076    }
077
078    @GraphQLField
079    @GraphQLDescription("unit of measure type")
080    public UnitOfMeasureTypeObject getUnitOfMeasureType(final DataFetchingEnvironment env) {
081        return UomSecurityUtils.getHasUnitOfMeasureTypeAccess(env)
082                ? new UnitOfMeasureTypeObject(inventoryLocation.getUnitOfMeasureType()) : null;
083    }
084
085    @GraphQLField
086    @GraphQLDescription("inventory condition")
087    public InventoryConditionObject getInventoryCondition(final DataFetchingEnvironment env) {
088        return InventorySecurityUtils.getHasInventoryConditionAccess(env)
089                ? new InventoryConditionObject(inventoryLocation.getInventoryCondition()) : null;
090    }
091
092    @GraphQLField
093    @GraphQLDescription("inventory location buckets")
094    @GraphQLNonNull
095    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
096    public CountingPaginatedData<InventoryLocationBucketObject> getInventoryLocationBuckets(final DataFetchingEnvironment env) {
097        if(InventorySecurityUtils.getHasInventoryLocationBucketsAccess(env)) {
098            var bucketControl = Session.getModelController(BucketControl.class);
099            var totalCount = bucketControl.countInventoryLocationBucketsByInventoryLocation(inventoryLocation);
100
101            try(var objectLimiter = new ObjectLimiter(env, InventoryLocationBucketConstants.COMPONENT_VENDOR_NAME,
102                    InventoryLocationBucketConstants.ENTITY_TYPE_NAME, totalCount)) {
103                var entities = bucketControl.getInventoryLocationBucketsByInventoryLocation(inventoryLocation);
104                var inventoryLocationBuckets = entities.stream().map(InventoryLocationBucketObject::new)
105                        .collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
106
107                return new CountedObjects<>(objectLimiter, inventoryLocationBuckets);
108            }
109        } else {
110            return Connections.emptyConnection();
111        }
112    }
113
114}