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 protected ForumThreadLogic() { 042 super(); 043 } 044 045 public ForumThread getForumThreadByName(final ExecutionErrorAccumulator eea, final String forumThreadName, 046 final EntityPermission entityPermission) { 047 var forumThread = forumControl.getForumThreadByName(forumThreadName, entityPermission); 048 049 if(forumThread == null) { 050 handleExecutionError(UnknownForumThreadNameException.class, eea, ExecutionErrors.UnknownForumThreadName.name(), forumThreadName); 051 } 052 053 return forumThread; 054 } 055 056 public ForumThread getForumThreadByName(final ExecutionErrorAccumulator eea, final String forumThreadName) { 057 return getForumThreadByName(eea, forumThreadName, EntityPermission.READ_ONLY); 058 } 059 060 public ForumThread getForumThreadByNameForUpdate(final ExecutionErrorAccumulator eea, final String forumThreadName) { 061 return getForumThreadByName(eea, forumThreadName, EntityPermission.READ_WRITE); 062 } 063 064 public ForumThread getForumThreadByUniversalSpec(final ExecutionErrorAccumulator eea, 065 final ForumThreadUniversalSpec universalSpec, final EntityPermission entityPermission) { 066 ForumThread forumThread = null; 067 var forumThreadName = universalSpec.getForumThreadName(); 068 var parameterCount = (forumThreadName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec); 069 070 switch(parameterCount) { 071 case 1 -> { 072 if(forumThreadName == null) { 073 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec, 074 ComponentVendors.ECHO_THREE.name(), EntityTypes.ForumThread.name()); 075 076 if(eea == null || !eea.hasExecutionErrors()) { 077 forumThread = forumControl.getForumThreadByEntityInstance(entityInstance, entityPermission); 078 } 079 } else { 080 forumThread = getForumThreadByName(eea, forumThreadName, entityPermission); 081 } 082 } 083 default -> 084 handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name()); 085 } 086 087 return forumThread; 088 } 089 090 public ForumThread getForumThreadByUniversalSpec(final ExecutionErrorAccumulator eea, 091 final ForumThreadUniversalSpec universalSpec) { 092 return getForumThreadByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY); 093 } 094 095 public ForumThread getForumThreadByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, 096 final ForumThreadUniversalSpec universalSpec) { 097 return getForumThreadByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE); 098 } 099 100}