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.selector.server.command;
018
019import com.echothree.control.user.selector.common.edit.SelectorEditFactory;
020import com.echothree.control.user.selector.common.edit.SelectorNodeDescriptionEdit;
021import com.echothree.control.user.selector.common.form.EditSelectorNodeDescriptionForm;
022import com.echothree.control.user.selector.common.result.SelectorResultFactory;
023import com.echothree.control.user.selector.common.spec.SelectorNodeDescriptionSpec;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.control.selector.server.control.SelectorControl;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.command.EditMode;
032import com.echothree.util.server.control.BaseEditCommand;
033import com.echothree.util.server.persistence.Session;
034import java.util.Arrays;
035import java.util.Collections;
036import java.util.List;
037import javax.enterprise.context.RequestScoped;
038
039@RequestScoped
040public class EditSelectorNodeDescriptionCommand
041        extends BaseEditCommand<SelectorNodeDescriptionSpec, SelectorNodeDescriptionEdit> {
042    
043    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
044    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
045    
046    static {
047        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
048            new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null),
049            new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null),
050            new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null),
051            new FieldDefinition("SelectorNodeName", FieldType.ENTITY_NAME, true, null, null),
052            new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
053        ));
054        
055        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
056            new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
057        ));
058    }
059    
060    /** Creates a new instance of EditSelectorNodeDescriptionCommand */
061    public EditSelectorNodeDescriptionCommand() {
062        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
063    }
064    
065    @Override
066    protected BaseResult execute() {
067        var selectorControl = Session.getModelController(SelectorControl.class);
068        var result = SelectorResultFactory.getEditSelectorNodeDescriptionResult();
069        var selectorKindName = spec.getSelectorKindName();
070        var selectorKind = selectorControl.getSelectorKindByName(selectorKindName);
071        
072        if(selectorKind != null) {
073            var selectorTypeName = spec.getSelectorTypeName();
074            var selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName);
075            
076            if(selectorType != null) {
077                var selectorName = spec.getSelectorName();
078                var selector = selectorControl.getSelectorByName(selectorType, selectorName);
079                
080                if(selector != null) {
081                    var selectorNodeName = spec.getSelectorNodeName();
082                    var selectorNode = selectorControl.getSelectorNodeByName(selector, selectorNodeName);
083                    
084                    if(selectorNode != null) {
085                        var partyControl = Session.getModelController(PartyControl.class);
086                        var languageIsoName = spec.getLanguageIsoName();
087                        var language = partyControl.getLanguageByIsoName(languageIsoName);
088                        
089                        if(language != null) {
090                            if(editMode.equals(EditMode.LOCK)) {
091                                var selectorNodeDescription = selectorControl.getSelectorNodeDescription(selectorNode, language);
092                                
093                                if(selectorNodeDescription != null) {
094                                    result.setSelectorNodeDescription(selectorControl.getSelectorNodeDescriptionTransfer(getUserVisit(), selectorNodeDescription));
095                                    
096                                    if(lockEntity(selectorNode)) {
097                                        var edit = SelectorEditFactory.getSelectorNodeDescriptionEdit();
098                                        
099                                        result.setEdit(edit);
100                                        edit.setDescription(selectorNodeDescription.getDescription());
101                                    } else {
102                                        addExecutionError(ExecutionErrors.EntityLockFailed.name());
103                                    }
104                                    
105                                    result.setEntityLock(getEntityLockTransfer(selectorNode));
106                                } else {
107                                    addExecutionError(ExecutionErrors.UnknownSelectorNodeDescription.name());
108                                }
109                            } else if(editMode.equals(EditMode.UPDATE)) {
110                                var selectorNodeDescriptionValue = selectorControl.getSelectorNodeDescriptionValueForUpdate(selectorNode, language);
111                                
112                                if(selectorNodeDescriptionValue != null) {
113                                    if(lockEntityForUpdate(selectorNode)) {
114                                        try {
115                                            var description = edit.getDescription();
116                                            
117                                            selectorNodeDescriptionValue.setDescription(description);
118                                            
119                                            selectorControl.updateSelectorNodeDescriptionFromValue(selectorNodeDescriptionValue, getPartyPK());
120                                        } finally {
121                                            unlockEntity(selectorNode);
122                                        }
123                                    } else {
124                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
125                                    }
126                                } else {
127                                    addExecutionError(ExecutionErrors.UnknownSelectorNodeDescription.name());
128                                }
129                            }
130                        } else {
131                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
132                        }
133                    } else {
134                        addExecutionError(ExecutionErrors.UnknownSelectorNodeName.name(), selectorNodeName);
135                    }
136                } else {
137                    addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
138                }
139            } else {
140                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
141            }
142        } else {
143            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
144        }
145        
146        return result;
147    }
148    
149}