/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.forms.impl;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import de.odysseus.calyxo.base.util.PropertyUtils;
import de.odysseus.calyxo.forms.ValidatorBase;
import de.odysseus.calyxo.forms.conf.ArgConfig;
import de.odysseus.calyxo.forms.conf.MessageConfig;
import de.odysseus.calyxo.forms.conf.ValidateConfig;
/**
* Message class used for validation errors.
*
* @author Christoph Beck
*/
public class MessageImpl extends de.odysseus.calyxo.base.Message {
private static final Log log = LogFactory.getLog(MessageImpl.class);
/**
* Constructor.
*/
public MessageImpl(MessageConfig messageConfig) {
init(messageConfig, null);
}
/**
* Constructor.
*/
public MessageImpl(ValidateConfig validateConfig, ValidatorBase bean) {
MessageConfig validateMessageConfig = validateConfig.getMessageConfig();
MessageConfig fieldMessageConfig =
validateConfig.getFieldConfig().getMessageConfig();
MessageConfig validatorMessageConfig =
validateConfig.getValidatorConfig().getMessageConfig();
if (validateMessageConfig != null && validateMessageConfig.getKey() != null) {
init(validateMessageConfig, bean);
} else if (fieldMessageConfig != null && fieldMessageConfig.getKey() != null) {
if (validateMessageConfig == null) {
init(fieldMessageConfig, bean);
} else {
init(validateMessageConfig, fieldMessageConfig, bean);
}
} else if (validatorMessageConfig != null && validatorMessageConfig.getKey() != null) {
if (fieldMessageConfig != null && validateMessageConfig != null) {
init(validateMessageConfig, fieldMessageConfig, validatorMessageConfig, bean);
} else if (fieldMessageConfig != null) {
init(fieldMessageConfig, validatorMessageConfig, bean);
} else if (validateMessageConfig != null) {
init(validateMessageConfig, validatorMessageConfig, bean);
} else {
init(validatorMessageConfig, bean);
}
} else if (validatorMessageConfig != null) {
log.error("No message key for " + validateConfig.toInlineString());
}
}
private void add(ArgConfig config, Object bean) {
if (config.getKey() != null) {
add(new ResourceArg(config.getBundle(), config.getKey()));
} else if (config.getProperty() != null) {
add(new ValueArg(getProperty(bean, config.getProperty())));
} else if (config.getValue() != null) {
add(new ValueArg(config.getValue()));
} else {
add(new ValueArg("???" + config.getName() + "???"));
}
}
private void init(MessageConfig keyMessageConfig, Object bean) {
setKey(keyMessageConfig.getKey());
setBundle(keyMessageConfig.getBundle());
Iterator params = keyMessageConfig.getArgConfigs();
while (params.hasNext()) {
add((ArgConfig)params.next(), bean);
}
}
private void init(MessageConfig messageConfig, MessageConfig keyMessageConfig, Object bean) {
setKey(keyMessageConfig.getKey());
if (messageConfig.getBundle() != null) {
setBundle(messageConfig.getBundle());
} else {
setBundle(keyMessageConfig.getBundle());
}
Iterator args = keyMessageConfig.getArgConfigs();
while (args.hasNext()) {
ArgConfig arg = (ArgConfig)args.next();
String name = arg.getName();
if (name != null) {
ArgConfig overriddenArg = messageConfig.getArgConfig(name);
if (overriddenArg != null) {
arg = overriddenArg;
}
}
add(arg, bean);
}
}
private void init(MessageConfig messageConfig, MessageConfig defaultMessageConfig, MessageConfig keyMessageConfig, Object bean) {
setKey(keyMessageConfig.getKey());
if (messageConfig.getBundle() != null) {
setBundle(messageConfig.getBundle());
} else if (defaultMessageConfig.getBundle() != null) {
setBundle(defaultMessageConfig.getBundle());
} else {
setBundle(keyMessageConfig.getBundle());
}
Iterator args = keyMessageConfig.getArgConfigs();
while (args.hasNext()) {
ArgConfig arg = (ArgConfig)args.next();
String name = arg.getName();
if (name != null) {
ArgConfig overriddenArg = messageConfig.getArgConfig(name);
if (overriddenArg == null) {
overriddenArg = defaultMessageConfig.getArgConfig(name);
}
if (overriddenArg != null) {
arg = overriddenArg;
}
}
add(arg, bean);
}
}
private Object getProperty(Object bean, String property) {
try {
return PropertyUtils.getProperty(bean, property);
} catch (Exception e) {
log.error(
"Could not get property " +
bean.getClass().getName() +
"." +
property,
e
);
}
return "???" + property + "???";
}
}