001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.util.common.command.BaseResult;
025import com.echothree.util.common.validation.FieldDefinition;
026import com.echothree.util.server.control.BaseSimpleCommand;
027import com.echothree.util.server.persistence.Session;
028import java.util.List;
029import javax.enterprise.context.RequestScoped;
030
031@RequestScoped
032public class GetEmployeeLoginDefaultsCommand
033        extends BaseSimpleCommand<GetEmployeeLoginDefaultsForm> {
034    
035    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
036
037    static {
038        FORM_FIELD_DEFINITIONS = List.of();
039    }
040    
041    /** Creates a new instance of GetEmployeeLoginDefaultsCommand */
042    public GetEmployeeLoginDefaultsCommand() {
043        super(null, FORM_FIELD_DEFINITIONS, true);
044    }
045    
046    @Override
047    protected BaseResult execute() {
048        var result = AuthenticationResultFactory.getGetEmployeeLoginDefaultsResult();
049        var userControl = getUserControl();
050        var userSession = userControl.getUserSessionByUserVisit(getUserVisit());
051        String username = null;
052        String companyName = null;
053        
054        if(userSession != null) {
055            var party = userSession.getParty();
056            
057            if(party != null) {
058                if(party.getLastDetail().getPartyType().getPartyTypeName().equals(PartyTypes.EMPLOYEE.name())) {
059                    var userLogin = userControl.getUserLogin(party);
060                    var partyRelationship = userSession.getPartyRelationship();
061                    
062                    username = userLogin.getUsername();
063                    
064                    if(partyRelationship != null) {
065                        var partyControl = Session.getModelController(PartyControl.class);
066                        var fromParty = partyRelationship.getFromParty();
067                        var partyCompany = partyControl.getPartyCompany(fromParty);
068                        
069                        if(partyCompany != null) {
070                            companyName = partyCompany.getPartyCompanyName();
071                        }
072                    }
073                }
074            }
075        }
076
077        var employeeLoginForm = AuthenticationFormFactory.getEmployeeLoginForm();
078        employeeLoginForm.setUsername(username);
079        employeeLoginForm.setCompanyName(companyName);
080        result.setEmployeeLoginForm(employeeLoginForm);
081        
082        return result;
083    }
084    
085}