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