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