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.GetForumMessagesForm;
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.server.logic.EntityInstanceLogic;
024import com.echothree.model.control.forum.common.ForumConstants;
025import com.echothree.model.control.forum.server.control.ForumControl;
026import com.echothree.model.control.forum.server.logic.ForumRoleTypeLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.data.forum.server.entity.ForumMessage;
029import com.echothree.model.data.forum.server.entity.ForumThread;
030import com.echothree.model.data.forum.server.factory.ForumMessageFactory;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
036import java.util.Collection;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class GetForumMessagesCommand
043        extends BasePaginatedMultipleEntitiesCommand<ForumMessage, GetForumMessagesForm> {
044
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046
047    static {
048        FORM_FIELD_DEFINITIONS = List.of(
049                new FieldDefinition("ForumThreadName", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
051                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
052        );
053    }
054
055    @Inject
056    ForumControl forumControl;
057
058    @Inject
059    EntityInstanceLogic entityInstanceLogic;
060
061    @Inject
062    ForumRoleTypeLogic forumLogic;
063
064    /** Creates a new instance of GetForumMessagesCommand */
065    public GetForumMessagesCommand() {
066        super(null, FORM_FIELD_DEFINITIONS, true);
067    }
068
069    ForumThread forumThread;
070
071    @Override
072    protected void handleForm() {
073        var forumThreadName = form.getForumThreadName();
074        var parameterCount = (forumThreadName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(form);
075
076        if(parameterCount == 1) {
077            if(forumThreadName == null) {
078                var entityInstance = entityInstanceLogic.getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
079                        EntityTypes.ForumThread.name());
080
081                if(!hasExecutionErrors()) {
082                    forumThread = forumControl.getForumThreadByEntityInstance(entityInstance);
083                }
084            } else {
085                forumThread = forumControl.getForumThreadByName(forumThreadName);
086
087                if(forumThread == null) {
088                    addExecutionError(ExecutionErrors.UnknownForumThreadName.name(), forumThreadName);
089                }
090            }
091
092            if(!hasExecutionErrors()) {
093                if(forumThread.getLastDetail().getPostedTime() > session.getStartTime()
094                        && (getParty() == null || !getPartyTypeName().equals(PartyTypes.EMPLOYEE.name()))) {
095                    addExecutionError(ExecutionErrors.UnpublishedForumThread.name(), forumThread.getLastDetail().getForumThreadName());
096                } else if(form.getUuid() == null && !forumLogic.isForumRoleTypePermitted(this, forumThread, getParty(), ForumConstants.ForumRoleType_READER)) {
097                    forumThread = null; // TODO: This should be an ExecutionError also.
098                }
099            }
100        } else {
101            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
102        }
103    }
104
105    @Override
106    protected Long getTotalEntities() {
107        return forumThread == null ? null : forumControl.countForumMessagesByForumThread(forumThread);
108    }
109
110    @Override
111    protected Collection<ForumMessage> getEntities() {
112        return forumThread == null ? null : forumControl.getForumMessagesByForumThread(forumThread);
113    }
114
115    @Override
116    protected BaseResult getResult(Collection<ForumMessage> entities) {
117        var result = ForumResultFactory.getGetForumMessagesResult();
118
119        if(entities != null) {
120            result.setForumThread(forumControl.getForumThreadTransfer(getUserVisit(), forumThread));
121
122            if(session.hasLimit(ForumMessageFactory.class)) {
123                result.setForumMessageCount(getTotalEntities());
124            }
125
126            result.setForumMessages(forumControl.getForumMessageTransfers(getUserVisit(), entities));
127        }
128
129        return result;
130    }
131
132}