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.edit.ClubEdit;
020import com.echothree.control.user.club.common.edit.ClubEditFactory;
021import com.echothree.control.user.club.common.form.EditClubForm;
022import com.echothree.control.user.club.common.result.ClubResultFactory;
023import com.echothree.control.user.club.common.spec.ClubSpec;
024import com.echothree.model.control.accounting.server.control.AccountingControl;
025import com.echothree.model.control.club.server.control.ClubControl;
026import com.echothree.model.control.filter.common.FilterKinds;
027import com.echothree.model.control.filter.common.FilterTypes;
028import com.echothree.model.control.filter.server.control.FilterControl;
029import com.echothree.model.control.subscription.common.SubscriptionConstants;
030import com.echothree.model.control.subscription.server.control.SubscriptionControl;
031import com.echothree.model.data.filter.server.entity.Filter;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.command.EditMode;
035import com.echothree.util.common.message.ExecutionErrors;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.server.control.BaseEditCommand;
039import com.echothree.util.server.persistence.Session;
040import java.util.Arrays;
041import java.util.Collections;
042import java.util.List;
043import javax.enterprise.context.RequestScoped;
044
045@RequestScoped
046public class EditClubCommand
047        extends BaseEditCommand<ClubSpec, ClubEdit> {
048    
049    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
050    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
051    
052    static {
053        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
054            new FieldDefinition("ClubName", FieldType.ENTITY_NAME, true, null, null)
055        ));
056        
057        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
058            new FieldDefinition("ClubName", FieldType.ENTITY_NAME, true, null, null),
059            new FieldDefinition("SubscriptionTypeName", FieldType.ENTITY_NAME, true, null, null),
060            new FieldDefinition("ClubPriceFilterName", FieldType.ENTITY_NAME, false, null, null),
061            new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
062            new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
063            new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
064            new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
065        ));
066    }
067    
068    /** Creates a new instance of EditClubCommand */
069    public EditClubCommand() {
070        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
071    }
072    
073    @Override
074    protected BaseResult execute() {
075        var clubControl = Session.getModelController(ClubControl.class);
076        var result = ClubResultFactory.getEditClubResult();
077        
078        if(editMode.equals(EditMode.LOCK)) {
079            var clubName = spec.getClubName();
080            var club = clubControl.getClubByName(clubName);
081            
082            if(club != null) {
083                result.setClub(clubControl.getClubTransfer(getUserVisit(), club));
084                
085                if(lockEntity(club)) {
086                    var clubDescription = clubControl.getClubDescription(club, getPreferredLanguage());
087                    var edit = ClubEditFactory.getClubEdit();
088                    var clubDetail = club.getLastDetail();
089                    var clubPriceFilter = clubDetail.getClubPriceFilter();
090                    var currency = clubDetail.getCurrency();
091                    
092                    result.setEdit(edit);
093                    edit.setClubName(clubDetail.getClubName());
094                    edit.setSubscriptionTypeName(clubDetail.getSubscriptionType().getLastDetail().getSubscriptionTypeName());
095                    edit.setClubPriceFilterName(clubPriceFilter == null? null: clubPriceFilter.getLastDetail().getFilterName());
096                    edit.setCurrencyIsoName(currency.getCurrencyIsoName());
097                    edit.setIsDefault(clubDetail.getIsDefault().toString());
098                    edit.setSortOrder(clubDetail.getSortOrder().toString());
099                    
100                    if(clubDescription != null) {
101                        edit.setDescription(clubDescription.getDescription());
102                    }
103                } else {
104                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
105                }
106                
107                result.setEntityLock(getEntityLockTransfer(club));
108            } else {
109                addExecutionError(ExecutionErrors.UnknownClubName.name(), clubName);
110            }
111        } else if(editMode.equals(EditMode.UPDATE)) {
112            var clubName = spec.getClubName();
113            var club = clubControl.getClubByNameForUpdate(clubName);
114            
115            if(club != null) {
116                clubName = edit.getClubName();
117                var duplicateClub = clubControl.getClubByName(clubName);
118                
119                if(duplicateClub == null || club.equals(duplicateClub)) {
120                    var subscriptionControl = Session.getModelController(SubscriptionControl.class);
121                    var subscriptionKind = subscriptionControl.getSubscriptionKindByName(SubscriptionConstants.SubscriptionKind_CLUB);
122                    var subscriptionTypeName = edit.getSubscriptionTypeName();
123                    var subscriptionType = subscriptionControl.getSubscriptionTypeByName(subscriptionKind, subscriptionTypeName);
124                    
125                    if(subscriptionType != null) {
126                        club = clubControl.getClubBySubscriptionType(subscriptionType);
127                        
128                        if(club == null) {
129                            var clubPriceFilterName = edit.getClubPriceFilterName();
130                            Filter clubPriceFilter = null;
131                            
132                            if(clubPriceFilterName != null) {
133                                var filterControl = Session.getModelController(FilterControl.class);
134                                var filterKind = filterControl.getFilterKindByName(FilterKinds.PRICE.name());
135                                var filterType = filterControl.getFilterTypeByName(filterKind, FilterTypes.CLUB.name());
136                                
137                                if(filterType != null) {
138                                    clubPriceFilter = filterControl.getFilterByName(filterType, clubPriceFilterName);
139                                }
140                            }
141                            
142                            if(clubPriceFilterName == null || clubPriceFilter != null) {
143                                var accountingControl = Session.getModelController(AccountingControl.class);
144                                var currencyIsoName = edit.getCurrencyIsoName();
145                                var currency = currencyIsoName == null? null: accountingControl.getCurrencyByIsoName(currencyIsoName);
146                                
147                                if(currencyIsoName == null || currency != null) {
148                                    if(lockEntityForUpdate(club)) {
149                                        try {
150                                            var partyPK = getPartyPK();
151                                            var clubDetailValue = clubControl.getClubDetailValueForUpdate(club);
152                                            var clubDescription = clubControl.getClubDescriptionForUpdate(club, getPreferredLanguage());
153                                            var description = edit.getDescription();
154                                            
155                                            clubDetailValue.setClubName(edit.getClubName());
156                                            clubDetailValue.setSubscriptionTypePK(subscriptionType.getPrimaryKey());
157                                            clubDetailValue.setClubPriceFilterPK(clubPriceFilter == null? null: clubPriceFilter.getPrimaryKey());
158                                            clubDetailValue.setCurrencyPK(currency.getPrimaryKey());
159                                            clubDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
160                                            clubDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
161                                            
162                                            clubControl.updateClubFromValue(clubDetailValue, partyPK);
163                                            
164                                            if(clubDescription == null && description != null) {
165                                                clubControl.createClubDescription(club, getPreferredLanguage(), description, partyPK);
166                                            } else if(clubDescription != null && description == null) {
167                                                clubControl.deleteClubDescription(clubDescription, partyPK);
168                                            } else if(clubDescription != null && description != null) {
169                                                var clubDescriptionValue = clubControl.getClubDescriptionValue(clubDescription);
170                                                
171                                                clubDescriptionValue.setDescription(description);
172                                                clubControl.updateClubDescriptionFromValue(clubDescriptionValue, partyPK);
173                                            }
174                                        } finally {
175                                            unlockEntity(club);
176                                        }
177                                    } else {
178                                        addExecutionError(ExecutionErrors.EntityLockStale.name());
179                                    }
180                                } else {
181                                    addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
182                                }
183                            } else {
184                                addExecutionError(ExecutionErrors.UnknownClubPriceFilterName.name(), clubPriceFilterName);
185                            }
186                        } else {
187                            addExecutionError(ExecutionErrors.DuplicateSubscriptionType.name());
188                        }
189                    } else {
190                        addExecutionError(ExecutionErrors.UnknownSubscriptionTypeName.name(), subscriptionTypeName);
191                    }
192                } else {
193                    addExecutionError(ExecutionErrors.DuplicateClubName.name(), clubName);
194                }
195            } else {
196                addExecutionError(ExecutionErrors.UnknownClubName.name(), clubName);
197            }
198        }
199        
200        return result;
201    }
202    
203}