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.CreateCarrierServiceForm; 020import com.echothree.model.control.carrier.server.control.CarrierControl; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.security.common.SecurityRoleGroups; 023import com.echothree.model.control.security.common.SecurityRoles; 024import com.echothree.model.control.selector.common.SelectorKinds; 025import com.echothree.model.control.selector.common.SelectorTypes; 026import com.echothree.model.control.selector.server.control.SelectorControl; 027import com.echothree.model.data.selector.server.entity.Selector; 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.control.CommandSecurityDefinition; 035import com.echothree.util.server.control.PartyTypeDefinition; 036import com.echothree.util.server.control.SecurityRoleDefinition; 037import java.util.List; 038import javax.enterprise.context.Dependent; 039import javax.inject.Inject; 040 041@Dependent 042public class CreateCarrierServiceCommand 043 extends BaseSimpleCommand<CreateCarrierServiceForm> { 044 045 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 046 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 047 048 static { 049 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 050 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 051 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 052 new SecurityRoleDefinition(SecurityRoleGroups.CarrierService.name(), SecurityRoles.Create.name()) 053 )) 054 )); 055 056 FORM_FIELD_DEFINITIONS = List.of( 057 new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null), 058 new FieldDefinition("CarrierServiceName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("GeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("ItemSelectorName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 062 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 063 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 064 ); 065 } 066 067 @Inject 068 CarrierControl carrierControl; 069 070 @Inject 071 SelectorControl selectorControl; 072 073 074 /** Creates a new instance of CreateCarrierServiceCommand */ 075 public CreateCarrierServiceCommand() { 076 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 077 } 078 079 @Override 080 protected BaseResult execute() { 081 var carrierName = form.getCarrierName(); 082 var carrier = carrierControl.getCarrierByName(carrierName); 083 084 if(carrier != null) { 085 var carrierParty = carrier.getParty(); 086 var carrierServiceName = form.getCarrierServiceName(); 087 var carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName); 088 089 if(carrierService == null) { 090 var geoCodeSelectorName = form.getGeoCodeSelectorName(); 091 Selector geoCodeSelector = null; 092 093 if(geoCodeSelectorName != null) { 094 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name()); 095 096 if(selectorKind != null) { 097 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 098 SelectorTypes.CARRIER.name()); 099 100 if(selectorType != null) { 101 geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName); 102 } else { 103 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.POSTAL_ADDRESS.name(), 104 SelectorTypes.CARRIER_SERVICE.name()); 105 } 106 } else { 107 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name()); 108 } 109 } 110 111 if(geoCodeSelectorName == null || geoCodeSelector != null) { 112 var itemSelectorName = form.getItemSelectorName(); 113 Selector itemSelector = null; 114 115 if(itemSelectorName != null) { 116 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 117 118 if(selectorKind != null) { 119 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 120 SelectorTypes.CARRIER.name()); 121 122 if(selectorType != null) { 123 itemSelector = selectorControl.getSelectorByName(selectorType, itemSelectorName); 124 } else { 125 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.ITEM.name(), 126 SelectorTypes.CARRIER_SERVICE.name()); 127 } 128 } else { 129 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 130 } 131 } 132 133 if(itemSelectorName == null || itemSelector != null) { 134 var createdBy = getPartyPK(); 135 var isDefault = Boolean.valueOf(form.getIsDefault()); 136 var sortOrder = Integer.valueOf(form.getSortOrder()); 137 var description = form.getDescription(); 138 139 carrierService = carrierControl.createCarrierService(carrierParty, carrierServiceName, geoCodeSelector, itemSelector, 140 isDefault, sortOrder, createdBy); 141 142 if(description != null) { 143 carrierControl.createCarrierServiceDescription(carrierService, getPreferredLanguage(), description, 144 createdBy); 145 } 146 } else { 147 addExecutionError(ExecutionErrors.UnknownItemSelectorName.name(), itemSelectorName); 148 } 149 } else { 150 addExecutionError(ExecutionErrors.UnknownGeoCodeSelectorName.name(), geoCodeSelectorName); 151 } 152 } else { 153 addExecutionError(ExecutionErrors.DuplicateCarrierServiceName.name(), carrierName, carrierServiceName); 154 } 155 } else { 156 addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName); 157 } 158 159 return null; 160 } 161 162}