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