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.control.user.party.server.command; 018 019import com.echothree.control.user.party.common.form.GetPartyAliasesForm; 020import com.echothree.control.user.party.common.result.PartyResultFactory; 021import com.echothree.control.user.party.server.command.util.PartyAliasUtil; 022import com.echothree.model.control.party.common.PartyTypes; 023import com.echothree.model.control.party.server.control.PartyControl; 024import com.echothree.model.control.party.server.logic.PartyAliasTypeLogic; 025import com.echothree.model.control.party.server.logic.PartyLogic; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.data.party.server.entity.Party; 028import com.echothree.model.data.party.server.entity.PartyAlias; 029import com.echothree.model.data.party.server.entity.PartyAliasType; 030import com.echothree.model.data.party.server.factory.PartyAliasFactory; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 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.BaseMultipleEntitiesCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import com.echothree.util.server.persistence.Session; 041import java.util.Collection; 042import java.util.List; 043 044public class GetPartyAliasesCommand 045 extends BaseMultipleEntitiesCommand<PartyAlias, GetPartyAliasesForm> { 046 047 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 048 049 static { 050 FORM_FIELD_DEFINITIONS = List.of( 051 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 052 new FieldDefinition("PartyTypeName", FieldType.ENTITY_NAME, false, null, null), 053 new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, false, null, null) 054 ); 055 } 056 057 /** Creates a new instance of GetPartyAliasesCommand */ 058 public GetPartyAliasesCommand(UserVisitPK userVisitPK, GetPartyAliasesForm form) { 059 super(userVisitPK, form, new CommandSecurityDefinition(List.of( 060 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 061 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 062 new SecurityRoleDefinition(PartyAliasUtil.getInstance().getSecurityRoleGroupNameBySpecs(form, form), SecurityRoles.List.name()) 063 )) 064 )), FORM_FIELD_DEFINITIONS, false); 065 } 066 067 Party party; 068 PartyAliasType partyAliasType; 069 070 @Override 071 protected Collection<PartyAlias> getEntities() { 072 var partyName = form.getPartyName(); 073 var partyTypeName = form.getPartyTypeName(); 074 var partyAliasTypeName = form.getPartyAliasTypeName(); 075 // Must specify either PartyName or PartyTypeName + PartyAliasTypeName 076 var parameterOption1 = (partyName != null) && (partyTypeName == null) && (partyAliasTypeName == null ); 077 var parameterOption2 = (partyName == null) && (partyTypeName != null) && (partyAliasTypeName != null ); 078 var parameterCount = (parameterOption1 ? 1 : 0) + (parameterOption2 ? 1 : 0); 079 Collection<PartyAlias> partyAliases = null; 080 081 if(parameterCount == 1) { 082 var partyControl = Session.getModelController(PartyControl.class); 083 084 if(parameterOption1) { 085 party = PartyLogic.getInstance().getPartyByName(this, form.getPartyName()); 086 087 if(!hasExecutionErrors()) { 088 partyAliases = partyControl.getPartyAliasesByParty(party); 089 } 090 } else { 091 partyAliasType = PartyAliasTypeLogic.getInstance().getPartyAliasTypeByName(this, partyTypeName, partyAliasTypeName); 092 093 if(!hasExecutionErrors()) { 094 partyAliases = partyControl.getPartyAliasesByPartyAliasType(partyAliasType); 095 } 096 } 097 } else { 098 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 099 } 100 101 return partyAliases; 102 } 103 104 @Override 105 protected BaseResult getResult(Collection<PartyAlias> entities) { 106 var result = PartyResultFactory.getGetPartyAliasesResult(); 107 108 if(entities != null) { 109 var partyControl = Session.getModelController(PartyControl.class); 110 111 if(session.hasLimit(PartyAliasFactory.class)) { 112 if(party != null) { 113 result.setPartyAliasCount(partyControl.countPartyAliasesByParty(party)); 114 } else { 115 result.setPartyAliasCount(partyControl.countPartyAliasesByPartyAliasType(partyAliasType)); 116 } 117 } 118 119 if(party != null) { 120 result.setParty(partyControl.getPartyTransfer(getUserVisit(), party)); 121 } 122 123 if(partyAliasType != null) { 124 result.setPartyAliasType(partyControl.getPartyAliasTypeTransfer(getUserVisit(), partyAliasType)); 125 } 126 127 result.setPartyAliases(partyControl.getPartyAliasTransfers(getUserVisit(), entities)); 128 } 129 130 return result; 131 } 132 133}