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.CreateForumGroupForm; 020import com.echothree.model.control.forum.server.control.ForumControl; 021import com.echothree.model.control.icon.common.IconConstants; 022import com.echothree.model.control.icon.server.control.IconControl; 023import com.echothree.model.data.forum.server.entity.ForumGroup; 024import com.echothree.model.data.icon.server.entity.Icon; 025import com.echothree.model.data.icon.server.entity.IconUsage; 026import com.echothree.model.data.icon.server.entity.IconUsageType; 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.Arrays; 035import java.util.Collections; 036import java.util.List; 037 038public class CreateForumGroupCommand 039 extends BaseSimpleCommand<CreateForumGroupForm> { 040 041 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 042 043 static { 044 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 045 new FieldDefinition("ForumGroupName", FieldType.ENTITY_NAME, true, null, null), 046 new FieldDefinition("IconName", FieldType.ENTITY_NAME, false, null, null), 047 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 048 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 049 )); 050 } 051 052 /** Creates a new instance of CreateForumGroupCommand */ 053 public CreateForumGroupCommand(UserVisitPK userVisitPK, CreateForumGroupForm form) { 054 super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false); 055 } 056 057 @Override 058 protected BaseResult execute() { 059 var forumControl = Session.getModelController(ForumControl.class); 060 String forumGroupName = form.getForumGroupName(); 061 ForumGroup forumGroup = forumControl.getForumGroupByName(forumGroupName); 062 063 if(forumGroup == null) { 064 var iconControl = Session.getModelController(IconControl.class); 065 String iconName = form.getIconName(); 066 Icon icon = iconName == null? null: iconControl.getIconByName(iconName); 067 068 if(iconName == null || icon != null) { 069 if(icon != null) { 070 IconUsageType iconUsageType = iconControl.getIconUsageTypeByName(IconConstants.IconUsageType_FORUM_GROUP); 071 IconUsage iconUsage = iconControl.getIconUsage(iconUsageType, icon); 072 073 if(iconUsage == null) { 074 addExecutionError(ExecutionErrors.UnknownIconUsage.name()); 075 } 076 } 077 078 if(!hasExecutionErrors()) { 079 var partyPK = getPartyPK(); 080 var sortOrder = Integer.valueOf(form.getSortOrder()); 081 var description = form.getDescription(); 082 083 forumGroup = forumControl.createForumGroup(forumGroupName, icon, sortOrder, partyPK); 084 085 if(description != null) { 086 forumControl.createForumGroupDescription(forumGroup, getPreferredLanguage(), description, partyPK); 087 } 088 } 089 } else { 090 addExecutionError(ExecutionErrors.UnknownIconName.name(), iconName); 091 } 092 } else { 093 addExecutionError(ExecutionErrors.DuplicateForumGroupName.name(), forumGroupName); 094 } 095 096 return null; 097 } 098 099}