001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.control.user.letter.server.command; 018 019import com.echothree.control.user.letter.common.form.GetQueuedLettersForm; 020import com.echothree.control.user.letter.common.result.LetterResultFactory; 021import com.echothree.model.control.chain.server.logic.ChainKindLogic; 022import com.echothree.model.control.chain.server.logic.ChainTypeLogic; 023import com.echothree.model.control.letter.server.control.LetterControl; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.data.chain.server.entity.ChainKind; 028import com.echothree.model.data.chain.server.entity.ChainType; 029import com.echothree.model.data.letter.server.entity.Letter; 030import com.echothree.model.data.letter.server.entity.QueuedLetter; 031import com.echothree.model.data.letter.server.factory.LetterFactory; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.Collection; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043import javax.inject.Inject; 044 045@Dependent 046public class GetQueuedLettersCommand 047 extends BasePaginatedMultipleEntitiesCommand<QueuedLetter, GetQueuedLettersForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.QueuedLetter.name(), SecurityRoles.List.name()) 057 )) 058 )); 059 060 FORM_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("ChainKindName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("ChainTypeName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("LetterName", FieldType.ENTITY_NAME, false, null, null) 064 ); 065 } 066 067 @Inject 068 LetterControl letterControl; 069 070 @Inject 071 ChainKindLogic chainKindLogic; 072 073 @Inject 074 ChainTypeLogic chainTypeLogic; 075 076 /** Creates a new instance of GetQueuedLettersCommand */ 077 public GetQueuedLettersCommand() { 078 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 079 } 080 081 private Letter letter; 082 083 @Override 084 protected void handleForm() { 085 var chainKindName = form.getChainKindName(); 086 var chainTypeName = form.getChainTypeName(); 087 var letterName = form.getLetterName(); 088 var parameterCount = (chainKindName != null ? 1 : 0) + (chainTypeName != null ? 1 : 0) + (letterName != null ? 1 : 0); 089 090 if(parameterCount == 0 || parameterCount == 3) { 091 ChainKind chainKind = null; 092 093 if(chainKindName != null) { 094 chainKind = chainKindLogic.getChainKindByName(this, chainKindName); 095 } 096 097 if(!hasExecutionErrors()) { 098 ChainType chainType = null; 099 100 if(chainTypeName != null) { 101 chainType = chainTypeLogic.getChainTypeByName(this, chainKind, chainTypeName); 102 } 103 104 if(!hasExecutionErrors()) { 105 if(letterName != null) { 106 letter = letterControl.getLetterByName(chainType, letterName); 107 108 if(letter == null) { 109 addExecutionError(ExecutionErrors.UnknownLetterName.name(), chainKindName, chainTypeName, letterName); 110 } 111 } 112 } 113 } 114 } else { 115 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 116 } 117 } 118 119 @Override 120 protected Long getTotalEntities() { 121 return hasExecutionErrors() ? null : letter == null ? letterControl.countQueuedLetters() : letterControl.countQueuedLettersByLetter(letter); 122 } 123 124 @Override 125 protected Collection<QueuedLetter> getEntities() { 126 return hasExecutionErrors() ? null : letter == null ? letterControl.getQueuedLetters() : letterControl.getQueuedLettersByLetter(letter); 127 } 128 129 @Override 130 protected BaseResult getResult(Collection<QueuedLetter> entities) { 131 var result = LetterResultFactory.getGetQueuedLettersResult(); 132 133 if(entities != null) { 134 var userVisit = getUserVisit(); 135 136 if(letter != null) { 137 result.setLetter(letterControl.getLetterTransfer(userVisit, letter)); 138 } 139 140 if(session.hasLimit(LetterFactory.class)) { 141 result.setQueuedLetterCount(getTotalEntities()); 142 } 143 144 result.setQueuedLetters(letterControl.getQueuedLetterTransfers(userVisit, entities)); 145 } 146 147 return result; 148 } 149 150}