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.warehouse.server.graphql;
018
019import com.echothree.model.control.party.server.graphql.BasePartyObject;
020import com.echothree.model.control.warehouse.server.control.WarehouseControl;
021import com.echothree.model.data.party.server.entity.Party;
022import com.echothree.model.data.warehouse.server.entity.Warehouse;
023import com.echothree.util.server.persistence.Session;
024import graphql.annotations.annotationTypes.GraphQLDescription;
025import graphql.annotations.annotationTypes.GraphQLField;
026import graphql.annotations.annotationTypes.GraphQLName;
027import graphql.annotations.annotationTypes.GraphQLNonNull;
028import graphql.schema.DataFetchingEnvironment;
029
030@GraphQLDescription("warehouse object")
031@GraphQLName("Warehouse")
032public class WarehouseObject
033        extends BasePartyObject {
034
035    public WarehouseObject(Party party) {
036        super(party);
037    }
038
039    public WarehouseObject(Warehouse warehouse) {
040        super(warehouse.getParty());
041
042        this.warehouse = warehouse;
043    }
044
045    private Warehouse warehouse;  // Optional, use getWarehouse()
046
047    protected Warehouse getWarehouse() {
048        if(warehouse == null) {
049            var warehouseControl = Session.getModelController(WarehouseControl.class);
050
051            warehouse = warehouseControl.getWarehouse(party);
052        }
053
054        return warehouse;
055    }
056
057    @GraphQLField
058    @GraphQLDescription("warehouse name")
059    @GraphQLNonNull
060    public String getWarehouseName() {
061        return getWarehouse().getWarehouseName();
062    }
063
064    @GraphQLField
065    @GraphQLDescription("warehouse type")
066    @GraphQLNonNull
067    public WarehouseTypeObject getWarehouseType(final DataFetchingEnvironment env) {
068        return WarehouseSecurityUtils.getHasWarehouseTypeAccess(env) ?
069                new WarehouseTypeObject(getWarehouse().getWarehouseType()) : null;
070    }
071
072    @GraphQLField
073    @GraphQLDescription("is default")
074    @GraphQLNonNull
075    public boolean getIsDefault() {
076        return getWarehouse().getIsDefault();
077    }
078
079    @GraphQLField
080    @GraphQLDescription("sort order")
081    @GraphQLNonNull
082    public int getSortOrder() {
083        return getWarehouse().getSortOrder();
084    }
085
086}