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.GenderDescriptionEdit; 020import com.echothree.control.user.party.common.edit.PartyEditFactory; 021import com.echothree.control.user.party.common.form.EditGenderDescriptionForm; 022import com.echothree.control.user.party.common.result.PartyResultFactory; 023import com.echothree.control.user.party.common.spec.GenderDescriptionSpec; 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.Session; 033import java.util.ArrayList; 034import java.util.Collections; 035import java.util.List; 036import javax.enterprise.context.RequestScoped; 037 038@RequestScoped 039public class EditGenderDescriptionCommand 040 extends BaseEditCommand<GenderDescriptionSpec, GenderDescriptionEdit> { 041 042 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 043 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 044 045 static { 046 List<FieldDefinition> temp = new ArrayList<>(2); 047 temp.add(new FieldDefinition("GenderName", FieldType.ENTITY_NAME, true, null, null)); 048 temp.add(new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null)); 049 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 050 051 temp = new ArrayList<>(1); 052 temp.add(new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)); 053 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(temp); 054 } 055 056 /** Creates a new instance of EditGenderDescriptionCommand */ 057 public EditGenderDescriptionCommand() { 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.getEditGenderDescriptionResult(); 065 var genderName = spec.getGenderName(); 066 var gender = partyControl.getGenderByName(genderName); 067 068 if(gender != null) { 069 var languageIsoName = spec.getLanguageIsoName(); 070 var language = partyControl.getLanguageByIsoName(languageIsoName); 071 072 if(language != null) { 073 if(editMode.equals(EditMode.LOCK)) { 074 var genderDescription = partyControl.getGenderDescription(gender, language); 075 076 if(genderDescription != null) { 077 result.setGenderDescription(partyControl.getGenderDescriptionTransfer(getUserVisit(), genderDescription)); 078 079 if(lockEntity(gender)) { 080 var edit = PartyEditFactory.getGenderDescriptionEdit(); 081 082 result.setEdit(edit); 083 edit.setDescription(genderDescription.getDescription()); 084 } else { 085 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 086 } 087 088 result.setEntityLock(getEntityLockTransfer(gender)); 089 } else { 090 addExecutionError(ExecutionErrors.UnknownGenderDescription.name()); 091 } 092 } else if(editMode.equals(EditMode.UPDATE)) { 093 var genderDescriptionValue = partyControl.getGenderDescriptionValueForUpdate(gender, language); 094 095 if(genderDescriptionValue != null) { 096 if(lockEntityForUpdate(gender)) { 097 try { 098 var description = edit.getDescription(); 099 100 genderDescriptionValue.setDescription(description); 101 102 partyControl.updateGenderDescriptionFromValue(genderDescriptionValue, getPartyPK()); 103 } finally { 104 unlockEntity(gender); 105 } 106 } else { 107 addExecutionError(ExecutionErrors.EntityLockStale.name()); 108 } 109 } else { 110 addExecutionError(ExecutionErrors.UnknownGenderDescription.name()); 111 } 112 } 113 } else { 114 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 115 } 116 } else { 117 addExecutionError(ExecutionErrors.UnknownGenderName.name(), genderName); 118 } 119 120 return result; 121 } 122 123}