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.model.control.forum.server.logic;
018
019import com.echothree.control.user.forum.common.spec.ForumThreadUniversalSpec;
020import com.echothree.model.control.core.common.ComponentVendors;
021import com.echothree.model.control.core.common.EntityTypes;
022import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
023import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
024import com.echothree.model.control.forum.common.exception.UnknownForumThreadNameException;
025import com.echothree.model.control.forum.server.control.ForumControl;
026import com.echothree.model.data.forum.server.entity.ForumThread;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.server.control.BaseLogic;
029import com.echothree.util.server.message.ExecutionErrorAccumulator;
030import com.echothree.util.server.persistence.EntityPermission;
031import javax.enterprise.context.ApplicationScoped;
032import javax.inject.Inject;
033
034@ApplicationScoped
035public class ForumThreadLogic
036        extends BaseLogic {
037
038    @Inject
039    ForumControl forumControl;
040
041    @Inject
042    EntityInstanceLogic entityInstanceLogic;
043
044    protected ForumThreadLogic() {
045        super();
046    }
047
048    public ForumThread getForumThreadByName(final ExecutionErrorAccumulator eea, final String forumThreadName,
049            final EntityPermission entityPermission) {
050        var forumThread = forumControl.getForumThreadByName(forumThreadName, entityPermission);
051
052        if(forumThread == null) {
053            handleExecutionError(UnknownForumThreadNameException.class, eea, ExecutionErrors.UnknownForumThreadName.name(), forumThreadName);
054        }
055
056        return forumThread;
057    }
058
059    public ForumThread getForumThreadByName(final ExecutionErrorAccumulator eea, final String forumThreadName) {
060        return getForumThreadByName(eea, forumThreadName, EntityPermission.READ_ONLY);
061    }
062
063    public ForumThread getForumThreadByNameForUpdate(final ExecutionErrorAccumulator eea, final String forumThreadName) {
064        return getForumThreadByName(eea, forumThreadName, EntityPermission.READ_WRITE);
065    }
066
067    public ForumThread getForumThreadByUniversalSpec(final ExecutionErrorAccumulator eea,
068            final ForumThreadUniversalSpec universalSpec, final EntityPermission entityPermission) {
069        ForumThread forumThread = null;
070        var forumThreadName = universalSpec.getForumThreadName();
071        var parameterCount = (forumThreadName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
072
073        switch(parameterCount) {
074            case 1 -> {
075                if(forumThreadName == null) {
076                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
077                            ComponentVendors.ECHO_THREE.name(), EntityTypes.ForumThread.name());
078
079                    if(eea == null || !eea.hasExecutionErrors()) {
080                        forumThread = forumControl.getForumThreadByEntityInstance(entityInstance, entityPermission);
081                    }
082                } else {
083                    forumThread = getForumThreadByName(eea, forumThreadName, entityPermission);
084                }
085            }
086            default ->
087                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
088        }
089
090        return forumThread;
091    }
092
093    public ForumThread getForumThreadByUniversalSpec(final ExecutionErrorAccumulator eea,
094            final ForumThreadUniversalSpec universalSpec) {
095        return getForumThreadByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY);
096    }
097
098    public ForumThread getForumThreadByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
099            final ForumThreadUniversalSpec universalSpec) {
100        return getForumThreadByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE);
101    }
102
103}