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