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.common.transfer.EntityLockTransfer; 020import com.echothree.model.control.core.server.control.EntityLockControl; 021import com.echothree.model.data.user.common.pk.UserVisitPK; 022import com.echothree.util.common.exception.EntityLockException; 023import com.echothree.util.common.validation.FieldDefinition; 024import com.echothree.util.common.command.EditMode; 025import com.echothree.util.common.form.BaseEdit; 026import com.echothree.util.common.form.BaseEditForm; 027import com.echothree.util.common.form.BaseForm; 028import com.echothree.util.common.form.BaseSpec; 029import com.echothree.util.common.form.ValidationResult; 030import com.echothree.util.common.persistence.BasePK; 031import com.echothree.util.server.persistence.BaseEntity; 032import com.echothree.util.server.persistence.BaseValue; 033import com.echothree.util.server.persistence.Session; 034import com.echothree.util.server.validation.Validator; 035import java.util.List; 036 037public abstract class BaseEditCommand<S extends BaseSpec, E extends BaseEdit> 038 extends BaseCommand { 039 040 private EntityLockControl entityLockControl = null; 041 private List<FieldDefinition> specFieldDefinitions = null; 042 private List<FieldDefinition> editFieldDefinitions = null; 043 protected EditMode editMode = null; 044 protected S spec = null; 045 protected E edit = null; 046 047 protected BaseEditCommand(UserVisitPK userVisitPK, BaseEditForm<S, E> editForm, CommandSecurityDefinition commandSecurityDefinition, 048 List<FieldDefinition> specFieldDefinitions, List<FieldDefinition> editFieldDefinitions) { 049 super(userVisitPK, commandSecurityDefinition); 050 051 this.specFieldDefinitions = specFieldDefinitions; 052 this.editFieldDefinitions = editFieldDefinitions; 053 054 if(editForm != null) { 055 editMode = editForm.getEditMode(); 056 spec = editForm.getSpec(); 057 edit = editForm.getEdit(); 058 } 059 } 060 061 protected void setupValidatorForSpec(Validator validator) { 062 // Nothing. 063 } 064 065 protected void setupValidatorForEdit(Validator validator, BaseForm specForm) { 066 // Nothing. 067 } 068 069 protected ValidationResult validateSpec(Validator validator) { 070 return validator.validate(spec, getSpecFieldDefinitions()); 071 } 072 073 protected ValidationResult validateEdit(Validator validator) { 074 return validator.validate(edit, getEditFieldDefinitions()); 075 } 076 077 protected ValidationResult validateLock() { 078 Validator validator = new Validator(this); 079 080 setupValidatorForSpec(validator); 081 082 return validateSpec(validator); 083 } 084 085 protected void saveResultAfterEditValidatorErrors() { 086 // Nothing. 087 } 088 089 protected ValidationResult validateUpdate() { 090 Validator validator = new Validator(this); 091 ValidationResult validationResult; 092 BaseForm specForm = spec; 093 094 setupValidatorForSpec(validator); 095 validationResult = validateSpec(validator); 096 097 if(!validationResult.getHasErrors()) { 098 setupValidatorForEdit(validator, specForm); 099 validationResult = validateEdit(validator); 100 101 if(validationResult.getHasErrors()) { 102 saveResultAfterEditValidatorErrors(); 103 } 104 } 105 106 return validationResult; 107 } 108 109 @Override 110 protected ValidationResult validate() { 111 ValidationResult validationResult = null; 112 113 if(editMode.equals(EditMode.LOCK)) { 114 validationResult = validateLock(); 115 } else if(editMode.equals(EditMode.UPDATE)) { 116 validationResult = validateUpdate(); 117 } 118 119 return validationResult; 120 } 121 122 protected List<FieldDefinition> getSpecFieldDefinitions() { 123 return specFieldDefinitions; 124 } 125 126 protected void setSpecFieldDefinitions(List<FieldDefinition> specFieldDefinitions) { 127 this.specFieldDefinitions = specFieldDefinitions; 128 } 129 130 protected List<FieldDefinition> getEditFieldDefinitions() { 131 return editFieldDefinitions; 132 } 133 134 protected void setEditFieldDefinitions(List<FieldDefinition> editFieldDefinitions) { 135 this.editFieldDefinitions = editFieldDefinitions; 136 } 137 138 public EntityLockControl getEntityLockControl() { 139 if(entityLockControl == null) { 140 entityLockControl = Session.getModelController(EntityLockControl.class); 141 } 142 143 return entityLockControl; 144 } 145 146 public EntityLockTransfer getEntityLockTransfer(BaseEntity lockTarget) 147 throws EntityLockException { 148 return getEntityLockControl().getEntityLockTransfer(getUserVisit(), lockTarget); 149 } 150 151 public EntityLockTransfer getEntityLockTransfer(BasePK lockTarget) 152 throws EntityLockException { 153 return getEntityLockControl().getEntityLockTransfer(getUserVisit(), lockTarget); 154 } 155 156 public EntityLockTransfer getEntityLockTransfer(BaseValue lockTarget) 157 throws EntityLockException { 158 return getEntityLockControl().getEntityLockTransfer(getUserVisit(), lockTarget); 159 } 160 161 public boolean lockEntity(BaseEntity lockTarget) 162 throws EntityLockException { 163 return getEntityLockControl().lockEntity(lockTarget, getPartyPK()) != 0; 164 } 165 166 public boolean lockEntity(BasePK lockTarget) 167 throws EntityLockException { 168 return getEntityLockControl().lockEntity(lockTarget, getPartyPK()) != 0; 169 } 170 171 public boolean lockEntity(BaseValue lockTarget) 172 throws EntityLockException { 173 return getEntityLockControl().lockEntity(lockTarget, getPartyPK()) != 0; 174 } 175 176 public boolean lockEntityForUpdate(BaseEntity lockTarget) 177 throws EntityLockException { 178 return getEntityLockControl().lockEntityForUpdate(lockTarget, getPartyPK()); 179 } 180 181 public boolean lockEntityForUpdate(BasePK lockTarget) 182 throws EntityLockException { 183 return getEntityLockControl().lockEntityForUpdate(lockTarget, getPartyPK()); 184 } 185 186 public boolean lockEntityForUpdate(BaseValue lockTarget) 187 throws EntityLockException { 188 return getEntityLockControl().lockEntityForUpdate(lockTarget, getPartyPK()); 189 } 190 191 public boolean unlockEntity(BaseEntity lockTarget) 192 throws EntityLockException { 193 return getEntityLockControl().unlockEntity(lockTarget, getPartyPK()); 194 } 195 196 public boolean unlockEntity(BasePK lockTarget) 197 throws EntityLockException { 198 return getEntityLockControl().unlockEntity(lockTarget, getPartyPK()); 199 } 200 201 public boolean unlockEntity(BaseValue lockTarget) 202 throws EntityLockException { 203 return getEntityLockControl().unlockEntity(lockTarget, getPartyPK()); 204 } 205 206 public boolean isEntityLocked(BaseEntity lockTarget) 207 throws EntityLockException { 208 return getEntityLockControl().isEntityLocked(lockTarget, getPartyPK()); 209 } 210 211 public boolean isEntityLocked(BasePK lockTarget) 212 throws EntityLockException { 213 return getEntityLockControl().isEntityLocked(lockTarget, getPartyPK()); 214 } 215 216 public boolean isEntityLocked(BaseValue lockTarget) 217 throws EntityLockException { 218 return getEntityLockControl().isEntityLocked(lockTarget, getPartyPK()); 219 } 220 221}