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.club.server.command; 018 019import com.echothree.control.user.club.common.form.CreateClubForm; 020import com.echothree.model.control.accounting.server.control.AccountingControl; 021import com.echothree.model.control.club.server.control.ClubControl; 022import com.echothree.model.control.filter.common.FilterKinds; 023import com.echothree.model.control.filter.common.FilterTypes; 024import com.echothree.model.control.filter.server.control.FilterControl; 025import com.echothree.model.control.filter.server.control.FilterKindControl; 026import com.echothree.model.control.filter.server.control.FilterTypeControl; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.control.subscription.common.SubscriptionConstants; 031import com.echothree.model.control.subscription.server.control.SubscriptionControl; 032import com.echothree.model.data.filter.server.entity.Filter; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.validation.FieldDefinition; 036import com.echothree.util.common.validation.FieldType; 037import com.echothree.util.server.control.BaseSimpleCommand; 038import com.echothree.util.server.control.CommandSecurityDefinition; 039import com.echothree.util.server.control.PartyTypeDefinition; 040import com.echothree.util.server.control.SecurityRoleDefinition; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043import javax.inject.Inject; 044 045@Dependent 046public class CreateClubCommand 047 extends BaseSimpleCommand<CreateClubForm> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 051 052 static { 053 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 054 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 055 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 056 new SecurityRoleDefinition(SecurityRoleGroups.Club.name(), SecurityRoles.Create.name()) 057 )) 058 )); 059 060 FORM_FIELD_DEFINITIONS = List.of( 061 new FieldDefinition("ClubName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("SubscriptionTypeName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("ClubPriceFilterName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 066 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 067 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 068 ); 069 } 070 071 @Inject 072 AccountingControl accountingControl; 073 074 @Inject 075 ClubControl clubControl; 076 077 @Inject 078 FilterControl filterControl; 079 080 @Inject 081 FilterKindControl filterKindControl; 082 083 @Inject 084 FilterTypeControl filterTypeControl; 085 086 @Inject 087 SubscriptionControl subscriptionControl; 088 089 090 /** Creates a new instance of CreateClubCommand */ 091 public CreateClubCommand() { 092 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 093 } 094 095 @Override 096 protected BaseResult execute() { 097 var clubName = form.getClubName(); 098 var club = clubControl.getClubByName(clubName); 099 100 if(club == null) { 101 var subscriptionKind = subscriptionControl.getSubscriptionKindByName(SubscriptionConstants.SubscriptionKind_CLUB); 102 var subscriptionTypeName = form.getSubscriptionTypeName(); 103 var subscriptionType = subscriptionControl.getSubscriptionTypeByName(subscriptionKind, subscriptionTypeName); 104 105 if(subscriptionType != null) { 106 club = clubControl.getClubBySubscriptionType(subscriptionType); 107 108 if(club == null) { 109 var clubPriceFilterName = form.getClubPriceFilterName(); 110 Filter clubPriceFilter = null; 111 112 if(clubPriceFilterName != null) { 113 var filterKind = filterKindControl.getFilterKindByName(FilterKinds.PRICE.name()); 114 var filterType = filterTypeControl.getFilterTypeByName(filterKind, FilterTypes.CLUB.name()); 115 116 if(filterType != null) { 117 clubPriceFilter = filterControl.getFilterByName(filterType, clubPriceFilterName); 118 } 119 } 120 121 if(clubPriceFilterName == null || clubPriceFilter != null) { 122 var currencyIsoName = form.getCurrencyIsoName(); 123 var currency = currencyIsoName == null? null: accountingControl.getCurrencyByIsoName(currencyIsoName); 124 125 if(currencyIsoName == null || currency != null) { 126 var partyPK = getPartyPK(); 127 var isDefault = Boolean.valueOf(form.getIsDefault()); 128 var sortOrder = Integer.valueOf(form.getSortOrder()); 129 var description = form.getDescription(); 130 131 club = clubControl.createClub(clubName, subscriptionType, clubPriceFilter, currency, isDefault, sortOrder, partyPK); 132 133 if(description != null) { 134 clubControl.createClubDescription(club, getPreferredLanguage(), description, partyPK); 135 } 136 } else { 137 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 138 } 139 } else { 140 addExecutionError(ExecutionErrors.UnknownClubPriceFilterName.name(), clubPriceFilterName); 141 } 142 } else { 143 addExecutionError(ExecutionErrors.DuplicateSubscriptionType.name()); 144 } 145 } else { 146 addExecutionError(ExecutionErrors.UnknownSubscriptionTypeName.name(), subscriptionTypeName); 147 } 148 } else { 149 addExecutionError(ExecutionErrors.DuplicateClubName.name(), clubName); 150 } 151 152 return null; 153 } 154 155}