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.core.common.spec.UniversalEntitySpec; 020import com.echothree.control.user.party.common.form.GetPartyForm; 021import com.echothree.control.user.party.common.result.PartyResultFactory; 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.control.PartyControl; 026import com.echothree.model.control.party.server.logic.PartyLogic; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.data.party.server.entity.Party; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.command.BaseResult; 032import com.echothree.util.common.command.SecurityResult; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseSingleEntityCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.echothree.util.server.persistence.Session; 040import java.util.List; 041 042public class GetPartyCommand 043 extends BaseSingleEntityCommand<Party, GetPartyForm> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 047 048 static { 049 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 050 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 051 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 052 new PartyTypeDefinition(PartyTypes.VENDOR.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 054 new SecurityRoleDefinition(SecurityRoleGroups.Party.name(), SecurityRoles.Review.name()) 055 )) 056 )); 057 058 FORM_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 061 new FieldDefinition("Key", FieldType.KEY, false, null, null), 062 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 063 new FieldDefinition("Ulid", FieldType.ULID, false, null, null) 064 ); 065 } 066 067 /** Creates a new instance of GetPartyCommand */ 068 public GetPartyCommand(UserVisitPK userVisitPK, GetPartyForm form) { 069 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 070 } 071 072 String partyName; 073 UniversalEntitySpec universalEntitySpec; 074 int parameterCount; 075 076 @Override 077 public SecurityResult security() { 078 var securityResult = super.security(); 079 080 partyName = form.getPartyName(); 081 universalEntitySpec = form; 082 parameterCount = (partyName == null ? 0 : 1) + 083 EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 084 085 if(!canSpecifyParty() && parameterCount != 0) { 086 securityResult = getInsufficientSecurityResult(); 087 } 088 089 return securityResult; 090 } 091 092 @Override 093 protected Party getEntity() { 094 Party party; 095 096 if(parameterCount == 0) { 097 party = getParty(); 098 } else { 099 party = PartyLogic.getInstance().getPartyByName(this, partyName, universalEntitySpec); 100 } 101 102 if(party != null) { 103 sendEvent(party.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 104 } 105 106 return party; 107 } 108 109 @Override 110 protected BaseResult getResult(Party party) { 111 var result = PartyResultFactory.getGetPartyResult(); 112 113 if(party != null) { 114 var partyControl = Session.getModelController(PartyControl.class); 115 116 result.setParty(partyControl.getPartyTransfer(getUserVisit(), party)); 117 } 118 119 return result; 120 } 121 122}