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.order.server.graphql;
018
019import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
020import com.echothree.model.control.graphql.server.util.BaseGraphQl;
021import com.echothree.model.control.order.server.control.OrderPriorityControl;
022import com.echothree.model.control.user.server.control.UserControl;
023import com.echothree.model.data.order.server.entity.OrderPriority;
024import com.echothree.model.data.order.server.entity.OrderPriorityDetail;
025import com.echothree.util.server.persistence.Session;
026import graphql.annotations.annotationTypes.GraphQLDescription;
027import graphql.annotations.annotationTypes.GraphQLField;
028import graphql.annotations.annotationTypes.GraphQLName;
029import graphql.annotations.annotationTypes.GraphQLNonNull;
030import graphql.schema.DataFetchingEnvironment;
031
032@GraphQLDescription("order priority object")
033@GraphQLName("OrderPriority")
034public class OrderPriorityObject
035        extends BaseEntityInstanceObject {
036    
037    private final OrderPriority orderPriority; // Always Present
038
039    public OrderPriorityObject(final OrderPriority orderPriority) {
040        super(orderPriority.getPrimaryKey());
041
042        this.orderPriority = orderPriority;
043    }
044
045    private OrderPriorityDetail orderPriorityDetail; // Optional, use getOrderPriorityDetail()
046    
047    private OrderPriorityDetail getOrderPriorityDetail() {
048        if(orderPriorityDetail == null) {
049            orderPriorityDetail = orderPriority.getLastDetail();
050        }
051        
052        return orderPriorityDetail;
053    }
054
055    @GraphQLField
056    @GraphQLDescription("order priority name")
057    @GraphQLNonNull
058    public String getOrderPriorityName() {
059        return getOrderPriorityDetail().getOrderPriorityName();
060    }
061
062    @GraphQLField
063    @GraphQLDescription("order type")
064    @GraphQLNonNull
065    public OrderTypeObject getOrderType(final DataFetchingEnvironment env) {
066        return OrderSecurityUtils.getHasOrderTypeAccess(env) ? new OrderTypeObject(getOrderPriorityDetail().getOrderType()) : null;
067    }
068
069    @GraphQLField
070    @GraphQLDescription("priority")
071    @GraphQLNonNull
072    public int getPriority() {
073        return getOrderPriorityDetail().getPriority();
074    }
075
076    @GraphQLField
077    @GraphQLDescription("is default")
078    @GraphQLNonNull
079    public boolean getIsDefault() {
080        return getOrderPriorityDetail().getIsDefault();
081    }
082
083    @GraphQLField
084    @GraphQLDescription("sort order")
085    @GraphQLNonNull
086    public int getSortOrder() {
087        return getOrderPriorityDetail().getSortOrder();
088    }
089    
090    @GraphQLField
091    @GraphQLDescription("description")
092    @GraphQLNonNull
093    public String getDescription(final DataFetchingEnvironment env) {
094        var orderPriorityControl = Session.getModelController(OrderPriorityControl.class);
095        var userControl = Session.getModelController(UserControl.class);
096
097        return orderPriorityControl.getBestOrderPriorityDescription(orderPriority, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
098    }
099
100}