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.carrier.server.command; 018 019import com.echothree.control.user.carrier.common.form.CreateCarrierForm; 020import com.echothree.control.user.carrier.common.result.CarrierResultFactory; 021import com.echothree.model.control.accounting.server.control.AccountingControl; 022import com.echothree.model.control.carrier.server.control.CarrierControl; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.party.server.control.PartyControl; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.control.selector.common.SelectorKinds; 028import com.echothree.model.control.selector.common.SelectorTypes; 029import com.echothree.model.control.selector.server.control.SelectorControl; 030import com.echothree.model.data.accounting.server.entity.Currency; 031import com.echothree.model.data.selector.server.entity.Selector; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.message.ExecutionErrors; 035import com.echothree.util.common.persistence.BasePK; 036import com.echothree.util.common.validation.FieldDefinition; 037import com.echothree.util.common.validation.FieldType; 038import com.echothree.util.server.control.BaseSimpleCommand; 039import com.echothree.util.server.control.CommandSecurityDefinition; 040import com.echothree.util.server.control.PartyTypeDefinition; 041import com.echothree.util.server.control.SecurityRoleDefinition; 042import com.echothree.util.server.persistence.Session; 043import java.util.List; 044import javax.enterprise.context.Dependent; 045 046@Dependent 047public class CreateCarrierCommand 048 extends BaseSimpleCommand<CreateCarrierForm> { 049 050 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 051 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 057 new SecurityRoleDefinition(SecurityRoleGroups.Carrier.name(), SecurityRoles.Create.name()) 058 )) 059 )); 060 061 FORM_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("CarrierTypeName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("Name", FieldType.STRING, true, 1L, 60L), 065 new FieldDefinition("PreferredLanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("PreferredCurrencyIsoName", FieldType.ENTITY_NAME, false, null, null), 067 new FieldDefinition("PreferredJavaTimeZoneName", FieldType.TIME_ZONE_NAME, false, null, null), 068 new FieldDefinition("PreferredDateTimeFormatName", FieldType.ENTITY_NAME, false, null, null), 069 new FieldDefinition("GeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null), 070 new FieldDefinition("ItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("AccountValidationPattern", FieldType.REGULAR_EXPRESSION, false, null, null), 072 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 073 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null) 074 ); 075 } 076 077 /** Creates a new instance of CreateCarrierCommand */ 078 public CreateCarrierCommand() { 079 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 080 } 081 082 @Override 083 protected BaseResult execute() { 084 var carrierControl = Session.getModelController(CarrierControl.class); 085 var result = CarrierResultFactory.getCreateCarrierResult(); 086 var carrierName = form.getCarrierName(); 087 var carrier = carrierControl.getCarrierByName(carrierName); 088 089 if(carrier == null) { 090 var carrierTypeName = form.getCarrierTypeName(); 091 var carrierType = carrierControl.getCarrierTypeByName(carrierTypeName); 092 093 if(carrierType != null) { 094 var partyControl = Session.getModelController(PartyControl.class); 095 var preferredLanguageIsoName = form.getPreferredLanguageIsoName(); 096 var preferredLanguage = preferredLanguageIsoName == null? null: partyControl.getLanguageByIsoName(preferredLanguageIsoName); 097 098 if(preferredLanguageIsoName == null || (preferredLanguage != null)) { 099 var preferredJavaTimeZoneName = form.getPreferredJavaTimeZoneName(); 100 var preferredTimeZone = preferredJavaTimeZoneName == null? null: partyControl.getTimeZoneByJavaName(preferredJavaTimeZoneName); 101 102 if(preferredJavaTimeZoneName == null || (preferredTimeZone != null)) { 103 var preferredDateTimeFormatName = form.getPreferredDateTimeFormatName(); 104 var preferredDateTimeFormat = preferredDateTimeFormatName == null? null: partyControl.getDateTimeFormatByName(preferredDateTimeFormatName); 105 106 if(preferredDateTimeFormatName == null || (preferredDateTimeFormat != null)) { 107 var preferredCurrencyIsoName = form.getPreferredCurrencyIsoName(); 108 Currency preferredCurrency; 109 110 if(preferredCurrencyIsoName == null) 111 preferredCurrency = null; 112 else { 113 var accountingControl = Session.getModelController(AccountingControl.class); 114 preferredCurrency = accountingControl.getCurrencyByIsoName(preferredCurrencyIsoName); 115 } 116 117 if(preferredCurrencyIsoName == null || (preferredCurrency != null)) { 118 var geoCodeSelectorName = form.getGeoCodeSelectorName(); 119 Selector geoCodeSelector = null; 120 121 if(geoCodeSelectorName != null) { 122 var selectorControl = Session.getModelController(SelectorControl.class); 123 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name()); 124 125 if(selectorKind != null) { 126 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 127 SelectorTypes.CARRIER.name()); 128 129 if(selectorType != null) { 130 geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName); 131 } else { 132 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.POSTAL_ADDRESS.name(), 133 SelectorTypes.CARRIER.name()); 134 } 135 } else { 136 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name()); 137 } 138 } 139 140 if(geoCodeSelectorName == null || geoCodeSelector != null) { 141 var itemSelectorName = form.getItemSelectorName(); 142 Selector itemSelector = null; 143 144 if(itemSelectorName != null) { 145 var selectorControl = Session.getModelController(SelectorControl.class); 146 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 147 148 if(selectorKind != null) { 149 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 150 SelectorTypes.CARRIER.name()); 151 152 if(selectorType != null) { 153 itemSelector = selectorControl.getSelectorByName(selectorType, itemSelectorName); 154 } else { 155 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.ITEM.name(), 156 SelectorTypes.CARRIER.name()); 157 } 158 } else { 159 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 160 } 161 } 162 163 if(itemSelectorName == null || itemSelector != null) { 164 var partyType = partyControl.getPartyTypeByName(PartyTypes.CARRIER.name()); 165 BasePK createdBy = getPartyPK(); 166 var name = form.getName(); 167 var accountValidationPattern = form.getAccountValidationPattern(); 168 var isDefault = Boolean.valueOf(form.getIsDefault()); 169 var sortOrder = Integer.valueOf(form.getSortOrder()); 170 171 var party = partyControl.createParty(null, partyType, preferredLanguage, preferredCurrency, preferredTimeZone, 172 preferredDateTimeFormat, createdBy); 173 partyControl.createPartyGroup(party, name, createdBy); 174 carrier = carrierControl.createCarrier(party, carrierName, carrierType, geoCodeSelector, itemSelector, 175 accountValidationPattern, isDefault, sortOrder, createdBy); 176 } else { 177 addExecutionError(ExecutionErrors.UnknownItemSelectorName.name(), itemSelectorName); 178 } 179 } else { 180 addExecutionError(ExecutionErrors.UnknownGeoCodeSelectorName.name(), geoCodeSelectorName); 181 } 182 } else { 183 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), preferredCurrencyIsoName); 184 } 185 } else { 186 addExecutionError(ExecutionErrors.UnknownDateTimeFormatName.name(), preferredDateTimeFormatName); 187 } 188 } else { 189 addExecutionError(ExecutionErrors.UnknownJavaTimeZoneName.name(), preferredJavaTimeZoneName); 190 } 191 } else { 192 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), preferredLanguageIsoName); 193 } 194 } else { 195 addExecutionError(ExecutionErrors.UnknownCarrierTypeName.name(), carrierName, carrierTypeName); 196 } 197 } else { 198 addExecutionError(ExecutionErrors.DuplicateCarrierName.name(), carrierName); 199 } 200 201 if(carrier != null) { 202 var party = carrier.getParty(); 203 204 result.setEntityRef(party.getPrimaryKey().getEntityRef()); 205 result.setCarrierName(carrier.getCarrierName()); 206 result.setPartyName(party.getLastDetail().getPartyName()); 207 } 208 209 return result; 210 } 211 212}