001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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    @GraphQLNonNull
090    public VendorTypeObject getVendorType(final DataFetchingEnvironment env) {
091        return VendorSecurityUtils.getHasVendorTypeAccess(env) ?
092                new VendorTypeObject(getVendor().getVendorType()) : null;
093    }
094
095    @GraphQLField
096    @GraphQLDescription("minimum purchase order lines")
097    public Integer getMinimumPurchaseOrderLines() {
098        return getVendor().getMinimumPurchaseOrderLines();
099    }
100
101    @GraphQLField
102    @GraphQLDescription("minimum purchase order lines")
103    public Integer getMaximumPurchaseOrderLines() {
104        return getVendor().getMaximumPurchaseOrderLines();
105    }
106
107    @GraphQLField
108    @GraphQLDescription("minimum purchase order amount")
109    public Long getMinimumPurchaseOrderAmount() {
110        return getVendor().getMinimumPurchaseOrderAmount();
111    }
112
113    @GraphQLField
114    @GraphQLDescription("maximum purchase order amount")
115    public Long getMaximumPurchaseOrderAmount() {
116        return getVendor().getMaximumPurchaseOrderAmount();
117    }
118
119    @GraphQLField
120    @GraphQLDescription("use item purchasing categories")
121    @GraphQLNonNull
122    public boolean getUseItemPurchasingCategories() {
123        return getVendor().getUseItemPurchasingCategories();
124    }
125
126    @GraphQLField
127    @GraphQLDescription("default item alias type")
128    public ItemAliasTypeObject getDefaultItemAliasType(final DataFetchingEnvironment env) {
129        var defaultItemAliasType = getVendor().getDefaultItemAliasType();
130
131        return defaultItemAliasType == null ? null : ItemSecurityUtils.getHasItemAliasAccess(env) ?
132                new ItemAliasTypeObject(defaultItemAliasType) : null;
133    }
134
135    @GraphQLField
136    @GraphQLDescription("cancellation policy")
137    public CancellationPolicyObject getCancellationPolicy(final DataFetchingEnvironment env) {
138        var cancellationPolicy = getVendor().getCancellationPolicy();
139
140        return cancellationPolicy == null ? null : CancellationPolicySecurityUtils.getHasCancellationPolicyAccess(env) ?
141                new CancellationPolicyObject(cancellationPolicy) : null;
142    }
143
144    @GraphQLField
145    @GraphQLDescription("return policy")
146    public ReturnPolicyObject getReturnPolicy(final DataFetchingEnvironment env) {
147        var returnPolicy = getVendor().getReturnPolicy();
148
149        return returnPolicy == null ? null : ReturnPolicySecurityUtils.getHasReturnPolicyAccess(env) ?
150                new ReturnPolicyObject(returnPolicy) : null;
151    }
152
153    @GraphQLField
154    @GraphQLDescription("AP GL account")
155    public GlAccountObject getApGlAccount(final DataFetchingEnvironment env) {
156        var apGlAccount = getVendor().getApGlAccount();
157
158        return apGlAccount == null ? null : AccountingSecurityUtils.getHasGlAccountAccess(env) ?
159                new GlAccountObject(apGlAccount) : null;
160    }
161
162    @GraphQLField
163    @GraphQLDescription("hold until complete")
164    @GraphQLNonNull
165    public boolean getHoldUntilComplete() {
166        return getVendor().getHoldUntilComplete();
167    }
168
169    @GraphQLField
170    @GraphQLDescription("allow backorders")
171    @GraphQLNonNull
172    public boolean getAllowBackorders() {
173        return getVendor().getAllowBackorders();
174    }
175
176    @GraphQLField
177    @GraphQLDescription("allow substitutions")
178    @GraphQLNonNull
179    public boolean getAllowSubstitutions() {
180        return getVendor().getAllowSubstitutions();
181    }
182
183    @GraphQLField
184    @GraphQLDescription("allow combining shipments")
185    @GraphQLNonNull
186    public boolean getAllowCombiningShipments() {
187        return getVendor().getAllowCombiningShipments();
188    }
189
190    @GraphQLField
191    @GraphQLDescription("require reference")
192    @GraphQLNonNull
193    public boolean getRequireReference() {
194        return getVendor().getRequireReference();
195    }
196
197    @GraphQLField
198    @GraphQLDescription("allow reference duplicates")
199    @GraphQLNonNull
200    public boolean getAllowReferenceDuplicates() {
201        return getVendor().getAllowReferenceDuplicates();
202    }
203
204    @GraphQLField
205    @GraphQLDescription("reference validation pattern")
206    public String getReferenceValidationPattern() {
207        return getVendor().getReferenceValidationPattern();
208    }
209
210    @GraphQLField
211    @GraphQLDescription("vendor item selector")
212    public SelectorObject getVendorItemSelector(final DataFetchingEnvironment env) {
213        var vendorItemSelector = getVendor().getVendorItemSelector();
214
215        return vendorItemSelector == null ? null : SelectorSecurityUtils.getHasSelectorAccess(env) ?
216                new SelectorObject(vendorItemSelector) : null;
217    }
218
219    @GraphQLField
220    @GraphQLDescription("vendor item cost filter")
221    public FilterObject getVendorItemCostFilter(final DataFetchingEnvironment env) {
222        var vendorItemCostFilter = getVendor().getVendorItemCostFilter();
223
224        return vendorItemCostFilter == null ? null : FilterSecurityUtils.getHasFilterAccess(env) ?
225                new FilterObject(vendorItemCostFilter) : null;
226    }
227
228    @GraphQLField
229    @GraphQLDescription("vendor status")
230    public WorkflowEntityStatusObject getVendorStatus(final DataFetchingEnvironment env) {
231        return getWorkflowEntityStatusObject(env, VendorStatusConstants.Workflow_VENDOR_STATUS);
232    }
233
234    @GraphQLField
235    @GraphQLDescription("vendor items")
236    @GraphQLNonNull
237    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
238    public CountingPaginatedData<VendorItemObject> getVendorItems(final DataFetchingEnvironment env) {
239        if(VendorSecurityUtils.getHasVendorItemsAccess(env)) {
240            var itemControl = Session.getModelController(VendorControl.class);
241            var totalCount = itemControl.countVendorItemsByVendorParty(party);
242
243            try(var objectLimiter = new ObjectLimiter(env, VendorItemConstants.COMPONENT_VENDOR_NAME, VendorItemConstants.ENTITY_TYPE_NAME, totalCount)) {
244                var entities = itemControl.getVendorItemsByVendorParty(party);
245                var items = entities.stream().map(VendorItemObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
246
247                return new CountedObjects<>(objectLimiter, items);
248            }
249        } else {
250            return Connections.emptyConnection();
251        }
252    }
253
254}