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.associate.server.command;
018
019import com.echothree.control.user.associate.common.edit.AssociateEditFactory;
020import com.echothree.control.user.associate.common.edit.AssociateProgramDescriptionEdit;
021import com.echothree.control.user.associate.common.form.EditAssociateProgramDescriptionForm;
022import com.echothree.control.user.associate.common.result.AssociateResultFactory;
023import com.echothree.control.user.associate.common.spec.AssociateProgramDescriptionSpec;
024import com.echothree.model.control.associate.server.control.AssociateControl;
025import com.echothree.model.control.party.server.control.PartyControl;
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.Arrays;
035import java.util.Collections;
036import java.util.List;
037import javax.enterprise.context.RequestScoped;
038
039@RequestScoped
040public class EditAssociateProgramDescriptionCommand
041        extends BaseEditCommand<AssociateProgramDescriptionSpec, AssociateProgramDescriptionEdit> {
042    
043    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
044    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
045    
046    static {
047        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
048            new FieldDefinition("AssociateProgramName", FieldType.ENTITY_NAME, true, null, null),
049            new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)
050        ));
051        
052        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
053            new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
054        ));
055    }
056    
057    /** Creates a new instance of EditAssociateProgramDescriptionCommand */
058    public EditAssociateProgramDescriptionCommand() {
059        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
060    }
061    
062    @Override
063    protected BaseResult execute() {
064        var associateControl = Session.getModelController(AssociateControl.class);
065        var result = AssociateResultFactory.getEditAssociateProgramDescriptionResult();
066        var associateProgramName = spec.getAssociateProgramName();
067        var associateProgram = associateControl.getAssociateProgramByName(associateProgramName);
068        
069        if(associateProgram != null) {
070            var partyControl = Session.getModelController(PartyControl.class);
071            var languageIsoName = spec.getLanguageIsoName();
072            var language = partyControl.getLanguageByIsoName(languageIsoName);
073            
074            if(language != null) {
075                if(editMode.equals(EditMode.LOCK)) {
076                    var associateProgramDescription = associateControl.getAssociateProgramDescription(associateProgram, language);
077                    
078                    if(associateProgramDescription != null) {
079                        result.setAssociateProgramDescription(associateControl.getAssociateProgramDescriptionTransfer(getUserVisit(), associateProgramDescription));
080                        
081                        if(lockEntity(associateProgram)) {
082                            var edit = AssociateEditFactory.getAssociateProgramDescriptionEdit();
083                            
084                            result.setEdit(edit);
085                            edit.setDescription(associateProgramDescription.getDescription());
086                        } else {
087                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
088                        }
089                        
090                        result.setEntityLock(getEntityLockTransfer(associateProgram));
091                    } else {
092                        addExecutionError(ExecutionErrors.UnknownAssociateProgramDescription.name());
093                    }
094                } else if(editMode.equals(EditMode.UPDATE)) {
095                    var associateProgramDescriptionValue = associateControl.getAssociateProgramDescriptionValueForUpdate(associateProgram, language);
096                    
097                    if(associateProgramDescriptionValue != null) {
098                        if(lockEntityForUpdate(associateProgram)) {
099                            try {
100                                var description = edit.getDescription();
101                                
102                                associateProgramDescriptionValue.setDescription(description);
103                                
104                                associateControl.updateAssociateProgramDescriptionFromValue(associateProgramDescriptionValue, getPartyPK());
105                            } finally {
106                                unlockEntity(associateProgram);
107                            }
108                        } else {
109                            addExecutionError(ExecutionErrors.EntityLockStale.name());
110                        }
111                    } else {
112                        addExecutionError(ExecutionErrors.UnknownAssociateProgramDescription.name());
113                    }
114                }
115            } else {
116                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
117            }
118        } else {
119            addExecutionError(ExecutionErrors.UnknownAssociateProgramName.name(), associateProgramName);
120        }
121        
122        return result;
123    }
124    
125}