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.GetVendorLoginDefaultsForm;
021import com.echothree.control.user.authentication.common.result.AuthenticationResultFactory;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.data.user.server.entity.UserLogin;
024import com.echothree.util.common.command.BaseResult;
025import com.echothree.util.common.validation.FieldDefinition;
026import com.echothree.util.server.control.BaseSingleEntityCommand;
027import java.util.List;
028import javax.enterprise.context.Dependent;
029
030@Dependent
031public class GetVendorLoginDefaultsCommand
032        extends BaseSingleEntityCommand<UserLogin, GetVendorLoginDefaultsForm> {
033    
034    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
035
036    static {
037        FORM_FIELD_DEFINITIONS = List.of();
038    }
039    
040    /** Creates a new instance of GetVendorLoginDefaultsCommand */
041    public GetVendorLoginDefaultsCommand() {
042        super(null, FORM_FIELD_DEFINITIONS, true);
043    }
044    
045    @Override
046    protected UserLogin getEntity() {
047        UserLogin userLogin = null;
048        var userSession = userControl.getUserSessionByUserVisit(getUserVisit());
049
050        if(userSession != null) {
051            var party = userSession.getParty();
052
053            if(party != null) {
054                if(party.getLastDetail().getPartyType().getPartyTypeName().equals(PartyTypes.VENDOR.name())) {
055                    userLogin = userControl.getUserLogin(party);
056                }
057            }
058        }
059
060        return userLogin;
061    }
062
063    @Override
064    protected BaseResult getResult(UserLogin userLogin) {
065        var result = AuthenticationResultFactory.getGetVendorLoginDefaultsResult();
066        var username = userLogin == null ? null : userLogin.getUsername();
067        var vendorLoginForm = AuthenticationFormFactory.getVendorLoginForm();
068
069        vendorLoginForm.setUsername(username);
070        result.setVendorLoginForm(vendorLoginForm);
071
072        return result;
073    }
074    
075}