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.result.CoreResultFactory; 022import com.echothree.control.user.core.common.result.EditEntityLongAttributeResult; 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.core.server.logic.EntityLongAttributeLogic; 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.EntityInstance; 030import com.echothree.model.data.core.server.entity.EntityLongAttribute; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.persistence.BasePK; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseEditCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.persistence.BaseEntity; 039import com.echothree.util.server.persistence.PersistenceUtils; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042import javax.inject.Inject; 043 044@Dependent 045public class EditEntityLongAttributeCommand 046 extends BaseEditCommand<EntityLongAttributeSpec, EntityLongAttributeEdit, EditEntityLongAttributeResult, EntityLongAttribute, EntityInstance> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 050 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null) 056 )); 057 058 SPEC_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 060 new FieldDefinition("Uuid", FieldType.UUID, false, null, null), 061 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("EntityAttributeUuid", FieldType.UUID, false, null, null) 063 ); 064 065 EDIT_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("LongAttribute", FieldType.SIGNED_LONG, true, null, null) 067 ); 068 } 069 070 @Inject 071 EntityAttributeLogic entityAttributeLogic; 072 073 @Inject 074 EntityInstanceLogic entityInstanceLogic; 075 076 @Inject 077 EntityLongAttributeLogic entityLongAttributeLogic; 078 079 private EntityInstance entityInstance; 080 private EntityAttribute entityAttribute; 081 private Long longAttribute; 082 083 /** Creates a new instance of EditEntityLongAttributeCommand */ 084 public EditEntityLongAttributeCommand() { 085 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 086 } 087 088 @Override 089 public EditEntityLongAttributeResult getResult() { 090 return CoreResultFactory.getEditEntityLongAttributeResult(); 091 } 092 093 @Override 094 public EntityLongAttributeEdit getEdit() { 095 return CoreEditFactory.getEntityLongAttributeEdit(); 096 } 097 098 @Override 099 public EntityLongAttribute getEntity(EditEntityLongAttributeResult result) { 100 EntityLongAttribute entity = null; 101 102 entityInstance = entityInstanceLogic.getEntityInstance(this, spec); 103 104 if(!hasExecutionErrors()) { 105 entityAttribute = entityAttributeLogic.getEntityAttribute(this, entityInstance, spec, spec); 106 } 107 108 if(!hasExecutionErrors()) { 109 entity = entityLongAttributeLogic.getEntityLongAttribute(this, entityAttribute, entityInstance, editModeToEntityPermission(editMode)); 110 } 111 112 return entity; 113 } 114 115 @Override 116 public EntityInstance getLockEntity(EntityLongAttribute entityLongAttribute) { 117 return entityInstance; 118 } 119 120 @Override 121 protected BasePK getLockTargetPK(BaseEntity lockTarget) { 122 return PersistenceUtils.getInstance().getBasePKFromEntityInstance((EntityInstance)lockTarget); 123 } 124 125 @Override 126 public void fillInResult(EditEntityLongAttributeResult result, EntityLongAttribute entityLongAttribute) { 127 result.setEntityLongAttribute(coreControl.getEntityLongAttributeTransfer(getUserVisit(), entityLongAttribute, entityInstance)); 128 } 129 130 @Override 131 public void doLock(EntityLongAttributeEdit edit, EntityLongAttribute entityLongAttribute) { 132 edit.setLongAttribute(entityLongAttribute.getLongAttribute().toString()); 133 } 134 135 @Override 136 protected void canUpdate(EntityLongAttribute entityLongAttribute) { 137 var entityAttributeLong = coreControl.getEntityAttributeLong(entityAttribute); 138 longAttribute = Long.valueOf(edit.getLongAttribute()); 139 140 if(entityAttributeLong != null) { 141 var upperRangeLongValue = entityAttributeLong.getUpperRangeLongValue(); 142 var lowerRangeLongValue = entityAttributeLong.getLowerRangeLongValue(); 143 144 if(upperRangeLongValue != null && longAttribute > upperRangeLongValue) { 145 addExecutionError(ExecutionErrors.UpperRangeExceeded.name(), 146 upperRangeLongValue, longAttribute); 147 } 148 149 if(lowerRangeLongValue != null && longAttribute < lowerRangeLongValue) { 150 addExecutionError(ExecutionErrors.LowerRangeExceeded.name(), 151 lowerRangeLongValue, longAttribute); 152 } 153 } 154 } 155 156 @Override 157 public void doUpdate(EntityLongAttribute entityLongAttribute) { 158 var entityLongAttributeValue = coreControl.getEntityLongAttributeValueForUpdate(entityLongAttribute); 159 160 entityLongAttributeValue.setLongAttribute(longAttribute); 161 162 entityLongAttributeLogic.updateEntityLongAttributeFromValue(this, entityLongAttributeValue, getPartyPK()); 163 } 164 165}