View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Blacksmith, Inc.
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.web;
20  
21  /***
22  * A pure java implementation of WOAssociation. <br><br>
23  *
24  * A WOAssociation represents the mapping of a property on a 
25  * WOComponent to a property on a WOAssociation. For example: <br><br>
26  * 
27  * MyAssociation: WOString { value = currentCustomer.location.city }; <br><br>
28  *
29  * This example represents a WOAssociation between the value field
30  * on a WOString element and the city property of the location property
31  * of the currentCustomer property of a WOComponent. <br><br>
32  *
33  * To resolve values, a property accessor method will be used in 
34  * preference to a public field, if both exist.  Any null value
35  * in the path will produce null. <br><br>
36  *
37  * A mapping represented in quotation marks: { value = "This is a test." }
38  * is considered a constant value.
39  *
40  * @author michael@mpowers.net
41  * @author $Author: cgruber $
42  * @version $Revision: 893 $
43  */
44  public class WOAssociation implements java.io.Serializable
45  {
46  	protected Object value;
47  	protected String path;
48  	
49  	/***
50  	* The default constructor.  The static factory methods should
51  	* be used to create instances of WOAssociation.
52  	*/
53      protected WOAssociation ()
54      {
55      	value = null;
56  	    path = null;
57      }
58  
59  	/***
60  	* Creates a WOAssociation that maps to a constant value.
61  	*/
62      public static WOAssociation associationWithValue (Object anObject)
63      {
64      	WOAssociation result = new WOAssociation();
65  	    result.value = anObject;
66  		return result;	    
67      }	
68      
69      /***
70      * Creates a WOAssociation that maps to the specified key path.
71      * If the path is null, the association will map to null.
72      * Throws an exception if the property cannot be resolved.
73      */
74      public static WOAssociation associationWithKeyPath (String aString)
75      {
76      	WOAssociation result = new WOAssociation();
77  	    result.path = aString;
78  		return result;	    
79      }	
80  
81  	/***
82  	* Returns the value for this association's key path in the
83  	* specified component, or null if any value in the path is 
84  	* null or if the key path is null.
85  	*/
86      public Object valueInComponent (WOComponent aComponent)
87      {
88          if ( aComponent == null ) return null;
89      	if ( value != null ) return value;
90  		if ( path != null ) return aComponent.valueForKey( path );
91  		throw new RuntimeException( 
92  			"WOAssociation: neither value nor path specified!" );
93      }
94      
95      /***
96      * Sets the property in the specified component to the specified value.
97      * Throws an exception if the property cannot be resolved.
98      */
99      public void setValue (Object aValue, WOComponent aComponent)
100     {
101 		if ( path != null )
102 		{
103 			aComponent.takeValueForKey( aValue, path );
104 			return;
105 		}
106 		throw new RuntimeException( 
107 			"WOAssociation: tried to set value but no path was specified!" );
108 	}
109 
110 	/***
111 	* Returns true if this association is writable; that is,
112 	* returns true if this association is not constant.
113 	*/ 
114     public boolean isValueSettable ()
115     {
116     	return ( path != null );
117     }
118     
119     /***
120 	* Returns true if this association is constant
121 	* and therefore read-only.
122 	*/
123     public boolean isValueConstant ()
124     {
125     	return ( path == null );
126     }
127     
128     /***
129     * For debugging purposes.
130     */
131     public String toString()
132     {
133     	if ( path != null )
134 	    {
135     		return "[WOAssociation:" + path + "]";
136 	    }
137 	    return "[WOAssociation:\"" + value + "\"]";
138     }
139 }	
140 
141 /*
142  * $Log$
143  * Revision 1.1  2006/02/16 13:22:22  cgruber
144  * Check in all sources in eclipse-friendly maven-enabled packages.
145  *
146  * Revision 1.6  2003/01/24 20:13:22  mpowers
147  * Now accepting immutable NSDictionary in constructor, not Map.
148  *
149  * Revision 1.5  2003/01/17 22:55:08  mpowers
150  * Straighted out the parent binding issue (I think).
151  * Fixes for woextensions compatibility.
152  *
153  * Revision 1.3  2003/01/15 19:50:49  mpowers
154  * Fixed issues with WOSession and Serializable.
155  * Can now persist sessions between classloaders (hot swap of class impls).
156  *
157  * Revision 1.2  2003/01/14 15:51:48  mpowers
158  * Removed value() method from WOAssociaton.
159  *
160  * Revision 1.1.1.1  2000/12/21 15:52:50  mpowers
161  * Contributing wotonomy.
162  *
163  * Revision 1.3  2000/12/20 16:25:49  michael
164  * Added log to all files.
165  *
166  *
167  */
168