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