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.EntityLongAttributeEdit; 021import com.echothree.control.user.core.common.form.EditEntityLongAttributeForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.spec.EntityLongAttributeSpec; 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.EntityLongAttribute; 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; 040 041@Dependent 042public class EditEntityLongAttributeCommand 043 extends BaseEditCommand<EntityLongAttributeSpec, EntityLongAttributeEdit> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 047 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 048 049 static { 050 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 051 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 052 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null) 053 )); 054 055 SPEC_FIELD_DEFINITIONS = List.of( 056 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 057 new FieldDefinition("Uuid", FieldType.UUID, false, null, null), 058 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null), 059 new FieldDefinition("EntityAttributeUuid", FieldType.UUID, false, null, null) 060 ); 061 062 EDIT_FIELD_DEFINITIONS = List.of( 063 new FieldDefinition("LongAttribute", FieldType.SIGNED_LONG, true, null, null) 064 ); 065 } 066 067 /** Creates a new instance of EditEntityLongAttributeCommand */ 068 public EditEntityLongAttributeCommand() { 069 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 070 } 071 072 @Override 073 protected BaseResult execute() { 074 var result = CoreResultFactory.getEditEntityLongAttributeResult(); 075 var parameterCount = EntityInstanceLogic.getInstance().countPossibleEntitySpecs(spec); 076 077 if(parameterCount == 1) { 078 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, spec); 079 080 if(!hasExecutionErrors()) { 081 var entityAttributeName = spec.getEntityAttributeName(); 082 var entityAttributeUuid = spec.getEntityAttributeUuid(); 083 084 parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUuid == null ? 0 : 1); 085 086 if(parameterCount == 1) { 087 var entityAttribute = entityAttributeName == null ? 088 EntityAttributeLogic.getInstance().getEntityAttributeByUuid(this, entityAttributeUuid) : 089 EntityAttributeLogic.getInstance().getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName); 090 091 if(!hasExecutionErrors()) { 092 if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) { 093 EntityLongAttribute entityLongAttribute = null; 094 var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 095 096 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 097 entityLongAttribute = coreControl.getEntityLongAttribute(entityAttribute, entityInstance); 098 099 if(entityLongAttribute != null) { 100 if(editMode.equals(EditMode.LOCK)) { 101 result.setEntityLongAttribute(coreControl.getEntityLongAttributeTransfer(getUserVisit(), entityLongAttribute, entityInstance)); 102 103 if(lockEntity(basePK)) { 104 var edit = CoreEditFactory.getEntityLongAttributeEdit(); 105 106 result.setEdit(edit); 107 edit.setLongAttribute(entityLongAttribute.getLongAttribute().toString()); 108 } else { 109 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 110 } 111 } else { // EditMode.ABANDON 112 unlockEntity(basePK); 113 basePK = null; 114 } 115 } else { 116 addExecutionError(ExecutionErrors.UnknownEntityLongAttribute.name(), 117 EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName); 118 } 119 } else if(editMode.equals(EditMode.UPDATE)) { 120 var entityAttributeLong = coreControl.getEntityAttributeLong(entityAttribute); 121 var longAttribute = Long.valueOf(edit.getLongAttribute()); 122 123 if(entityAttributeLong != null) { 124 var upperRangeLongValue = entityAttributeLong.getUpperRangeLongValue(); 125 var lowerRangeLongValue = entityAttributeLong.getLowerRangeLongValue(); 126 127 if(upperRangeLongValue != null && longAttribute > upperRangeLongValue){ 128 addExecutionError(ExecutionErrors.UpperRangeExceeded.name(), 129 upperRangeLongValue, longAttribute); 130 } 131 132 if(lowerRangeLongValue != null && longAttribute < lowerRangeLongValue) { 133 addExecutionError(ExecutionErrors.LowerRangeExceeded.name(), 134 lowerRangeLongValue, longAttribute); 135 } 136 } 137 138 139 if(!hasExecutionErrors()) { 140 entityLongAttribute = coreControl.getEntityLongAttributeForUpdate(entityAttribute, entityInstance); 141 142 if(entityLongAttribute != null) { 143 if(lockEntityForUpdate(basePK)) { 144 try { 145 var entityLongAttributeValue = coreControl.getEntityLongAttributeValueForUpdate(entityLongAttribute); 146 147 entityLongAttributeValue.setLongAttribute(longAttribute); 148 149 coreControl.updateEntityLongAttributeFromValue(entityLongAttributeValue, getPartyPK()); 150 } finally { 151 unlockEntity(basePK); 152 basePK = null; 153 } 154 } else { 155 addExecutionError(ExecutionErrors.EntityLockStale.name()); 156 } 157 } else { 158 addExecutionError(ExecutionErrors.UnknownEntityLongAttribute.name(), 159 EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), 160 entityAttribute.getLastDetail().getEntityAttributeName()); 161 } 162 } 163 } 164 165 if(basePK != null) { 166 result.setEntityLock(getEntityLockTransfer(basePK)); 167 } 168 169 if(entityLongAttribute != null) { 170 result.setEntityLongAttribute(coreControl.getEntityLongAttributeTransfer(getUserVisit(), entityLongAttribute, entityInstance)); 171 } 172 } else { 173 addExecutionError(ExecutionErrors.MismatchedEntityType.name()); 174 } 175 } 176 } else { 177 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 178 } 179 } 180 } else { 181 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 182 } 183 184 return result; 185 } 186 187}