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.term.server.command;
018
019import com.echothree.control.user.term.common.edit.TermDescriptionEdit;
020import com.echothree.control.user.term.common.edit.TermEditFactory;
021import com.echothree.control.user.term.common.form.EditTermDescriptionForm;
022import com.echothree.control.user.term.common.result.TermResultFactory;
023import com.echothree.control.user.term.common.spec.TermDescriptionSpec;
024import com.echothree.model.control.party.server.control.PartyControl;
025import com.echothree.model.control.term.server.control.TermControl;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.command.EditMode;
032import com.echothree.util.server.control.BaseEditCommand;
033import com.echothree.util.server.persistence.Session;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036
037@Dependent
038public class EditTermDescriptionCommand
039        extends BaseEditCommand<TermDescriptionSpec, TermDescriptionEdit> {
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("TermName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
048                );
049        
050        EDIT_FIELD_DEFINITIONS = List.of(
051                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
052                );
053    }
054    
055    /** Creates a new instance of EditTermDescriptionCommand */
056    public EditTermDescriptionCommand() {
057        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
058    }
059    
060    @Override
061    protected BaseResult execute() {
062        var termControl = Session.getModelController(TermControl.class);
063        var result = TermResultFactory.getEditTermDescriptionResult();
064        var termName = spec.getTermName();
065        var term = termControl.getTermByName(termName);
066        
067        if(term != null) {
068            var partyControl = Session.getModelController(PartyControl.class);
069            var languageIsoName = spec.getLanguageIsoName();
070            var language = partyControl.getLanguageByIsoName(languageIsoName);
071            
072            if(language != null) {
073                if(editMode.equals(EditMode.LOCK)) {
074                    var termDescription = termControl.getTermDescription(term, language);
075                    
076                    if(termDescription != null) {
077                        result.setTermDescription(termControl.getTermDescriptionTransfer(getUserVisit(), termDescription));
078                        
079                        if(lockEntity(term)) {
080                            var edit = TermEditFactory.getTermDescriptionEdit();
081                            
082                            result.setEdit(edit);
083                            edit.setDescription(termDescription.getDescription());
084                        } else {
085                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
086                        }
087                        
088                        result.setEntityLock(getEntityLockTransfer(term));
089                    } else {
090                        addExecutionError(ExecutionErrors.UnknownTermDescription.name());
091                    }
092                } else if(editMode.equals(EditMode.UPDATE)) {
093                    var termDescriptionValue = termControl.getTermDescriptionValueForUpdate(term, language);
094                    
095                    if(termDescriptionValue != null) {
096                        if(lockEntityForUpdate(term)) {
097                            try {
098                                var description = edit.getDescription();
099                                
100                                termDescriptionValue.setDescription(description);
101                                
102                                termControl.updateTermDescriptionFromValue(termDescriptionValue, getPartyPK());
103                            } finally {
104                                unlockEntity(term);
105                            }
106                        } else {
107                            addExecutionError(ExecutionErrors.EntityLockStale.name());
108                        }
109                    } else {
110                        addExecutionError(ExecutionErrors.UnknownTermDescription.name());
111                    }
112                }
113            } else {
114                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
115            }
116        } else {
117            addExecutionError(ExecutionErrors.UnknownTermName.name(), termName);
118        }
119        
120        return result;
121    }
122    
123}