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.content.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.content.server.control.ContentControl;
022import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
023import com.echothree.model.control.graphql.server.graphql.UnitPriceObject;
024import com.echothree.model.control.inventory.server.graphql.InventoryConditionObject;
025import com.echothree.model.control.inventory.server.graphql.InventorySecurityUtils;
026import com.echothree.model.control.item.common.ItemPriceTypes;
027import com.echothree.model.control.item.server.graphql.ItemObject;
028import com.echothree.model.control.item.server.graphql.ItemSecurityUtils;
029import com.echothree.model.control.uom.server.graphql.UnitOfMeasureTypeObject;
030import com.echothree.model.control.uom.server.graphql.UomSecurityUtils;
031import com.echothree.model.data.content.server.entity.ContentCatalogItem;
032import com.echothree.model.data.content.server.entity.ContentCatalogItemFixedPrice;
033import com.echothree.model.data.content.server.entity.ContentCatalogItemVariablePrice;
034import com.echothree.util.server.persistence.Session;
035import graphql.annotations.annotationTypes.GraphQLDescription;
036import graphql.annotations.annotationTypes.GraphQLField;
037import graphql.annotations.annotationTypes.GraphQLName;
038import graphql.schema.DataFetchingEnvironment;
039
040@GraphQLDescription("content catalog item object")
041@GraphQLName("ContentCatalogItem")
042public class ContentCatalogItemObject
043        extends BaseEntityInstanceObject {
044    
045    private final ContentCatalogItem contentCatalogItem; // Always Present
046    
047    public ContentCatalogItemObject(ContentCatalogItem contentCatalogItem) {
048        super(contentCatalogItem.getPrimaryKey());
049        
050        this.contentCatalogItem = contentCatalogItem;
051    }
052
053    String itemPriceTypeName;
054
055    private String getItemPriceTypeName() {
056        if(itemPriceTypeName == null) {
057            itemPriceTypeName = contentCatalogItem.getItem().getLastDetail().getItemPriceType().getItemPriceTypeName();
058        }
059
060        return itemPriceTypeName;
061    }
062
063    ContentCatalogItemFixedPrice contentCatalogItemFixedPrice;
064
065    private ContentCatalogItemFixedPrice getContentCatalogItemFixedPrice() {
066        if(contentCatalogItemFixedPrice == null) {
067            if(getItemPriceTypeName().equals(ItemPriceTypes.FIXED.name())) {
068                var contentControl = Session.getModelController(ContentControl.class);
069
070                contentCatalogItemFixedPrice = contentControl.getContentCatalogItemFixedPrice(contentCatalogItem);
071            }
072        }
073
074        return contentCatalogItemFixedPrice;
075    }
076
077    ContentCatalogItemVariablePrice contentCatalogItemVariablePrice;
078
079    private ContentCatalogItemVariablePrice getContentCatalogItemVariablePrice() {
080        if(contentCatalogItemVariablePrice == null) {
081            if(getItemPriceTypeName().equals(ItemPriceTypes.VARIABLE.name())) {
082                var contentControl = Session.getModelController(ContentControl.class);
083
084                contentCatalogItemVariablePrice = contentControl.getContentCatalogItemVariablePrice(contentCatalogItem);
085            }
086        }
087
088        return contentCatalogItemVariablePrice;
089    }
090
091    @GraphQLField
092    @GraphQLDescription("content catalog")
093    public ContentCatalogObject getContentCatalog(final DataFetchingEnvironment env) {
094        return ContentSecurityUtils.getHasContentCatalogAccess(env) ? new ContentCatalogObject(contentCatalogItem.getContentCatalog()) : null;
095    }
096
097    @GraphQLField
098    @GraphQLDescription("item")
099    public ItemObject getItem(final DataFetchingEnvironment env) {
100        return ItemSecurityUtils.getHasItemAccess(env) ? new ItemObject(contentCatalogItem.getItem()) : null;
101    }
102
103    @GraphQLField
104    @GraphQLDescription("inventory condition")
105    public InventoryConditionObject getInventoryCondition(final DataFetchingEnvironment env) {
106        return InventorySecurityUtils.getHasInventoryConditionAccess(env) ? new InventoryConditionObject(contentCatalogItem.getInventoryCondition()) : null;
107    }
108
109    @GraphQLField
110    @GraphQLDescription("unit of measure type")
111    public UnitOfMeasureTypeObject getUnitOfMeasureType(final DataFetchingEnvironment env) {
112        return UomSecurityUtils.getHasUnitOfMeasureTypeAccess(env) ? new UnitOfMeasureTypeObject(contentCatalogItem.getUnitOfMeasureType()) : null;
113    }
114
115    @GraphQLField
116    @GraphQLDescription("currency")
117    public CurrencyObject getCurrency(final DataFetchingEnvironment env) {
118        return AccountingSecurityUtils.getHasCurrencyAccess(env) ? new CurrencyObject(contentCatalogItem.getCurrency()) : null;
119    }
120
121    @GraphQLField
122    @GraphQLDescription("unit price")
123    public UnitPriceObject getUnitPrice(final DataFetchingEnvironment env) {
124        var contentCatalogItemFixedPrice = getContentCatalogItemFixedPrice();
125
126        return contentCatalogItemFixedPrice == null ? null :
127                new UnitPriceObject(contentCatalogItem.getCurrency(), contentCatalogItemFixedPrice.getUnitPrice());
128    }
129
130    @GraphQLField
131    @GraphQLDescription("minimum unit price")
132    public UnitPriceObject getMinimumUnitPrice(final DataFetchingEnvironment env) {
133        var contentCatalogItemVariablePrice = getContentCatalogItemVariablePrice();
134
135        return contentCatalogItemVariablePrice == null ? null :
136                new UnitPriceObject(contentCatalogItem.getCurrency(), contentCatalogItemVariablePrice.getMinimumUnitPrice());
137    }
138
139    @GraphQLField
140    @GraphQLDescription("maximum unit price")
141    public UnitPriceObject getMaximumUnitPrice(final DataFetchingEnvironment env) {
142        var contentCatalogItemVariablePrice = getContentCatalogItemVariablePrice();
143
144        return contentCatalogItemVariablePrice == null ? null :
145                new UnitPriceObject(contentCatalogItem.getCurrency(), contentCatalogItemVariablePrice.getMaximumUnitPrice());
146    }
147
148    @GraphQLField
149    @GraphQLDescription("unit price increment")
150    public UnitPriceObject getUnitPriceIncrement(final DataFetchingEnvironment env) {
151        var contentCatalogItemVariablePrice = getContentCatalogItemVariablePrice();
152
153        return contentCatalogItemVariablePrice == null ? null :
154                new UnitPriceObject(contentCatalogItem.getCurrency(), contentCatalogItemVariablePrice.getUnitPriceIncrement());
155    }
156
157}