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.contact.server.command; 018 019import com.echothree.control.user.contact.common.form.GetPartyContactMechanismsForm; 020import com.echothree.control.user.contact.common.result.ContactResultFactory; 021import com.echothree.model.control.contact.server.control.ContactControl; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.control.party.server.logic.PartyLogic; 024import com.echothree.model.data.contact.server.entity.PartyContactMechanism; 025import com.echothree.model.data.contact.server.factory.PartyContactMechanismFactory; 026import com.echothree.model.data.party.server.entity.Party; 027import com.echothree.util.common.command.BaseResult; 028import com.echothree.util.common.validation.FieldDefinition; 029import com.echothree.util.common.validation.FieldType; 030import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 031import java.util.Collection; 032import java.util.List; 033import javax.enterprise.context.Dependent; 034import javax.inject.Inject; 035 036@Dependent 037public class GetPartyContactMechanismsCommand 038 extends BasePaginatedMultipleEntitiesCommand<PartyContactMechanism, GetPartyContactMechanismsForm> { 039 040 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 041 042 static { 043 FORM_FIELD_DEFINITIONS = List.of( 044 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null) 045 ); 046 } 047 048 @Inject 049 ContactControl contactControl; 050 051 @Inject 052 PartyControl partyControl; 053 054 @Inject 055 PartyLogic partyLogic; 056 057 058 private Party party; 059 060 /** Creates a new instance of GetPartyContactMechanismsCommand */ 061 public GetPartyContactMechanismsCommand() { 062 super(null, FORM_FIELD_DEFINITIONS, true); 063 } 064 065 @Override 066 protected void handleForm() { 067 var partyName = form.getPartyName(); 068 069 if(partyName != null) { 070 party = partyLogic.getPartyByName(this, partyName); 071 } 072 } 073 074 @Override 075 protected Long getTotalEntities() { 076 return hasExecutionErrors() ? null : (party == null ? contactControl.countPartyContactMechanisms() : contactControl.countPartyContactMechanismsByParty(party)); 077 } 078 079 @Override 080 protected Collection<PartyContactMechanism> getEntities() { 081 return hasExecutionErrors() ? null : (party == null ? contactControl.getPartyContactMechanisms() : contactControl.getPartyContactMechanismsByParty(party)); 082 } 083 084 @Override 085 protected BaseResult getResult(Collection<PartyContactMechanism> entities) { 086 var result = ContactResultFactory.getGetPartyContactMechanismsResult(); 087 088 if(entities != null) { 089 if(party != null) { 090 result.setParty(partyControl.getPartyTransfer(getUserVisit(), party)); 091 } 092 093 if(session.hasLimit(PartyContactMechanismFactory.class)) { 094 result.setPartyContactMechanismCount(getTotalEntities()); 095 } 096 097 result.setPartyContactMechanisms(contactControl.getPartyContactMechanismTransfers(getUserVisit(), entities)); 098 } 099 100 return result; 101 } 102 103}