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.EntityIntegerAttributeEdit; 021import com.echothree.control.user.core.common.result.CoreResultFactory; 022import com.echothree.control.user.core.common.result.EditEntityIntegerAttributeResult; 023import com.echothree.control.user.core.common.spec.EntityIntegerAttributeSpec; 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.EntityIntegerAttributeLogic; 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.EntityIntegerAttribute; 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 EditEntityIntegerAttributeCommand 046 extends BaseEditCommand<EntityIntegerAttributeSpec, EntityIntegerAttributeEdit, EditEntityIntegerAttributeResult, EntityIntegerAttribute, 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("IntegerAttribute", FieldType.SIGNED_INTEGER, true, null, null) 067 ); 068 } 069 070 @Inject 071 EntityAttributeLogic entityAttributeLogic; 072 073 @Inject 074 EntityInstanceLogic entityInstanceLogic; 075 076 @Inject 077 EntityIntegerAttributeLogic entityIntegerAttributeLogic; 078 079 private EntityInstance entityInstance; 080 private EntityAttribute entityAttribute; 081 private Integer integerAttribute; 082 083 /** Creates a new instance of EditEntityIntegerAttributeCommand */ 084 public EditEntityIntegerAttributeCommand() { 085 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 086 } 087 088 @Override 089 public EditEntityIntegerAttributeResult getResult() { 090 return CoreResultFactory.getEditEntityIntegerAttributeResult(); 091 } 092 093 @Override 094 public EntityIntegerAttributeEdit getEdit() { 095 return CoreEditFactory.getEntityIntegerAttributeEdit(); 096 } 097 098 @Override 099 public EntityIntegerAttribute getEntity(EditEntityIntegerAttributeResult result) { 100 EntityIntegerAttribute 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 = entityIntegerAttributeLogic.getEntityIntegerAttribute(this, entityAttribute, entityInstance, editModeToEntityPermission(editMode)); 110 } 111 112 return entity; 113 } 114 115 @Override 116 public EntityInstance getLockEntity(EntityIntegerAttribute entityIntegerAttribute) { 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(EditEntityIntegerAttributeResult result, EntityIntegerAttribute entityIntegerAttribute) { 127 result.setEntityIntegerAttribute(coreControl.getEntityIntegerAttributeTransfer(getUserVisit(), entityIntegerAttribute, entityInstance)); 128 } 129 130 @Override 131 public void doLock(EntityIntegerAttributeEdit edit, EntityIntegerAttribute entityIntegerAttribute) { 132 edit.setIntegerAttribute(entityIntegerAttribute.getIntegerAttribute().toString()); 133 } 134 135 @Override 136 protected void canUpdate(EntityIntegerAttribute entityIntegerAttribute) { 137 var entityAttributeInteger = coreControl.getEntityAttributeInteger(entityAttribute); 138 integerAttribute = Integer.valueOf(edit.getIntegerAttribute()); 139 140 if(entityAttributeInteger != null) { 141 var upperRangeIntegerValue = entityAttributeInteger.getUpperRangeIntegerValue(); 142 var lowerRangeIntegerValue = entityAttributeInteger.getLowerRangeIntegerValue(); 143 144 if(upperRangeIntegerValue != null && integerAttribute > upperRangeIntegerValue) { 145 addExecutionError(ExecutionErrors.UpperRangeExceeded.name(), 146 upperRangeIntegerValue, integerAttribute); 147 } 148 149 if(lowerRangeIntegerValue != null && integerAttribute < lowerRangeIntegerValue) { 150 addExecutionError(ExecutionErrors.LowerRangeExceeded.name(), 151 lowerRangeIntegerValue, integerAttribute); 152 } 153 } 154 } 155 156 @Override 157 public void doUpdate(EntityIntegerAttribute entityIntegerAttribute) { 158 var entityIntegerAttributeValue = coreControl.getEntityIntegerAttributeValueForUpdate(entityIntegerAttribute); 159 160 entityIntegerAttributeValue.setIntegerAttribute(integerAttribute); 161 162 entityIntegerAttributeLogic.updateEntityIntegerAttributeFromValue(this, entityIntegerAttributeValue, getPartyPK()); 163 } 164 165}