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.track.server.command; 018 019import com.echothree.control.user.track.common.form.CreateUserVisitTrackForm; 020import com.echothree.model.control.track.server.control.TrackControl; 021import com.echothree.model.data.track.server.entity.Track; 022import com.echothree.model.data.user.common.pk.UserVisitPK; 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.common.command.BaseResult; 028import com.echothree.util.server.control.BaseSimpleCommand; 029import com.echothree.util.server.persistence.PersistenceUtils; 030import com.echothree.util.server.persistence.Session; 031import java.util.Arrays; 032import java.util.Collections; 033import java.util.List; 034 035public class CreateUserVisitTrackCommand 036 extends BaseSimpleCommand<CreateUserVisitTrackForm> { 037 038 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 039 040 static { 041 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 042 new FieldDefinition("TrackValue", FieldType.STRING, false, null, null) 043 )); 044 } 045 046 /** Creates a new instance of CreateTrackCommand */ 047 public CreateUserVisitTrackCommand(UserVisitPK userVisitPK, CreateUserVisitTrackForm form) { 048 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false); 049 } 050 051 @Override 052 protected BaseResult execute() { 053 String trackValue = form.getTrackValue(); 054 var parameterCount = trackValue == null ? 0 : 1; 055 056 if(parameterCount > 0) { 057 var trackControl = Session.getModelController(TrackControl.class); 058 Track track = null; 059 060 if(trackValue != null) { 061 track = trackControl.getTrackByValue(trackValue); 062 063 if(track == null) { 064 try { 065 track = trackControl.createTrack(trackValue, Boolean.FALSE, 0, getPartyPK()); 066 } catch(PersistenceDatabaseException pde) { 067 if(PersistenceUtils.getInstance().isIntegrityConstraintViolation(pde)) { 068 track = trackControl.getTrackByValue(trackValue); 069 pde = null; 070 } 071 072 if(pde != null) { 073 throw pde; 074 } 075 } 076 } 077 } 078 079 trackControl.createUserVisitTrack(getUserVisit(), session.START_TIME_LONG, track); 080 } else { 081 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 082 } 083 084 return null; 085 } 086 087}