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.GetUserVisitForm;
020import com.echothree.control.user.authentication.common.result.AuthenticationResultFactory;
021import com.echothree.model.data.user.server.entity.UserKey;
022import com.echothree.model.data.user.server.entity.UserKeyDetail;
023import com.echothree.util.common.command.BaseResult;
024import com.echothree.util.server.control.BaseSimpleCommand;
025import javax.enterprise.context.Dependent;
026import javax.inject.Inject;
027
028@Dependent
029public class GetUserVisitCommand
030        extends BaseSimpleCommand<GetUserVisitForm> {
031    
032    /** Creates a new instance of GetUserVisitCommand */
033
034    public GetUserVisitCommand() {
035        super(null, null, false);
036    }
037    
038    @Override
039    protected BaseResult execute() {
040        var result = AuthenticationResultFactory.getGetUserVisitResult();
041        var userKeyName = form.getUserKeyName();
042        UserKeyDetail userKeyDetail = null;
043        
044        if(userKeyName != null) {
045            userKeyDetail = userControl.getUserKeyDetailByNameForUpdate(userKeyName);
046        }
047        
048        UserKey userKey;
049        if(userKeyDetail == null) {
050            userKey = userControl.createUserKey();
051            userKeyDetail = userKey.getLastDetail();
052            userKeyName = userKeyDetail.getUserKeyName();
053        } else {
054            userKey = userKeyDetail.getUserKey();
055            userControl.getUserKeyStatusForUpdate(userKey).setLastSeenTime(session.getStartTime());
056        }
057        
058        setUserVisitPK(userControl.createUserVisit(userKey, null, null, null, null, null, null, null).getPrimaryKey());
059
060        var party = userKeyDetail.getParty();
061        if(party != null) {
062            userControl.associatePartyToUserVisit(getUserVisitForUpdate(), party, userKeyDetail.getPartyRelationship(), null);
063        }
064        
065        result.setUserKeyName(userKeyName);
066        result.setUserVisitPK(getUserVisitPK());
067        
068        return result;
069    }
070    
071}