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.SelectorDescriptionEdit;
020import com.echothree.control.user.selector.common.edit.SelectorEditFactory;
021import com.echothree.control.user.selector.common.result.EditSelectorDescriptionResult;
022import com.echothree.control.user.selector.common.result.SelectorResultFactory;
023import com.echothree.control.user.selector.common.spec.SelectorDescriptionSpec;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.control.selector.server.control.SelectorControl;
029import com.echothree.model.data.selector.server.entity.Selector;
030import com.echothree.model.data.selector.server.entity.SelectorDescription;
031import com.echothree.util.common.command.EditMode;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseAbstractEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditSelectorDescriptionCommand
045        extends BaseAbstractEditCommand<SelectorDescriptionSpec, SelectorDescriptionEdit, EditSelectorDescriptionResult, SelectorDescription, Selector> {
046
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.Selector.name(), SecurityRoles.Description.name())
056                ))
057        ));
058
059        SPEC_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
064        );
065        
066        EDIT_FIELD_DEFINITIONS = List.of(
067                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
068        );
069    }
070    
071    /** Creates a new instance of EditSelectorDescriptionCommand */
072    public EditSelectorDescriptionCommand() {
073        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
074    }
075
076    @Inject
077    PartyControl partyControl;
078
079    @Inject
080    SelectorControl selectorControl;
081
082    @Override
083    public EditSelectorDescriptionResult getResult() {
084        return SelectorResultFactory.getEditSelectorDescriptionResult();
085    }
086
087    @Override
088    public SelectorDescriptionEdit getEdit() {
089        return SelectorEditFactory.getSelectorDescriptionEdit();
090    }
091
092    @Override
093    public SelectorDescription getEntity(EditSelectorDescriptionResult result) {
094        SelectorDescription selectorDescription = null;
095        var selectorKindName = spec.getSelectorKindName();
096        var selectorKind = selectorControl.getSelectorKindByName(selectorKindName);
097
098        if(selectorKind != null) {
099            var selectorTypeName = spec.getSelectorTypeName();
100            var selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName);
101
102            if(selectorType != null) {
103                var selectorName = spec.getSelectorName();
104                var selector = selectorControl.getSelectorByName(selectorType, selectorName);
105
106                if(selector != null) {
107                    var languageIsoName = spec.getLanguageIsoName();
108                    var language = partyControl.getLanguageByIsoName(languageIsoName);
109
110                    if(language != null) {
111                        if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
112                            selectorDescription = selectorControl.getSelectorDescription(selector, language);
113                        } else { // EditMode.UPDATE
114                            selectorDescription = selectorControl.getSelectorDescriptionForUpdate(selector, language);
115                        }
116
117                        if(selectorDescription == null) {
118                            addExecutionError(ExecutionErrors.UnknownSelectorDescription.name(), selectorKindName, selectorTypeName, selectorName, languageIsoName);
119                        }
120                    } else {
121                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
122                    }
123                } else {
124                    addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
125                }
126            } else {
127                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
128            }
129        } else {
130            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
131        }
132
133        return selectorDescription;
134    }
135
136    @Override
137    public Selector getLockEntity(SelectorDescription selectorDescription) {
138        return selectorDescription.getSelector();
139    }
140
141    @Override
142    public void fillInResult(EditSelectorDescriptionResult result, SelectorDescription selectorDescription) {
143        result.setSelectorDescription(selectorControl.getSelectorDescriptionTransfer(getUserVisit(), selectorDescription));
144    }
145
146    @Override
147    public void doLock(SelectorDescriptionEdit edit, SelectorDescription selectorDescription) {
148        edit.setDescription(selectorDescription.getDescription());
149    }
150
151    @Override
152    public void doUpdate(SelectorDescription selectorDescription) {
153        var selectorDescriptionValue = selectorControl.getSelectorDescriptionValue(selectorDescription);
154
155        selectorDescriptionValue.setDescription(edit.getDescription());
156
157        selectorControl.updateSelectorDescriptionFromValue(selectorDescriptionValue, getPartyPK());
158    }
159
160}