001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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.opensource.org/licenses/ecl1.php
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: package org.kuali.core.bo;
017:
018: import java.util.Iterator;
019: import java.util.LinkedHashMap;
020: import java.util.Map;
021:
022: import org.apache.commons.lang.StringUtils;
023: import org.kuali.core.util.TypeUtils;
024:
025: /**
026: * Transient Business Object Base Business Object
027: */
028: public abstract class BusinessObjectBase implements BusinessObject {
029: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
030: .getLogger(BusinessObjectBase.class);
031:
032: /**
033: * Default constructor. Required to do some of the voodoo involved in letting the DataDictionary validate attributeNames for a
034: * given BusinessObject subclass.
035: */
036: public BusinessObjectBase() {
037: }
038:
039: /**
040: * @param fieldValues
041: * @return consistently-formatted String containing the given fieldnames and their values
042: */
043: protected String toStringBuilder(LinkedHashMap fieldValues) {
044: String built = null;
045: String className = StringUtils.uncapitalize(StringUtils
046: .substringAfterLast(this .getClass().getName(), "."));
047:
048: if ((fieldValues == null) || fieldValues.isEmpty()) {
049: built = super .toString();
050: } else {
051:
052: StringBuffer prefix = new StringBuffer(className);
053: StringBuffer suffix = new StringBuffer("=");
054:
055: prefix.append("(");
056: suffix.append("(");
057: for (Iterator i = fieldValues.entrySet().iterator(); i
058: .hasNext();) {
059: Map.Entry e = (Map.Entry) i.next();
060:
061: String fieldName = e.getKey().toString();
062: Object fieldValue = e.getValue();
063:
064: String fieldValueString = String.valueOf(e.getValue()); // prevent NullPointerException;
065:
066: if ((fieldValue == null)
067: || TypeUtils
068: .isSimpleType(fieldValue.getClass())) {
069: prefix.append(fieldName);
070: suffix.append(fieldValueString);
071: } else {
072: prefix.append("{");
073: prefix.append(fieldName);
074: prefix.append("}");
075:
076: suffix.append("{");
077: suffix.append(fieldValueString);
078: suffix.append("}");
079: }
080:
081: if (i.hasNext()) {
082: prefix.append(",");
083: suffix.append(",");
084: }
085: }
086: prefix.append(")");
087: suffix.append(")");
088:
089: built = prefix.toString() + suffix.toString();
090: }
091:
092: return built;
093: }
094:
095: /**
096: * @return Map containing the fieldValues of the key fields for this class, indexed by fieldName
097: */
098: abstract protected LinkedHashMap toStringMapper();
099:
100: /**
101: * @see java.lang.Object#toString()
102: */
103: public String toString() {
104: if (!StringUtils.contains(this .getClass().getName(), "$$")) {
105: return toStringBuilder(toStringMapper());
106: } else {
107: return "Proxy: " + this.getClass().getName();
108: }
109: }
110: }
|