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.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.Arrays; 039import java.util.Collections; 040import java.util.List; 041import javax.enterprise.context.RequestScoped; 042 043@RequestScoped 044public class EditEntityLongAttributeCommand 045 extends BaseEditCommand<EntityLongAttributeSpec, EntityLongAttributeEdit> { 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("LongAttribute", FieldType.SIGNED_LONG, true, null, null) 066 )); 067 } 068 069 /** Creates a new instance of EditEntityLongAttributeCommand */ 070 public EditEntityLongAttributeCommand() { 071 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 072 } 073 074 @Override 075 protected BaseResult execute() { 076 var result = CoreResultFactory.getEditEntityLongAttributeResult(); 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 EntityLongAttribute entityLongAttribute = null; 096 var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 097 098 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 099 entityLongAttribute = coreControl.getEntityLongAttribute(entityAttribute, entityInstance); 100 101 if(entityLongAttribute != null) { 102 if(editMode.equals(EditMode.LOCK)) { 103 result.setEntityLongAttribute(coreControl.getEntityLongAttributeTransfer(getUserVisit(), entityLongAttribute, entityInstance)); 104 105 if(lockEntity(basePK)) { 106 var edit = CoreEditFactory.getEntityLongAttributeEdit(); 107 108 result.setEdit(edit); 109 edit.setLongAttribute(entityLongAttribute.getLongAttribute().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.UnknownEntityLongAttribute.name(), 119 EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName); 120 } 121 } else if(editMode.equals(EditMode.UPDATE)) { 122 var entityAttributeLong = coreControl.getEntityAttributeLong(entityAttribute); 123 var longAttribute = Long.valueOf(edit.getLongAttribute()); 124 125 if(entityAttributeLong != null) { 126 var upperRangeLongValue = entityAttributeLong.getUpperRangeLongValue(); 127 var lowerRangeLongValue = entityAttributeLong.getLowerRangeLongValue(); 128 129 if(upperRangeLongValue != null && longAttribute > upperRangeLongValue){ 130 addExecutionError(ExecutionErrors.UpperRangeExceeded.name(), 131 upperRangeLongValue, longAttribute); 132 } 133 134 if(lowerRangeLongValue != null && longAttribute < lowerRangeLongValue) { 135 addExecutionError(ExecutionErrors.LowerRangeExceeded.name(), 136 lowerRangeLongValue, longAttribute); 137 } 138 } 139 140 141 if(!hasExecutionErrors()) { 142 entityLongAttribute = coreControl.getEntityLongAttributeForUpdate(entityAttribute, entityInstance); 143 144 if(entityLongAttribute != null) { 145 if(lockEntityForUpdate(basePK)) { 146 try { 147 var entityLongAttributeValue = coreControl.getEntityLongAttributeValueForUpdate(entityLongAttribute); 148 149 entityLongAttributeValue.setLongAttribute(longAttribute); 150 151 coreControl.updateEntityLongAttributeFromValue(entityLongAttributeValue, getPartyPK()); 152 } finally { 153 unlockEntity(basePK); 154 basePK = null; 155 } 156 } else { 157 addExecutionError(ExecutionErrors.EntityLockStale.name()); 158 } 159 } else { 160 addExecutionError(ExecutionErrors.UnknownEntityLongAttribute.name(), 161 EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), 162 entityAttribute.getLastDetail().getEntityAttributeName()); 163 } 164 } 165 } 166 167 if(basePK != null) { 168 result.setEntityLock(getEntityLockTransfer(basePK)); 169 } 170 171 if(entityLongAttribute != null) { 172 result.setEntityLongAttribute(coreControl.getEntityLongAttributeTransfer(getUserVisit(), entityLongAttribute, entityInstance)); 173 } 174 } else { 175 addExecutionError(ExecutionErrors.MismatchedEntityType.name()); 176 } 177 } 178 } else { 179 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 180 } 181 } 182 } else { 183 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 184 } 185 186 return result; 187 } 188 189}