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.comment.server.command;
018
019import com.echothree.control.user.comment.common.form.GetCommentStatusChoicesForm;
020import com.echothree.control.user.comment.common.result.CommentResultFactory;
021import com.echothree.control.user.comment.common.result.GetCommentStatusChoicesResult;
022import com.echothree.model.control.comment.server.control.CommentControl;
023import com.echothree.model.control.core.common.ComponentVendors;
024import com.echothree.model.control.core.common.EntityTypes;
025import com.echothree.model.control.core.server.logic.EntityTypeLogic;
026import com.echothree.model.data.comment.server.entity.Comment;
027import com.echothree.model.data.comment.server.entity.CommentType;
028import com.echothree.model.data.core.server.entity.EntityType;
029import com.echothree.model.data.user.common.pk.UserVisitPK;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.persistence.Session;
036import java.util.Arrays;
037import java.util.Collections;
038import java.util.List;
039
040public class GetCommentStatusChoicesCommand
041        extends BaseSimpleCommand<GetCommentStatusChoicesForm> {
042    
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
047                new FieldDefinition("CommentTypeName", FieldType.ENTITY_NAME, false, null, null),
048                new FieldDefinition("CommentName", FieldType.ENTITY_NAME, false, null, null),
049                new FieldDefinition("DefaultCommentStatusChoice", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("AllowNullChoice", FieldType.BOOLEAN, true, null, null)
051                ));
052    }
053    
054    /** Creates a new instance of GetCommentStatusChoicesCommand */
055    public GetCommentStatusChoicesCommand(UserVisitPK userVisitPK, GetCommentStatusChoicesForm form) {
056        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
057    }
058    
059    @Override
060    protected BaseResult execute() {
061        GetCommentStatusChoicesResult result = CommentResultFactory.getGetCommentStatusChoicesResult();
062        var commentControl = Session.getModelController(CommentControl.class);
063        String commentTypeName = form.getCommentTypeName();
064        String commentName = form.getCommentName();
065        var parameterCount = (commentTypeName == null ? 0 : 1) + (commentName == null ? 0 : 1);
066
067        if(parameterCount == 1) {
068            CommentType commentType = null;
069            Comment comment = null;
070            
071            if(commentTypeName != null) {
072                EntityType entityType = EntityTypeLogic.getInstance().getEntityTypeByName(this, ComponentVendors.ECHO_THREE.name(), EntityTypes.Comment.name());
073                
074                if(!hasExecutionErrors()) {
075                    commentType = commentControl.getCommentTypeByName(entityType, commentTypeName);
076                    
077                    if(commentType == null) {
078                        addExecutionError(ExecutionErrors.UnknownCommentTypeName.name(), ComponentVendors.ECHO_THREE.name(), EntityTypes.Comment.name(),
079                                commentTypeName);
080                    }
081                }
082            } else {
083                comment = commentControl.getCommentByName(commentName);
084                
085                if(comment == null) {
086                    addExecutionError(ExecutionErrors.UnknownCommentName.name(), commentName);
087                } else {
088                    commentType = comment.getLastDetail().getCommentType();
089                }
090            }
091
092            if(!hasExecutionErrors()) {
093                String defaultCommentStatusChoice = form.getDefaultCommentStatusChoice();
094                boolean allowNullChoice = Boolean.parseBoolean(form.getAllowNullChoice());
095
096                result.setCommentStatusChoices(commentControl.getCommentStatusChoices(this, defaultCommentStatusChoice, getPreferredLanguage(),
097                        allowNullChoice, commentType, comment, getPartyPK()));
098            }
099        } else {
100            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
101        }
102        
103        return result;
104    }
105    
106}