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