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.message.server.command;
018
019import com.echothree.control.user.message.common.edit.MessageDescriptionEdit;
020import com.echothree.control.user.message.common.edit.MessageEditFactory;
021import com.echothree.control.user.message.common.form.EditMessageDescriptionForm;
022import com.echothree.control.user.message.common.result.MessageResultFactory;
023import com.echothree.control.user.message.common.spec.MessageDescriptionSpec;
024import com.echothree.model.control.message.server.control.MessageControl;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.command.EditMode;
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.server.control.BaseEditCommand;
033import java.util.ArrayList;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036import javax.inject.Inject;
037
038@Dependent
039public class EditMessageDescriptionCommand
040        extends BaseEditCommand<MessageDescriptionSpec, MessageDescriptionEdit> {
041    
042    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
043    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
044    
045    static {
046        SPEC_FIELD_DEFINITIONS = List.of(
047                new FieldDefinition("ComponentVendorName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("EntityTypeName", FieldType.ENTITY_TYPE_NAME, true, null, null),
049                new FieldDefinition("MessageName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
051        );
052        
053        EDIT_FIELD_DEFINITIONS = List.of(
054                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
055        );
056    }
057
058    @Inject
059    MessageControl messageControl;
060
061    @Inject
062    PartyControl partyControl;
063
064    
065    /** Creates a new instance of EditMessageDescriptionCommand */
066    public EditMessageDescriptionCommand() {
067        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
068    }
069    
070    @Override
071    protected BaseResult execute() {
072        var result = MessageResultFactory.getEditMessageDescriptionResult();
073        var componentVendorName = spec.getComponentVendorName();
074        var componentVendor = componentControl.getComponentVendorByName(componentVendorName);
075        
076        if(componentVendor != null) {
077            var entityTypeName = spec.getEntityTypeName();
078            var entityType = entityTypeControl.getEntityTypeByName(componentVendor, entityTypeName);
079            
080            if(entityType != null) {
081                var messageTypeName = spec.getMessageTypeName();
082                var messageType = messageControl.getMessageTypeByName(entityType, messageTypeName);
083                
084                if(messageType != null) {
085                    var messageName = spec.getMessageName();
086                    var message = messageControl.getMessageByName(messageType, messageName);
087                    
088                    if(message != null) {
089                        var languageIsoName = spec.getLanguageIsoName();
090                        var language = partyControl.getLanguageByIsoName(languageIsoName);
091                        
092                        if(language != null) {
093                            if(editMode.equals(EditMode.LOCK)) {
094                                var messageDescription = messageControl.getMessageDescription(message, language);
095                                
096                                if(messageDescription != null) {
097                                    result.setMessageDescription(messageControl.getMessageDescriptionTransfer(getUserVisit(), messageDescription));
098                                    
099                                    if(lockEntity(message)) {
100                                        var edit = MessageEditFactory.getMessageDescriptionEdit();
101                                        
102                                        result.setEdit(edit);
103                                        edit.setDescription(messageDescription.getDescription());
104                                    } else {
105                                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
106                                    }
107                                    
108                                    result.setEntityLock(getEntityLockTransfer(message));
109                                } else {
110                                    addExecutionError(ExecutionErrors.UnknownMessageDescription.name());
111                                }
112                            } else if(editMode.equals(EditMode.UPDATE)) {
113                                var messageDescriptionValue = messageControl.getMessageDescriptionValueForUpdate(message, language);
114                                
115                                if(messageDescriptionValue != null) {
116                                    if(lockEntityForUpdate(message)) {
117                                        try {
118                                            var description = edit.getDescription();
119                                            
120                                            messageDescriptionValue.setDescription(description);
121                                            
122                                            messageControl.updateMessageDescriptionFromValue(messageDescriptionValue, getPartyPK());
123                                        } finally {
124                                            unlockEntity(message);
125                                        }
126                                    } else {
127                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
128                                    }
129                                } else {
130                                    addExecutionError(ExecutionErrors.UnknownMessageDescription.name());
131                                }
132                            }
133                        } else {
134                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
135                        }
136                    } else {
137                        addExecutionError(ExecutionErrors.UnknownMessageName.name(), messageName);
138                    }
139                } else {
140                    addExecutionError(ExecutionErrors.UnknownMessageTypeName.name(), messageTypeName);
141                }
142            } else {
143                addExecutionError(ExecutionErrors.UnknownEntityTypeName.name(), entityTypeName);
144            }
145        } else {
146            addExecutionError(ExecutionErrors.UnknownComponentVendorName.name(), componentVendorName);
147        }
148        
149        return result;
150    }
151    
152}