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.CreateBlogCommentForm; 020import com.echothree.control.user.forum.common.result.ForumResultFactory; 021import com.echothree.model.control.core.server.control.MimeTypeControl; 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.ForumLogic; 025import com.echothree.model.control.icon.common.IconConstants; 026import com.echothree.model.control.icon.server.control.IconControl; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.control.user.server.control.UserControl; 029import com.echothree.util.common.command.BaseResult; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.persistence.BasePK; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.server.control.BaseSimpleCommand; 035import com.echothree.util.server.persistence.Session; 036import java.util.List; 037import javax.enterprise.context.Dependent; 038 039@Dependent 040public class CreateBlogCommentCommand 041 extends BaseSimpleCommand<CreateBlogCommentForm> { 042 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = List.of( 047 new FieldDefinition("Username", FieldType.STRING, false, 1L, 80L), 048 new FieldDefinition("ParentForumMessageName", FieldType.ENTITY_NAME, true, null, null), 049 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 050 new FieldDefinition("PostedTime", FieldType.DATE_TIME, false, null, null), 051 new FieldDefinition("ForumMessageIconName", FieldType.ENTITY_NAME, false, null, null), 052 new FieldDefinition("Title", FieldType.STRING, true, 1L, 512L), 053 new FieldDefinition("ContentMimeTypeName", FieldType.MIME_TYPE, true, null, null), 054 new FieldDefinition("Content", FieldType.STRING, true, null, null) 055 ); 056 } 057 058 /** Creates a new instance of CreateBlogCommentCommand */ 059 public CreateBlogCommentCommand() { 060 super(null, FORM_FIELD_DEFINITIONS, false); 061 } 062 063 @Override 064 protected BaseResult execute() { 065 var userControl = Session.getModelController(UserControl.class); 066 var result = ForumResultFactory.getCreateBlogCommentResult(); 067 var username = form.getUsername(); 068 var userLogin = username == null ? null : userControl.getUserLoginByUsername(username); 069 070 if(username == null || userLogin != null) { 071 var forumControl = Session.getModelController(ForumControl.class); 072 var parentForumMessageName = form.getParentForumMessageName(); 073 var parentForumMessage = forumControl.getForumMessageByName(parentForumMessageName); 074 075 if(parentForumMessage != null) { 076 var parentForumMessageDetail = parentForumMessage.getLastDetail(); 077 var forumMessageTypeName = parentForumMessageDetail.getForumMessageType().getForumMessageTypeName(); 078 079 if(forumMessageTypeName.equals(ForumConstants.ForumMessageType_BLOG_ENTRY) || forumMessageTypeName.equals(ForumConstants.ForumMessageType_BLOG_COMMENT)) { 080 var forumRoleType = ForumLogic.getInstance().getForumRoleTypeByName(this, ForumConstants.ForumRoleType_COMMENTOR); 081 082 if(!hasExecutionErrors()) { 083 var party = userLogin == null ? getParty() : userLogin.getParty(); 084 var forumThread = parentForumMessageDetail.getForumThread(); 085 var forum = forumControl.getDefaultForumForumThread(forumThread).getForum(); 086 087 if(ForumLogic.getInstance().isForumRoleTypePermitted(this, forum, party, forumRoleType)) { 088 var partyControl = Session.getModelController(PartyControl.class); 089 var languageIsoName = form.getLanguageIsoName(); 090 var language = languageIsoName == null? getPreferredLanguage(): partyControl.getLanguageByIsoName(languageIsoName); 091 092 if(language != null) { 093 var iconControl = Session.getModelController(IconControl.class); 094 var forumMessageIconName = form.getForumMessageIconName(); 095 var forumMessageIcon = iconControl.getIconByName(forumMessageIconName); 096 097 if(forumMessageIconName == null || forumMessageIcon != null) { 098 if(forumMessageIcon != null) { 099 var iconUsageType = iconControl.getIconUsageTypeByName(IconConstants.IconUsageType_FORUM_MESSAGE); 100 var iconUsage = iconControl.getIconUsage(iconUsageType, forumMessageIcon); 101 102 if(iconUsage == null) { 103 addExecutionError(ExecutionErrors.UnknownIconUsage.name()); 104 } 105 } 106 107 if(!hasExecutionErrors()) { 108 var mimeTypeControl = Session.getModelController(MimeTypeControl.class); 109 var contentMimeTypeName = form.getContentMimeTypeName(); 110 var contentMimeType = contentMimeTypeName == null? null: mimeTypeControl.getMimeTypeByName(contentMimeTypeName); 111 112 if(contentMimeType != null) { 113 var forumMimeType = forumControl.getForumMimeType(forum, contentMimeType); 114 115 if(forumMimeType != null) { 116 var title = form.getTitle(); 117 var rawPostedTime = form.getPostedTime(); 118 var postedTime = rawPostedTime == null? session.getStartTime(): Long.valueOf(rawPostedTime); 119 var content = form.getContent(); 120 BasePK createdBy = getPartyPK(); 121 122 var forumMessageType = forumControl.getForumMessageTypeByName(ForumConstants.ForumMessageType_BLOG_COMMENT); 123 var forumMessage = forumControl.createForumMessage(forumThread, forumMessageType, parentForumMessage, forumMessageIcon, postedTime, createdBy); 124 forumControl.createForumMessageRole(forumMessage, forumRoleType, party, createdBy); 125 126 var forumMessagePartType = forumControl.getForumMessagePartTypeByName(ForumConstants.ForumMessagePartType_TITLE); 127 var forumMessagePart = forumControl.createForumMessagePart(forumMessage, forumMessagePartType, language, null, createdBy); 128 forumControl.createForumStringMessagePart(forumMessagePart, title, createdBy); 129 130 forumMessagePartType = forumControl.getForumMessagePartTypeByName(ForumConstants.ForumMessagePartType_CONTENT); 131 forumMessagePart = forumControl.createForumMessagePart(forumMessage, forumMessagePartType, language, contentMimeType, createdBy); 132 forumControl.createForumClobMessagePart(forumMessagePart, content, createdBy); 133 134 result.setEntityRef(forumMessage.getPrimaryKey().getEntityRef()); 135 result.setForumMessageName(forumMessage.getLastDetail().getForumMessageName()); 136 } else { 137 addExecutionError(ExecutionErrors.UnknownForumMimeType.name()); 138 } 139 } else { 140 addExecutionError(ExecutionErrors.UnknownContentMimeTypeName.name(), contentMimeTypeName); 141 } 142 } 143 } else { 144 addExecutionError(ExecutionErrors.UnknownForumMessageIconName.name(), forumMessageIconName); 145 } 146 } else { 147 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 148 } 149 } 150 } 151 } else { 152 addExecutionError(ExecutionErrors.InvalidForumMessageType.name(), forumMessageTypeName); 153 } 154 } else { 155 addExecutionError(ExecutionErrors.UnknownForumParentMessageName.name(), parentForumMessageName); 156 } 157 } else { 158 addExecutionError(ExecutionErrors.UnknownUsername.name(), username); 159 } 160 161 return result; 162 } 163 164}