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.ForumGroupUniversalSpec;
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.UnknownForumGroupNameException;
025import com.echothree.model.control.forum.server.control.ForumControl;
026import com.echothree.model.data.forum.server.entity.ForumGroup;
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 ForumGroupLogic
036        extends BaseLogic {
037
038    @Inject
039    ForumControl forumControl;
040
041    protected ForumGroupLogic() {
042        super();
043    }
044
045    public ForumGroup getForumGroupByName(final ExecutionErrorAccumulator eea, final String forumGroupName,
046            final EntityPermission entityPermission) {
047        var forumGroup = forumControl.getForumGroupByName(forumGroupName, entityPermission);
048
049        if(forumGroup == null) {
050            handleExecutionError(UnknownForumGroupNameException.class, eea, ExecutionErrors.UnknownForumGroupName.name(), forumGroupName);
051        }
052
053        return forumGroup;
054    }
055
056    public ForumGroup getForumGroupByName(final ExecutionErrorAccumulator eea, final String forumGroupName) {
057        return getForumGroupByName(eea, forumGroupName, EntityPermission.READ_ONLY);
058    }
059
060    public ForumGroup getForumGroupByNameForUpdate(final ExecutionErrorAccumulator eea, final String forumGroupName) {
061        return getForumGroupByName(eea, forumGroupName, EntityPermission.READ_WRITE);
062    }
063
064    public ForumGroup getForumGroupByUniversalSpec(final ExecutionErrorAccumulator eea,
065            final ForumGroupUniversalSpec universalSpec, final EntityPermission entityPermission) {
066        ForumGroup forumGroup = null;
067        var forumGroupName = universalSpec.getForumGroupName();
068        var parameterCount = (forumGroupName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec);
069
070        switch(parameterCount) {
071            case 1 -> {
072                if(forumGroupName == null) {
073                    var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec,
074                            ComponentVendors.ECHO_THREE.name(), EntityTypes.ForumGroup.name());
075
076                    if(eea == null || !eea.hasExecutionErrors()) {
077                        forumGroup = forumControl.getForumGroupByEntityInstance(entityInstance, entityPermission);
078                    }
079                } else {
080                    forumGroup = getForumGroupByName(eea, forumGroupName, entityPermission);
081                }
082            }
083            default ->
084                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
085        }
086
087        return forumGroup;
088    }
089
090    public ForumGroup getForumGroupByUniversalSpec(final ExecutionErrorAccumulator eea,
091            final ForumGroupUniversalSpec universalSpec) {
092        return getForumGroupByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY);
093    }
094
095    public ForumGroup getForumGroupByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
096            final ForumGroupUniversalSpec universalSpec) {
097        return getForumGroupByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE);
098    }
099
100}