001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.model.control.forum.common.exception.UnknownForumPartyRoleException; 020import com.echothree.model.control.forum.common.exception.UnknownForumPartyTypeRoleException; 021import com.echothree.model.control.forum.common.exception.UnknownForumRoleTypeNameException; 022import com.echothree.model.control.forum.server.control.ForumControl; 023import com.echothree.model.control.party.common.exception.PartyRequiredException; 024import com.echothree.model.data.forum.server.entity.Forum; 025import com.echothree.model.data.forum.server.entity.ForumMessage; 026import com.echothree.model.data.forum.server.entity.ForumRoleType; 027import com.echothree.model.data.forum.server.entity.ForumThread; 028import com.echothree.model.data.party.server.entity.Party; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.server.control.BaseLogic; 031import com.echothree.util.server.message.ExecutionErrorAccumulator; 032import com.echothree.util.server.persistence.Session; 033import javax.enterprise.context.ApplicationScoped; 034import javax.enterprise.inject.spi.CDI; 035 036@ApplicationScoped 037public class ForumLogic 038 extends BaseLogic { 039 040 protected ForumLogic() { 041 super(); 042 } 043 044 public static ForumLogic getInstance() { 045 return CDI.current().select(ForumLogic.class).get(); 046 } 047 048 public ForumRoleType getForumRoleTypeByName(final ExecutionErrorAccumulator eea, final String forumRoleTypeName) { 049 var forumControl = Session.getModelController(ForumControl.class); 050 var forumRoleType = forumControl.getForumRoleTypeByName(forumRoleTypeName); 051 052 if(forumRoleType == null) { 053 handleExecutionError(UnknownForumRoleTypeNameException.class, eea, ExecutionErrors.UnknownForumRoleTypeName.name(), forumRoleTypeName); 054 } 055 056 return forumRoleType; 057 } 058 059 public boolean isForumRoleTypePermitted(final ExecutionErrorAccumulator eea, final Forum forum, final Party party, final String forumRoleTypeName) { 060 var forumRoleType = getForumRoleTypeByName(eea, forumRoleTypeName); 061 062 return isForumRoleTypePermitted(eea, forum, party, forumRoleType); 063 } 064 065 public boolean isForumRoleTypePermitted(final ExecutionErrorAccumulator eea, final Forum forum, final Party party, final ForumRoleType forumRoleType) { 066 var forumControl = Session.getModelController(ForumControl.class); 067 var hasForumPartyTypeRoles = forumControl.hasForumPartyTypeRoles(forum, forumRoleType); 068 var hasForumPartyRoles = forumControl.hasForumPartyRoles(forum, forumRoleType); 069 var permitted = !(hasForumPartyTypeRoles || hasForumPartyRoles); 070 071 if(!permitted) { 072 if(party == null) { 073 handleExecutionError(PartyRequiredException.class, eea, ExecutionErrors.PartyRequired.name()); 074 } else { 075 var hasForumPartyTypeRole = false; 076 var hasForumPartyRole = false; 077 078 if(hasForumPartyTypeRoles) { 079 var partyType = party.getLastDetail().getPartyType(); 080 081 hasForumPartyTypeRole = forumControl.hasForumPartyTypeRole(forum, partyType, forumRoleType); 082 } 083 084 if(hasForumPartyRoles) { 085 hasForumPartyRole = forumControl.hasForumPartyRole(forum, party, forumRoleType); 086 } 087 088 permitted |= hasForumPartyTypeRole || hasForumPartyRole; 089 090 if(!permitted) { 091 var forumName = forum.getLastDetail().getForumName(); 092 var forumRoleTypeName = forumRoleType.getForumRoleTypeName(); 093 094 if(!hasForumPartyRole) { 095 var partyName = party.getLastDetail().getPartyName(); 096 097 handleExecutionError(UnknownForumPartyRoleException.class, eea, ExecutionErrors.UnknownForumPartyRole.name(), forumName, partyName, forumRoleTypeName); 098 } 099 100 if(!hasForumPartyTypeRole) { 101 var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName(); 102 103 handleExecutionError(UnknownForumPartyTypeRoleException.class, eea, ExecutionErrors.UnknownForumPartyTypeRole.name(), forumName, partyTypeName, forumRoleTypeName); 104 } 105 } 106 } 107 } 108 109 return permitted; 110 } 111 112 public boolean isForumRoleTypePermitted(final ExecutionErrorAccumulator eea, final ForumThread forumThread, final Party party, final String forumRoleTypeName) { 113 var forumRoleType = getForumRoleTypeByName(eea, forumRoleTypeName); 114 var permitted = false; 115 116 if(!hasExecutionErrors(eea)) { 117 permitted = isForumRoleTypePermitted(eea, forumThread, party, forumRoleType); 118 } 119 120 return permitted; 121 } 122 123 public boolean isForumRoleTypePermitted(final ExecutionErrorAccumulator eea, final ForumThread forumThread, final Party party, final ForumRoleType forumRoleType) { 124 var forumControl = Session.getModelController(ForumControl.class); 125 var forumForumThreads = forumControl.getForumForumThreadsByForumThread(forumThread); 126 var permitted = false; 127 128 for(var forumForumThread : forumForumThreads) { 129 var forum = forumForumThread.getForum(); 130 131 permitted |= isForumRoleTypePermitted(eea, forum, party, forumRoleType); 132 133 if(permitted) { 134 break; 135 } 136 } 137 138 return permitted; 139 } 140 141 public boolean isForumRoleTypePermitted(final ExecutionErrorAccumulator eea, final ForumMessage forumMessage, final Party party, final String forumRoleTypeName) { 142 var forumRoleType = getForumRoleTypeByName(eea, forumRoleTypeName); 143 var permitted = false; 144 145 if(!hasExecutionErrors(eea)) { 146 permitted = isForumRoleTypePermitted(eea, forumMessage, party, forumRoleType); 147 } 148 149 return permitted; 150 } 151 152 public boolean isForumRoleTypePermitted(final ExecutionErrorAccumulator eea, final ForumMessage forumMessage, final Party party, final ForumRoleType forumRoleType) { 153 var forumThread = forumMessage.getLastDetail().getForumThread(); 154 155 return isForumRoleTypePermitted(eea, forumThread, party, forumRoleType); 156 } 157 158}