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.SelectorEdit;
020import com.echothree.control.user.selector.common.edit.SelectorEditFactory;
021import com.echothree.control.user.selector.common.result.EditSelectorResult;
022import com.echothree.control.user.selector.common.result.SelectorResultFactory;
023import com.echothree.control.user.selector.common.spec.SelectorSpec;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.control.selector.server.control.SelectorControl;
028import com.echothree.model.data.selector.server.entity.Selector;
029import com.echothree.model.data.selector.server.entity.SelectorType;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseAbstractEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import com.echothree.util.server.persistence.Session;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class EditSelectorCommand
045        extends BaseAbstractEditCommand<SelectorSpec, SelectorEdit, EditSelectorResult, Selector, 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.Edit.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        );
064        
065        EDIT_FIELD_DEFINITIONS = List.of(
066                new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null),
067                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
068                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
069                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
070        );
071    }
072    
073    /** Creates a new instance of EditSelectorCommand */
074    public EditSelectorCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077
078    @Inject
079    SelectorControl selectorControl;
080
081    @Override
082    public EditSelectorResult getResult() {
083        return SelectorResultFactory.getEditSelectorResult();
084    }
085
086    @Override
087    public SelectorEdit getEdit() {
088        return SelectorEditFactory.getSelectorEdit();
089    }
090
091    @Override
092    public Selector getEntity(EditSelectorResult result) {
093        Selector selector = null;
094        var selectorKindName = spec.getSelectorKindName();
095        var selectorKind = selectorControl.getSelectorKindByName(selectorKindName);
096
097        if(selectorKind != null) {
098            var selectorTypeName = spec.getSelectorTypeName();
099            var selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName);
100
101            if(selectorType != null) {
102                var selectorName = spec.getSelectorName();
103
104                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
105                    selector = selectorControl.getSelectorByName(selectorType, selectorName);
106                } else { // EditMode.UPDATE
107                    selector = selectorControl.getSelectorByNameForUpdate(selectorType, selectorName);
108                }
109
110                if(selector == null) {
111                    addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
112                }
113            } else {
114                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
115            }
116        } else {
117            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
118        }
119
120        return selector;
121    }
122
123    @Override
124    public Selector getLockEntity(Selector selector) {
125        return selector;
126    }
127
128    @Override
129    public void fillInResult(EditSelectorResult result, Selector selector) {
130        result.setSelector(selectorControl.getSelectorTransfer(getUserVisit(), selector));
131    }
132
133    @Override
134    public void doLock(SelectorEdit edit, Selector selector) {
135        var selectorDescription = selectorControl.getSelectorDescription(selector, getPreferredLanguage());
136        var selectorDetail = selector.getLastDetail();
137
138        edit.setSelectorName(selectorDetail.getSelectorName());
139        edit.setIsDefault(selectorDetail.getIsDefault().toString());
140        edit.setSortOrder(selectorDetail.getSortOrder().toString());
141
142        if(selectorDescription != null) {
143            edit.setDescription(selectorDescription.getDescription());
144        }
145    }
146
147    @Override
148    public void canUpdate(Selector selector) {
149        var selectorType = selector.getLastDetail().getSelectorType();
150        var selectorName = edit.getSelectorName();
151        var duplicateSelector = selectorControl.getSelectorByName(selectorType, selectorName);
152
153        if(duplicateSelector != null && !selector.equals(duplicateSelector)) {
154            addExecutionError(ExecutionErrors.DuplicateSelectorName.name(), selectorName);
155        }
156    }
157
158    @Override
159    public void doUpdate(Selector selector) {
160        var partyPK = getPartyPK();
161        var selectorDetailValue = selectorControl.getSelectorDetailValueForUpdate(selector);
162        var selectorDescription = selectorControl.getSelectorDescriptionForUpdate(selector, getPreferredLanguage());
163        var description = edit.getDescription();
164
165        selectorDetailValue.setSelectorName(edit.getSelectorName());
166        selectorDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
167        selectorDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
168
169        selectorControl.updateSelectorFromValue(selectorDetailValue, partyPK);
170
171        if(selectorDescription == null && description != null) {
172            selectorControl.createSelectorDescription(selector, getPreferredLanguage(), description, partyPK);
173        } else if(selectorDescription != null && description == null) {
174            selectorControl.deleteSelectorDescription(selectorDescription, partyPK);
175        } else if(selectorDescription != null && description != null) {
176            var selectorDescriptionValue = selectorControl.getSelectorDescriptionValue(selectorDescription);
177
178            selectorDescriptionValue.setDescription(description);
179            selectorControl.updateSelectorDescriptionFromValue(selectorDescriptionValue, partyPK);
180        }
181    }
182    
183}