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