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