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.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.BaseGraphQl;
025import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
026import com.echothree.model.control.inventory.server.control.InventoryDispositionControl;
027import com.echothree.model.control.inventory.server.control.InventoryTransactionReasonControl;
028import com.echothree.model.control.inventory.server.control.InventoryDispositionAdjustmentControl;
029import com.echothree.model.control.user.server.control.UserControl;
030import com.echothree.model.data.inventory.common.InventoryTransactionReasonConstants;
031import com.echothree.model.data.inventory.common.InventoryDispositionAdjustmentConstants;
032import com.echothree.model.data.inventory.server.entity.InventoryDisposition;
033import com.echothree.model.data.inventory.server.entity.InventoryDispositionDetail;
034import com.echothree.util.server.persistence.Session;
035import graphql.annotations.annotationTypes.GraphQLDescription;
036import graphql.annotations.annotationTypes.GraphQLField;
037import graphql.annotations.annotationTypes.GraphQLName;
038import graphql.annotations.annotationTypes.GraphQLNonNull;
039import graphql.annotations.connection.GraphQLConnection;
040import graphql.schema.DataFetchingEnvironment;
041import java.util.ArrayList;
042import java.util.stream.Collectors;
043
044@GraphQLDescription("inventory disposition object")
045@GraphQLName("InventoryDisposition")
046public class InventoryDispositionObject
047        extends BaseEntityInstanceObject {
048    
049    private final InventoryDisposition inventoryDisposition; // Always Present
050
051    public InventoryDispositionObject(final InventoryDisposition inventoryDisposition) {
052        super(inventoryDisposition.getPrimaryKey());
053
054        this.inventoryDisposition = inventoryDisposition;
055    }
056
057    private InventoryDispositionDetail inventoryDispositionDetail; // Optional, use getInventoryDispositionDetail()
058    
059    private InventoryDispositionDetail getInventoryDispositionDetail() {
060        if(inventoryDispositionDetail == null) {
061            inventoryDispositionDetail = inventoryDisposition.getLastDetail();
062        }
063        
064        return inventoryDispositionDetail;
065    }
066
067    @GraphQLField
068    @GraphQLDescription("inventory disposition name")
069    @GraphQLNonNull
070    public String getInventoryDispositionName() {
071        return getInventoryDispositionDetail().getInventoryDispositionName();
072    }
073
074    @GraphQLField
075    @GraphQLDescription("inventory transaction type")
076    @GraphQLNonNull
077    public InventoryTransactionTypeObject getInventoryTransactionType(final DataFetchingEnvironment env) {
078        return InventorySecurityUtils.getHasInventoryTransactionTypeAccess(
079                env) ? new InventoryTransactionTypeObject(getInventoryDispositionDetail().getInventoryTransactionType()) : null;
080    }
081
082    @GraphQLField
083    @GraphQLDescription("is default")
084    @GraphQLNonNull
085    public boolean getIsDefault() {
086        return getInventoryDispositionDetail().getIsDefault();
087    }
088
089    @GraphQLField
090    @GraphQLDescription("sort order")
091    @GraphQLNonNull
092    public int getSortOrder() {
093        return getInventoryDispositionDetail().getSortOrder();
094    }
095    
096    @GraphQLField
097    @GraphQLDescription("description")
098    @GraphQLNonNull
099    public String getDescription(final DataFetchingEnvironment env) {
100        var inventoryDispositionControl = Session.getModelController(InventoryDispositionControl.class);
101        var userControl = Session.getModelController(UserControl.class);
102
103        return inventoryDispositionControl.getBestInventoryDispositionDescription(inventoryDisposition,
104                userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
105    }
106
107    @GraphQLField
108    @GraphQLDescription("inventory transaction reasons")
109    @GraphQLNonNull
110    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
111    public CountingPaginatedData<InventoryTransactionReasonObject> getInventoryTransactionReasons(final DataFetchingEnvironment env) {
112        if(InventorySecurityUtils.getHasInventoryTransactionReasonsAccess(env)) {
113            var inventoryTransactionReasonControl = Session.getModelController(InventoryTransactionReasonControl.class);
114            var totalCount = inventoryTransactionReasonControl.countInventoryTransactionReasonsByInventoryDisposition(inventoryDisposition);
115
116            try(var objectLimiter = new ObjectLimiter(env, InventoryTransactionReasonConstants.COMPONENT_VENDOR_NAME,
117                    InventoryTransactionReasonConstants.ENTITY_TYPE_NAME, totalCount)) {
118                var entities = inventoryTransactionReasonControl.getInventoryTransactionReasonsByInventoryDisposition(inventoryDisposition);
119                var reasons = entities.stream()
120                        .map(InventoryTransactionReasonObject::new)
121                        .collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
122
123                return new CountedObjects<>(objectLimiter, reasons);
124            }
125        } else {
126            return Connections.emptyConnection();
127        }
128    }
129
130    @GraphQLField
131    @GraphQLDescription("inventory disposition adjustments")
132    @GraphQLNonNull
133    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
134    public CountingPaginatedData<InventoryDispositionAdjustmentObject> getInventoryDispositionAdjustments(
135            final DataFetchingEnvironment env) {
136        if(InventorySecurityUtils.getHasInventoryDispositionAdjustmentsAccess(env)) {
137            var control = Session.getModelController(InventoryDispositionAdjustmentControl.class);
138            var totalCount = control.countInventoryDispositionAdjustmentsByInventoryDisposition(inventoryDisposition);
139            try(var objectLimiter = new ObjectLimiter(env, InventoryDispositionAdjustmentConstants.COMPONENT_VENDOR_NAME,
140                    InventoryDispositionAdjustmentConstants.ENTITY_TYPE_NAME, totalCount)) {
141                var entities = control.getInventoryDispositionAdjustments(inventoryDisposition);
142                var objects = entities.stream().map(InventoryDispositionAdjustmentObject::new)
143                        .collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
144                return new CountedObjects<>(objectLimiter, objects);
145            }
146        } else {
147            return Connections.emptyConnection();
148        }
149    }
150
151}