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.edit.CarrierEditFactory; 020import com.echothree.control.user.carrier.common.edit.CarrierServiceEdit; 021import com.echothree.control.user.carrier.common.form.EditCarrierServiceForm; 022import com.echothree.control.user.carrier.common.result.CarrierResultFactory; 023import com.echothree.control.user.carrier.common.result.EditCarrierServiceResult; 024import com.echothree.control.user.carrier.common.spec.CarrierServiceSpec; 025import com.echothree.model.control.carrier.server.control.CarrierControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.security.common.SecurityRoleGroups; 028import com.echothree.model.control.security.common.SecurityRoles; 029import com.echothree.model.control.selector.common.SelectorKinds; 030import com.echothree.model.control.selector.common.SelectorTypes; 031import com.echothree.model.control.selector.server.control.SelectorControl; 032import com.echothree.model.data.carrier.server.entity.Carrier; 033import com.echothree.model.data.carrier.server.entity.CarrierService; 034import com.echothree.model.data.carrier.server.entity.CarrierServiceDescription; 035import com.echothree.model.data.carrier.server.entity.CarrierServiceDetail; 036import com.echothree.model.data.carrier.server.value.CarrierServiceDescriptionValue; 037import com.echothree.model.data.carrier.server.value.CarrierServiceDetailValue; 038import com.echothree.model.data.party.server.entity.Party; 039import com.echothree.model.data.selector.server.entity.Selector; 040import com.echothree.model.data.selector.server.entity.SelectorKind; 041import com.echothree.model.data.selector.server.entity.SelectorType; 042import com.echothree.model.data.user.common.pk.UserVisitPK; 043import com.echothree.util.common.command.EditMode; 044import com.echothree.util.common.message.ExecutionErrors; 045import com.echothree.util.common.validation.FieldDefinition; 046import com.echothree.util.common.validation.FieldType; 047import com.echothree.util.server.control.BaseAbstractEditCommand; 048import com.echothree.util.server.control.CommandSecurityDefinition; 049import com.echothree.util.server.control.PartyTypeDefinition; 050import com.echothree.util.server.control.SecurityRoleDefinition; 051import com.echothree.util.server.persistence.Session; 052import java.util.Arrays; 053import java.util.Collections; 054import java.util.List; 055 056public class EditCarrierServiceCommand 057 extends BaseAbstractEditCommand<CarrierServiceSpec, CarrierServiceEdit, EditCarrierServiceResult, CarrierService, CarrierService> { 058 059 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 060 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 061 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 062 063 static { 064 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 065 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 066 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 067 new SecurityRoleDefinition(SecurityRoleGroups.CarrierService.name(), SecurityRoles.Edit.name()) 068 ))) 069 ))); 070 071 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 072 new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null), 073 new FieldDefinition("CarrierServiceName", FieldType.ENTITY_NAME, true, null, null) 074 )); 075 076 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 077 new FieldDefinition("CarrierServiceName", FieldType.ENTITY_NAME, true, null, null), 078 new FieldDefinition("GeoCodeSelectorName", FieldType.ENTITY_NAME, false, null, null), 079 new FieldDefinition("ItemSelectorName", FieldType.ENTITY_NAME, true, null, null), 080 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 081 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 082 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 083 )); 084 } 085 086 /** Creates a new instance of EditCarrierServiceCommand */ 087 public EditCarrierServiceCommand(UserVisitPK userVisitPK, EditCarrierServiceForm form) { 088 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 089 } 090 091 @Override 092 public EditCarrierServiceResult getResult() { 093 return CarrierResultFactory.getEditCarrierServiceResult(); 094 } 095 096 @Override 097 public CarrierServiceEdit getEdit() { 098 return CarrierEditFactory.getCarrierServiceEdit(); 099 } 100 101 Party carrierParty; 102 103 @Override 104 public CarrierService getEntity(EditCarrierServiceResult result) { 105 var carrierControl = Session.getModelController(CarrierControl.class); 106 CarrierService carrierService = null; 107 String carrierName = spec.getCarrierName(); 108 Carrier carrier = carrierControl.getCarrierByName(carrierName); 109 110 if(carrier != null) { 111 String carrierServiceName = spec.getCarrierServiceName(); 112 113 carrierParty = carrier.getParty(); 114 115 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 116 carrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName); 117 } else { // EditMode.UPDATE 118 carrierService = carrierControl.getCarrierServiceByNameForUpdate(carrierParty, carrierServiceName); 119 } 120 121 if(carrierService != null) { 122 result.setCarrierService(carrierControl.getCarrierServiceTransfer(getUserVisit(), carrierService)); 123 } else { 124 addExecutionError(ExecutionErrors.UnknownCarrierServiceName.name(), carrierName, carrierServiceName); 125 } 126 } else { 127 addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName); 128 } 129 130 return carrierService; 131 } 132 133 @Override 134 public CarrierService getLockEntity(CarrierService carrierService) { 135 return carrierService; 136 } 137 138 @Override 139 public void fillInResult(EditCarrierServiceResult result, CarrierService carrierService) { 140 var carrierControl = Session.getModelController(CarrierControl.class); 141 142 result.setCarrierService(carrierControl.getCarrierServiceTransfer(getUserVisit(), carrierService)); 143 } 144 145 Selector geoCodeSelector; 146 Selector itemSelector; 147 148 @Override 149 public void doLock(CarrierServiceEdit edit, CarrierService carrierService) { 150 var carrierControl = Session.getModelController(CarrierControl.class); 151 CarrierServiceDescription carrierServiceDescription = carrierControl.getCarrierServiceDescription(carrierService, getPreferredLanguage()); 152 CarrierServiceDetail carrierServiceDetail = carrierService.getLastDetail(); 153 154 geoCodeSelector = carrierServiceDetail.getGeoCodeSelector(); 155 itemSelector = carrierServiceDetail.getItemSelector(); 156 157 edit.setCarrierServiceName(carrierServiceDetail.getCarrierServiceName()); 158 edit.setGeoCodeSelectorName(itemSelector == null ? null: geoCodeSelector.getLastDetail().getSelectorName()); 159 edit.setItemSelectorName(itemSelector == null ? null: itemSelector.getLastDetail().getSelectorName()); 160 edit.setIsDefault(carrierServiceDetail.getIsDefault().toString()); 161 edit.setSortOrder(carrierServiceDetail.getSortOrder().toString()); 162 163 if(carrierServiceDescription != null) { 164 edit.setDescription(carrierServiceDescription.getDescription()); 165 } 166 } 167 168 @Override 169 public void canUpdate(CarrierService carrierService) { 170 var carrierControl = Session.getModelController(CarrierControl.class); 171 String carrierServiceName = edit.getCarrierServiceName(); 172 CarrierService duplicateCarrierService = carrierControl.getCarrierServiceByName(carrierParty, carrierServiceName); 173 174 if(duplicateCarrierService == null || carrierService.equals(duplicateCarrierService)) { 175 String geoCodeSelectorName = edit.getGeoCodeSelectorName(); 176 177 if(geoCodeSelectorName != null) { 178 var selectorControl = Session.getModelController(SelectorControl.class); 179 SelectorKind selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.POSTAL_ADDRESS.name()); 180 181 if(selectorKind != null) { 182 SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind, 183 SelectorTypes.CARRIER.name()); 184 185 if(selectorType != null) { 186 geoCodeSelector = selectorControl.getSelectorByName(selectorType, geoCodeSelectorName); 187 } else { 188 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.POSTAL_ADDRESS.name(), 189 SelectorTypes.CARRIER_SERVICE.name()); 190 } 191 } else { 192 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.POSTAL_ADDRESS.name()); 193 } 194 } 195 196 if(geoCodeSelectorName == null || geoCodeSelector != null) { 197 String itemSelectorName = edit.getItemSelectorName(); 198 199 if(itemSelectorName != null) { 200 var selectorControl = Session.getModelController(SelectorControl.class); 201 SelectorKind selectorKind = selectorControl.getSelectorKindByName(SelectorKinds.ITEM.name()); 202 203 if(selectorKind != null) { 204 SelectorType selectorType = selectorControl.getSelectorTypeByName(selectorKind, 205 SelectorTypes.CARRIER.name()); 206 207 if(selectorType != null) { 208 itemSelector = selectorControl.getSelectorByName(selectorType, itemSelectorName); 209 } else { 210 addExecutionError(ExecutionErrors.UnknownSelectorTypeName.name(), SelectorKinds.ITEM.name(), 211 SelectorTypes.CARRIER_SERVICE.name()); 212 } 213 } else { 214 addExecutionError(ExecutionErrors.UnknownSelectorKindName.name(), SelectorKinds.ITEM.name()); 215 } 216 } 217 218 if(itemSelectorName != null && itemSelector == null) { 219 addExecutionError(ExecutionErrors.UnknownItemSelectorName.name(), itemSelectorName); 220 } 221 } else { 222 addExecutionError(ExecutionErrors.UnknownGeoCodeSelectorName.name(), geoCodeSelectorName); 223 } 224 } else { 225 addExecutionError(ExecutionErrors.DuplicateCarrierServiceName.name(), carrierServiceName); 226 } 227 } 228 229 @Override 230 public void doUpdate(CarrierService carrierService) { 231 var carrierControl = Session.getModelController(CarrierControl.class); 232 var partyPK = getPartyPK(); 233 CarrierServiceDetailValue carrierServiceDetailValue = carrierControl.getCarrierServiceDetailValueForUpdate(carrierService); 234 CarrierServiceDescription carrierServiceDescription = carrierControl.getCarrierServiceDescriptionForUpdate(carrierService, getPreferredLanguage()); 235 String description = edit.getDescription(); 236 237 carrierServiceDetailValue.setCarrierServiceName(edit.getCarrierServiceName()); 238 carrierServiceDetailValue.setGeoCodeSelectorPK(geoCodeSelector == null ? null : geoCodeSelector.getPrimaryKey()); 239 carrierServiceDetailValue.setItemSelectorPK(itemSelector == null ? null : itemSelector.getPrimaryKey()); 240 carrierServiceDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 241 carrierServiceDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 242 243 carrierControl.updateCarrierServiceFromValue(carrierServiceDetailValue, partyPK); 244 245 if(carrierServiceDescription == null && description != null) { 246 carrierControl.createCarrierServiceDescription(carrierService, getPreferredLanguage(), description, partyPK); 247 } else { 248 if(carrierServiceDescription != null && description == null) { 249 carrierControl.deleteCarrierServiceDescription(carrierServiceDescription, partyPK); 250 } else { 251 if(carrierServiceDescription != null && description != null) { 252 CarrierServiceDescriptionValue carrierServiceDescriptionValue = carrierControl.getCarrierServiceDescriptionValue(carrierServiceDescription); 253 254 carrierServiceDescriptionValue.setDescription(description); 255 carrierControl.updateCarrierServiceDescriptionFromValue(carrierServiceDescriptionValue, partyPK); 256 } 257 } 258 } 259 } 260 261}