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.GetForumMessageForm;
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.ForumLogic;
028import com.echothree.model.control.party.common.PartyTypes;
029import com.echothree.model.data.forum.server.entity.ForumMessage;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
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.BaseSimpleCommand;
036import com.echothree.util.server.persistence.Session;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039
040@Dependent
041public class GetForumMessageCommand
042        extends BaseSimpleCommand<GetForumMessageForm> {
043    
044    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
045    
046    static {
047        FORM_FIELD_DEFINITIONS = List.of(
048                new FieldDefinition("ForumMessageName", FieldType.ENTITY_NAME, false, null, null),
049                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
050                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
051                );
052    }
053    
054    /** Creates a new instance of GetForumMessageCommand */
055    public GetForumMessageCommand() {
056        super(null, FORM_FIELD_DEFINITIONS, true);
057    }
058    
059    @Override
060    protected BaseResult execute() {
061        var result = ForumResultFactory.getGetForumMessageResult();
062        var forumMessageName = form.getForumMessageName();
063        var parameterCount = (forumMessageName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
064
065        if(parameterCount == 1) {
066            var forumControl = Session.getModelController(ForumControl.class);
067            ForumMessage forumMessage = null;
068
069            if(forumMessageName == null) {
070                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
071                        EntityTypes.ForumMessage.name());
072                
073                if(!hasExecutionErrors()) {
074                    forumMessage = forumControl.getForumMessageByEntityInstance(entityInstance);
075                }
076            } else {
077                forumMessage = forumControl.getForumMessageByName(forumMessageName);
078
079                if(forumMessage == null) {
080                    addExecutionError(ExecutionErrors.UnknownForumMessageName.name(), forumMessageName);
081                }
082            }
083            
084            if(!hasExecutionErrors()) {
085                if(forumMessage.getLastDetail().getPostedTime() <= session.getStartTime()
086                        || (getParty() == null ? false : getPartyTypeName().equals(PartyTypes.EMPLOYEE.name()))) {
087                    if(form.getUuid() != null || ForumLogic.getInstance().isForumRoleTypePermitted(this, forumMessage, getParty(), ForumConstants.ForumRoleType_READER)) {
088                        result.setForumMessage(forumControl.getForumMessageTransfer(getUserVisit(), forumMessage));
089                        sendEvent(forumMessage.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
090                    } else {
091                        addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER);
092                    }
093                } else {
094                    addExecutionError(ExecutionErrors.UnpublishedForumMessage.name(), forumMessage.getLastDetail().getForumMessageName());
095                }
096            }
097        } else {
098            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
099        }
100        
101        return result;
102    }
103    
104}