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.cancellationpolicy.server.graphql; 018 019import com.echothree.model.control.cancellationpolicy.server.control.CancellationPolicyControl; 020import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject; 021import com.echothree.model.control.graphql.server.util.BaseGraphQl; 022import com.echothree.model.control.graphql.server.util.count.ObjectLimiter; 023import com.echothree.model.control.graphql.server.graphql.count.Connections; 024import com.echothree.model.control.graphql.server.graphql.count.CountedObjects; 025import com.echothree.model.control.graphql.server.graphql.count.CountingDataConnectionFetcher; 026import com.echothree.model.control.graphql.server.graphql.count.CountingPaginatedData; 027import com.echothree.model.control.sequence.server.graphql.SequenceSecurityUtils; 028import com.echothree.model.control.sequence.server.graphql.SequenceTypeObject; 029import com.echothree.model.control.user.server.control.UserControl; 030import com.echothree.model.data.cancellationpolicy.common.CancellationPolicyConstants; 031import com.echothree.model.data.cancellationpolicy.server.entity.CancellationKind; 032import com.echothree.model.data.cancellationpolicy.server.entity.CancellationKindDetail; 033import com.echothree.util.server.persistence.Session; 034import graphql.annotations.annotationTypes.GraphQLDescription; 035import graphql.annotations.annotationTypes.GraphQLField; 036import graphql.annotations.annotationTypes.GraphQLName; 037import graphql.annotations.annotationTypes.GraphQLNonNull; 038import graphql.annotations.connection.GraphQLConnection; 039import graphql.schema.DataFetchingEnvironment; 040import java.util.ArrayList; 041import java.util.stream.Collectors; 042 043@GraphQLDescription("cancellation kind object") 044@GraphQLName("CancellationKind") 045public class CancellationKindObject 046 extends BaseEntityInstanceObject { 047 048 private final CancellationKind cancellationKind; // Always Present 049 050 public CancellationKindObject(CancellationKind cancellationKind) { 051 super(cancellationKind.getPrimaryKey()); 052 053 this.cancellationKind = cancellationKind; 054 } 055 056 private CancellationKindDetail cancellationKindDetail; // Optional, use getCancellationKindDetail() 057 058 private CancellationKindDetail getCancellationKindDetail() { 059 if(cancellationKindDetail == null) { 060 cancellationKindDetail = cancellationKind.getLastDetail(); 061 } 062 063 return cancellationKindDetail; 064 } 065 066 @GraphQLField 067 @GraphQLDescription("cancellation kind name") 068 @GraphQLNonNull 069 public String getCancellationKindName() { 070 return getCancellationKindDetail().getCancellationKindName(); 071 } 072 073 @GraphQLField 074 @GraphQLDescription("cancellation sequence type") 075 public SequenceTypeObject getCancellationSequenceType(final DataFetchingEnvironment env) { 076 return SequenceSecurityUtils.getHasSequenceTypeAccess(env) ? new SequenceTypeObject(getCancellationKindDetail().getCancellationSequenceType()) : null; 077 } 078 079 @GraphQLField 080 @GraphQLDescription("is default") 081 @GraphQLNonNull 082 public boolean getIsDefault() { 083 return getCancellationKindDetail().getIsDefault(); 084 } 085 086 @GraphQLField 087 @GraphQLDescription("sort order") 088 @GraphQLNonNull 089 public int getSortOrder() { 090 return getCancellationKindDetail().getSortOrder(); 091 } 092 093 @GraphQLField 094 @GraphQLDescription("description") 095 @GraphQLNonNull 096 public String getDescription(final DataFetchingEnvironment env) { 097 var cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class); 098 var userControl = Session.getModelController(UserControl.class); 099 100 return cancellationPolicyControl.getBestCancellationKindDescription(cancellationKind, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env))); 101 } 102 103 @GraphQLField 104 @GraphQLDescription("cancellation policies") 105 @GraphQLNonNull 106 @GraphQLConnection(connectionFetcher = CountingDataConnectionFetcher.class) 107 public CountingPaginatedData<CancellationPolicyObject> getCancellationPolicies(final DataFetchingEnvironment env) { 108 if(CancellationPolicySecurityUtils.getHasCancellationPoliciesAccess(env)) { 109 var cancellationPolicyControl = Session.getModelController(CancellationPolicyControl.class); 110 var totalCount = cancellationPolicyControl.countCancellationPoliciesByCancellationKind(cancellationKind); 111 112 try(var objectLimiter = new ObjectLimiter(env, CancellationPolicyConstants.COMPONENT_VENDOR_NAME, CancellationPolicyConstants.ENTITY_TYPE_NAME, totalCount)) { 113 var entities = cancellationPolicyControl.getCancellationPolicies(cancellationKind); 114 var items = entities.stream().map(CancellationPolicyObject::new).collect(Collectors.toCollection(() -> new ArrayList<>(entities.size()))); 115 116 return new CountedObjects<>(objectLimiter, items); 117 } 118 } else { 119 return Connections.emptyConnection(); 120 } 121 } 122 123}