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.customer.server.command; 018 019import com.echothree.control.user.core.common.spec.UniversalEntitySpec; 020import com.echothree.control.user.customer.common.form.GetCustomerForm; 021import com.echothree.control.user.customer.common.result.CustomerResultFactory; 022import com.echothree.model.control.core.common.EventTypes; 023import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 024import com.echothree.model.control.customer.server.control.CustomerControl; 025import com.echothree.model.control.customer.server.logic.CustomerLogic; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.party.server.logic.PartyLogic; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.control.wishlist.server.control.WishlistControl; 031import com.echothree.model.data.customer.server.entity.Customer; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.command.SecurityResult; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.server.control.BaseSingleEntityCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import com.echothree.util.server.persistence.Session; 042import java.util.List; 043 044public class GetCustomerCommand 045 extends BaseSingleEntityCommand<Customer, GetCustomerForm> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 049 050 static { 051 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 052 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 053 new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.Customer.name(), SecurityRoles.Review.name()) 056 )) 057 )); 058 059 FORM_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("CustomerName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 063 new FieldDefinition("Key", FieldType.KEY, false, null, null), 064 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 065 new FieldDefinition("Ulid", FieldType.ULID, false, null, null) 066 ); 067 } 068 069 /** Creates a new instance of GetCustomerCommand */ 070 public GetCustomerCommand(UserVisitPK userVisitPK, GetCustomerForm form) { 071 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 072 } 073 074 String customerName; 075 String partyName; 076 UniversalEntitySpec universalEntitySpec; 077 int parameterCount; 078 079 @Override 080 public SecurityResult security() { 081 var securityResult = super.security(); 082 083 customerName = form.getCustomerName(); 084 partyName = form.getPartyName(); 085 universalEntitySpec = form; 086 parameterCount = (customerName == null ? 0 : 1) + (partyName == null ? 0 : 1) + 087 EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 088 089 if(!canSpecifyParty() && parameterCount != 0) { 090 securityResult = getInsufficientSecurityResult(); 091 } 092 093 return securityResult; 094 } 095 096 @Override 097 protected Customer getEntity() { 098 Customer customer = null; 099 100 if(parameterCount == 0) { 101 var party = getParty(); 102 103 PartyLogic.getInstance().checkPartyType(this, party, PartyTypes.CUSTOMER.name()); 104 105 if(!hasExecutionErrors()) { 106 var customerControl = Session.getModelController(CustomerControl.class); 107 108 customer = customerControl.getCustomer(party); 109 } 110 } else { 111 customer = CustomerLogic.getInstance().getCustomerByName(this, customerName, partyName, universalEntitySpec); 112 } 113 114 if(customer != null) { 115 sendEvent(customer.getPartyPK(), EventTypes.READ, null, null, getPartyPK()); 116 } 117 118 return customer; 119 } 120 121 @Override 122 protected BaseResult getResult(Customer customer) { 123 var result = CustomerResultFactory.getGetCustomerResult(); 124 125 if(customer != null) { 126 var customerControl = Session.getModelController(CustomerControl.class); 127 var userVisit = getUserVisit(); 128 var companyParty = getCompanyParty(); 129 130 result.setCustomer(customerControl.getCustomerTransfer(userVisit, customer)); 131 132 if(companyParty != null) { 133 var wishlistControl = Session.getModelController(WishlistControl.class); 134 135 result.setWishlists(wishlistControl.getWishlistTransfers(userVisit, companyParty, customer.getParty())); 136 } 137 } 138 139 return result; 140 } 141 142}