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.EntityBooleanAttributeEdit;
021import com.echothree.control.user.core.common.form.EditEntityBooleanAttributeForm;
022import com.echothree.control.user.core.common.result.CoreResultFactory;
023import com.echothree.control.user.core.common.spec.EntityBooleanAttributeSpec;
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.EntityBooleanAttribute;
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 EditEntityBooleanAttributeCommand
044        extends BaseEditCommand<EntityBooleanAttributeSpec, EntityBooleanAttributeEdit> {
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("BooleanAttribute", FieldType.BOOLEAN, 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 EditEntityBooleanAttributeCommand */
076    public EditEntityBooleanAttributeCommand() {
077        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
078    }
079    
080    @Override
081    protected BaseResult execute() {
082        var result = CoreResultFactory.getEditEntityBooleanAttributeResult();
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                            EntityBooleanAttribute entityBooleanAttribute = null;
102                            var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance);
103
104                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
105                                entityBooleanAttribute = coreControl.getEntityBooleanAttribute(entityAttribute, entityInstance);
106
107                                if(entityBooleanAttribute != null) {
108                                    if(editMode.equals(EditMode.LOCK)) {
109                                        result.setEntityBooleanAttribute(coreControl.getEntityBooleanAttributeTransfer(getUserVisit(), entityBooleanAttribute, entityInstance));
110
111                                        if(lockEntity(basePK)) {
112                                            var edit = CoreEditFactory.getEntityBooleanAttributeEdit();
113
114                                            result.setEdit(edit);
115                                            edit.setBooleanAttribute(entityBooleanAttribute.getBooleanAttribute().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.UnknownEntityBooleanAttribute.name(),
125                                            entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
126                                }
127                            } else if(editMode.equals(EditMode.UPDATE)) {
128                                entityBooleanAttribute = coreControl.getEntityBooleanAttributeForUpdate(entityAttribute, entityInstance);
129
130                                if(entityBooleanAttribute != null) {
131                                    if(lockEntityForUpdate(basePK)) {
132                                        try {
133                                            var entityBooleanAttributeValue = coreControl.getEntityBooleanAttributeValueForUpdate(entityBooleanAttribute);
134
135                                            entityBooleanAttributeValue.setBooleanAttribute(Boolean.valueOf(edit.getBooleanAttribute()));
136
137                                            coreControl.updateEntityBooleanAttributeFromValue(entityBooleanAttributeValue, getPartyPK());
138                                        } finally {
139                                            unlockEntity(basePK);
140                                            basePK = null;
141                                        }
142                                    } else {
143                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
144                                    }
145                                } else {
146                                    addExecutionError(ExecutionErrors.UnknownEntityBooleanAttribute.name(),
147                                            entityInstanceLogic.getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
148                                }
149                            }
150
151                            if(basePK != null) {
152                                result.setEntityLock(getEntityLockTransfer(basePK));
153                            }
154
155                            if(entityBooleanAttribute != null) {
156                                result.setEntityBooleanAttribute(coreControl.getEntityBooleanAttributeTransfer(getUserVisit(), entityBooleanAttribute, entityInstance));
157                            }
158                        } else {
159                            addExecutionError(ExecutionErrors.MismatchedEntityType.name());
160                        }
161                    }
162                } else {
163                    addExecutionError(ExecutionErrors.InvalidParameterCount.name());
164                }
165            }
166        } else {
167            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
168        }
169
170        return result;
171    }
172    
173}