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.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.result.EditSelectorNodeDescriptionResult;
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.selector.server.entity.SelectorNode;
027import com.echothree.model.data.selector.server.entity.SelectorNodeDescription;
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.BaseAbstractEditCommand;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class EditSelectorNodeDescriptionCommand
039        extends BaseAbstractEditCommand<SelectorNodeDescriptionSpec, SelectorNodeDescriptionEdit, EditSelectorNodeDescriptionResult, SelectorNodeDescription, SelectorNode> {
040
041    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
042    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
043
044    static {
045        SPEC_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null),
049                new FieldDefinition("SelectorNodeName", 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    PartyControl partyControl;
060
061    @Inject
062    SelectorControl selectorControl;
063
064    /** Creates a new instance of EditSelectorNodeDescriptionCommand */
065    public EditSelectorNodeDescriptionCommand() {
066        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
067    }
068
069    @Override
070    public EditSelectorNodeDescriptionResult getResult() {
071        return SelectorResultFactory.getEditSelectorNodeDescriptionResult();
072    }
073
074    @Override
075    public SelectorNodeDescriptionEdit getEdit() {
076        return SelectorEditFactory.getSelectorNodeDescriptionEdit();
077    }
078
079    @Override
080    public SelectorNodeDescription getEntity(EditSelectorNodeDescriptionResult result) {
081        SelectorNodeDescription selectorNodeDescription = null;
082        var selectorKindName = spec.getSelectorKindName();
083        var selectorKind = selectorControl.getSelectorKindByName(selectorKindName);
084
085        if(selectorKind != null) {
086            var selectorTypeName = spec.getSelectorTypeName();
087            var selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName);
088
089            if(selectorType != null) {
090                var selectorName = spec.getSelectorName();
091                var selector = selectorControl.getSelectorByName(selectorType, selectorName);
092
093                if(selector != null) {
094                    var selectorNodeName = spec.getSelectorNodeName();
095                    var selectorNode = selectorControl.getSelectorNodeByName(selector, selectorNodeName);
096
097                    if(selectorNode != null) {
098                        var languageIsoName = spec.getLanguageIsoName();
099                        var language = partyControl.getLanguageByIsoName(languageIsoName);
100
101                        if(language != null) {
102                            if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
103                                selectorNodeDescription = selectorControl.getSelectorNodeDescription(selectorNode, language);
104                            } else { // EditMode.UPDATE
105                                selectorNodeDescription = selectorControl.getSelectorNodeDescriptionForUpdate(selectorNode, language);
106                            }
107
108                            if(selectorNodeDescription == null) {
109                                addExecutionError(ExecutionErrors.UnknownSelectorNodeDescription.name(), selectorKindName, selectorTypeName, selectorName, selectorNodeName, languageIsoName);
110                            }
111                        } else {
112                            addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
113                        }
114                    } else {
115                        addExecutionError(ExecutionErrors.UnknownSelectorNodeName.name(), selectorKindName, selectorTypeName, selectorName, selectorNodeName);
116                    }
117                } else {
118                    addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorKindName, selectorTypeName, selectorName);
119                }
120            } else {
121                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorKindName, selectorTypeName);
122            }
123        } else {
124            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
125        }
126
127        return selectorNodeDescription;
128    }
129
130    @Override
131    public SelectorNode getLockEntity(SelectorNodeDescription selectorNodeDescription) {
132        return selectorNodeDescription.getSelectorNode();
133    }
134
135    @Override
136    public void fillInResult(EditSelectorNodeDescriptionResult result, SelectorNodeDescription selectorNodeDescription) {
137        result.setSelectorNodeDescription(selectorControl.getSelectorNodeDescriptionTransfer(getUserVisit(), selectorNodeDescription));
138    }
139
140    @Override
141    public void doLock(SelectorNodeDescriptionEdit edit, SelectorNodeDescription selectorNodeDescription) {
142        edit.setDescription(selectorNodeDescription.getDescription());
143    }
144
145    @Override
146    public void doUpdate(SelectorNodeDescription selectorNodeDescription) {
147        var selectorNodeDescriptionValue = selectorControl.getSelectorNodeDescriptionValue(selectorNodeDescription);
148
149        selectorNodeDescriptionValue.setDescription(edit.getDescription());
150
151        selectorControl.updateSelectorNodeDescriptionFromValue(selectorNodeDescriptionValue, getPartyPK());
152    }
153
154}