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.GenderEdit; 020import com.echothree.control.user.party.common.edit.PartyEditFactory; 021import com.echothree.control.user.party.common.form.EditGenderForm; 022import com.echothree.control.user.party.common.result.PartyResultFactory; 023import com.echothree.control.user.party.common.spec.GenderSpec; 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 java.util.ArrayList; 033import java.util.List; 034import javax.enterprise.context.Dependent; 035import javax.inject.Inject; 036 037@Dependent 038public class EditGenderCommand 039 extends BaseEditCommand<GenderSpec, GenderEdit> { 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("GenderName", FieldType.ENTITY_NAME, true, null, null) 047 ); 048 049 EDIT_FIELD_DEFINITIONS = List.of( 050 new FieldDefinition("GenderName", FieldType.ENTITY_NAME, true, null, null), 051 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 052 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 053 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 054 ); 055 } 056 057 @Inject 058 PartyControl partyControl; 059 060 061 /** Creates a new instance of EditGenderCommand */ 062 public EditGenderCommand() { 063 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 064 } 065 066 @Override 067 protected BaseResult execute() { 068 var result = PartyResultFactory.getEditGenderResult(); 069 070 if(editMode.equals(EditMode.LOCK)) { 071 var genderName = spec.getGenderName(); 072 var gender = partyControl.getGenderByName(genderName); 073 074 if(gender != null) { 075 result.setGender(partyControl.getGenderTransfer(getUserVisit(), gender)); 076 077 if(lockEntity(gender)) { 078 var genderDescription = partyControl.getGenderDescription(gender, getPreferredLanguage()); 079 var edit = PartyEditFactory.getGenderEdit(); 080 var genderDetail = gender.getLastDetail(); 081 082 result.setEdit(edit); 083 edit.setGenderName(genderDetail.getGenderName()); 084 edit.setIsDefault(genderDetail.getIsDefault().toString()); 085 edit.setSortOrder(genderDetail.getSortOrder().toString()); 086 087 if(genderDescription != null) 088 edit.setDescription(genderDescription.getDescription()); 089 } else { 090 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 091 } 092 093 result.setEntityLock(getEntityLockTransfer(gender)); 094 } else { 095 addExecutionError(ExecutionErrors.UnknownGenderName.name(), genderName); 096 } 097 } else if(editMode.equals(EditMode.UPDATE)) { 098 var genderName = spec.getGenderName(); 099 var gender = partyControl.getGenderByNameForUpdate(genderName); 100 101 if(gender != null) { 102 genderName = edit.getGenderName(); 103 var duplicateGender = partyControl.getGenderByName(genderName); 104 105 if(duplicateGender == null || gender.equals(duplicateGender)) { 106 if(lockEntityForUpdate(gender)) { 107 try { 108 var partyPK = getPartyPK(); 109 var genderDetailValue = partyControl.getGenderDetailValueForUpdate(gender); 110 var genderDescription = partyControl.getGenderDescriptionForUpdate(gender, getPreferredLanguage()); 111 var description = edit.getDescription(); 112 113 genderDetailValue.setGenderName(edit.getGenderName()); 114 genderDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 115 genderDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 116 117 partyControl.updateGenderFromValue(genderDetailValue, partyPK); 118 119 if(genderDescription == null && description != null) { 120 partyControl.createGenderDescription(gender, getPreferredLanguage(), description, partyPK); 121 } else if(genderDescription != null && description == null) { 122 partyControl.deleteGenderDescription(genderDescription, partyPK); 123 } else if(genderDescription != null && description != null) { 124 var genderDescriptionValue = partyControl.getGenderDescriptionValue(genderDescription); 125 126 genderDescriptionValue.setDescription(description); 127 partyControl.updateGenderDescriptionFromValue(genderDescriptionValue, partyPK); 128 } 129 } finally { 130 unlockEntity(gender); 131 } 132 } else { 133 addExecutionError(ExecutionErrors.EntityLockStale.name()); 134 } 135 } else { 136 addExecutionError(ExecutionErrors.DuplicateGenderName.name(), genderName); 137 } 138 } else { 139 addExecutionError(ExecutionErrors.UnknownGenderName.name(), genderName); 140 } 141 142 if(hasExecutionErrors()) { 143 result.setGender(partyControl.getGenderTransfer(getUserVisit(), gender)); 144 result.setEntityLock(getEntityLockTransfer(gender)); 145 } 146 } 147 148 return result; 149 } 150 151}