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.employee.server.command; 018 019import com.echothree.control.user.employee.common.form.GetPartyResponsibilitiesForm; 020import com.echothree.control.user.employee.common.result.EmployeeResultFactory; 021import com.echothree.model.control.employee.server.control.EmployeeControl; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.control.party.server.logic.PartyLogic; 024import com.echothree.model.data.employee.server.entity.PartyResponsibility; 025import com.echothree.model.data.employee.server.entity.ResponsibilityType; 026import com.echothree.model.data.employee.server.factory.PartyResponsibilityFactory; 027import com.echothree.model.data.party.server.entity.Party; 028import com.echothree.util.common.command.BaseResult; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 033import java.util.Collection; 034import java.util.List; 035import javax.enterprise.context.Dependent; 036import javax.inject.Inject; 037 038@Dependent 039public class GetPartyResponsibilitiesCommand 040 extends BasePaginatedMultipleEntitiesCommand<PartyResponsibility, GetPartyResponsibilitiesForm> { 041 042 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 043 044 static { 045 FORM_FIELD_DEFINITIONS = List.of( 046 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 047 new FieldDefinition("ResponsibilityTypeName", FieldType.ENTITY_NAME, false, null, null) 048 ); 049 } 050 051 @Inject 052 EmployeeControl employeeControl; 053 054 @Inject 055 PartyControl partyControl; 056 057 @Inject 058 PartyLogic partyLogic; 059 060 /** Creates a new instance of GetPartyResponsibilitiesCommand */ 061 public GetPartyResponsibilitiesCommand() { 062 super(null, FORM_FIELD_DEFINITIONS, true); 063 } 064 065 private Party party; 066 private ResponsibilityType responsibilityType; 067 068 @Override 069 protected void handleForm() { 070 var partyName = form.getPartyName(); 071 var responsibilityTypeName = form.getResponsibilityTypeName(); 072 var parameterCount = (partyName == null ? 0 : 1) + (responsibilityTypeName == null ? 0 : 1); 073 074 if(parameterCount == 1) { 075 if(partyName != null) { 076 party = partyLogic.getPartyByName(this, partyName); 077 } else { 078 responsibilityType = employeeControl.getResponsibilityTypeByName(responsibilityTypeName); 079 080 if(responsibilityType == null) { 081 addExecutionError(ExecutionErrors.UnknownResponsibilityTypeName.name(), responsibilityTypeName); 082 } 083 } 084 } else { 085 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 086 } 087 } 088 089 @Override 090 protected Long getTotalEntities() { 091 Long total = null; 092 093 if(!hasExecutionErrors()) { 094 if(party != null) { 095 total = employeeControl.countPartyResponsibilitiesByParty(party); 096 } else { 097 total = employeeControl.countPartyResponsibilitiesByResponsibilityType(responsibilityType); 098 } 099 } 100 101 return total; 102 } 103 104 @Override 105 protected Collection<PartyResponsibility> getEntities() { 106 Collection<PartyResponsibility> entities = null; 107 108 if(!hasExecutionErrors()) { 109 if(party != null) { 110 entities = employeeControl.getPartyResponsibilitiesByParty(party); 111 } else { 112 entities = employeeControl.getPartyResponsibilitiesByResponsibilityType(responsibilityType); 113 } 114 } 115 116 return entities; 117 } 118 119 @Override 120 protected BaseResult getResult(Collection<PartyResponsibility> entities) { 121 var result = EmployeeResultFactory.getGetPartyResponsibilitiesResult(); 122 123 if(entities != null) { 124 if(party != null) { 125 result.setParty(partyControl.getPartyTransfer(getUserVisit(), party)); 126 } else { 127 result.setResponsibilityType(employeeControl.getResponsibilityTypeTransfer(getUserVisit(), responsibilityType)); 128 } 129 130 if(session.hasLimit(PartyResponsibilityFactory.class)) { 131 result.setPartyResponsibilityCount(getTotalEntities()); 132 } 133 134 result.setPartyResponsibilities(employeeControl.getPartyResponsibilityTransfers(getUserVisit(), entities)); 135 } 136 137 return result; 138 } 139 140}