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