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.GetUserVisitForm;
020import com.echothree.control.user.authentication.common.result.AuthenticationResultFactory;
021import com.echothree.control.user.authentication.common.result.GetUserVisitResult;
022import com.echothree.model.control.user.server.control.UserControl;
023import com.echothree.model.data.party.server.entity.Party;
024import com.echothree.model.data.user.server.entity.UserKey;
025import com.echothree.model.data.user.server.entity.UserKeyDetail;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.server.control.BaseSimpleCommand;
028
029public class GetUserVisitCommand
030        extends BaseSimpleCommand<GetUserVisitForm> {
031    
032    /** Creates a new instance of GetUserVisitCommand */
033    public GetUserVisitCommand(GetUserVisitForm form) {
034        super(null, form, null, null, false);
035    }
036    
037    @Override
038    protected BaseResult execute() {
039        GetUserVisitResult result = AuthenticationResultFactory.getGetUserVisitResult();
040        UserControl userControl = getUserControl();
041        String 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.START_TIME_LONG);
056        }
057        
058        setUserVisitPK(userControl.createUserVisit(userKey, null, null, null, null, null, null, null).getPrimaryKey());
059        
060        Party 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}