001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.control.user.forum.common.result.GetForumMessagesResult;
022import com.echothree.model.control.core.common.ComponentVendors;
023import com.echothree.model.control.core.common.EntityTypes;
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.core.server.entity.EntityInstance;
030import com.echothree.model.data.forum.server.entity.ForumThread;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
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.common.command.BaseResult;
036import com.echothree.util.server.control.BaseSimpleCommand;
037import com.echothree.util.server.persistence.Session;
038import java.util.Arrays;
039import java.util.Collections;
040import java.util.List;
041
042public class GetForumMessagesCommand
043        extends BaseSimpleCommand<GetForumMessagesForm> {
044    
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046    
047    static {
048        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
049                new FieldDefinition("ForumThreadName", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
051                new FieldDefinition("Key", FieldType.KEY, false, null, null),
052                new FieldDefinition("Guid", FieldType.GUID, false, null, null),
053                new FieldDefinition("Ulid", FieldType.ULID, false, null, null)
054                ));
055    }
056    
057    /** Creates a new instance of GetForumMessagesCommand */
058    public GetForumMessagesCommand(UserVisitPK userVisitPK, GetForumMessagesForm form) {
059        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, true);
060    }
061    
062    @Override
063    protected BaseResult execute() {
064        GetForumMessagesResult result = ForumResultFactory.getGetForumMessagesResult();
065        String forumThreadName = form.getForumThreadName();
066        var parameterCount = (forumThreadName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
067
068        if(parameterCount == 1) {
069            var forumControl = Session.getModelController(ForumControl.class);
070            ForumThread forumThread = null;
071
072            if(forumThreadName == null) {
073                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
074                        EntityTypes.ForumThread.name());
075                
076                if(!hasExecutionErrors()) {
077                    forumThread = forumControl.getForumThreadByEntityInstance(entityInstance);
078                }
079            } else {
080                forumThread = forumControl.getForumThreadByName(forumThreadName);
081
082                if(forumThread == null) {
083                    addExecutionError(ExecutionErrors.UnknownForumThreadName.name(), forumThreadName);
084                }
085            }
086
087            if(!hasExecutionErrors()) {
088                if(forumThread.getLastDetail().getPostedTime() <= session.START_TIME
089                        || (getParty() == null ? false : getPartyTypeName().equals(PartyTypes.EMPLOYEE.name()))) {
090                    if(form.getKey() != null || ForumLogic.getInstance().isForumRoleTypePermitted(this, forumThread, getParty(), ForumConstants.ForumRoleType_READER)) {
091                        result.setForumMessages(forumControl.getForumMessageTransfersByForumThread(getUserVisit(), forumThread));
092                    }
093                } else {
094                    addExecutionError(ExecutionErrors.UnpublishedForumThread.name(), forumThread.getLastDetail().getForumThreadName());
095                }
096            }
097        } else {
098            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
099        }
100        
101        return result;
102    }
103    
104}