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.control.user.forum.server.command; 018 019import com.echothree.control.user.forum.common.form.GetForumMessageForm; 020import com.echothree.control.user.forum.common.result.ForumResultFactory; 021import com.echothree.model.control.core.common.EventTypes; 022import com.echothree.model.control.forum.common.ForumConstants; 023import com.echothree.model.control.forum.server.control.ForumControl; 024import com.echothree.model.control.forum.server.logic.ForumMessageLogic; 025import com.echothree.model.control.forum.server.logic.ForumRoleTypeLogic; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.data.forum.server.entity.ForumMessage; 028import com.echothree.util.common.command.BaseResult; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BaseSingleEntityCommand; 033import java.util.List; 034import javax.enterprise.context.Dependent; 035import javax.inject.Inject; 036 037@Dependent 038public class GetForumMessageCommand 039 extends BaseSingleEntityCommand<ForumMessage, GetForumMessageForm> { 040 041 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 042 043 static { 044 FORM_FIELD_DEFINITIONS = List.of( 045 new FieldDefinition("ForumMessageName", FieldType.ENTITY_NAME, false, null, null), 046 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 047 new FieldDefinition("Uuid", FieldType.UUID, false, null, null) 048 ); 049 } 050 051 @Inject 052 ForumControl forumControl; 053 054 @Inject 055 ForumMessageLogic forumMessageLogic; 056 057 @Inject 058 ForumRoleTypeLogic forumRoleTypeLogic; 059 060 /** Creates a new instance of GetForumMessageCommand */ 061 public GetForumMessageCommand() { 062 super(null, FORM_FIELD_DEFINITIONS, true); 063 } 064 065 @Override 066 protected ForumMessage getEntity() { 067 var forumMessage = forumMessageLogic.getForumMessageByUniversalSpec(this, form); 068 069 if(!hasExecutionErrors() && forumMessage != null) { 070 // UUID lookup bypasses the reader-role check, but never the publication-time check. 071 if(forumMessage.getLastDetail().getPostedTime() <= session.getStartTime() 072 || (getParty() == null ? false : getPartyTypeName().equals(PartyTypes.EMPLOYEE.name()))) { 073 if(form.getUuid() != null || forumRoleTypeLogic.isForumRoleTypePermitted(this, forumMessage, getParty(), ForumConstants.ForumRoleType_READER)) { 074 if(!hasExecutionErrors()) { 075 sendEvent(forumMessage.getPrimaryKey(), EventTypes.READ, null, null, getPartyPK()); 076 } 077 } else { 078 addExecutionError(ExecutionErrors.MissingRequiredForumRoleType.name(), ForumConstants.ForumRoleType_READER); 079 } 080 } else { 081 addExecutionError(ExecutionErrors.UnpublishedForumMessage.name(), forumMessage.getLastDetail().getForumMessageName()); 082 } 083 } 084 085 return hasExecutionErrors() ? null : forumMessage; 086 } 087 088 @Override 089 protected BaseResult getResult(ForumMessage forumMessage) { 090 var result = ForumResultFactory.getGetForumMessageResult(); 091 092 if(forumMessage != null) { 093 result.setForumMessage(forumControl.getForumMessageTransfer(getUserVisit(), forumMessage)); 094 } 095 096 return result; 097 } 098 099}