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.vendor.server.graphql;
018
019import com.echothree.model.control.accounting.server.graphql.AccountingSecurityUtils;
020import com.echothree.model.control.accounting.server.graphql.GlAccountObject;
021import com.echothree.model.control.cancellationpolicy.server.graphql.CancellationPolicyObject;
022import com.echothree.model.control.cancellationpolicy.server.graphql.CancellationPolicySecurityUtils;
023import com.echothree.model.control.filter.server.graphql.FilterObject;
024import com.echothree.model.control.filter.server.graphql.FilterSecurityUtils;
025import com.echothree.model.control.graphql.server.graphql.count.Connections;
026import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
027import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
028import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
029import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
030import com.echothree.model.control.item.server.graphql.ItemAliasTypeObject;
031import com.echothree.model.control.item.server.graphql.ItemSecurityUtils;
032import com.echothree.model.control.party.server.graphql.BasePartyObject;
033import com.echothree.model.control.returnpolicy.server.graphql.ReturnPolicyObject;
034import com.echothree.model.control.returnpolicy.server.graphql.ReturnPolicySecurityUtils;
035import com.echothree.model.control.selector.server.graphql.SelectorObject;
036import com.echothree.model.control.selector.server.graphql.SelectorSecurityUtils;
037import com.echothree.model.control.vendor.common.workflow.VendorStatusConstants;
038import com.echothree.model.control.vendor.server.control.VendorControl;
039import com.echothree.model.control.workflow.server.graphql.WorkflowEntityStatusObject;
040import com.echothree.model.data.party.server.entity.Party;
041import com.echothree.model.data.vendor.common.VendorItemConstants;
042import com.echothree.model.data.vendor.server.entity.Vendor;
043import com.echothree.util.server.persistence.Session;
044import graphql.annotations.annotationTypes.GraphQLDescription;
045import graphql.annotations.annotationTypes.GraphQLField;
046import graphql.annotations.annotationTypes.GraphQLName;
047import graphql.annotations.annotationTypes.GraphQLNonNull;
048import graphql.annotations.connection.GraphQLConnection;
049import graphql.schema.DataFetchingEnvironment;
050import java.util.ArrayList;
051import java.util.stream.Collectors;
052
053@GraphQLDescription("vendor object")
054@GraphQLName("Vendor")
055public class VendorObject
056        extends BasePartyObject {
057
058    public VendorObject(Party party) {
059        super(party);
060    }
061
062    public VendorObject(Vendor vendor) {
063        super(vendor.getParty());
064
065        this.vendor = vendor;
066    }
067
068    private Vendor vendor;  // Optional, use getVendor()
069
070    protected Vendor getVendor() {
071        if(vendor == null) {
072            var vendorControl = Session.getModelController(VendorControl.class);
073
074            vendor = vendorControl.getVendor(party);
075        }
076
077        return vendor;
078    }
079
080    @GraphQLField
081    @GraphQLDescription("vendor name")
082    @GraphQLNonNull
083    public String getVendorName() {
084        return getVendor().getVendorName();
085    }
086
087    @GraphQLField
088    @GraphQLDescription("vendor type")
089    public VendorTypeObject getVendorType(final DataFetchingEnvironment env) {
090        return VendorSecurityUtils.getHasVendorTypeAccess(env) ?
091                new VendorTypeObject(getVendor().getVendorType()) : null;
092    }
093
094    @GraphQLField
095    @GraphQLDescription("minimum purchase order lines")
096    public Integer getMinimumPurchaseOrderLines() {
097        return getVendor().getMinimumPurchaseOrderLines();
098    }
099
100    @GraphQLField
101    @GraphQLDescription("minimum purchase order lines")
102    public Integer getMaximumPurchaseOrderLines() {
103        return getVendor().getMaximumPurchaseOrderLines();
104    }
105
106    @GraphQLField
107    @GraphQLDescription("minimum purchase order amount")
108    public Long getMinimumPurchaseOrderAmount() {
109        return getVendor().getMinimumPurchaseOrderAmount();
110    }
111
112    @GraphQLField
113    @GraphQLDescription("maximum purchase order amount")
114    public Long getMaximumPurchaseOrderAmount() {
115        return getVendor().getMaximumPurchaseOrderAmount();
116    }
117
118    @GraphQLField
119    @GraphQLDescription("use item purchasing categories")
120    @GraphQLNonNull
121    public boolean getUseItemPurchasingCategories() {
122        return getVendor().getUseItemPurchasingCategories();
123    }
124
125    @GraphQLField
126    @GraphQLDescription("default item alias type")
127    public ItemAliasTypeObject getDefaultItemAliasType(final DataFetchingEnvironment env) {
128        var defaultItemAliasType = getVendor().getDefaultItemAliasType();
129
130        return defaultItemAliasType == null ? null : ItemSecurityUtils.getHasItemAliasAccess(env) ?
131                new ItemAliasTypeObject(defaultItemAliasType) : null;
132    }
133
134    @GraphQLField
135    @GraphQLDescription("cancellation policy")
136    public CancellationPolicyObject getCancellationPolicy(final DataFetchingEnvironment env) {
137        var cancellationPolicy = getVendor().getCancellationPolicy();
138
139        return cancellationPolicy == null ? null : CancellationPolicySecurityUtils.getHasCancellationPolicyAccess(env) ?
140                new CancellationPolicyObject(cancellationPolicy) : null;
141    }
142
143    @GraphQLField
144    @GraphQLDescription("return policy")
145    public ReturnPolicyObject getReturnPolicy(final DataFetchingEnvironment env) {
146        var returnPolicy = getVendor().getReturnPolicy();
147
148        return returnPolicy == null ? null : ReturnPolicySecurityUtils.getHasReturnPolicyAccess(env) ?
149                new ReturnPolicyObject(returnPolicy) : null;
150    }
151
152    @GraphQLField
153    @GraphQLDescription("AP GL account")
154    public GlAccountObject getApGlAccount(final DataFetchingEnvironment env) {
155        var apGlAccount = getVendor().getApGlAccount();
156
157        return apGlAccount == null ? null : AccountingSecurityUtils.getHasGlAccountAccess(env) ?
158                new GlAccountObject(apGlAccount) : null;
159    }
160
161    @GraphQLField
162    @GraphQLDescription("hold until complete")
163    @GraphQLNonNull
164    public boolean getHoldUntilComplete() {
165        return getVendor().getHoldUntilComplete();
166    }
167
168    @GraphQLField
169    @GraphQLDescription("allow backorders")
170    @GraphQLNonNull
171    public boolean getAllowBackorders() {
172        return getVendor().getAllowBackorders();
173    }
174
175    @GraphQLField
176    @GraphQLDescription("allow substitutions")
177    @GraphQLNonNull
178    public boolean getAllowSubstitutions() {
179        return getVendor().getAllowSubstitutions();
180    }
181
182    @GraphQLField
183    @GraphQLDescription("allow combining shipments")
184    @GraphQLNonNull
185    public boolean getAllowCombiningShipments() {
186        return getVendor().getAllowCombiningShipments();
187    }
188
189    @GraphQLField
190    @GraphQLDescription("require reference")
191    @GraphQLNonNull
192    public boolean getRequireReference() {
193        return getVendor().getRequireReference();
194    }
195
196    @GraphQLField
197    @GraphQLDescription("allow reference duplicates")
198    @GraphQLNonNull
199    public boolean getAllowReferenceDuplicates() {
200        return getVendor().getAllowReferenceDuplicates();
201    }
202
203    @GraphQLField
204    @GraphQLDescription("reference validation pattern")
205    public String getReferenceValidationPattern() {
206        return getVendor().getReferenceValidationPattern();
207    }
208
209    @GraphQLField
210    @GraphQLDescription("vendor item selector")
211    public SelectorObject getVendorItemSelector(final DataFetchingEnvironment env) {
212        var vendorItemSelector = getVendor().getVendorItemSelector();
213
214        return vendorItemSelector == null ? null : SelectorSecurityUtils.getHasSelectorAccess(env) ?
215                new SelectorObject(vendorItemSelector) : null;
216    }
217
218    @GraphQLField
219    @GraphQLDescription("vendor item cost filter")
220    public FilterObject getVendorItemCostFilter(final DataFetchingEnvironment env) {
221        var vendorItemCostFilter = getVendor().getVendorItemCostFilter();
222
223        return vendorItemCostFilter == null ? null : FilterSecurityUtils.getHasFilterAccess(env) ?
224                new FilterObject(vendorItemCostFilter) : null;
225    }
226
227    @GraphQLField
228    @GraphQLDescription("vendor status")
229    public WorkflowEntityStatusObject getVendorStatus(final DataFetchingEnvironment env) {
230        return getWorkflowEntityStatusObject(env, VendorStatusConstants.Workflow_VENDOR_STATUS);
231    }
232
233    @GraphQLField
234    @GraphQLDescription("vendor items")
235    @GraphQLNonNull
236    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
237    public CountingPaginatedData<VendorItemObject> getVendorItems(final DataFetchingEnvironment env) {
238        if(VendorSecurityUtils.getHasVendorItemsAccess(env)) {
239            var itemControl = Session.getModelController(VendorControl.class);
240            var totalCount = itemControl.countVendorItemsByVendorParty(party);
241
242            try(var objectLimiter = new ObjectLimiter(env, VendorItemConstants.COMPONENT_VENDOR_NAME, VendorItemConstants.ENTITY_TYPE_NAME, totalCount)) {
243                var entities = itemControl.getVendorItemsByVendorParty(party);
244                var items = entities.stream().map(VendorItemObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
245
246                return new CountedObjects<>(objectLimiter, items);
247            }
248        } else {
249            return Connections.emptyConnection();
250        }
251    }
252
253}