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