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