001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.form.DeleteEntityLongAttributeForm;
020import com.echothree.model.control.core.server.logic.EntityAttributeLogic;
021import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.data.user.common.pk.UserVisitPK;
024import com.echothree.util.common.message.ExecutionErrors;
025import com.echothree.util.common.validation.FieldDefinition;
026import com.echothree.util.common.validation.FieldType;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.server.control.BaseSimpleCommand;
029import com.echothree.util.server.control.CommandSecurityDefinition;
030import com.echothree.util.server.control.PartyTypeDefinition;
031import java.util.Arrays;
032import java.util.Collections;
033import java.util.List;
034import javax.enterprise.context.RequestScoped;
035
036@RequestScoped
037public class DeleteEntityLongAttributeCommand
038        extends BaseSimpleCommand<DeleteEntityLongAttributeForm> {
039
040    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
045                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
046                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null)
047        ));
048
049        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
050                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
051                new FieldDefinition("Uuid", FieldType.UUID, false, null, null),
052                new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null),
053                new FieldDefinition("EntityAttributeUuid", FieldType.UUID, false, null, null)
054                ));
055    }
056    
057    /** Creates a new instance of DeleteEntityLongAttributeCommand */
058    public DeleteEntityLongAttributeCommand() {
059        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
060    }
061    
062    @Override
063    protected BaseResult execute() {
064        var parameterCount = EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
065
066        if(parameterCount == 1) {
067            var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form);
068
069            if(!hasExecutionErrors()) {
070                var entityAttributeName = form.getEntityAttributeName();
071                var entityAttributeUuid = form.getEntityAttributeUuid();
072                
073                parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUuid == null ? 0 : 1);
074                
075                if(parameterCount == 1) {
076                    var entityAttribute = entityAttributeName == null ?
077                            EntityAttributeLogic.getInstance().getEntityAttributeByUuid(this, entityAttributeUuid) :
078                            EntityAttributeLogic.getInstance().getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName);
079
080                    if(!hasExecutionErrors()) {
081                        if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) {
082                            var entityLongAttribute = coreControl.getEntityLongAttributeForUpdate(entityAttribute, entityInstance);
083
084                            if(entityLongAttribute != null) {
085                                coreControl.deleteEntityLongAttribute(entityLongAttribute, getPartyPK());
086                            } else {
087                                addExecutionError(ExecutionErrors.UnknownEntityLongAttribute.name());
088                            }
089                        } else {
090                            addExecutionError(ExecutionErrors.MismatchedEntityType.name());
091                        }
092                    }
093                } else {
094                    addExecutionError(ExecutionErrors.InvalidParameterCount.name());
095                }
096            }
097        } else {
098            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
099        }
100
101        return null;
102    }
103    
104}