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