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