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.party.server.command; 018 019import com.echothree.control.user.party.common.edit.PartyEditFactory; 020import com.echothree.control.user.party.common.edit.PersonalTitleEdit; 021import com.echothree.control.user.party.common.form.EditPersonalTitleForm; 022import com.echothree.control.user.party.common.result.EditPersonalTitleResult; 023import com.echothree.control.user.party.common.result.PartyResultFactory; 024import com.echothree.control.user.party.common.spec.PersonalTitleSpec; 025import com.echothree.model.control.party.server.control.PartyControl; 026import com.echothree.model.data.party.common.pk.PersonalTitlePK; 027import com.echothree.model.data.party.server.entity.PersonalTitle; 028import com.echothree.model.data.party.server.entity.PersonalTitleDetail; 029import com.echothree.model.data.party.server.value.PersonalTitleDetailValue; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.common.command.BaseResult; 035import com.echothree.util.common.command.EditMode; 036import com.echothree.util.server.control.BaseEditCommand; 037import com.echothree.util.server.persistence.EntityPermission; 038import com.echothree.util.server.persistence.Session; 039import java.util.Arrays; 040import java.util.Collections; 041import java.util.List; 042 043public class EditPersonalTitleCommand 044 extends BaseEditCommand<PersonalTitleSpec, PersonalTitleEdit> { 045 046 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 047 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 048 049 static { 050 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 051 new FieldDefinition("PersonalTitleId", FieldType.ID, true, null, null) 052 )); 053 054 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 055 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L), 056 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 057 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null) 058 )); 059 } 060 061 /** Creates a new instance of EditPersonalTitleCommand */ 062 public EditPersonalTitleCommand(UserVisitPK userVisitPK, EditPersonalTitleForm form) { 063 super(userVisitPK, form, null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 064 } 065 066 @Override 067 protected BaseResult execute() { 068 var partyControl = Session.getModelController(PartyControl.class); 069 EditPersonalTitleResult result = PartyResultFactory.getEditPersonalTitleResult(); 070 071 if(editMode.equals(EditMode.LOCK)) { 072 String personalTitleId = spec.getPersonalTitleId(); 073 PersonalTitle personalTitle = partyControl.convertPersonalTitleIdToEntity(personalTitleId, EntityPermission.READ_ONLY); 074 075 if(personalTitle != null) { 076 result.setPersonalTitle(partyControl.getPersonalTitleTransfer(getUserVisit(), personalTitle)); 077 078 if(lockEntity(personalTitle)) { 079 PersonalTitleEdit edit = PartyEditFactory.getPersonalTitleEdit(); 080 PersonalTitleDetail personalTitleDetail = personalTitle.getLastDetail(); 081 082 result.setEdit(edit); 083 edit.setDescription(personalTitleDetail.getDescription()); 084 edit.setIsDefault(personalTitleDetail.getIsDefault().toString()); 085 edit.setSortOrder(personalTitleDetail.getSortOrder().toString()); 086 } else { 087 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 088 } 089 090 result.setEntityLock(getEntityLockTransfer(personalTitle)); 091 } else { 092 addExecutionError(ExecutionErrors.UnknownPersonalTitleId.name(), personalTitleId); 093 } 094 } else if(editMode.equals(EditMode.UPDATE)) { 095 String personalTitleId = spec.getPersonalTitleId(); 096 PersonalTitlePK personalTitlePK = partyControl.convertPersonalTitleIdToPK(personalTitleId); 097 098 if(personalTitlePK != null) { 099 if(lockEntityForUpdate(personalTitlePK)) { 100 try { 101 var partyPK = getPartyPK(); 102 PersonalTitleDetailValue personalTitleDetailValue = partyControl.getPersonalTitleDetailValueByPKForUpdate(personalTitlePK); 103 104 personalTitleDetailValue.setDescription(edit.getDescription()); 105 personalTitleDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 106 personalTitleDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 107 108 partyControl.updatePersonalTitleFromValue(personalTitleDetailValue, partyPK); 109 } finally { 110 unlockEntity(personalTitlePK); 111 } 112 } else { 113 addExecutionError(ExecutionErrors.EntityLockStale.name()); 114 } 115 } else { 116 addExecutionError(ExecutionErrors.UnknownPersonalTitleId.name(), personalTitleId); 117 } 118 } 119 120 return result; 121 } 122 123}