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.party.server.control.PartyControl;
025import com.echothree.model.data.item.common.ItemConstants;
026import com.echothree.model.data.party.server.entity.Party;
027import com.echothree.model.data.party.server.entity.PartyDivision;
028import com.echothree.util.server.persistence.Session;
029import graphql.annotations.annotationTypes.GraphQLDescription;
030import graphql.annotations.annotationTypes.GraphQLField;
031import graphql.annotations.annotationTypes.GraphQLName;
032import graphql.annotations.annotationTypes.GraphQLNonNull;
033import graphql.annotations.connection.GraphQLConnection;
034import graphql.schema.DataFetchingEnvironment;
035import java.util.ArrayList;
036import java.util.stream.Collectors;
037
038@GraphQLDescription("division object")
039@GraphQLName("Division")
040public class DivisionObject
041        extends BasePartyObject {
042
043    public DivisionObject(Party party) {
044        super(party);
045    }
046
047    public DivisionObject(PartyDivision partyDivision) {
048        super(partyDivision.getParty());
049
050        this.partyDivision = partyDivision;
051    }
052
053    private PartyDivision partyDivision;  // Optional, use getPartyDivision()
054
055    protected PartyDivision getPartyDivision() {
056        if(partyDivision == null) {
057            var partyControl = Session.getModelController(PartyControl.class);
058
059            partyDivision = partyControl.getPartyDivision(party);
060        }
061
062        return partyDivision;
063    }
064    @GraphQLField
065    @GraphQLDescription("company")
066    public CompanyObject getCompany(final DataFetchingEnvironment env) {
067        var companyParty = getPartyDivision().getCompanyParty();
068
069        return PartySecurityUtils.getHasPartyAccess(env, companyParty) ? new CompanyObject(companyParty) : null;
070    }
071
072    @GraphQLField
073    @GraphQLDescription("division name")
074    @GraphQLNonNull
075    public String getDivisionName() {
076        return getPartyDivision().getPartyDivisionName();
077    }
078
079    @GraphQLField
080    @GraphQLDescription("is default")
081    @GraphQLNonNull
082    public boolean getIsDefault() {
083        return getPartyDivision().getIsDefault();
084    }
085
086    @GraphQLField
087    @GraphQLDescription("sort order")
088    @GraphQLNonNull
089    public int getSortOrder() {
090        return getPartyDivision().getSortOrder();
091    }
092
093    @GraphQLField
094    @GraphQLDescription("departments")
095    @GraphQLNonNull
096    @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class)
097    public CountingPaginatedData<DepartmentObject> getDepartments(final DataFetchingEnvironment env) {
098        if(PartySecurityUtils.getHasDepartmentsAccess(env)) {
099            var partyControl = Session.getModelController(PartyControl.class);
100            var totalCount = partyControl.countPartyDepartments(party);
101
102            try(var objectLimiter = new ObjectLimiter(env, ItemConstants.COMPONENT_VENDOR_NAME, ItemConstants.ENTITY_TYPE_NAME, totalCount)) {
103                var entities = partyControl.getDepartmentsByDivision(party);
104                var departments = entities.stream().map(DepartmentObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size())));
105
106                return new CountedObjects<>(objectLimiter, departments);
107            }
108        } else {
109            return Connections.emptyConnection();
110        }
111    }
112
113}