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.form.EditEntityIntegerAttributeForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
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.party.common.PartyTypes;
027import com.echothree.model.data.core.server.entity.EntityIntegerAttribute;
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 EditEntityIntegerAttributeCommand
043        extends BaseEditCommand<EntityIntegerAttributeSpec, EntityIntegerAttributeEdit> {
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("IntegerAttribute", FieldType.SIGNED_INTEGER, true, null, null)
064                );
065    }
066    
067    /** Creates a new instance of EditEntityIntegerAttributeCommand */
068    public EditEntityIntegerAttributeCommand() {
069        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
070    }
071    
072    @Override
073    protected BaseResult execute() {
074        var result = CoreResultFactory.getEditEntityIntegerAttributeResult();
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                            EntityIntegerAttribute entityIntegerAttribute = null;
094                            var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance);
095
096                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
097                                entityIntegerAttribute = coreControl.getEntityIntegerAttribute(entityAttribute, entityInstance);
098
099                                if(entityIntegerAttribute != null) {
100                                    if(editMode.equals(EditMode.LOCK)) {
101                                        result.setEntityIntegerAttribute(coreControl.getEntityIntegerAttributeTransfer(getUserVisit(), entityIntegerAttribute, entityInstance));
102
103                                        if(lockEntity(basePK)) {
104                                            var edit = CoreEditFactory.getEntityIntegerAttributeEdit();
105
106                                            result.setEdit(edit);
107                                            edit.setIntegerAttribute(entityIntegerAttribute.getIntegerAttribute().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.UnknownEntityIntegerAttribute.name(),
117                                            EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
118                                }
119                            } else if(editMode.equals(EditMode.UPDATE)) {
120                                var entityAttributeInteger = coreControl.getEntityAttributeInteger(entityAttribute);
121                                var integerAttribute = Integer.valueOf(edit.getIntegerAttribute());
122
123                                if(entityAttributeInteger != null) {
124                                    var upperRangeIntegerValue = entityAttributeInteger.getUpperRangeIntegerValue();
125                                    var lowerRangeIntegerValue = entityAttributeInteger.getLowerRangeIntegerValue();
126
127                                    if(upperRangeIntegerValue != null && integerAttribute > upperRangeIntegerValue){
128                                        addExecutionError(ExecutionErrors.UpperRangeExceeded.name(),
129                                                upperRangeIntegerValue, integerAttribute);
130                                    }
131
132                                    if(lowerRangeIntegerValue != null && integerAttribute < lowerRangeIntegerValue) {
133                                        addExecutionError(ExecutionErrors.LowerRangeExceeded.name(),
134                                                lowerRangeIntegerValue, integerAttribute);
135                                    }
136                                }
137
138                                if(!hasExecutionErrors()) {
139                                    entityIntegerAttribute = coreControl.getEntityIntegerAttributeForUpdate(entityAttribute, entityInstance);
140
141                                    if(entityIntegerAttribute != null) {
142                                        if(lockEntityForUpdate(basePK)) {
143                                            try {
144                                                var entityIntegerAttributeValue = coreControl.getEntityIntegerAttributeValueForUpdate(entityIntegerAttribute);
145
146                                                entityIntegerAttributeValue.setIntegerAttribute(integerAttribute);
147
148                                                coreControl.updateEntityIntegerAttributeFromValue(entityIntegerAttributeValue, getPartyPK());
149                                            } finally {
150                                                unlockEntity(basePK);
151                                                basePK = null;
152                                            }
153                                        } else {
154                                            addExecutionError(ExecutionErrors.EntityLockStale.name());
155                                        }
156                                    } else {
157                                        addExecutionError(ExecutionErrors.UnknownEntityIntegerAttribute.name(),
158                                                EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance),
159                                                entityAttribute.getLastDetail().getEntityAttributeName());
160                                    }
161                                }
162                            }
163
164                            if(basePK != null) {
165                                result.setEntityLock(getEntityLockTransfer(basePK));
166                            }
167
168                            if(entityIntegerAttribute != null) {
169                                result.setEntityIntegerAttribute(coreControl.getEntityIntegerAttributeTransfer(getUserVisit(), entityIntegerAttribute, entityInstance));
170                            }
171                        } else {
172                            addExecutionError(ExecutionErrors.MismatchedEntityType.name());
173                        }
174                    }
175                } else {
176                    addExecutionError(ExecutionErrors.InvalidParameterCount.name());
177                }
178            }
179        } else {
180            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
181        }
182
183        return result;
184    }
185    
186}