001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.party.server.graphql;
018
019import com.echothree.model.data.inventory.common.InventoryCostingPoolConstants;
020import com.echothree.model.control.inventory.server.graphql.InventorySecurityUtils;
021import com.echothree.model.control.inventory.server.graphql.InventoryCostingPoolObject;
022import com.echothree.model.control.inventory.server.control.InventoryCostingPoolControl;
023import com.echothree.model.control.graphql.server.util.count.ObjectLimiter;
024import com.echothree.model.control.graphql.server.graphql.count.Connections;
025import com.echothree.model.control.graphql.server.graphql.count.CountedObjects;
026import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher;
027import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData;
028import com.echothree.model.control.item.server.control.ItemControl;
029import com.echothree.model.control.item.server.graphql.ItemObject;
030import com.echothree.model.control.item.server.graphql.ItemSecurityUtils;
031import com.echothree.model.control.party.server.control.PartyControl;
032import com.echothree.model.data.item.common.ItemConstants;
033import com.echothree.model.data.party.server.entity.Party;
034import com.echothree.model.data.party.server.entity.PartyCompany;
035import com.echothree.util.server.persistence.Session;
036import graphql.annotations.annotationTypes.GraphQLDescription;
037import graphql.annotations.annotationTypes.GraphQLField;
038import graphql.annotations.annotationTypes.GraphQLName;
039import graphql.annotations.annotationTypes.GraphQLNonNull;
040import graphql.annotations.connection.GraphQLConnection;
041import graphql.schema.DataFetchingEnvironment;
042import java.util.ArrayList;
043import java.util.stream.Collectors;
044
045@GraphQLDescription("company object")
046@GraphQLName("Company")
047public class CompanyObject
048        extends BasePartyObject {
049
050    public CompanyObject(Party party) {
051        super(party);
052    }
053
054    public CompanyObject(PartyCompany partyCompany) {
055        super(partyCompany.getParty());
056
057        this.partyCompany = partyCompany;
058    }
059
060    private PartyCompany partyCompany;  // Optional, use getPartyCompany()
061
062    protected PartyCompany getPartyCompany() {
063        if(partyCompany == null) {
064            var partyControl = Session.getModelController(PartyControl.class);
065
066            partyCompany = partyControl.getPartyCompany(party);
067        }
068
069        return partyCompany;
070    }
071
072    @GraphQLField
073    @GraphQLDescription("company name")
074    @GraphQLNonNull
075    public String getCompanyName() {
076        return getPartyCompany().getPartyCompanyName();
077    }
078
079    @GraphQLField
080    @GraphQLDescription("is default")
081    @GraphQLNonNull
082    public boolean getIsDefault() {
083        return getPartyCompany().getIsDefault();
084    }
085
086    @GraphQLField
087    @GraphQLDescription("sort order")
088    @GraphQLNonNull
089    public int getSortOrder() {
090        return getPartyCompany().getSortOrder();
091    }
092
093    @GraphQLField
094    @GraphQLDescription("divisions")
095    @GraphQLNonNull
096    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
097    public CountingPaginatedData<DivisionObject> getDivisions(final DataFetchingEnvironment env) {
098        if(PartySecurityUtils.getHasDivisionsAccess(env)) {
099            var partyControl = Session.getModelController(PartyControl.class);
100            var totalCount = partyControl.countPartyDivisions(party);
101
102            try(var objectLimiter = new ObjectLimiter(env, ItemConstants.COMPONENT_VENDOR_NAME, ItemConstants.ENTITY_TYPE_NAME, totalCount)) {
103                var entities = partyControl.getDivisionsByCompany(party);
104                var divisions = entities.stream().map(DivisionObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
105
106                return new CountedObjects<>(objectLimiter, divisions);
107            }
108        } else {
109            return Connections.emptyConnection();
110        }
111    }
112
113    @GraphQLField
114    @GraphQLDescription("items")
115    @GraphQLNonNull
116    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
117    public CountingPaginatedData<ItemObject> getItems(final DataFetchingEnvironment env) {
118        if(ItemSecurityUtils.getHasItemsAccess(env)) {
119            var itemControl = Session.getModelController(ItemControl.class);
120            var totalCount = itemControl.countItemsByCompanyParty(party);
121
122            try(var objectLimiter = new ObjectLimiter(env, ItemConstants.COMPONENT_VENDOR_NAME, ItemConstants.ENTITY_TYPE_NAME, totalCount)) {
123                var entities = itemControl.getItemsByCompanyParty(party);
124                var items = entities.stream().map(ItemObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
125
126                return new CountedObjects<>(objectLimiter, items);
127            }
128        } else {
129            return Connections.emptyConnection();
130        }
131    }
132
133    @GraphQLField
134    @GraphQLDescription("inventory costing pools")
135    @GraphQLNonNull
136    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
137    public CountingPaginatedData<InventoryCostingPoolObject> getInventoryCostingPools(final DataFetchingEnvironment env) {
138        if(InventorySecurityUtils.getHasInventoryCostingPoolsAccess(env)) {
139            var inventoryCostingPoolControl = Session.getModelController(InventoryCostingPoolControl.class);
140            var totalCount = inventoryCostingPoolControl.countInventoryCostingPoolsByCompanyParty(party);
141
142            try(var objectLimiter = new ObjectLimiter(env, InventoryCostingPoolConstants.COMPONENT_VENDOR_NAME, InventoryCostingPoolConstants.ENTITY_TYPE_NAME, totalCount)) {
143                var entities = inventoryCostingPoolControl.getInventoryCostingPoolsByCompanyParty(party);
144                var inventoryCostingPools = entities.stream()
145                        .map(InventoryCostingPoolObject::new)
146                        .collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
147
148                return new CountedObjects<>(objectLimiter, inventoryCostingPools);
149            }
150        } else {
151            return Connections.emptyConnection();
152        }
153    }
154
155}