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.party.server.command; 018 019import com.echothree.control.user.party.common.edit.NameSuffixEdit; 020import com.echothree.control.user.party.common.edit.PartyEditFactory; 021import com.echothree.control.user.party.common.form.EditNameSuffixForm; 022import com.echothree.control.user.party.common.result.PartyResultFactory; 023import com.echothree.control.user.party.common.spec.NameSuffixSpec; 024import com.echothree.model.control.party.server.control.PartyControl; 025import com.echothree.model.data.user.common.pk.UserVisitPK; 026import com.echothree.util.common.message.ExecutionErrors; 027import com.echothree.util.common.validation.FieldDefinition; 028import com.echothree.util.common.validation.FieldType; 029import com.echothree.util.common.command.BaseResult; 030import com.echothree.util.common.command.EditMode; 031import com.echothree.util.server.control.BaseEditCommand; 032import com.echothree.util.server.persistence.EntityPermission; 033import com.echothree.util.server.persistence.Session; 034import java.util.List; 035import javax.enterprise.context.Dependent; 036 037@Dependent 038public class EditNameSuffixCommand 039 extends BaseEditCommand<NameSuffixSpec, NameSuffixEdit> { 040 041 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 042 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 043 044 static { 045 SPEC_FIELD_DEFINITIONS = List.of( 046 new FieldDefinition("NameSuffixId", FieldType.ID, true, null, null) 047 ); 048 049 EDIT_FIELD_DEFINITIONS = List.of( 050 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L), 051 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 052 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null) 053 ); 054 } 055 056 /** Creates a new instance of EditNameSuffixCommand */ 057 public EditNameSuffixCommand() { 058 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 059 } 060 061 @Override 062 protected BaseResult execute() { 063 var partyControl = Session.getModelController(PartyControl.class); 064 var result = PartyResultFactory.getEditNameSuffixResult(); 065 066 if(editMode.equals(EditMode.LOCK)) { 067 var nameSuffixId = spec.getNameSuffixId(); 068 var nameSuffix = partyControl.convertNameSuffixIdToEntity(nameSuffixId, EntityPermission.READ_ONLY); 069 070 if(nameSuffix != null) { 071 result.setNameSuffix(partyControl.getNameSuffixTransfer(getUserVisit(), nameSuffix)); 072 073 if(lockEntity(nameSuffix)) { 074 var edit = PartyEditFactory.getNameSuffixEdit(); 075 var nameSuffixDetail = nameSuffix.getLastDetail(); 076 077 result.setEdit(edit); 078 edit.setDescription(nameSuffixDetail.getDescription()); 079 edit.setIsDefault(nameSuffixDetail.getIsDefault().toString()); 080 edit.setSortOrder(nameSuffixDetail.getSortOrder().toString()); 081 } else { 082 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 083 } 084 085 result.setEntityLock(getEntityLockTransfer(nameSuffix)); 086 } else { 087 addExecutionError(ExecutionErrors.UnknownNameSuffixId.name(), nameSuffixId); 088 } 089 } else if(editMode.equals(EditMode.UPDATE)) { 090 var nameSuffixId = spec.getNameSuffixId(); 091 var nameSuffixPK = partyControl.convertNameSuffixIdToPK(nameSuffixId); 092 093 if(nameSuffixPK != null) { 094 if(lockEntityForUpdate(nameSuffixPK)) { 095 try { 096 var partyPK = getPartyPK(); 097 var nameSuffixDetailValue = partyControl.getNameSuffixDetailValueByPKForUpdate(nameSuffixPK); 098 099 nameSuffixDetailValue.setDescription(edit.getDescription()); 100 nameSuffixDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 101 nameSuffixDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 102 103 partyControl.updateNameSuffixFromValue(nameSuffixDetailValue, partyPK); 104 } finally { 105 unlockEntity(nameSuffixPK); 106 } 107 } else { 108 addExecutionError(ExecutionErrors.EntityLockStale.name()); 109 } 110 } else { 111 addExecutionError(ExecutionErrors.UnknownNameSuffixId.name(), nameSuffixId); 112 } 113 } 114 115 return result; 116 } 117 118}