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.EventTypes;
022import com.echothree.model.control.forum.common.ForumConstants;
023import com.echothree.model.control.forum.server.control.ForumControl;
024import com.echothree.model.control.forum.server.logic.ForumLogic;
025import com.echothree.model.control.forum.server.logic.ForumRoleTypeLogic;
026import com.echothree.model.data.forum.server.entity.Forum;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BaseSingleEntityCommand;
032import java.util.List;
033import javax.enterprise.context.Dependent;
034import javax.inject.Inject;
035
036@Dependent
037public class GetForumCommand
038        extends BaseSingleEntityCommand<Forum, GetForumForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = List.of(
044                new FieldDefinition("ForumName", FieldType.ENTITY_NAME, false, null, null),
045                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
046                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
047        );
048    }
049
050    @Inject
051    ForumControl forumControl;
052
053    @Inject
054    ForumLogic forumLogic;
055
056    @Inject
057    ForumRoleTypeLogic forumRoleTypeLogic;
058
059    /** Creates a new instance of GetForumCommand */
060    public GetForumCommand() {
061        super(null, FORM_FIELD_DEFINITIONS, true);
062    }
063    
064    @Override
065    protected Forum getEntity() {
066        var forum = forumLogic.getForumByUniversalSpec(this, form);
067
068        if(!hasExecutionErrors() && forum != null) {
069            // If the UUID for the Forum is specified, then bypass the ForumRoleType check.
070            if(form.getUuid() != null || forumRoleTypeLogic.isForumRoleTypePermitted(this, forum, getParty(), ForumConstants.ForumRoleType_READER)) {
071                if(!hasExecutionErrors()) {
072                    sendEvent(forum.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK());
073                }
074            } else {
075                addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER);
076            }
077        }
078
079        return hasExecutionErrors() ? null : forum;
080    }
081
082    @Override
083    protected BaseResult getResult(Forum forum) {
084        var result = ForumResultFactory.getGetForumResult();
085
086        if(forum != null) {
087            result.setForum(forumControl.getForumTransfer(getUserVisit(), forum));
088        }
089
090        return result;
091    }
092    
093}