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