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.club.server.command;
018
019import com.echothree.control.user.club.common.edit.ClubDescriptionEdit;
020import com.echothree.control.user.club.common.edit.ClubEditFactory;
021import com.echothree.control.user.club.common.form.EditClubDescriptionForm;
022import com.echothree.control.user.club.common.result.ClubResultFactory;
023import com.echothree.control.user.club.common.spec.ClubDescriptionSpec;
024import com.echothree.model.control.club.server.control.ClubControl;
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.List;
035import javax.enterprise.context.Dependent;
036
037@Dependent
038public class EditClubDescriptionCommand
039        extends BaseEditCommand<ClubDescriptionSpec, ClubDescriptionEdit> {
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("ClubName", 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 EditClubDescriptionCommand */
056    public EditClubDescriptionCommand() {
057        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
058    }
059    
060    @Override
061    protected BaseResult execute() {
062        var clubControl = Session.getModelController(ClubControl.class);
063        var result = ClubResultFactory.getEditClubDescriptionResult();
064        var clubName = spec.getClubName();
065        var club = clubControl.getClubByName(clubName);
066        
067        if(club != 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 clubDescription = clubControl.getClubDescription(club, language);
075                    
076                    if(clubDescription != null) {
077                        result.setClubDescription(clubControl.getClubDescriptionTransfer(getUserVisit(), clubDescription));
078                        
079                        if(lockEntity(club)) {
080                            var edit = ClubEditFactory.getClubDescriptionEdit();
081                            
082                            result.setEdit(edit);
083                            edit.setDescription(clubDescription.getDescription());
084                        } else {
085                            addExecutionError(ExecutionErrors.EntityLockFailed.name());
086                        }
087                        
088                        result.setEntityLock(getEntityLockTransfer(club));
089                    } else {
090                        addExecutionError(ExecutionErrors.UnknownClubDescription.name());
091                    }
092                } else if(editMode.equals(EditMode.UPDATE)) {
093                    var clubDescriptionValue = clubControl.getClubDescriptionValueForUpdate(club, language);
094                    
095                    if(clubDescriptionValue != null) {
096                        if(lockEntityForUpdate(club)) {
097                            try {
098                                var description = edit.getDescription();
099                                
100                                clubDescriptionValue.setDescription(description);
101                                
102                                clubControl.updateClubDescriptionFromValue(clubDescriptionValue, getPartyPK());
103                            } finally {
104                                unlockEntity(club);
105                            }
106                        } else {
107                            addExecutionError(ExecutionErrors.EntityLockStale.name());
108                        }
109                    } else {
110                        addExecutionError(ExecutionErrors.UnknownClubDescription.name());
111                    }
112                }
113            } else {
114                addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
115            }
116        } else {
117            addExecutionError(ExecutionErrors.UnknownClubName.name(), clubName);
118        }
119        
120        return result;
121    }
122    
123}