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