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 com.echothree.util.server.persistence.Session;
034import java.util.List;
035import javax.enterprise.context.Dependent;
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    /** Creates a new instance of GetCommentStatusChoicesCommand */
053    public GetCommentStatusChoicesCommand() {
054        super(null, FORM_FIELD_DEFINITIONS, false);
055    }
056    
057    @Override
058    protected BaseResult execute() {
059        var result = CommentResultFactory.getGetCommentStatusChoicesResult();
060        var commentControl = Session.getModelController(CommentControl.class);
061        var commentTypeName = form.getCommentTypeName();
062        var commentName = form.getCommentName();
063        var parameterCount = (commentTypeName == null ? 0 : 1) + (commentName == null ? 0 : 1);
064
065        if(parameterCount == 1) {
066            CommentType commentType = null;
067            Comment comment = null;
068            
069            if(commentTypeName != null) {
070                var entityType = EntityTypeLogic.getInstance().getEntityTypeByName(this, ComponentVendors.ECHO_THREE.name(), EntityTypes.Comment.name());
071                
072                if(!hasExecutionErrors()) {
073                    commentType = commentControl.getCommentTypeByName(entityType, commentTypeName);
074                    
075                    if(commentType == null) {
076                        addExecutionError(ExecutionErrors.UnknownCommentTypeName.name(), ComponentVendors.ECHO_THREE.name(), EntityTypes.Comment.name(),
077                                commentTypeName);
078                    }
079                }
080            } else {
081                comment = commentControl.getCommentByName(commentName);
082                
083                if(comment == null) {
084                    addExecutionError(ExecutionErrors.UnknownCommentName.name(), commentName);
085                } else {
086                    commentType = comment.getLastDetail().getCommentType();
087                }
088            }
089
090            if(!hasExecutionErrors()) {
091                var defaultCommentStatusChoice = form.getDefaultCommentStatusChoice();
092                var allowNullChoice = Boolean.parseBoolean(form.getAllowNullChoice());
093
094                result.setCommentStatusChoices(commentControl.getCommentStatusChoices(this, defaultCommentStatusChoice, getPreferredLanguage(),
095                        allowNullChoice, commentType, comment, getPartyPK()));
096            }
097        } else {
098            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
099        }
100        
101        return result;
102    }
103    
104}