001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.core.server.command; 018 019import com.echothree.control.user.core.common.edit.CoreEditFactory; 020import com.echothree.control.user.core.common.edit.EntityIntegerAttributeEdit; 021import com.echothree.control.user.core.common.form.EditEntityIntegerAttributeForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.spec.EntityIntegerAttributeSpec; 024import com.echothree.model.control.core.server.logic.EntityAttributeLogic; 025import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.data.core.server.entity.EntityIntegerAttribute; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.command.EditMode; 034import com.echothree.util.server.control.BaseEditCommand; 035import com.echothree.util.server.control.CommandSecurityDefinition; 036import com.echothree.util.server.control.PartyTypeDefinition; 037import com.echothree.util.server.persistence.PersistenceUtils; 038import java.util.List; 039import javax.enterprise.context.Dependent; 040import javax.inject.Inject; 041 042@Dependent 043public class EditEntityIntegerAttributeCommand 044 extends BaseEditCommand<EntityIntegerAttributeSpec, EntityIntegerAttributeEdit> { 045 046 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 047 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 048 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 049 050 static { 051 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 052 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null) 054 )); 055 056 SPEC_FIELD_DEFINITIONS = List.of( 057 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 058 new FieldDefinition("Uuid", FieldType.UUID, false, null, null), 059 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("EntityAttributeUuid", FieldType.UUID, false, null, null) 061 ); 062 063 EDIT_FIELD_DEFINITIONS = List.of( 064 new FieldDefinition("IntegerAttribute", FieldType.SIGNED_INTEGER, true, null, null) 065 ); 066 } 067 068 @Inject 069 EntityAttributeLogic entityAttributeLogic; 070 071 @Inject 072 EntityInstanceLogic entityInstanceLogic; 073 074 075 /** Creates a new instance of EditEntityIntegerAttributeCommand */ 076 public EditEntityIntegerAttributeCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 protected BaseResult execute() { 082 var result = CoreResultFactory.getEditEntityIntegerAttributeResult(); 083 var parameterCount = entityInstanceLogic.countPossibleEntitySpecs(spec); 084 085 if(parameterCount == 1) { 086 var entityInstance = entityInstanceLogic.getEntityInstance(this, spec); 087 088 if(!hasExecutionErrors()) { 089 var entityAttributeName = spec.getEntityAttributeName(); 090 var entityAttributeUuid = spec.getEntityAttributeUuid(); 091 092 parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUuid == null ? 0 : 1); 093 094 if(parameterCount == 1) { 095 var entityAttribute = entityAttributeName == null ? 096 entityAttributeLogic.getEntityAttributeByUuid(this, entityAttributeUuid) : 097 entityAttributeLogic.getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName); 098 099 if(!hasExecutionErrors()) { 100 if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) { 101 EntityIntegerAttribute entityIntegerAttribute = null; 102 var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 103 104 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 105 entityIntegerAttribute = coreControl.getEntityIntegerAttribute(entityAttribute, entityInstance); 106 107 if(entityIntegerAttribute != null) { 108 if(editMode.equals(EditMode.LOCK)) { 109 result.setEntityIntegerAttribute(coreControl.getEntityIntegerAttributeTransfer(getUserVisit(), entityIntegerAttribute, entityInstance)); 110 111 if(lockEntity(basePK)) { 112 var edit = CoreEditFactory.getEntityIntegerAttributeEdit(); 113 114 result.setEdit(edit); 115 edit.setIntegerAttribute(entityIntegerAttribute.getIntegerAttribute().toString()); 116 } else { 117 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 118 } 119 } else { // EditMode.ABANDON 120 unlockEntity(basePK); 121 basePK = null; 122 } 123 } else { 124 addExecutionError(ExecutionErrors.UnknownEntityIntegerAttribute.name(), 125 entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), entityAttributeName); 126 } 127 } else if(editMode.equals(EditMode.UPDATE)) { 128 var entityAttributeInteger = coreControl.getEntityAttributeInteger(entityAttribute); 129 var integerAttribute = Integer.valueOf(edit.getIntegerAttribute()); 130 131 if(entityAttributeInteger != null) { 132 var upperRangeIntegerValue = entityAttributeInteger.getUpperRangeIntegerValue(); 133 var lowerRangeIntegerValue = entityAttributeInteger.getLowerRangeIntegerValue(); 134 135 if(upperRangeIntegerValue != null && integerAttribute > upperRangeIntegerValue){ 136 addExecutionError(ExecutionErrors.UpperRangeExceeded.name(), 137 upperRangeIntegerValue, integerAttribute); 138 } 139 140 if(lowerRangeIntegerValue != null && integerAttribute < lowerRangeIntegerValue) { 141 addExecutionError(ExecutionErrors.LowerRangeExceeded.name(), 142 lowerRangeIntegerValue, integerAttribute); 143 } 144 } 145 146 if(!hasExecutionErrors()) { 147 entityIntegerAttribute = coreControl.getEntityIntegerAttributeForUpdate(entityAttribute, entityInstance); 148 149 if(entityIntegerAttribute != null) { 150 if(lockEntityForUpdate(basePK)) { 151 try { 152 var entityIntegerAttributeValue = coreControl.getEntityIntegerAttributeValueForUpdate(entityIntegerAttribute); 153 154 entityIntegerAttributeValue.setIntegerAttribute(integerAttribute); 155 156 coreControl.updateEntityIntegerAttributeFromValue(entityIntegerAttributeValue, getPartyPK()); 157 } finally { 158 unlockEntity(basePK); 159 basePK = null; 160 } 161 } else { 162 addExecutionError(ExecutionErrors.EntityLockStale.name()); 163 } 164 } else { 165 addExecutionError(ExecutionErrors.UnknownEntityIntegerAttribute.name(), 166 entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), 167 entityAttribute.getLastDetail().getEntityAttributeName()); 168 } 169 } 170 } 171 172 if(basePK != null) { 173 result.setEntityLock(getEntityLockTransfer(basePK)); 174 } 175 176 if(entityIntegerAttribute != null) { 177 result.setEntityIntegerAttribute(coreControl.getEntityIntegerAttributeTransfer(getUserVisit(), entityIntegerAttribute, entityInstance)); 178 } 179 } else { 180 addExecutionError(ExecutionErrors.MismatchedEntityType.name()); 181 } 182 } 183 } else { 184 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 185 } 186 } 187 } else { 188 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 189 } 190 191 return result; 192 } 193 194}