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.SelectorEdit;
020import com.echothree.control.user.selector.common.edit.SelectorEditFactory;
021import com.echothree.control.user.selector.common.form.EditSelectorForm;
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.user.common.pk.UserVisitPK;
029import com.echothree.util.common.command.BaseResult;
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.BaseEditCommand;
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.RequestScoped;
041
042@RequestScoped
043public class EditSelectorCommand
044        extends BaseEditCommand<SelectorSpec, SelectorEdit> {
045
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
048    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
049    
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.Selector.name(), SecurityRoles.Edit.name())
055                ))
056        ));
057
058        SPEC_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null)
062        );
063        
064        EDIT_FIELD_DEFINITIONS = List.of(
065                new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null),
066                new FieldDefinition("InitialSelectorAdjustmentName", FieldType.ENTITY_NAME, false, null, null),
067                new FieldDefinition("SelectorItemSelectorName", FieldType.ENTITY_NAME, false, null, null),
068                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
069                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
070                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
071        );
072    }
073    
074    /** Creates a new instance of EditSelectorCommand */
075    public EditSelectorCommand() {
076        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
077    }
078    
079    @Override
080    protected BaseResult execute() {
081        var selectorControl = Session.getModelController(SelectorControl.class);
082        var result = SelectorResultFactory.getEditSelectorResult();
083        var selectorKindName = spec.getSelectorKindName();
084        var selectorKind = selectorControl.getSelectorKindByName(selectorKindName);
085        
086        if(selectorKind != null) {
087            var selectorTypeName = spec.getSelectorTypeName();
088            var selectorType = selectorControl.getSelectorTypeByName(selectorKind, selectorTypeName);
089            
090            if(selectorType != null) {
091                if(editMode.equals(EditMode.LOCK)) {
092                    var selectorName = spec.getSelectorName();
093                    var selector = selectorControl.getSelectorByName(selectorType, selectorName);
094                    
095                    if(selector != null) {
096                        result.setSelector(selectorControl.getSelectorTransfer(getUserVisit(), selector));
097                        
098                        if(lockEntity(selector)) {
099                            var selectorDescription = selectorControl.getSelectorDescription(selector, getPreferredLanguage());
100                            var edit = SelectorEditFactory.getSelectorEdit();
101                            var selectorDetail = selector.getLastDetail();
102                            
103                            result.setEdit(edit);
104                            edit.setSelectorName(selectorDetail.getSelectorName());
105                            edit.setIsDefault(selectorDetail.getIsDefault().toString());
106                            edit.setSortOrder(selectorDetail.getSortOrder().toString());
107                            
108                            if(selectorDescription != null) {
109                                edit.setDescription(selectorDescription.getDescription());
110                            }
111                        } else {
112                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
113                        }
114                        
115                        result.setEntityLock(getEntityLockTransfer(selector));
116                    } else {
117                        addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
118                    }
119                } else if(editMode.equals(EditMode.UPDATE)) {
120                    var selectorName = spec.getSelectorName();
121                    var selector = selectorControl.getSelectorByNameForUpdate(selectorType, selectorName);
122                    
123                    if(selector != null) {
124                        selectorName = edit.getSelectorName();
125                        var duplicateSelector = selectorControl.getSelectorByName(selectorType, selectorName);
126                        
127                        if(duplicateSelector == null || selector.equals(duplicateSelector)) {
128                            if(lockEntityForUpdate(selector)) {
129                                try {
130                                    var partyPK = getPartyPK();
131                                    var selectorDetailValue = selectorControl.getSelectorDetailValueForUpdate(selector);
132                                    var selectorDescription = selectorControl.getSelectorDescriptionForUpdate(selector, getPreferredLanguage());
133                                    var description = edit.getDescription();
134                                    
135                                    selectorDetailValue.setSelectorName(edit.getSelectorName());
136                                    selectorDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
137                                    selectorDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
138                                    
139                                    selectorControl.updateSelectorFromValue(selectorDetailValue, partyPK);
140                                    
141                                    if(selectorDescription == null && description != null) {
142                                        selectorControl.createSelectorDescription(selector, getPreferredLanguage(), description, partyPK);
143                                    } else if(selectorDescription != null && description == null) {
144                                        selectorControl.deleteSelectorDescription(selectorDescription, partyPK);
145                                    } else if(selectorDescription != null && description != null) {
146                                        var selectorDescriptionValue = selectorControl.getSelectorDescriptionValue(selectorDescription);
147                                        
148                                        selectorDescriptionValue.setDescription(description);
149                                        selectorControl.updateSelectorDescriptionFromValue(selectorDescriptionValue, partyPK);
150                                    }
151                                } finally {
152                                    unlockEntity(selector);
153                                }
154                            } else {
155                                addExecutionError(ExecutionErrors.EntityLockStale.name());
156                            }
157                        } else {
158                            addExecutionError(ExecutionErrors.DuplicateSelectorName.name(), selectorName);
159                        }
160                    } else {
161                        addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
162                    }
163                }
164            } else {
165                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
166            }
167        } else {
168            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
169        }
170        
171        return result;
172    }
173    
174}