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.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 java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class GetPartyCommand
045        extends BaseSingleEntityCommand<Party, GetPartyForm> {
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.VENDOR.name(), null),
055                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
056                        new SecurityRoleDefinition(SecurityRoleGroups.Party.name(), SecurityRoles.Review.name())
057                ))
058        ));
059
060        FORM_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
063                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
064        );
065    }
066
067    @Inject
068    PartyControl partyControl;
069
070    @Inject
071    EntityInstanceLogic entityInstanceLogic;
072
073    @Inject
074    PartyLogic partyLogic;
075
076    /** Creates a new instance of GetPartyCommand */
077    public GetPartyCommand() {
078        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
079    }
080
081    String partyName;
082    UniversalEntitySpec universalEntitySpec;
083    int parameterCount;
084
085    @Override
086    public SecurityResult security() {
087        var securityResult = super.security();
088
089        partyName = form.getPartyName();
090        universalEntitySpec = form;
091        parameterCount = (partyName == null ? 0 : 1) +
092                entityInstanceLogic.countPossibleEntitySpecs(form);
093
094        if(!canSpecifyParty() && parameterCount != 0) {
095            securityResult = getInsufficientSecurityResult();
096        }
097
098        return securityResult;
099    }
100
101    @Override
102    protected Party getEntity() {
103        Party party;
104
105        if(parameterCount == 0) {
106            party = getParty();
107        } else {
108            party = partyLogic.getPartyByName(this, partyName, universalEntitySpec);
109        }
110
111        if(party != null) {
112            sendEvent(party.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
113        }
114
115        return party;
116    }
117
118    @Override
119    protected BaseResult getResult(Party party) {
120        var result = PartyResultFactory.getGetPartyResult();
121
122        if(party != null) {
123            result.setParty(partyControl.getPartyTransfer(getUserVisit(), party));
124        }
125
126        return result;
127    }
128
129}