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; 040import javax.inject.Inject; 041 042@Dependent 043public class EditEntityLongAttributeCommand 044 extends BaseEditCommand<EntityLongAttributeSpec, EntityLongAttributeEdit> { 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("LongAttribute", FieldType.SIGNED_LONG, 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 EditEntityLongAttributeCommand */ 076 public EditEntityLongAttributeCommand() { 077 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 078 } 079 080 @Override 081 protected BaseResult execute() { 082 var result = CoreResultFactory.getEditEntityLongAttributeResult(); 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 EntityLongAttribute entityLongAttribute = null; 102 var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 103 104 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 105 entityLongAttribute = coreControl.getEntityLongAttribute(entityAttribute, entityInstance); 106 107 if(entityLongAttribute != null) { 108 if(editMode.equals(EditMode.LOCK)) { 109 result.setEntityLongAttribute(coreControl.getEntityLongAttributeTransfer(getUserVisit(), entityLongAttribute, entityInstance)); 110 111 if(lockEntity(basePK)) { 112 var edit = CoreEditFactory.getEntityLongAttributeEdit(); 113 114 result.setEdit(edit); 115 edit.setLongAttribute(entityLongAttribute.getLongAttribute().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.UnknownEntityLongAttribute.name(), 125 entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), entityAttributeName); 126 } 127 } else if(editMode.equals(EditMode.UPDATE)) { 128 var entityAttributeLong = coreControl.getEntityAttributeLong(entityAttribute); 129 var longAttribute = Long.valueOf(edit.getLongAttribute()); 130 131 if(entityAttributeLong != null) { 132 var upperRangeLongValue = entityAttributeLong.getUpperRangeLongValue(); 133 var lowerRangeLongValue = entityAttributeLong.getLowerRangeLongValue(); 134 135 if(upperRangeLongValue != null && longAttribute > upperRangeLongValue){ 136 addExecutionError(ExecutionErrors.UpperRangeExceeded.name(), 137 upperRangeLongValue, longAttribute); 138 } 139 140 if(lowerRangeLongValue != null && longAttribute < lowerRangeLongValue) { 141 addExecutionError(ExecutionErrors.LowerRangeExceeded.name(), 142 lowerRangeLongValue, longAttribute); 143 } 144 } 145 146 147 if(!hasExecutionErrors()) { 148 entityLongAttribute = coreControl.getEntityLongAttributeForUpdate(entityAttribute, entityInstance); 149 150 if(entityLongAttribute != null) { 151 if(lockEntityForUpdate(basePK)) { 152 try { 153 var entityLongAttributeValue = coreControl.getEntityLongAttributeValueForUpdate(entityLongAttribute); 154 155 entityLongAttributeValue.setLongAttribute(longAttribute); 156 157 coreControl.updateEntityLongAttributeFromValue(entityLongAttributeValue, getPartyPK()); 158 } finally { 159 unlockEntity(basePK); 160 basePK = null; 161 } 162 } else { 163 addExecutionError(ExecutionErrors.EntityLockStale.name()); 164 } 165 } else { 166 addExecutionError(ExecutionErrors.UnknownEntityLongAttribute.name(), 167 entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), 168 entityAttribute.getLastDetail().getEntityAttributeName()); 169 } 170 } 171 } 172 173 if(basePK != null) { 174 result.setEntityLock(getEntityLockTransfer(basePK)); 175 } 176 177 if(entityLongAttribute != null) { 178 result.setEntityLongAttribute(coreControl.getEntityLongAttributeTransfer(getUserVisit(), entityLongAttribute, 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}