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.form.GetForumThreadForm;
020import com.echothree.control.user.forum.common.result.ForumResultFactory;
021import com.echothree.model.control.core.common.ComponentVendors;
022import com.echothree.model.control.core.common.EntityTypes;
023import com.echothree.model.control.core.common.EventTypes;
024import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
025import com.echothree.model.control.forum.common.ForumConstants;
026import com.echothree.model.control.forum.server.control.ForumControl;
027import com.echothree.model.control.forum.server.logic.ForumRoleTypeLogic;
028import com.echothree.model.control.party.common.PartyTypes;
029import com.echothree.model.data.forum.server.entity.ForumThread;
030import com.echothree.util.common.command.BaseResult;
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.server.control.BaseSimpleCommand;
035import java.util.List;
036import javax.enterprise.context.Dependent;
037import javax.inject.Inject;
038
039@Dependent
040public class GetForumThreadCommand
041        extends BaseSimpleCommand<GetForumThreadForm> {
042
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        FORM_FIELD_DEFINITIONS = List.of(
047                new FieldDefinition("ForumThreadName", FieldType.ENTITY_NAME, false, null, null),
048                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
049                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
050        );
051    }
052
053    @Inject
054    ForumControl forumControl;
055
056    @Inject
057    EntityInstanceLogic entityInstanceLogic;
058
059    @Inject
060    ForumRoleTypeLogic forumLogic;
061
062    /** Creates a new instance of GetForumThreadCommand */
063    public GetForumThreadCommand() {
064        super(null, FORM_FIELD_DEFINITIONS, true);
065    }
066    
067    @Override
068    protected BaseResult execute() {
069        var result = ForumResultFactory.getGetForumThreadResult();
070        var forumThreadName = form.getForumThreadName();
071        var parameterCount = (forumThreadName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(form);
072
073        if(parameterCount == 1) {
074            ForumThread forumThread = null;
075
076            if(forumThreadName == null) {
077                var entityInstance = entityInstanceLogic.getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
078                        EntityTypes.ForumThread.name());
079                
080                if(!hasExecutionErrors()) {
081                    forumThread = forumControl.getForumThreadByEntityInstance(entityInstance);
082                }
083            } else {
084                forumThread = forumControl.getForumThreadByName(forumThreadName);
085
086                if(forumThread == null) {
087                    addExecutionError(ExecutionErrors.UnknownForumThreadName.name(), forumThreadName);
088                }
089            }
090
091            if(!hasExecutionErrors()) {
092                if(forumThread.getLastDetail().getPostedTime() <= session.getStartTime()
093                        || (getParty() == null ? false : getPartyTypeName().equals(PartyTypes.EMPLOYEE.name()))) {
094                    if(form.getUuid() != null || forumLogic.isForumRoleTypePermitted(this, forumThread, getParty(), ForumConstants.ForumRoleType_READER)) {
095                        result.setForumThread(forumControl.getForumThreadTransfer(getUserVisit(), forumThread));
096                        sendEvent(forumThread.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
097                    } else {
098                        addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER);
099                    }
100                } else {
101                    addExecutionError(ExecutionErrors.UnpublishedForumThread.name(), forumThread.getLastDetail().getForumThreadName());
102                }
103            }
104        } else {
105            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
106        }
107        
108        return result;
109    }
110    
111}