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;
040
041@Dependent
042public class EditEntityBooleanAttributeCommand
043        extends BaseEditCommand<EntityBooleanAttributeSpec, EntityBooleanAttributeEdit> {
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("BooleanAttribute", FieldType.BOOLEAN, true, null, null)
064                );
065    }
066    
067    /** Creates a new instance of EditEntityBooleanAttributeCommand */
068    public EditEntityBooleanAttributeCommand() {
069        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
070    }
071    
072    @Override
073    protected BaseResult execute() {
074        var result = CoreResultFactory.getEditEntityBooleanAttributeResult();
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                            EntityBooleanAttribute entityBooleanAttribute = null;
094                            var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance);
095
096                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
097                                entityBooleanAttribute = coreControl.getEntityBooleanAttribute(entityAttribute, entityInstance);
098
099                                if(entityBooleanAttribute != null) {
100                                    if(editMode.equals(EditMode.LOCK)) {
101                                        result.setEntityBooleanAttribute(coreControl.getEntityBooleanAttributeTransfer(getUserVisit(), entityBooleanAttribute, entityInstance));
102
103                                        if(lockEntity(basePK)) {
104                                            var edit = CoreEditFactory.getEntityBooleanAttributeEdit();
105
106                                            result.setEdit(edit);
107                                            edit.setBooleanAttribute(entityBooleanAttribute.getBooleanAttribute().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.UnknownEntityBooleanAttribute.name(),
117                                            EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
118                                }
119                            } else if(editMode.equals(EditMode.UPDATE)) {
120                                entityBooleanAttribute = coreControl.getEntityBooleanAttributeForUpdate(entityAttribute, entityInstance);
121
122                                if(entityBooleanAttribute != null) {
123                                    if(lockEntityForUpdate(basePK)) {
124                                        try {
125                                            var entityBooleanAttributeValue = coreControl.getEntityBooleanAttributeValueForUpdate(entityBooleanAttribute);
126
127                                            entityBooleanAttributeValue.setBooleanAttribute(Boolean.valueOf(edit.getBooleanAttribute()));
128
129                                            coreControl.updateEntityBooleanAttributeFromValue(entityBooleanAttributeValue, getPartyPK());
130                                        } finally {
131                                            unlockEntity(basePK);
132                                            basePK = null;
133                                        }
134                                    } else {
135                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
136                                    }
137                                } else {
138                                    addExecutionError(ExecutionErrors.UnknownEntityBooleanAttribute.name(),
139                                            EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName);
140                                }
141                            }
142
143                            if(basePK != null) {
144                                result.setEntityLock(getEntityLockTransfer(basePK));
145                            }
146
147                            if(entityBooleanAttribute != null) {
148                                result.setEntityBooleanAttribute(coreControl.getEntityBooleanAttributeTransfer(getUserVisit(), entityBooleanAttribute, entityInstance));
149                            }
150                        } else {
151                            addExecutionError(ExecutionErrors.MismatchedEntityType.name());
152                        }
153                    }
154                } else {
155                    addExecutionError(ExecutionErrors.InvalidParameterCount.name());
156                }
157            }
158        } else {
159            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
160        }
161
162        return result;
163    }
164    
165}