001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.filter.server.graphql.FilterObject;
020import com.echothree.model.control.filter.server.graphql.FilterSecurityUtils;
021import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
022import com.echothree.model.control.graphql.server.graphql.count.Connections;
023import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
024import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
025import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
026import com.echothree.model.control.graphql.server.util.BaseGraphQl;
027import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
028import com.echothree.model.control.offer.server.control.OfferControl;
029import com.echothree.model.control.offer.server.control.OfferItemControl;
030import com.echothree.model.control.party.server.graphql.DepartmentObject;
031import com.echothree.model.control.party.server.graphql.PartySecurityUtils;
032import com.echothree.model.control.selector.server.graphql.SelectorObject;
033import com.echothree.model.control.selector.server.graphql.SelectorSecurityUtils;
034import com.echothree.model.control.sequence.server.graphql.SequenceObject;
035import com.echothree.model.control.sequence.server.graphql.SequenceSecurityUtils;
036import com.echothree.model.control.user.server.control.UserControl;
037import com.echothree.model.data.offer.common.OfferItemConstants;
038import com.echothree.model.data.offer.server.entity.Offer;
039import com.echothree.model.data.offer.server.entity.OfferDetail;
040import com.echothree.util.server.persistence.Session;
041import graphql.annotations.annotationTypes.GraphQLDescription;
042import graphql.annotations.annotationTypes.GraphQLField;
043import graphql.annotations.annotationTypes.GraphQLName;
044import graphql.annotations.annotationTypes.GraphQLNonNull;
045import graphql.annotations.connection.GraphQLConnection;
046import graphql.schema.DataFetchingEnvironment;
047import java.util.ArrayList;
048import java.util.stream.Collectors;
049
050@GraphQLDescription("offer object")
051@GraphQLName("Offer")
052public class OfferObject
053        extends BaseEntityInstanceObject {
054    
055    private final Offer offer; // Always Present
056    
057    public OfferObject(Offer offer) {
058        super(offer.getPrimaryKey());
059        
060        this.offer = offer;
061    }
062
063    private OfferDetail offerDetail; // Optional, offer getOfferDetail()
064    
065    private OfferDetail getOfferDetail() {
066        if(offerDetail == null) {
067            offerDetail = offer.getLastDetail();
068        }
069        
070        return offerDetail;
071    }
072    
073    @GraphQLField
074    @GraphQLDescription("offer name")
075    @GraphQLNonNull
076    public String getOfferName() {
077        return getOfferDetail().getOfferName();
078    }
079
080    @GraphQLField
081    @GraphQLDescription("sales order sequence")
082    public SequenceObject getSalesOrderSequence(final DataFetchingEnvironment env) {
083        if(SequenceSecurityUtils.getHasSequenceAccess(env)) {
084            var salesOrderSequence = getOfferDetail().getSalesOrderSequence();
085
086            return salesOrderSequence == null ? null : new SequenceObject(salesOrderSequence);
087        } else {
088            return null;
089        }
090    }
091
092    @GraphQLField
093    @GraphQLDescription("department")
094    public DepartmentObject getDepartment(final DataFetchingEnvironment env) {
095        var departmentParty = getOfferDetail().getDepartmentParty();
096
097        return PartySecurityUtils.getHasPartyAccess(env, departmentParty) ? new DepartmentObject(departmentParty) : null;
098    }
099
100    @GraphQLField
101    @GraphQLDescription("offer item selector")
102    public SelectorObject getOfferItemSelector(final DataFetchingEnvironment env) {
103        if(SelectorSecurityUtils.getHasSelectorAccess(env)) {
104            var offerItemSelector = getOfferDetail().getOfferItemSelector();
105
106            return offerItemSelector == null ? null : new SelectorObject(offerItemSelector);
107        } else {
108            return null;
109        }
110    }
111
112    @GraphQLField
113    @GraphQLDescription("offer item price filter")
114    public FilterObject getOfferItemPriceFilter(final DataFetchingEnvironment env) {
115        if(FilterSecurityUtils.getHasFilterAccess(env)) {
116            var offerItemPriceFilter = getOfferDetail().getOfferItemPriceFilter();
117
118            return offerItemPriceFilter == null ? null : new FilterObject(offerItemPriceFilter);
119        } else {
120            return null;
121        }
122    }
123
124    @GraphQLField
125    @GraphQLDescription("is default")
126    @GraphQLNonNull
127    public boolean getIsDefault() {
128        return getOfferDetail().getIsDefault();
129    }
130    
131    @GraphQLField
132    @GraphQLDescription("sort order")
133    @GraphQLNonNull
134    public int getSortOrder() {
135        return getOfferDetail().getSortOrder();
136    }
137    
138    @GraphQLField
139    @GraphQLDescription("description")
140    @GraphQLNonNull
141    public String getDescription(final DataFetchingEnvironment env) {
142        var offerControl = Session.getModelController(OfferControl.class);
143        var userControl = Session.getModelController(UserControl.class);
144
145        return offerControl.getBestOfferDescription(offer, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
146    }
147
148    @GraphQLField
149    @GraphQLDescription("offer items")
150    @GraphQLNonNull
151    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
152    public CountingPaginatedData<OfferItemObject> getOfferItems(final DataFetchingEnvironment env) {
153        if(OfferSecurityUtils.getHasOfferItemsAccess(env)) {
154            var offerItemControl = Session.getModelController(OfferItemControl.class);
155            var totalCount = offerItemControl.countOfferItemsByOffer(offer);
156
157            try(var objectLimiter = new ObjectLimiter(env, OfferItemConstants.COMPONENT_VENDOR_NAME, OfferItemConstants.ENTITY_TYPE_NAME, totalCount)) {
158                var entities = offerItemControl.getOfferItemsByOffer(offer);
159                var offerItems = entities.stream().map(OfferItemObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
160
161                return new CountedObjects<>(objectLimiter, offerItems);
162            }
163        } else {
164            return Connections.emptyConnection();
165        }
166    }
167
168}