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