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