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.GetForumForm;
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.ForumRoleTypeLogic;
028import com.echothree.model.data.forum.server.entity.Forum;
029import com.echothree.util.common.command.BaseResult;
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.server.control.BaseSimpleCommand;
034import com.echothree.util.server.persistence.Session;
035import java.util.List;
036import javax.enterprise.context.Dependent;
037
038@Dependent
039public class GetForumCommand
040        extends BaseSimpleCommand<GetForumForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("ForumName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
048                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
049                );
050    }
051    
052    /** Creates a new instance of GetForumCommand */
053    public GetForumCommand() {
054        super(null, FORM_FIELD_DEFINITIONS, true);
055    }
056    
057    @Override
058    protected BaseResult execute() {
059        var result = ForumResultFactory.getGetForumResult();
060        var forumName = form.getForumName();
061        var parameterCount = (forumName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form);
062
063        if(parameterCount == 1) {
064            var forumControl = Session.getModelController(ForumControl.class);
065            Forum forum = null;
066
067            if(forumName == null) {
068                var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form, ComponentVendors.ECHO_THREE.name(),
069                        EntityTypes.Forum.name());
070                
071                if(!hasExecutionErrors()) {
072                    forum = forumControl.getForumByEntityInstance(entityInstance);
073                }
074            } else {
075                forum = forumControl.getForumByName(forumName);
076
077                if(forum == null) {
078                    addExecutionError(ExecutionErrors.UnknownForumName.name(), forumName);
079                }
080            }
081
082            // If the UUID for the Forum is specified, then bypass the ForumRoleType check.
083            if(!hasExecutionErrors()) {
084                if(form.getUuid() != null || ForumRoleTypeLogic.getInstance().isForumRoleTypePermitted(this, forum, getParty(), ForumConstants.ForumRoleType_READER)) {
085                    result.setForum(forumControl.getForumTransfer(getUserVisit(), forum));
086                    sendEvent(forum.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
087                }
088            } else {
089                addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER);
090            }
091        } else {
092            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
093        }
094
095        return result;
096    }
097    
098}