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.CarrierOptionDescriptionEdit; 021import com.echothree.control.user.carrier.common.form.EditCarrierOptionDescriptionForm; 022import com.echothree.control.user.carrier.common.result.CarrierResultFactory; 023import com.echothree.control.user.carrier.common.result.EditCarrierOptionDescriptionResult; 024import com.echothree.control.user.carrier.common.spec.CarrierOptionDescriptionSpec; 025import com.echothree.model.control.carrier.server.control.CarrierControl; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.control.security.common.SecurityRoleGroups; 029import com.echothree.model.control.security.common.SecurityRoles; 030import com.echothree.model.data.carrier.server.entity.Carrier; 031import com.echothree.model.data.carrier.server.entity.CarrierOption; 032import com.echothree.model.data.carrier.server.entity.CarrierOptionDescription; 033import com.echothree.model.data.carrier.server.value.CarrierOptionDescriptionValue; 034import com.echothree.model.data.party.server.entity.Language; 035import com.echothree.model.data.party.server.entity.Party; 036import com.echothree.model.data.user.common.pk.UserVisitPK; 037import com.echothree.util.common.message.ExecutionErrors; 038import com.echothree.util.common.validation.FieldDefinition; 039import com.echothree.util.common.validation.FieldType; 040import com.echothree.util.common.command.EditMode; 041import com.echothree.util.server.control.BaseAbstractEditCommand; 042import com.echothree.util.server.control.CommandSecurityDefinition; 043import com.echothree.util.server.control.PartyTypeDefinition; 044import com.echothree.util.server.control.SecurityRoleDefinition; 045import com.echothree.util.server.persistence.Session; 046import java.util.Arrays; 047import java.util.Collections; 048import java.util.List; 049 050public class EditCarrierOptionDescriptionCommand 051 extends BaseAbstractEditCommand<CarrierOptionDescriptionSpec, CarrierOptionDescriptionEdit, EditCarrierOptionDescriptionResult, CarrierOptionDescription, CarrierOption> { 052 053 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 054 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 055 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 056 057 static { 058 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 059 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 060 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 061 new SecurityRoleDefinition(SecurityRoleGroups.CarrierOption.name(), SecurityRoles.Description.name()) 062 ))) 063 ))); 064 065 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 066 new FieldDefinition("CarrierName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("CarrierOptionName", FieldType.ENTITY_NAME, true, null, null), 068 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 069 )); 070 071 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 072 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 073 )); 074 } 075 076 /** Creates a new instance of EditCarrierOptionDescriptionCommand */ 077 public EditCarrierOptionDescriptionCommand(UserVisitPK userVisitPK, EditCarrierOptionDescriptionForm form) { 078 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 079 } 080 081 @Override 082 public EditCarrierOptionDescriptionResult getResult() { 083 return CarrierResultFactory.getEditCarrierOptionDescriptionResult(); 084 } 085 086 @Override 087 public CarrierOptionDescriptionEdit getEdit() { 088 return CarrierEditFactory.getCarrierOptionDescriptionEdit(); 089 } 090 091 @Override 092 public CarrierOptionDescription getEntity(EditCarrierOptionDescriptionResult result) { 093 var carrierControl = Session.getModelController(CarrierControl.class); 094 CarrierOptionDescription carrierOptionDescription = null; 095 String carrierName = spec.getCarrierName(); 096 Carrier carrier = carrierControl.getCarrierByName(carrierName); 097 098 if(carrier != null) { 099 Party carrierParty = carrier.getParty(); 100 String carrierOptionName = spec.getCarrierOptionName(); 101 CarrierOption carrierOption = carrierControl.getCarrierOptionByName(carrierParty, carrierOptionName); 102 103 if(carrierOption != null) { 104 var partyControl = Session.getModelController(PartyControl.class); 105 String languageIsoName = spec.getLanguageIsoName(); 106 Language language = partyControl.getLanguageByIsoName(languageIsoName); 107 108 if(language != null) { 109 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 110 carrierOptionDescription = carrierControl.getCarrierOptionDescription(carrierOption, language); 111 } else { // EditMode.UPDATE 112 carrierOptionDescription = carrierControl.getCarrierOptionDescriptionForUpdate(carrierOption, language); 113 } 114 115 if(carrierOptionDescription == null) { 116 addExecutionError(ExecutionErrors.UnknownCarrierOptionDescription.name(), carrierOptionName, languageIsoName); 117 } 118 } else { 119 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 120 } 121 } else { 122 addExecutionError(ExecutionErrors.UnknownCarrierOptionName.name(), carrierName, carrierOptionName); 123 } 124 } else { 125 addExecutionError(ExecutionErrors.UnknownCarrierName.name(), carrierName); 126 } 127 128 return carrierOptionDescription; 129 } 130 131 @Override 132 public CarrierOption getLockEntity(CarrierOptionDescription carrierOptionDescription) { 133 return carrierOptionDescription.getCarrierOption(); 134 } 135 136 @Override 137 public void fillInResult(EditCarrierOptionDescriptionResult result, CarrierOptionDescription carrierOptionDescription) { 138 var carrierControl = Session.getModelController(CarrierControl.class); 139 140 result.setCarrierOptionDescription(carrierControl.getCarrierOptionDescriptionTransfer(getUserVisit(), carrierOptionDescription)); 141 } 142 143 @Override 144 public void doLock(CarrierOptionDescriptionEdit edit, CarrierOptionDescription carrierOptionDescription) { 145 edit.setDescription(carrierOptionDescription.getDescription()); 146 } 147 148 @Override 149 public void doUpdate(CarrierOptionDescription carrierOptionDescription) { 150 var carrierControl = Session.getModelController(CarrierControl.class); 151 CarrierOptionDescriptionValue carrierOptionDescriptionValue = carrierControl.getCarrierOptionDescriptionValue(carrierOptionDescription); 152 153 carrierOptionDescriptionValue.setDescription(edit.getDescription()); 154 155 carrierControl.updateCarrierOptionDescriptionFromValue(carrierOptionDescriptionValue, getPartyPK()); 156 } 157 158}