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.vendor.server.command; 018 019import com.echothree.control.user.core.common.spec.UniversalEntitySpec; 020import com.echothree.control.user.vendor.common.form.GetVendorForm; 021import com.echothree.control.user.vendor.common.result.VendorResultFactory; 022import com.echothree.model.control.core.common.EventTypes; 023import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.party.server.logic.PartyLogic; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.control.vendor.server.control.VendorControl; 029import com.echothree.model.control.vendor.server.logic.VendorLogic; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.model.data.vendor.server.entity.Vendor; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.command.SecurityResult; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BaseSingleEntityCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.control.SecurityRoleDefinition; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class GetVendorCommand 046 extends BaseSingleEntityCommand<Vendor, GetVendorForm> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.Vendor.name(), SecurityRoles.Review.name()) 057 )) 058 )); 059 060 FORM_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 064 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 065 ); 066 } 067 068 @Inject 069 VendorControl vendorControl; 070 071 @Inject 072 EntityInstanceLogic entityInstanceLogic; 073 074 @Inject 075 PartyLogic partyLogic; 076 077 @Inject 078 VendorLogic vendorLogic; 079 080 /** Creates a new instance of GetVendorCommand */ 081 public GetVendorCommand() { 082 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 083 } 084 085 String vendorName; 086 String partyName; 087 UniversalEntitySpec universalEntitySpec; 088 int parameterCount; 089 090 @Override 091 public SecurityResult security() { 092 var securityResult = super.security(); 093 094 vendorName = form.getVendorName(); 095 partyName = form.getPartyName(); 096 universalEntitySpec = form; 097 parameterCount = (vendorName == null ? 0 : 1) + (partyName == null ? 0 : 1) + 098 entityInstanceLogic.countPossibleEntitySpecs(form); 099 100 if(!canSpecifyParty() && parameterCount != 0) { 101 securityResult = getInsufficientSecurityResult(); 102 } 103 104 return securityResult; 105 } 106 107 @Override 108 protected Vendor getEntity() { 109 Vendor vendor = null; 110 111 if(parameterCount == 0) { 112 var party = getParty(); 113 114 partyLogic.checkPartyType(this, party, PartyTypes.VENDOR.name()); 115 116 if(!hasExecutionErrors()) { 117 vendor = vendorControl.getVendor(party); 118 } 119 } else { 120 vendor = vendorLogic.getVendorByName(this, vendorName, partyName, universalEntitySpec); 121 } 122 123 if(vendor != null) { 124 sendEvent(vendor.getPartyPK(), EventTypes.READ, null, null, getPartyPK()); 125 } 126 127 return vendor; 128 } 129 130 @Override 131 protected BaseResult getResult(Vendor vendor) { 132 var result = VendorResultFactory.getGetVendorResult(); 133 134 if(vendor != null) { 135 result.setVendor(vendorControl.getVendorTransfer(getUserVisit(), vendor)); 136 } 137 138 return result; 139 } 140 141}