001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 Echo Three, LLC 003// Copyright 1999-2005 The Apache Software Foundation. 004// 005// Licensed under the Apache License, Version 2.0 (the "License"); 006// you may not use this file except in compliance with the License. 007// You may obtain a copy of the License at 008// 009// http://www.apache.org/licenses/LICENSE-2.0 010// 011// Unless required by applicable law or agreed to in writing, software 012// distributed under the License is distributed on an "AS IS" BASIS, 013// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014// See the License for the specific language governing permissions and 015// limitations under the License. 016// -------------------------------------------------------------------------------- 017 018package com.echothree.util.server.message; 019 020import com.echothree.model.control.core.common.exception.UnknownCommandMessageTypeNameException; 021import com.echothree.model.control.core.server.control.CoreControl; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.data.core.server.entity.CommandMessage; 024import com.echothree.model.data.core.server.entity.CommandMessageTranslation; 025import com.echothree.model.data.core.server.entity.CommandMessageType; 026import com.echothree.model.data.party.server.entity.Language; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.message.Message; 029import com.echothree.util.common.message.Messages; 030import com.echothree.util.server.persistence.Session; 031import java.text.MessageFormat; 032import java.util.Iterator; 033 034public class MessageUtils { 035 036 private static final MessageUtils instance = new MessageUtils(); 037 038 protected MessageUtils() { 039 super(); 040 } 041 042 public static MessageUtils getInstance() { 043 return instance; 044 } 045 046 /** 047 * Escape any single quote characters that are included in the specified 048 * message string. 049 * 050 * @param string The string to be escaped 051 */ 052 protected String escape(String string) { 053 if((string == null) || (string.indexOf('\'') < 0)) { 054 return string; 055 } 056 057 int n = string.length(); 058 StringBuilder sb = new StringBuilder(n); 059 060 for (int i = 0; i < n; i++) { 061 char ch = string.charAt(i); 062 063 if (ch == '\'') { 064 sb.append('\''); 065 } 066 067 sb.append(ch); 068 } 069 070 return sb.toString(); 071 } 072 073 /** 074 * Returns a text message after parametric replacement of the specified 075 * parameter placeholders. A null string result will be returned by this 076 * method if no resource bundle has been configured. 077 */ 078 protected void fillInMessage(CoreControl coreControl, Language language, CommandMessageType commandMessageType, Message message) { 079 String key = message.getKey(); 080 CommandMessage commandMessage = coreControl.getCommandMessageByKey(commandMessageType, key); 081 String translation = null; 082 083 if(commandMessage != null) { 084 CommandMessageTranslation commandMessageTranslation = coreControl.getBestCommandMessageTranslation(commandMessage, language); 085 086 if(commandMessageTranslation != null) { 087 translation = commandMessageTranslation.getTranslation(); 088 089 if(translation != null) { 090 translation = new MessageFormat(escape(translation)).format(message.getValues()); 091 } 092 } 093 } 094 095 message.setMessage(translation == null ? new StringBuilder("??").append(key).append("??").toString() : translation); 096 } 097 098 public void fillInMessages(Language language, String commandMessageTypeName, Messages messages) { 099 if(messages != null) { 100 var coreControl = Session.getModelController(CoreControl.class); 101 CommandMessageType commandMessageType = coreControl.getCommandMessageTypeByName(commandMessageTypeName); 102 103 if(commandMessageType != null) { 104 Iterator<Message> iter = messages.get(); 105 106 while(iter.hasNext()) { 107 fillInMessage(coreControl, language, commandMessageType, iter.next()); 108 } 109 } 110 } 111 } 112 113 // The language used in the Exceptions will always be the default Language (typically English). 114 public String getExceptionMessage(String commandMessageTypeName, Message message) { 115 var coreControl = Session.getModelController(CoreControl.class); 116 CommandMessageType commandMessageType = coreControl.getCommandMessageTypeByName(commandMessageTypeName); 117 118 if(commandMessageType == null) { 119 throw new UnknownCommandMessageTypeNameException(new Message(ExecutionErrors.UnknownCommandMessageTypeName.name(), commandMessageTypeName)); 120 } else { 121 var partyControl = Session.getModelController(PartyControl.class); 122 Language language = partyControl.getDefaultLanguage(); 123 124 fillInMessage(coreControl, language, commandMessageType, message); 125 } 126 127 return message.getMessage(); 128 } 129 130}