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.order.server.graphql;
018
019import com.echothree.model.control.accounting.server.graphql.AccountingSecurityUtils;
020import com.echothree.model.control.accounting.server.graphql.CurrencyObject;
021import com.echothree.model.control.cancellationpolicy.server.graphql.CancellationPolicyObject;
022import com.echothree.model.control.cancellationpolicy.server.graphql.CancellationPolicySecurityUtils;
023import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
024import com.echothree.model.control.returnpolicy.server.graphql.ReturnPolicyObject;
025import com.echothree.model.control.returnpolicy.server.graphql.ReturnPolicySecurityUtils;
026import com.echothree.model.control.shipment.server.graphql.FreeOnBoardObject;
027import com.echothree.model.control.shipment.server.graphql.ShipmentSecurityUtils;
028import com.echothree.model.control.term.server.graphql.TermObject;
029import com.echothree.model.control.term.server.graphql.TermSecurityUtils;
030import com.echothree.model.data.order.server.entity.Order;
031import com.echothree.model.data.order.server.entity.OrderDetail;
032import graphql.annotations.annotationTypes.GraphQLDescription;
033import graphql.annotations.annotationTypes.GraphQLField;
034import graphql.annotations.annotationTypes.GraphQLNonNull;
035import graphql.schema.DataFetchingEnvironment;
036
037public abstract class BaseOrderObject
038        extends BaseEntityInstanceObject {
039
040    private final Order order; // Always Present
041
042    protected BaseOrderObject(final Order order) {
043        super(order.getPrimaryKey());
044
045        this.order = order;
046    }
047
048    private OrderDetail orderDetail; // Optional, use getOrderDetail()
049    
050    private OrderDetail getOrderDetail() {
051        if(orderDetail == null) {
052            orderDetail = order.getLastDetail();
053        }
054        
055        return orderDetail;
056    }
057
058    @GraphQLField
059    @GraphQLDescription("order type")
060    public OrderTypeObject getOrderType(final DataFetchingEnvironment env) {
061        return OrderSecurityUtils.getHasOrderTypeAccess(env) ? new OrderTypeObject(getOrderDetail().getOrderType()) : null;
062    }
063
064    @GraphQLField
065    @GraphQLDescription("order type name")
066    @GraphQLNonNull
067    public String getOrderName() {
068        return getOrderDetail().getOrderName();
069    }
070
071    @GraphQLField
072    @GraphQLDescription("order priority")
073    public OrderPriorityObject getOrderPriority(final DataFetchingEnvironment env) {
074        var orderPriority = getOrderDetail().getOrderPriority();
075
076        return orderPriority == null ? null : OrderSecurityUtils.getHasOrderPriorityAccess(env) ?
077                new OrderPriorityObject(orderPriority) : null;
078    }
079
080    @GraphQLField
081    @GraphQLDescription("currency")
082    public CurrencyObject getCurrency(final DataFetchingEnvironment env) {
083        return AccountingSecurityUtils.getHasCurrencyAccess(env) ?
084                new CurrencyObject(getOrderDetail().getCurrency()) : null;
085    }
086
087    @GraphQLField
088    @GraphQLDescription("hold until complete")
089    public Boolean getHoldUntilComplete() {
090        return getOrderDetail().getHoldUntilComplete();
091    }
092
093    @GraphQLField
094    @GraphQLDescription("allow backorders")
095    public Boolean getAllowBackorders() {
096        return getOrderDetail().getAllowBackorders();
097    }
098
099    @GraphQLField
100    @GraphQLDescription("allow substitutions")
101    public Boolean getAllowSubstitutions() {
102        return getOrderDetail().getAllowSubstitutions();
103    }
104
105    @GraphQLField
106    @GraphQLDescription("allow combining shipments")
107    public Boolean getAllowCombiningShipments() {
108        return getOrderDetail().getAllowCombiningShipments();
109    }
110
111    @GraphQLField
112    @GraphQLDescription("term")
113    public TermObject getTerm(final DataFetchingEnvironment env) {
114        var term = getOrderDetail().getTerm();
115
116        return term == null ? null : (TermSecurityUtils.getHasTermAccess(env) ?
117                new TermObject(getOrderDetail().getTerm()) : null);
118    }
119
120    @GraphQLField
121    @GraphQLDescription("freeOnBoard")
122    public FreeOnBoardObject getFreeOnBoard(final DataFetchingEnvironment env) {
123        var freeOnBoard = getOrderDetail().getFreeOnBoard();
124
125        return freeOnBoard == null ? null : ShipmentSecurityUtils.getHasFreeOnBoardAccess(env) ?
126                new FreeOnBoardObject(getOrderDetail().getFreeOnBoard()) : null;
127    }
128
129    @GraphQLField
130    @GraphQLDescription("reference")
131    public String getReference() {
132        return getOrderDetail().getReference();
133    }
134
135    @GraphQLField
136    @GraphQLDescription("description")
137    public String getDescription() {
138        return getOrderDetail().getDescription();
139    }
140
141    @GraphQLField
142    @GraphQLDescription("cancellation policy")
143    public CancellationPolicyObject getCancellationPolicy(final DataFetchingEnvironment env) {
144        var cancellationPolicy = getOrderDetail().getCancellationPolicy();
145
146        return cancellationPolicy == null ? null : CancellationPolicySecurityUtils.getHasCancellationPolicyAccess(env) ?
147                new CancellationPolicyObject(cancellationPolicy) : null;
148    }
149
150    @GraphQLField
151    @GraphQLDescription("return policy")
152    public ReturnPolicyObject getReturnPolicy(final DataFetchingEnvironment env) {
153        var returnPolicy = getOrderDetail().getReturnPolicy();
154
155        return returnPolicy == null ? null : ReturnPolicySecurityUtils.getHasReturnPolicyAccess(env) ?
156                new ReturnPolicyObject(returnPolicy) : null;
157    }
158
159    @GraphQLField
160    @GraphQLDescription("taxable")
161    public Boolean getTaxable() {
162        return getOrderDetail().getTaxable();
163    }
164
165}