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 com.echothree.util.server.persistence.Session;
035import java.util.List;
036import javax.enterprise.context.Dependent;
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    /** Creates a new instance of EditForumForumThreadCommand */
058    public EditForumForumThreadCommand() {
059        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
060    }
061    
062    @Override
063    public EditForumForumThreadResult getResult() {
064        return ForumResultFactory.getEditForumForumThreadResult();
065    }
066
067    @Override
068    public ForumForumThreadEdit getEdit() {
069        return ForumEditFactory.getForumForumThreadEdit();
070    }
071
072    @Override
073    public ForumForumThread getEntity(EditForumForumThreadResult result) {
074        var forumControl = Session.getModelController(ForumControl.class);
075        ForumForumThread forumForumThread = null;
076        var forumName = spec.getForumName();
077        var forum = forumControl.getForumByName(forumName);
078
079        if(forum != null) {
080            var forumThreadName = spec.getForumThreadName();
081            var forumThread = forumControl.getForumThreadByName(forumThreadName);
082
083            if(forumThread != null) {
084                if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
085                    forumForumThread = forumControl.getForumForumThread(forum, forumThread);
086                } else { // EditMode.UPDATE
087                    forumForumThread = forumControl.getForumForumThreadForUpdate(forum, forumThread);
088                }
089
090                if(forumForumThread == null) {
091                    addExecutionError(ExecutionErrors.UnknownForumForumThread.name(), forumName, forumThreadName);
092                }
093            } else {
094                addExecutionError(ExecutionErrors.UnknownForumThreadName.name(), forumThreadName);
095            }
096        } else {
097            addExecutionError(ExecutionErrors.UnknownForumName.name(), forumName);
098        }
099
100        return forumForumThread;
101    }
102
103    @Override
104    public Forum getLockEntity(ForumForumThread forumForumThread) {
105        return forumForumThread.getForum();
106    }
107
108    @Override
109    public void fillInResult(EditForumForumThreadResult result, ForumForumThread forumForumThread) {
110        var forumControl = Session.getModelController(ForumControl.class);
111
112        result.setForumForumThread(forumControl.getForumForumThreadTransfer(getUserVisit(), forumForumThread));
113    }
114
115    @Override
116    public void doLock(ForumForumThreadEdit edit, ForumForumThread forumForumThread) {
117        edit.setIsDefault(forumForumThread.getIsDefault().toString());
118        edit.setSortOrder(forumForumThread.getSortOrder().toString());
119    }
120
121    @Override
122    public void doUpdate(ForumForumThread forumForumThread) {
123        var forumControl = Session.getModelController(ForumControl.class);
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}