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.forum.server.command;
018
019import com.echothree.control.user.forum.common.edit.ForumEditFactory;
020import com.echothree.control.user.forum.common.edit.ForumForumThreadEdit;
021import com.echothree.control.user.forum.common.form.EditForumForumThreadForm;
022import com.echothree.control.user.forum.common.result.EditForumForumThreadResult;
023import com.echothree.control.user.forum.common.result.ForumResultFactory;
024import com.echothree.control.user.forum.common.spec.ForumForumThreadSpec;
025import com.echothree.model.control.forum.server.control.ForumControl;
026import com.echothree.model.data.forum.server.entity.Forum;
027import com.echothree.model.data.forum.server.entity.ForumForumThread;
028import com.echothree.model.data.user.common.pk.UserVisitPK;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.common.command.EditMode;
033import com.echothree.util.server.control.BaseAbstractEditCommand;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036import javax.inject.Inject;
037
038@Dependent
039public class EditForumForumThreadCommand
040        extends BaseAbstractEditCommand<ForumForumThreadSpec, ForumForumThreadEdit, EditForumForumThreadResult, ForumForumThread, Forum> {
041    
042    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
043    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
044    
045    static {
046        SPEC_FIELD_DEFINITIONS = List.of(
047                new FieldDefinition("ForumName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("ForumThreadName", FieldType.ENTITY_NAME, true, null, null)
049        );
050        
051        EDIT_FIELD_DEFINITIONS = List.of(
052                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
053                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
054        );
055    }
056
057    @Inject
058    ForumControl forumControl;
059
060    
061    /** Creates a new instance of EditForumForumThreadCommand */
062    public EditForumForumThreadCommand() {
063        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
064    }
065    
066    @Override
067    public EditForumForumThreadResult getResult() {
068        return ForumResultFactory.getEditForumForumThreadResult();
069    }
070
071    @Override
072    public ForumForumThreadEdit getEdit() {
073        return ForumEditFactory.getForumForumThreadEdit();
074    }
075
076    @Override
077    public ForumForumThread getEntity(EditForumForumThreadResult result) {
078        ForumForumThread forumForumThread = null;
079        var forumName = spec.getForumName();
080        var forum = forumControl.getForumByName(forumName);
081
082        if(forum != null) {
083            var forumThreadName = spec.getForumThreadName();
084            var forumThread = forumControl.getForumThreadByName(forumThreadName);
085
086            if(forumThread != null) {
087                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
088                    forumForumThread = forumControl.getForumForumThread(forum, forumThread);
089                } else { // EditMode.UPDATE
090                    forumForumThread = forumControl.getForumForumThreadForUpdate(forum, forumThread);
091                }
092
093                if(forumForumThread == null) {
094                    addExecutionError(ExecutionErrors.UnknownForumForumThread.name(), forumName, forumThreadName);
095                }
096            } else {
097                addExecutionError(ExecutionErrors.UnknownForumThreadName.name(), forumThreadName);
098            }
099        } else {
100            addExecutionError(ExecutionErrors.UnknownForumName.name(), forumName);
101        }
102
103        return forumForumThread;
104    }
105
106    @Override
107    public Forum getLockEntity(ForumForumThread forumForumThread) {
108        return forumForumThread.getForum();
109    }
110
111    @Override
112    public void fillInResult(EditForumForumThreadResult result, ForumForumThread forumForumThread) {
113        result.setForumForumThread(forumControl.getForumForumThreadTransfer(getUserVisit(), forumForumThread));
114    }
115
116    @Override
117    public void doLock(ForumForumThreadEdit edit, ForumForumThread forumForumThread) {
118        edit.setIsDefault(forumForumThread.getIsDefault().toString());
119        edit.setSortOrder(forumForumThread.getSortOrder().toString());
120    }
121
122    @Override
123    public void doUpdate(ForumForumThread forumForumThread) {
124        var forumForumThreadValue = forumControl.getForumForumThreadValue(forumForumThread);
125
126        forumForumThreadValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
127        forumForumThreadValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
128
129        forumControl.updateForumForumThreadFromValue(forumForumThreadValue, getPartyPK());
130    }
131    
132}