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.SelectorDescriptionEdit;
020import com.echothree.control.user.selector.common.edit.SelectorEditFactory;
021import com.echothree.control.user.selector.common.form.EditSelectorDescriptionForm;
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.user.common.pk.UserVisitPK;
030import com.echothree.util.common.command.BaseResult;
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.BaseEditCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import com.echothree.util.server.persistence.Session;
040import java.util.Arrays;
041import java.util.Collections;
042import java.util.List;
043import javax.enterprise.context.RequestScoped;
044
045@RequestScoped
046public class EditSelectorDescriptionCommand
047        extends BaseEditCommand<SelectorDescriptionSpec, SelectorDescriptionEdit> {
048
049    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
050    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
051    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
052    
053    static {
054        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
055                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
056                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
057                        new SecurityRoleDefinition(SecurityRoleGroups.Selector.name(), SecurityRoles.Description.name())
058                ))
059        ));
060
061        SPEC_FIELD_DEFINITIONS = List.of(
062                new FieldDefinition("SelectorKindName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("SelectorTypeName", FieldType.ENTITY_NAME, true, null, null),
064                new FieldDefinition("SelectorName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
066        );
067        
068        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
069            new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
070        ));
071    }
072    
073    /** Creates a new instance of EditSelectorDescriptionCommand */
074    public EditSelectorDescriptionCommand() {
075        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
076    }
077    
078    @Override
079    protected BaseResult execute() {
080        var selectorControl = Session.getModelController(SelectorControl.class);
081        var result = SelectorResultFactory.getEditSelectorDescriptionResult();
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 partyControl = Session.getModelController(PartyControl.class);
095                    var languageIsoName = spec.getLanguageIsoName();
096                    var language = partyControl.getLanguageByIsoName(languageIsoName);
097                    
098                    if(language != null) {
099                        if(editMode.equals(EditMode.LOCK)) {
100                            var selectorDescription = selectorControl.getSelectorDescription(selector, language);
101                            
102                            if(selectorDescription != null) {
103                                result.setSelectorDescription(selectorControl.getSelectorDescriptionTransfer(getUserVisit(), selectorDescription));
104                                
105                                if(lockEntity(selector)) {
106                                    var edit = SelectorEditFactory.getSelectorDescriptionEdit();
107                                    
108                                    result.setEdit(edit);
109                                    edit.setDescription(selectorDescription.getDescription());
110                                } else {
111                                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
112                                }
113                                
114                                result.setEntityLock(getEntityLockTransfer(selector));
115                            } else {
116                                addExecutionError(ExecutionErrors.UnknownSelectorDescription.name());
117                            }
118                        } else if(editMode.equals(EditMode.UPDATE)) {
119                            var selectorDescriptionValue = selectorControl.getSelectorDescriptionValueForUpdate(selector, language);
120                            
121                            if(selectorDescriptionValue != null) {
122                                if(lockEntityForUpdate(selector)) {
123                                    try {
124                                        var description = edit.getDescription();
125                                        
126                                        selectorDescriptionValue.setDescription(description);
127                                        
128                                        selectorControl.updateSelectorDescriptionFromValue(selectorDescriptionValue, getPartyPK());
129                                    } finally {
130                                        unlockEntity(selector);
131                                    }
132                                } else {
133                                    addExecutionError(ExecutionErrors.EntityLockStale.name());
134                                }
135                            } else {
136                                addExecutionError(ExecutionErrors.UnknownSelectorDescription.name());
137                            }
138                        }
139                    } else {
140                        addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
141                    }
142                } else {
143                    addExecutionError(ExecutionErrors.UnknownSelectorName.name(), selectorName);
144                }
145            } else {
146                addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), selectorTypeName);
147            }
148        } else {
149            addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), selectorKindName);
150        }
151        
152        return result;
153    }
154    
155}