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.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 java.util.List;
042import javax.enterprise.context.Dependent;
043import javax.inject.Inject;
044
045@Dependent
046public class GetCustomerCommand
047        extends BaseSingleEntityCommand<Customer, GetCustomerForm> {
048
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
051
052    static {
053        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
054                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
055                new PartyTypeDefinition(PartyTypes.CUSTOMER.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
057                        new SecurityRoleDefinition(SecurityRoleGroups.Customer.name(), SecurityRoles.Review.name())
058                ))
059        ));
060
061        FORM_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("CustomerName", FieldType.ENTITY_NAME, false, null, null),
063                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
064                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
065                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
066        );
067    }
068
069    @Inject
070    CustomerControl customerControl;
071
072    @Inject
073    WishlistControl wishlistControl;
074
075    @Inject
076    CustomerLogic customerLogic;
077
078    @Inject
079    EntityInstanceLogic entityInstanceLogic;
080
081    @Inject
082    PartyLogic partyLogic;
083
084    /** Creates a new instance of GetCustomerCommand */
085    public GetCustomerCommand() {
086        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
087    }
088
089    String customerName;
090    String partyName;
091    UniversalEntitySpec universalEntitySpec;
092    int parameterCount;
093
094    @Override
095    public SecurityResult security() {
096        var securityResult = super.security();
097
098        customerName = form.getCustomerName();
099        partyName = form.getPartyName();
100        universalEntitySpec = form;
101        parameterCount = (customerName == null ? 0 : 1) + (partyName == null ? 0 : 1) +
102                entityInstanceLogic.countPossibleEntitySpecs(form);
103
104        if(!canSpecifyParty() && parameterCount != 0) {
105            securityResult = getInsufficientSecurityResult();
106        }
107
108        return securityResult;
109    }
110
111    @Override
112    protected Customer getEntity() {
113        Customer customer = null;
114
115        if(parameterCount == 0) {
116            var party = getParty();
117
118            partyLogic.checkPartyType(this, party, PartyTypes.CUSTOMER.name());
119
120            if(!hasExecutionErrors()) {
121                customer = customerControl.getCustomer(party);
122            }
123        } else {
124            customer = customerLogic.getCustomerByName(this, customerName, partyName, universalEntitySpec);
125        }
126
127        if(customer != null) {
128            sendEvent(customer.getPartyPK(), EventTypes.READ, null, null, getPartyPK());
129        }
130
131        return customer;
132    }
133
134    @Override
135    protected BaseResult getResult(Customer customer) {
136        var result = CustomerResultFactory.getGetCustomerResult();
137
138        if(customer != null) {
139            var userVisit = getUserVisit();
140            var companyParty = getCompanyParty();
141
142            result.setCustomer(customerControl.getCustomerTransfer(userVisit, customer));
143
144            if(companyParty != null) {
145                result.setWishlists(wishlistControl.getWishlistTransfers(userVisit, companyParty, customer.getParty()));
146            }
147        }
148
149        return result;
150    }
151
152}