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