001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * 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, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.rules.reporting;
017
018 import java.util.Locale;
019
020 import org.springframework.context.MessageSource;
021 import org.springframework.rules.constraint.Constraint;
022 import org.springframework.richclient.application.ApplicationServicesLocator;
023 import org.springframework.richclient.core.Severity;
024
025 /**
026 * @author Keith Donald
027 */
028 public class PropertyResults implements ValidationResults {
029
030 private String propertyName;
031 private Object rejectedValue;
032 private Constraint violatedConstraint;
033 private Severity severity = Severity.ERROR;
034 private MessageTranslatorFactory messageTranslatorFactory;
035
036 public PropertyResults(String propertyName, Object rejectedValue,
037 Constraint violatedConstraint) {
038 this.propertyName = propertyName;
039 this.rejectedValue = rejectedValue;
040 this.violatedConstraint = violatedConstraint;
041 this.messageTranslatorFactory = (MessageTranslatorFactory) ApplicationServicesLocator.services().getService(MessageTranslatorFactory.class);
042 }
043
044 /**
045 * @deprecated MessageSource is configured by MessageTranslator. use <code>buildMessage(Locale)</code>
046 * @see #buildMessage(Locale)
047 */
048 public String buildMessage(MessageSource messages, Locale locale) {
049 return buildMessage(locale);
050 }
051
052 public String buildMessage(Locale locale) {
053 MessageTranslator messageTranslator = messageTranslatorFactory.createTranslator(null, locale);
054 return messageTranslator.getMessage(this);
055 }
056
057 public String getPropertyName() {
058 return propertyName;
059 }
060
061 public Object getRejectedValue() {
062 return rejectedValue;
063 }
064
065 public Constraint getViolatedConstraint() {
066 return violatedConstraint;
067 }
068
069 public int getViolatedCount() {
070 return new SummingVisitor(getViolatedConstraint()).sum();
071 }
072
073 public Severity getSeverity() {
074 return severity;
075 }
076
077 }