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.offer.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.control.sequence.server.graphql.SequenceObject;
021import com.echothree.model.control.sequence.server.graphql.SequenceSecurityUtils;
022import com.echothree.model.data.offer.server.entity.OfferUse;
023import com.echothree.model.data.offer.server.entity.OfferUseDetail;
024import graphql.annotations.annotationTypes.GraphQLDescription;
025import graphql.annotations.annotationTypes.GraphQLField;
026import graphql.annotations.annotationTypes.GraphQLName;
027import graphql.schema.DataFetchingEnvironment;
028
029@GraphQLDescription("offer use object")
030@GraphQLName("OfferUse")
031public class OfferUseObject
032        extends BaseEntityInstanceObject {
033
034    private final OfferUse offerUse; // Always Present
035
036    public OfferUseObject(OfferUse offerUse) {
037        super(offerUse.getPrimaryKey());
038        
039        this.offerUse = offerUse;
040    }
041
042    private OfferUseDetail offerUseDetail; // Optional, offerUse getOfferUseDetail()
043    
044    private OfferUseDetail getOfferUseDetail() {
045        if(offerUseDetail == null) {
046            offerUseDetail = offerUse.getLastDetail();
047        }
048        
049        return offerUseDetail;
050    }
051
052    @GraphQLField
053    @GraphQLDescription("offer")
054    public OfferObject getOffer(final DataFetchingEnvironment env) {
055        return OfferSecurityUtils.getHasUseAccess(env) ? new OfferObject(getOfferUseDetail().getOffer()) : null;
056    }
057
058    @GraphQLField
059    @GraphQLDescription("use")
060    public UseObject getUse(final DataFetchingEnvironment env) {
061        return OfferSecurityUtils.getHasUseAccess(env) ? new UseObject(getOfferUseDetail().getUse()) : null;
062    }
063    
064    @GraphQLField
065    @GraphQLDescription("sales order sequence")
066    public SequenceObject getSalesOrderSequence(final DataFetchingEnvironment env) {
067        if(SequenceSecurityUtils.getHasSequenceAccess(env)) {
068            var salesOrderSequence = getOfferUseDetail().getSalesOrderSequence();
069
070            return salesOrderSequence == null ? null : new SequenceObject(salesOrderSequence);
071        } else {
072            return null;
073        }
074    }
075
076}