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