001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.util.server.control; 018 019import com.echothree.model.data.user.common.pk.UserVisitPK; 020import com.echothree.util.common.command.BaseResult; 021import com.echothree.util.common.command.SecurityResult; 022import com.echothree.util.common.form.BaseForm; 023import com.echothree.util.common.validation.FieldDefinition; 024import com.echothree.util.server.persistence.BaseEntity; 025import java.util.Collection; 026import java.util.List; 027 028public abstract class BasePaginatedMultipleEntitiesCommand<BE extends BaseEntity, F extends BaseForm> 029 extends BaseSimpleCommand<F> 030 implements GraphQlSecurityCommand<F> { 031 032 protected BasePaginatedMultipleEntitiesCommand(CommandSecurityDefinition commandSecurityDefinition, 033 List<FieldDefinition> formFieldDefinitions, boolean allowLimits) { 034 super(commandSecurityDefinition, formFieldDefinitions, allowLimits); 035 } 036 037 private boolean formHandled = false; 038 039 protected abstract void handleForm(); 040 protected abstract Long getTotalEntities(); 041 protected abstract Collection<BE> getEntities(); 042 protected abstract BaseResult getResult(Collection<BE> entities); 043 044 public Long getTotalEntitiesForGraphQl(UserVisitPK userVisitPK, F form) { 045 Long totalEntities = null; 046 047 if(formHandled || canQueryByGraphQl(userVisitPK, form)) { // formHandled == true avoids call to canQueryByGraphQl() 048 if(!formHandled) { 049 handleForm(); 050 formHandled = true; 051 } 052 totalEntities = getTotalEntities(); 053 } 054 055 return totalEntities; 056 } 057 058 public Collection<BE> getEntitiesForGraphQl(UserVisitPK userVisitPK, F form) { 059 Collection<BE> entities = null; 060 061 if(formHandled || canQueryByGraphQl(userVisitPK, form)) { // formHandled == true avoids call to canQueryByGraphQl() 062 if(!formHandled) { 063 handleForm(); 064 formHandled = true; 065 } 066 entities = getEntities(); 067 } 068 069 return entities; 070 } 071 072 @Override 073 public SecurityResult security(UserVisitPK userVisitPK, F form) { 074 this.form = form; 075 setUserVisitPK(userVisitPK); 076 077 return super.security(); 078 } 079 080 @Override 081 protected BaseResult execute() { 082 handleForm(); 083 var entities = getEntities(); 084 085 return getResult(entities); 086 } 087 088}