View Javadoc

1   /*
2    * Copyright 2005-2007 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kns.bo;
17  
18  import java.util.Iterator;
19  import java.util.LinkedHashMap;
20  import java.util.Map;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.rice.kns.util.TypeUtils;
24  
25  /**
26   * Transient Business Object Base Business Object
27   */
28  public abstract class BusinessObjectBase implements BusinessObject {
29      //private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BusinessObjectBase.class);
30  
31      /**
32       * Default constructor. Required to do some of the voodoo involved in letting the DataDictionary validate attributeNames for a
33       * given BusinessObject subclass.
34       */
35      public BusinessObjectBase() {
36      }
37  
38      /**
39       * @param fieldValues
40       * @return consistently-formatted String containing the given fieldnames and their values
41       */
42      protected String toStringBuilder(LinkedHashMap fieldValues) {
43          String built = null;
44          String className = StringUtils.uncapitalize(StringUtils.substringAfterLast(this.getClass().getName(), "."));
45  
46          if ((fieldValues == null) || fieldValues.isEmpty()) {
47              built = super.toString();
48          }
49          else {
50  
51              StringBuffer prefix = new StringBuffer(className);
52              StringBuffer suffix = new StringBuffer("=");
53  
54              prefix.append("(");
55              suffix.append("(");
56              for (Iterator i = fieldValues.entrySet().iterator(); i.hasNext();) {
57                  Map.Entry e = (Map.Entry) i.next();
58  
59                  String fieldName = e.getKey().toString();
60                  Object fieldValue = e.getValue();
61  
62                  String fieldValueString = String.valueOf(e.getValue()); // prevent NullPointerException;
63  
64  
65                  if ((fieldValue == null) || TypeUtils.isSimpleType(fieldValue.getClass())) {
66                      prefix.append(fieldName);
67                      suffix.append(fieldValueString);
68                  }
69                  else {
70                      prefix.append("{");
71                      prefix.append(fieldName);
72                      prefix.append("}");
73  
74                      suffix.append("{");
75                      suffix.append(fieldValueString);
76                      suffix.append("}");
77                  }
78  
79                  if (i.hasNext()) {
80                      prefix.append(",");
81                      suffix.append(",");
82                  }
83              }
84              prefix.append(")");
85              suffix.append(")");
86  
87              built = prefix.toString() + suffix.toString();
88          }
89  
90          return built;
91      }
92  
93      /**
94       * @return Map containing the fieldValues of the key fields for this class, indexed by fieldName
95       */
96      abstract protected LinkedHashMap toStringMapper();
97  
98  
99      /**
100      * @see java.lang.Object#toString()
101      */
102     public String toString() {
103         if (!StringUtils.contains(this.getClass().getName(), "$$")) {
104             return toStringBuilder(toStringMapper());
105         }
106         else {
107             return "Proxy: " + this.getClass().getName();
108         }
109     }
110     
111     public void prepareForWorkflow() {
112 	// do nothing
113     }
114 }