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.ForumLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.data.forum.server.entity.ForumThread;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.persistence.Session;
036import java.util.List;
037import javax.enterprise.context.Dependent;
038
039@Dependent
040public class GetForumMessagesCommand
041        extends BaseSimpleCommand<GetForumMessagesForm> {
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    /** Creates a new instance of GetForumMessagesCommand */
054    public GetForumMessagesCommand() {
055        super(null, FORM_FIELD_DEFINITIONS, true);
056    }
057    
058    @Override
059    protected BaseResult execute() {
060        var result = ForumResultFactory.getGetForumMessagesResult();
061        var forumThreadName = form.getForumThreadName();
062        var parameterCount = (forumThreadName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
063
064        if(parameterCount == 1) {
065            var forumControl = Session.getModelController(ForumControl.class);
066            ForumThread forumThread = null;
067
068            if(forumThreadName == null) {
069                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
070                        EntityTypes.ForumThread.name());
071                
072                if(!hasExecutionErrors()) {
073                    forumThread = forumControl.getForumThreadByEntityInstance(entityInstance);
074                }
075            } else {
076                forumThread = forumControl.getForumThreadByName(forumThreadName);
077
078                if(forumThread == null) {
079                    addExecutionError(ExecutionErrors.UnknownForumThreadName.name(), forumThreadName);
080                }
081            }
082
083            if(!hasExecutionErrors()) {
084                if(forumThread.getLastDetail().getPostedTime() <= session.getStartTime()
085                        || (getParty() == null ? false : getPartyTypeName().equals(PartyTypes.EMPLOYEE.name()))) {
086                    if(form.getUuid() != null || ForumLogic.getInstance().isForumRoleTypePermitted(this, forumThread, getParty(), ForumConstants.ForumRoleType_READER)) {
087                        result.setForumMessages(forumControl.getForumMessageTransfersByForumThread(getUserVisit(), forumThread));
088                    }
089                } else {
090                    addExecutionError(ExecutionErrors.UnpublishedForumThread.name(), forumThread.getLastDetail().getForumThreadName());
091                }
092            }
093        } else {
094            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
095        }
096        
097        return result;
098    }
099    
100}