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.associate.server.command; 018 019import com.echothree.control.user.associate.common.edit.AssociateEditFactory; 020import com.echothree.control.user.associate.common.edit.AssociateProgramDescriptionEdit; 021import com.echothree.control.user.associate.common.form.EditAssociateProgramDescriptionForm; 022import com.echothree.control.user.associate.common.result.AssociateResultFactory; 023import com.echothree.control.user.associate.common.spec.AssociateProgramDescriptionSpec; 024import com.echothree.model.control.associate.server.control.AssociateControl; 025import com.echothree.model.control.party.server.control.PartyControl; 026import com.echothree.model.data.user.common.pk.UserVisitPK; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.validation.FieldDefinition; 029import com.echothree.util.common.validation.FieldType; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.common.command.EditMode; 032import com.echothree.util.server.control.BaseEditCommand; 033import java.util.List; 034import javax.enterprise.context.Dependent; 035import javax.inject.Inject; 036 037@Dependent 038public class EditAssociateProgramDescriptionCommand 039 extends BaseEditCommand<AssociateProgramDescriptionSpec, AssociateProgramDescriptionEdit> { 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("AssociateProgramName", FieldType.ENTITY_NAME, true, null, null), 047 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 048 ); 049 050 EDIT_FIELD_DEFINITIONS = List.of( 051 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 052 ); 053 } 054 055 @Inject 056 AssociateControl associateControl; 057 058 @Inject 059 PartyControl partyControl; 060 061 062 /** Creates a new instance of EditAssociateProgramDescriptionCommand */ 063 public EditAssociateProgramDescriptionCommand() { 064 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 065 } 066 067 @Override 068 protected BaseResult execute() { 069 var result = AssociateResultFactory.getEditAssociateProgramDescriptionResult(); 070 var associateProgramName = spec.getAssociateProgramName(); 071 var associateProgram = associateControl.getAssociateProgramByName(associateProgramName); 072 073 if(associateProgram != null) { 074 var languageIsoName = spec.getLanguageIsoName(); 075 var language = partyControl.getLanguageByIsoName(languageIsoName); 076 077 if(language != null) { 078 if(editMode.equals(EditMode.LOCK)) { 079 var associateProgramDescription = associateControl.getAssociateProgramDescription(associateProgram, language); 080 081 if(associateProgramDescription != null) { 082 result.setAssociateProgramDescription(associateControl.getAssociateProgramDescriptionTransfer(getUserVisit(), associateProgramDescription)); 083 084 if(lockEntity(associateProgram)) { 085 var edit = AssociateEditFactory.getAssociateProgramDescriptionEdit(); 086 087 result.setEdit(edit); 088 edit.setDescription(associateProgramDescription.getDescription()); 089 } else { 090 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 091 } 092 093 result.setEntityLock(getEntityLockTransfer(associateProgram)); 094 } else { 095 addExecutionError(ExecutionErrors.UnknownAssociateProgramDescription.name()); 096 } 097 } else if(editMode.equals(EditMode.UPDATE)) { 098 var associateProgramDescriptionValue = associateControl.getAssociateProgramDescriptionValueForUpdate(associateProgram, language); 099 100 if(associateProgramDescriptionValue != null) { 101 if(lockEntityForUpdate(associateProgram)) { 102 try { 103 var description = edit.getDescription(); 104 105 associateProgramDescriptionValue.setDescription(description); 106 107 associateControl.updateAssociateProgramDescriptionFromValue(associateProgramDescriptionValue, getPartyPK()); 108 } finally { 109 unlockEntity(associateProgram); 110 } 111 } else { 112 addExecutionError(ExecutionErrors.EntityLockStale.name()); 113 } 114 } else { 115 addExecutionError(ExecutionErrors.UnknownAssociateProgramDescription.name()); 116 } 117 } 118 } else { 119 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 120 } 121 } else { 122 addExecutionError(ExecutionErrors.UnknownAssociateProgramName.name(), associateProgramName); 123 } 124 125 return result; 126 } 127 128}