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.CreateForumMessageAttachmentDescriptionForm;
020import com.echothree.model.control.forum.server.control.ForumControl;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.data.forum.server.entity.ForumMessage;
023import com.echothree.model.data.forum.server.entity.ForumMessageAttachment;
024import com.echothree.model.data.forum.server.entity.ForumMessageAttachmentDescription;
025import com.echothree.model.data.party.server.entity.Language;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.server.control.BaseSimpleCommand;
032import com.echothree.util.server.persistence.Session;
033import java.util.Arrays;
034import java.util.Collections;
035import java.util.List;
036
037public class CreateForumMessageAttachmentDescriptionCommand
038        extends BaseSimpleCommand<CreateForumMessageAttachmentDescriptionForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
044                new FieldDefinition("ForumMessageName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("ForumMessageAttachmentSequence", FieldType.UNSIGNED_INTEGER, true, null, null),
046                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
048                ));
049    }
050    
051    /** Creates a new instance of CreateForumMessageAttachmentDescriptionCommand */
052    public CreateForumMessageAttachmentDescriptionCommand(UserVisitPK userVisitPK, CreateForumMessageAttachmentDescriptionForm form) {
053        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
054    }
055    
056    @Override
057    protected BaseResult execute() {
058        var forumControl = Session.getModelController(ForumControl.class);
059        String forumMessageName = form.getForumMessageName();
060        ForumMessage forumMessage = forumControl.getForumMessageByNameForUpdate(forumMessageName);
061
062        if(forumMessage != null) {
063            Integer forumMessageAttachmentSequence = Integer.valueOf(form.getForumMessageAttachmentSequence());
064            ForumMessageAttachment forumMessageAttachment = forumControl.getForumMessageAttachmentBySequence(forumMessage, forumMessageAttachmentSequence);
065
066            if(forumMessageAttachment != null) {
067                var partyControl = Session.getModelController(PartyControl.class);
068                String languageIsoName = form.getLanguageIsoName();
069                Language language = partyControl.getLanguageByIsoName(languageIsoName);
070
071                if(language != null) {
072                    ForumMessageAttachmentDescription forumMessageAttachmentDescription = forumControl.getForumMessageAttachmentDescription(forumMessageAttachment, language);
073
074                    if(forumMessageAttachmentDescription == null) {
075                        var description = form.getDescription();
076
077                        forumControl.createForumMessageAttachmentDescription(forumMessageAttachment, language, description, getPartyPK());
078                    } else {
079                        addExecutionError(ExecutionErrors.DuplicateForumMessageAttachmentDescription.name(), forumMessageName,
080                                forumMessageAttachmentSequence.toString(), languageIsoName);
081                    }
082                } else {
083                    addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName);
084                }
085            } else {
086                addExecutionError(ExecutionErrors.UnknownForumMessageAttachment.name(), forumMessageName, forumMessageAttachmentSequence.toString());
087            }
088        } else {
089            addExecutionError(ExecutionErrors.UnknownForumMessageName.name(), forumMessageName);
090        }
091        
092        return null;
093    }
094    
095}