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