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.authentication.server.command;
018
019import com.echothree.control.user.authentication.common.form.AuthenticationFormFactory;
020import com.echothree.control.user.authentication.common.form.GetEmployeeLoginDefaultsForm;
021import com.echothree.control.user.authentication.common.result.AuthenticationResultFactory;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.control.PartyControl;
024import com.echothree.model.data.user.server.entity.UserLogin;
025import com.echothree.util.common.command.BaseResult;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.server.control.BaseSingleEntityCommand;
028import java.util.List;
029import javax.enterprise.context.Dependent;
030import javax.inject.Inject;
031
032@Dependent
033public class GetEmployeeLoginDefaultsCommand
034        extends BaseSingleEntityCommand<UserLogin, GetEmployeeLoginDefaultsForm> {
035    
036    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
037
038    static {
039        FORM_FIELD_DEFINITIONS = List.of();
040    }
041    
042    @Inject
043    PartyControl partyControl;
044
045    /** Creates a new instance of GetEmployeeLoginDefaultsCommand */
046    public GetEmployeeLoginDefaultsCommand() {
047        super(null, FORM_FIELD_DEFINITIONS, true);
048    }
049    
050    @Override
051    protected UserLogin getEntity() {
052        UserLogin userLogin = null;
053        var userSession = userControl.getUserSessionByUserVisit(getUserVisit());
054
055        if(userSession != null) {
056            var party = userSession.getParty();
057
058            if(party != null) {
059                if(party.getLastDetail().getPartyType().getPartyTypeName().equals(PartyTypes.EMPLOYEE.name())) {
060                    userLogin = userControl.getUserLogin(party);
061                }
062            }
063        }
064
065        return userLogin;
066    }
067
068    @Override
069    protected BaseResult getResult(UserLogin userLogin) {
070        var result = AuthenticationResultFactory.getGetEmployeeLoginDefaultsResult();
071        String username = null;
072        String companyName = null;
073
074        if(userLogin != null) {
075            var userSession = userControl.getUserSessionByUserVisit(getUserVisit());
076
077            username = userLogin.getUsername();
078
079            if(userSession != null) {
080                var partyRelationship = userSession.getPartyRelationship();
081
082                if(partyRelationship != null) {
083                    var fromParty = partyRelationship.getFromParty();
084                    var partyCompany = partyControl.getPartyCompany(fromParty);
085
086                    if(partyCompany != null) {
087                        companyName = partyCompany.getPartyCompanyName();
088                    }
089                }
090            }
091        }
092
093        var employeeLoginForm = AuthenticationFormFactory.getEmployeeLoginForm();
094        employeeLoginForm.setUsername(username);
095        employeeLoginForm.setCompanyName(companyName);
096        result.setEmployeeLoginForm(employeeLoginForm);
097
098        return result;
099    }
100    
101}