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
046    /** Creates a new instance of GetEmployeeLoginDefaultsCommand */
047    public GetEmployeeLoginDefaultsCommand() {
048        super(null, FORM_FIELD_DEFINITIONS, true);
049    }
050    
051    @Override
052    protected UserLogin getEntity() {
053        UserLogin userLogin = null;
054        var userSession = userControl.getUserSessionByUserVisit(getUserVisit());
055
056        if(userSession != null) {
057            var party = userSession.getParty();
058
059            if(party != null) {
060                if(party.getLastDetail().getPartyType().getPartyTypeName().equals(PartyTypes.EMPLOYEE.name())) {
061                    userLogin = userControl.getUserLogin(party);
062                }
063            }
064        }
065
066        return userLogin;
067    }
068
069    @Override
070    protected BaseResult getResult(UserLogin userLogin) {
071        var result = AuthenticationResultFactory.getGetEmployeeLoginDefaultsResult();
072        String username = null;
073        String companyName = null;
074
075        if(userLogin != null) {
076            var userSession = userControl.getUserSessionByUserVisit(getUserVisit());
077
078            username = userLogin.getUsername();
079
080            if(userSession != null) {
081                var partyRelationship = userSession.getPartyRelationship();
082
083                if(partyRelationship != null) {
084                    var fromParty = partyRelationship.getFromParty();
085                    var partyCompany = partyControl.getPartyCompany(fromParty);
086
087                    if(partyCompany != null) {
088                        companyName = partyCompany.getPartyCompanyName();
089                    }
090                }
091            }
092        }
093
094        var employeeLoginForm = AuthenticationFormFactory.getEmployeeLoginForm();
095        employeeLoginForm.setUsername(username);
096        employeeLoginForm.setCompanyName(companyName);
097        result.setEmployeeLoginForm(employeeLoginForm);
098
099        return result;
100    }
101    
102}