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