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