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.core.server.command; 018 019import com.echothree.control.user.core.common.edit.CommandMessageTranslationEdit; 020import com.echothree.control.user.core.common.edit.CoreEditFactory; 021import com.echothree.control.user.core.common.form.EditCommandMessageTranslationForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditCommandMessageTranslationResult; 024import com.echothree.control.user.core.common.spec.CommandMessageTranslationSpec; 025import com.echothree.model.control.core.server.control.CommandControl; 026import com.echothree.model.control.party.server.control.PartyControl; 027import com.echothree.model.data.core.server.entity.CommandMessage; 028import com.echothree.model.data.core.server.entity.CommandMessageTranslation; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.command.EditMode; 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.server.control.BaseAbstractEditCommand; 035import com.echothree.util.server.persistence.Session; 036import java.util.List; 037import javax.enterprise.context.Dependent; 038 039@Dependent 040public class EditCommandMessageTranslationCommand 041 extends BaseAbstractEditCommand<CommandMessageTranslationSpec, CommandMessageTranslationEdit, EditCommandMessageTranslationResult, CommandMessageTranslation, CommandMessage> { 042 043 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 044 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 045 046 static { 047 SPEC_FIELD_DEFINITIONS = List.of( 048 new FieldDefinition("CommandMessageTypeName", FieldType.ENTITY_NAME, true, null, null), 049 new FieldDefinition("CommandMessageKey", FieldType.COMMAND_NAME, true, null, null), 050 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 051 ); 052 053 EDIT_FIELD_DEFINITIONS = List.of( 054 new FieldDefinition("Translation", FieldType.STRING, true, 1L, 512L) 055 ); 056 } 057 058 /** Creates a new instance of EditCommandMessageTranslationCommand */ 059 public EditCommandMessageTranslationCommand() { 060 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 061 } 062 063 @Override 064 public EditCommandMessageTranslationResult getResult() { 065 return CoreResultFactory.getEditCommandMessageTranslationResult(); 066 } 067 068 @Override 069 public CommandMessageTranslationEdit getEdit() { 070 return CoreEditFactory.getCommandMessageTranslationEdit(); 071 } 072 073 @Override 074 public CommandMessageTranslation getEntity(EditCommandMessageTranslationResult result) { 075 var commandControl = Session.getModelController(CommandControl.class); 076 CommandMessageTranslation commandMessageTranslation = null; 077 var commandMessageTypeName = spec.getCommandMessageTypeName(); 078 var commandMessageType = commandControl.getCommandMessageTypeByName(commandMessageTypeName); 079 080 if(commandMessageType != null) { 081 var commandMessageKey = spec.getCommandMessageKey(); 082 var commandMessage = commandControl.getCommandMessageByKey(commandMessageType, commandMessageKey); 083 084 if(commandMessage != null) { 085 var partyControl = Session.getModelController(PartyControl.class); 086 var languageIsoName = spec.getLanguageIsoName(); 087 var language = partyControl.getLanguageByIsoName(languageIsoName); 088 089 if(language != null) { 090 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 091 commandMessageTranslation = commandControl.getCommandMessageTranslation(commandMessage, language); 092 } else { // EditMode.UPDATE 093 commandMessageTranslation = commandControl.getCommandMessageTranslationForUpdate(commandMessage, language); 094 } 095 096 if(commandMessageTranslation == null) { 097 addExecutionError(ExecutionErrors.UnknownCommandMessageTranslation.name(), commandMessageTypeName, commandMessageKey, languageIsoName); 098 } 099 } else { 100 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 101 } 102 } else { 103 addExecutionError(ExecutionErrors.UnknownCommandMessageKey.name(), commandMessageTypeName, commandMessageKey); 104 } 105 } else { 106 addExecutionError(ExecutionErrors.UnknownCommandMessageTypeName.name(), commandMessageTypeName); 107 } 108 109 return commandMessageTranslation; 110 } 111 112 @Override 113 public CommandMessage getLockEntity(CommandMessageTranslation commandMessageTranslation) { 114 return commandMessageTranslation.getCommandMessage(); 115 } 116 117 @Override 118 public void fillInResult(EditCommandMessageTranslationResult result, CommandMessageTranslation commandMessageTranslation) { 119 var commandControl = Session.getModelController(CommandControl.class); 120 121 result.setCommandMessageTranslation(commandControl.getCommandMessageTranslationTransfer(getUserVisit(), commandMessageTranslation)); 122 } 123 124 @Override 125 public void doLock(CommandMessageTranslationEdit edit, CommandMessageTranslation commandMessageTranslation) { 126 edit.setTranslation(commandMessageTranslation.getTranslation()); 127 } 128 129 @Override 130 public void doUpdate(CommandMessageTranslation commandMessageTranslation) { 131 var commandControl = Session.getModelController(CommandControl.class); 132 var commandMessageTranslationValue = commandControl.getCommandMessageTranslationValue(commandMessageTranslation); 133 commandMessageTranslationValue.setTranslation(edit.getTranslation()); 134 135 commandControl.updateCommandMessageTranslationFromValue(commandMessageTranslationValue, getPartyPK()); 136 } 137 138}