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.GetJobUserVisitForm;
020import com.echothree.control.user.authentication.common.result.AuthenticationResultFactory;
021import com.echothree.model.control.job.server.control.JobControl;
022import com.echothree.util.common.command.BaseResult;
023import com.echothree.util.common.exception.PersistenceDatabaseException;
024import com.echothree.util.common.message.ExecutionErrors;
025import com.echothree.util.common.validation.FieldDefinition;
026import com.echothree.util.common.validation.FieldType;
027import com.echothree.util.server.control.BaseSimpleCommand;
028import com.echothree.util.server.persistence.Session;
029import java.sql.SQLException;
030import java.util.Arrays;
031import java.util.Collections;
032import java.util.List;
033import javax.enterprise.context.RequestScoped;
034
035@RequestScoped
036public class GetJobUserVisitCommand
037        extends BaseSimpleCommand<GetJobUserVisitForm> {
038    
039    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
040    
041    static {
042        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
043                new FieldDefinition("JobName", FieldType.ENTITY_NAME, true, null, null)
044                ));
045    }
046    
047    /** Creates a new instance of GetJobUserVisitCommand */
048    public GetJobUserVisitCommand() {
049        super(null, FORM_FIELD_DEFINITIONS, false);
050
051        // Isn't really the user executing this command, don't update the last command time.
052        setUpdateLastCommandTime(false);
053    }
054    
055    private static final String SQL_STATE_BASE_TABLE_OR_VIEW_NOT_FOUND = "42S02";
056    
057    @Override
058    protected BaseResult execute() {
059        var jobControl = Session.getModelController(JobControl.class);
060        var result = AuthenticationResultFactory.getGetJobUserVisitResult();
061        
062        try {
063            var jobName = form.getJobName();
064            var job = jobControl.getJobByName(jobName);
065
066            if(job != null) {
067                var userControl = getUserControl();
068                var userVisit = userControl.createUserVisit(null, null, null, null, null, null, null, null);
069
070                userControl.associatePartyToUserVisit(userVisit, job.getLastDetail().getRunAsParty(), null, null);
071
072                result.setUserVisitPK(userVisit.getPrimaryKey());
073                result.setJob(jobControl.getJobTransfer(userVisit, job));
074            } else {
075                addExecutionError(ExecutionErrors.UnknownJobName.name(), jobName);
076            }
077        } catch(PersistenceDatabaseException pde) {
078            var cause = pde.getCause();
079
080            if(cause instanceof SQLException se) {
081                if(se.getSQLState().equals(SQL_STATE_BASE_TABLE_OR_VIEW_NOT_FOUND)) {
082                    getLog().info("Ignoring \"" + se.getMessage() + "\"");
083
084                    // The table jobs did not exist, don't pass along exception. Tested w/ MySQL only.
085                    pde = null;
086                }
087            }
088
089            if(pde != null) {
090                throw pde;
091            }
092        }
093        
094        return result;
095    }
096    
097}