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