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 com.echothree.util.server.persistence.Session; 038import java.util.List; 039import javax.enterprise.context.Dependent; 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 /** Creates a new instance of CreateCarrierServiceCommand */ 068 public CreateCarrierServiceCommand() { 069 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 070 } 071 072 @Override 073 protected BaseResult execute() { 074 var carrierControl = Session.getModelController(CarrierControl.class); 075 var carrierName = form.getCarrierName(); 076 var carrier = carrierControl.getCarrierByName(carrierName); 077 078 if(carrier != null) { 079 var carrierParty = carrier.getParty(); 080 var carrierServiceName = form.getCarrierServiceName(); 081 var carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName); 082 083 if(carrierService == null) { 084 var geoCodeSelectorName = form.getGeoCodeSelectorName(); 085 Selector geoCodeSelector = null; 086 087 if(geoCodeSelectorName != null) { 088 var selectorControl = Session.getModelController(SelectorControl.class); 089 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name()); 090 091 if(selectorKind != null) { 092 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 093 SelectorTypes.CARRIER.name()); 094 095 if(selectorType != null) { 096 geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName); 097 } else { 098 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.POSTAL_ADDRESS.name(), 099 SelectorTypes.CARRIER_SERVICE.name()); 100 } 101 } else { 102 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name()); 103 } 104 } 105 106 if(geoCodeSelectorName == null || geoCodeSelector != null) { 107 var itemSelectorName = form.getItemSelectorName(); 108 Selector itemSelector = null; 109 110 if(itemSelectorName != null) { 111 var selectorControl = Session.getModelController(SelectorControl.class); 112 var selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 113 114 if(selectorKind != null) { 115 var selectorType = selectorControl.getSelectorTypeByName(selectorKind, 116 SelectorTypes.CARRIER.name()); 117 118 if(selectorType != null) { 119 itemSelector = selectorControl.getSelectorByName(selectorType, itemSelectorName); 120 } else { 121 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.ITEM.name(), 122 SelectorTypes.CARRIER_SERVICE.name()); 123 } 124 } else { 125 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 126 } 127 } 128 129 if(itemSelectorName == null || itemSelector != null) { 130 var createdBy = getPartyPK(); 131 var isDefault = Boolean.valueOf(form.getIsDefault()); 132 var sortOrder = Integer.valueOf(form.getSortOrder()); 133 var description = form.getDescription(); 134 135 carrierService = carrierControl.createCarrierService(carrierParty, carrierServiceName, geoCodeSelector, itemSelector, 136 isDefault, sortOrder, createdBy); 137 138 if(description != null) { 139 carrierControl.createCarrierServiceDescription(carrierService, getPreferredLanguage(), description, 140 createdBy); 141 } 142 } else { 143 addExecutionError(ExecutionErrors.UnknownItemSelectorName.name(), itemSelectorName); 144 } 145 } else { 146 addExecutionError(ExecutionErrors.UnknownGeoCodeSelectorName.name(), geoCodeSelectorName); 147 } 148 } else { 149 addExecutionError(ExecutionErrors.DuplicateCarrierServiceName.name(), carrierName, carrierServiceName); 150 } 151 } else { 152 addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName); 153 } 154 155 return null; 156 } 157 158}