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.offer.server.control.OfferControl; 025import com.echothree.model.control.offer.server.graphql.OfferObject; 026import com.echothree.model.control.offer.server.graphql.OfferSecurityUtils; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.data.offer.common.OfferConstants; 029import com.echothree.model.data.party.server.entity.Party; 030import com.echothree.model.data.party.server.entity.PartyDepartment; 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("department object") 042@GraphQLName("Department") 043public class DepartmentObject 044 extends BasePartyObject { 045 046 public DepartmentObject(Party party) { 047 super(party); 048 } 049 050 public DepartmentObject(PartyDepartment partyDepartment) { 051 super(partyDepartment.getParty()); 052 053 this.partyDepartment = partyDepartment; 054 } 055 056 private PartyDepartment partyDepartment; // Optional, use getPartyDepartment() 057 058 protected PartyDepartment getPartyDepartment() { 059 if(partyDepartment == null) { 060 var partyControl = Session.getModelController(PartyControl.class); 061 062 partyDepartment = partyControl.getPartyDepartment(party); 063 } 064 065 return partyDepartment; 066 } 067 @GraphQLField 068 @GraphQLDescription("division") 069 public DivisionObject getDivision(final DataFetchingEnvironment env) { 070 var divisionParty = getPartyDepartment().getDivisionParty(); 071 072 return PartySecurityUtils.getHasPartyAccess(env, divisionParty) ? new DivisionObject(divisionParty) : null; 073 } 074 075 @GraphQLField 076 @GraphQLDescription("department name") 077 @GraphQLNonNull 078 public String getDepartmentName() { 079 return getPartyDepartment().getPartyDepartmentName(); 080 } 081 082 @GraphQLField 083 @GraphQLDescription("is default") 084 @GraphQLNonNull 085 public boolean getIsDefault() { 086 return getPartyDepartment().getIsDefault(); 087 } 088 089 @GraphQLField 090 @GraphQLDescription("sort order") 091 @GraphQLNonNull 092 public int getSortOrder() { 093 return getPartyDepartment().getSortOrder(); 094 } 095 096 @GraphQLField 097 @GraphQLDescription("offers") 098 @GraphQLNonNull 099 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 100 public CountingPaginatedData<OfferObject> getOffers(final DataFetchingEnvironment env) { 101 if(OfferSecurityUtils.getHasOffersAccess(env)) { 102 var offerControl = Session.getModelController(OfferControl.class); 103 var totalCount = offerControl.countOffersByDepartmentParty(party); 104 105 try(var objectLimiter = new ObjectLimiter(env, OfferConstants.COMPONENT_VENDOR_NAME, OfferConstants.ENTITY_TYPE_NAME, totalCount)) { 106 var entities = offerControl.getOffersByDepartmentParty(party); 107 var departments = entities.stream().map(OfferObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 108 109 return new CountedObjects<>(objectLimiter, departments); 110 } 111 } else { 112 return Connections.emptyConnection(); 113 } 114 } 115 116}