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.inventory.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.graphql.server.graphql.BaseEntityInstanceObject;
022import com.echothree.model.control.graphql.server.graphql.UnitCostObject;
023import com.echothree.model.control.item.server.graphql.ItemObject;
024import com.echothree.model.control.item.server.graphql.ItemSecurityUtils;
025import com.echothree.model.control.party.server.graphql.PartyObject;
026import com.echothree.model.control.party.server.graphql.PartySecurityUtils;
027import com.echothree.model.control.uom.server.graphql.UnitOfMeasureTypeObject;
028import com.echothree.model.control.uom.server.graphql.UomSecurityUtils;
029import com.echothree.model.data.inventory.server.entity.Lot;
030import com.echothree.model.data.inventory.server.entity.LotDetail;
031import graphql.annotations.annotationTypes.GraphQLDescription;
032import graphql.annotations.annotationTypes.GraphQLField;
033import graphql.annotations.annotationTypes.GraphQLName;
034import graphql.annotations.annotationTypes.GraphQLNonNull;
035import graphql.schema.DataFetchingEnvironment;
036
037@GraphQLDescription("lot object")
038@GraphQLName("Lot")
039public class LotObject
040        extends BaseEntityInstanceObject {
041    
042    private final Lot lot; // Always Present
043    
044    public LotObject(Lot lot) {
045        super(lot.getPrimaryKey());
046        
047        this.lot = lot;
048    }
049
050    private LotDetail lotDetail; // Optional, use getLotDetail()
051    
052    private LotDetail getLotDetail() {
053        if(lotDetail == null) {
054            lotDetail = lot.getLastDetail();
055        }
056        
057        return lotDetail;
058    }
059    
060    @GraphQLField
061    @GraphQLDescription("lot name")
062    @GraphQLNonNull
063    public String getLotName() {
064        return getLotDetail().getLotName();
065    }
066
067    @GraphQLField
068    @GraphQLDescription("owner party")
069    public PartyObject getOwnerParty(final DataFetchingEnvironment env) {
070        var ownerParty = getLotDetail().getOwnerParty();
071
072        return PartySecurityUtils.getHasPartyAccess(env, ownerParty) ? new PartyObject(ownerParty) : null;
073    }
074
075    @GraphQLField
076    @GraphQLDescription("item")
077    public ItemObject getItem(final DataFetchingEnvironment env) {
078        return ItemSecurityUtils.getHasItemAccess(env) ? new ItemObject(getLotDetail().getItem()) : null;
079    }
080
081    @GraphQLField
082    @GraphQLDescription("inventory condition")
083    public InventoryConditionObject getInventoryCondition(final DataFetchingEnvironment env) {
084        return InventorySecurityUtils.getHasInventoryConditionAccess(env) ? new InventoryConditionObject(getLotDetail().getInventoryCondition()) : null;
085    }
086
087    @GraphQLField
088    @GraphQLDescription("unit of measure type")
089    public UnitOfMeasureTypeObject getUnitOfMeasureType(final DataFetchingEnvironment env) {
090        return UomSecurityUtils.getHasUnitOfMeasureTypeAccess(env) ? new UnitOfMeasureTypeObject(getLotDetail().getUnitOfMeasureType()) : null;
091    }
092
093    @GraphQLField
094    @GraphQLDescription("quantity")
095    public Long getQuantity() {
096        return getLotDetail().getQuantity();
097    }
098
099    @GraphQLField
100    @GraphQLDescription("currency")
101    public CurrencyObject getCurrency(final DataFetchingEnvironment env) {
102        return AccountingSecurityUtils.getHasCurrencyAccess(env) ? new CurrencyObject(getLotDetail().getCurrency()) : null;
103    }
104
105    @GraphQLField
106    @GraphQLDescription("unit cost")
107    public UnitCostObject getUnitCost() {
108        return new UnitCostObject(getLotDetail().getCurrency(), getLotDetail().getUnitCost());
109    }
110
111}