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.forum.server.command;
018
019import com.echothree.control.user.forum.common.form.CreateForumGroupForm;
020import com.echothree.control.user.forum.common.result.ForumResultFactory;
021import com.echothree.model.control.forum.server.control.ForumControl;
022import com.echothree.model.control.icon.common.IconConstants;
023import com.echothree.model.control.icon.server.control.IconControl;
024import com.echothree.model.data.user.common.pk.UserVisitPK;
025import com.echothree.util.common.command.BaseResult;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.server.control.BaseSimpleCommand;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
033
034@Dependent
035public class CreateForumGroupCommand
036        extends BaseSimpleCommand<CreateForumGroupForm> {
037    
038    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
039    
040    static {
041        FORM_FIELD_DEFINITIONS = List.of(
042                new FieldDefinition("ForumGroupName", FieldType.ENTITY_NAME, true, null, null),
043                new FieldDefinition("IconName", FieldType.ENTITY_NAME, false, null, null),
044                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
045                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
046        );
047    }
048
049    @Inject
050    ForumControl forumControl;
051
052    @Inject
053    IconControl iconControl;
054
055    
056    /** Creates a new instance of CreateForumGroupCommand */
057    public CreateForumGroupCommand() {
058        super(null, FORM_FIELD_DEFINITIONS, false);
059    }
060    
061    @Override
062    protected BaseResult execute() {
063        var result = ForumResultFactory.getCreateForumGroupResult();
064        var forumGroupName = form.getForumGroupName();
065        var forumGroup = forumControl.getForumGroupByName(forumGroupName);
066        
067        if(forumGroup == null) {
068            var iconName = form.getIconName();
069            var icon = iconName == null? null: iconControl.getIconByName(iconName);
070            
071            if(iconName == null || icon != null) {
072                if(icon != null) {
073                    var iconUsageType = iconControl.getIconUsageTypeByName(IconConstants.IconUsageType_FORUM_GROUP);
074                    var iconUsage = iconControl.getIconUsage(iconUsageType, icon);
075                    
076                    if(iconUsage == null) {
077                        addExecutionError(ExecutionErrors.UnknownIconUsage.name());
078                    }
079                }
080                
081                if(!hasExecutionErrors()) {
082                    var partyPK = getPartyPK();
083                    var sortOrder = Integer.valueOf(form.getSortOrder());
084                    var description = form.getDescription();
085                    
086                    forumGroup = forumControl.createForumGroup(forumGroupName, icon, sortOrder, partyPK);
087                    
088                    if(description != null) {
089                        forumControl.createForumGroupDescription(forumGroup, getPreferredLanguage(), description, partyPK);
090                    }
091                }
092            } else {
093                addExecutionError(ExecutionErrors.UnknownIconName.name(), iconName);
094            }
095        } else {
096            addExecutionError(ExecutionErrors.DuplicateForumGroupName.name(), forumGroupName);
097        }
098        
099        if(forumGroup != null) {
100            result.setEntityRef(forumGroup.getPrimaryKey().getEntityRef());
101            result.setForumGroupName(forumGroup.getLastDetail().getForumGroupName());
102        }
103
104        return result;
105    }
106    
107}