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.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 java.sql.SQLException; 029import java.util.List; 030import javax.enterprise.context.Dependent; 031import javax.inject.Inject; 032 033@Dependent 034public class GetJobUserVisitCommand 035 extends BaseSimpleCommand<GetJobUserVisitForm> { 036 037 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 038 039 static { 040 FORM_FIELD_DEFINITIONS = List.of( 041 new FieldDefinition("JobName", FieldType.ENTITY_NAME, true, null, null) 042 ); 043 } 044 045 @Inject 046 JobControl jobControl; 047 048 049 /** Creates a new instance of GetJobUserVisitCommand */ 050 public GetJobUserVisitCommand() { 051 super(null, FORM_FIELD_DEFINITIONS, false); 052 053 // Isn't really the user executing this command, don't update the last command time. 054 setUpdateLastCommandTime(false); 055 } 056 057 private static final String SQL_STATE_BASE_TABLE_OR_VIEW_NOT_FOUND = "42S02"; 058 059 @Override 060 protected BaseResult execute() { 061 var result = AuthenticationResultFactory.getGetJobUserVisitResult(); 062 063 try { 064 var jobName = form.getJobName(); 065 var job = jobControl.getJobByName(jobName); 066 067 if(job != null) { 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 log.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}