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.util.server.control; 018 019import com.echothree.model.control.core.server.control.EntityLockControl; 020import com.echothree.util.common.command.BaseSetStatusResult; 021import com.echothree.util.common.form.BaseForm; 022import com.echothree.util.common.message.ExecutionErrors; 023import com.echothree.util.common.validation.FieldDefinition; 024import com.echothree.util.server.persistence.BaseEntity; 025import com.echothree.util.server.persistence.Session; 026import java.util.List; 027 028public abstract class BaseSetStatusCommand<F extends BaseForm, R extends BaseSetStatusResult, BE extends BaseEntity, LE extends BaseEntity> 029 extends BaseSimpleCommand<F> { 030 031 protected BaseSetStatusCommand(CommandSecurityDefinition commandSecurityDefinition, List<FieldDefinition> formFieldDefinitions) { 032 super(commandSecurityDefinition, formFieldDefinitions, false); 033 } 034 035 protected abstract R getResult(); 036 037 protected abstract BE getEntity(); 038 039 protected abstract LE getLockEntity(BE baseEntity); 040 041 protected abstract void doUpdate(BE baseEntity); 042 043 @Override 044 protected R execute() { 045 var result = getResult(); 046 var baseEntity = getEntity(); 047 048 if(!hasExecutionErrors()) { 049 var entityLockControl = Session.getModelController(EntityLockControl.class); 050 var lockEntity = getLockEntity(baseEntity); 051 052 if(entityLockControl.lockEntity(lockEntity, getPartyPK()) != 0) { 053 if(entityLockControl.lockEntityForUpdate(lockEntity, getPartyPK())) { 054 try { 055 doUpdate(baseEntity); 056 } finally { 057 entityLockControl.unlockEntity(lockEntity, getPartyPK()); 058 } 059 } else { 060 addExecutionError(ExecutionErrors.EntityLockStale.name()); 061 } 062 } else { 063 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 064 } 065 066 if(hasExecutionErrors()) { 067 result.setEntityLock(entityLockControl.getEntityLockTransfer(getUserVisit(), lockEntity)); 068 } 069 } 070 071 return result; 072 } 073 074}