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.EventTypes;
022import com.echothree.model.control.forum.common.ForumConstants;
023import com.echothree.model.control.forum.server.control.ForumControl;
024import com.echothree.model.control.forum.server.logic.ForumRoleTypeLogic;
025import com.echothree.model.control.forum.server.logic.ForumThreadLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.data.forum.server.entity.ForumThread;
028import com.echothree.util.common.command.BaseResult;
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.server.control.BaseSingleEntityCommand;
033import java.util.List;
034import javax.enterprise.context.Dependent;
035import javax.inject.Inject;
036
037@Dependent
038public class GetForumThreadCommand
039        extends BaseSingleEntityCommand<ForumThread, GetForumThreadForm> {
040
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042    
043    static {
044        FORM_FIELD_DEFINITIONS = List.of(
045                new FieldDefinition("ForumThreadName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
047                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
048        );
049    }
050
051    @Inject
052    ForumControl forumControl;
053
054    @Inject
055    ForumRoleTypeLogic forumRoleTypeLogic;
056
057    @Inject
058    ForumThreadLogic forumThreadLogic;
059
060    /** Creates a new instance of GetForumThreadCommand */
061    public GetForumThreadCommand() {
062        super(null, FORM_FIELD_DEFINITIONS, true);
063    }
064    
065    @Override
066    protected ForumThread getEntity() {
067        var forumThread = forumThreadLogic.getForumThreadByUniversalSpec(this, form);
068
069        if(!hasExecutionErrors() && forumThread != null) {
070            // UUID lookup bypasses the reader-role check, but never the publication-time check.
071            if(forumThread.getLastDetail().getPostedTime() <= session.getStartTime()
072                    || (getParty() == null ? false : getPartyTypeName().equals(PartyTypes.EMPLOYEE.name()))) {
073                if(form.getUuid() != null || forumRoleTypeLogic.isForumRoleTypePermitted(this, forumThread, getParty(), ForumConstants.ForumRoleType_READER)) {
074                    if(!hasExecutionErrors()) {
075                        sendEvent(forumThread.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
076                    }
077                } else {
078                    addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER);
079                }
080            } else {
081                addExecutionError(ExecutionErrors.UnpublishedForumThread.name(), forumThread.getLastDetail().getForumThreadName());
082            }
083        }
084
085        return hasExecutionErrors() ? null : forumThread;
086    }
087
088    @Override
089    protected BaseResult getResult(ForumThread forumThread) {
090        var result = ForumResultFactory.getGetForumThreadResult();
091
092        if(forumThread != null) {
093            result.setForumThread(forumControl.getForumThreadTransfer(getUserVisit(), forumThread));
094        }
095
096        return result;
097    }
098    
099}