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.employee.server.command;
018
019import com.echothree.control.user.core.common.spec.UniversalEntitySpec;
020import com.echothree.control.user.employee.common.form.GetEmployeeForm;
021import com.echothree.control.user.employee.common.result.EmployeeResultFactory;
022import com.echothree.model.control.core.common.EventTypes;
023import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
024import com.echothree.model.control.employee.server.control.EmployeeControl;
025import com.echothree.model.control.employee.server.logic.PartyEmployeeLogic;
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.data.employee.server.entity.PartyEmployee;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
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 GetEmployeeCommand
046        extends BaseSingleEntityCommand<PartyEmployee, GetEmployeeForm> {
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.Employee.name(), SecurityRoles.Review.name())
057                ))
058        ));
059
060        FORM_FIELD_DEFINITIONS = List.of(
061                new FieldDefinition("EmployeeName", 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    EmployeeControl employeeControl;
070
071    @Inject
072    EntityInstanceLogic entityInstanceLogic;
073
074    @Inject
075    PartyEmployeeLogic partyEmployeeLogic;
076
077    @Inject
078    PartyLogic partyLogic;
079
080    /** Creates a new instance of GetEmployeeCommand */
081    public GetEmployeeCommand() {
082        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
083    }
084
085    String employeeName;
086    String partyName;
087    UniversalEntitySpec universalEntitySpec;
088    int parameterCount;
089
090    @Override
091    public SecurityResult security() {
092        var securityResult = super.security();
093
094        employeeName = form.getEmployeeName();
095        partyName = form.getPartyName();
096        universalEntitySpec = form;
097        parameterCount = (employeeName == 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 PartyEmployee getEntity() {
109        PartyEmployee partyEmployee = null;
110
111        if(parameterCount == 0) {
112            var party = getParty();
113
114            partyLogic.checkPartyType(this, party, PartyTypes.EMPLOYEE.name());
115
116            if(!hasExecutionErrors()) {
117                partyEmployee = employeeControl.getPartyEmployee(party);
118            }
119        } else {
120            partyEmployee = partyEmployeeLogic.getPartyEmployeeByName(this, employeeName, partyName, universalEntitySpec);
121        }
122
123        if(partyEmployee != null) {
124            sendEvent(partyEmployee.getPartyPK(), EventTypes.READ, null, null, getPartyPK());
125        }
126
127        return partyEmployee;
128    }
129
130    @Override
131    protected BaseResult getResult(PartyEmployee partyEmployee) {
132        var result = EmployeeResultFactory.getGetEmployeeResult();
133
134        if(partyEmployee != null) {
135            result.setEmployee(employeeControl.getEmployeeTransfer(getUserVisit(), partyEmployee));
136        }
137
138        return result;
139    }
140
141}