Source code: com/gammastream/validity/GSVAttribute.java
1 package com.gammastream.validity;
2
3 import com.webobjects.appserver.xml.*;
4 import com.webobjects.foundation.*;
5 import com.webobjects.appserver.*;
6 import com.webobjects.eocontrol.*;
7 import java.lang.*;
8 import java.net.*;
9 import java.math.*;
10
11 /**
12 * This structure maps validation rules to a specific attribute of an entity.
13 * For instance, we might wish to map a 'Provided text is > 5 characters' to
14 * the 'username' attribute of the 'user' entity.
15 *
16 * @author GammaStream Technologies, Inc.
17 */
18 public final class GSVAttribute extends Object implements WOXMLCoding {
19
20 //persistant attribute attributes
21 private GSVEntity entity;
22 private String name;
23 private NSMutableArray rules;
24
25 /**
26 * Creates a new GSVAttribute with the provided information.
27 *
28 * @param anEntity The entity this attribute belongs to.
29 * @param aName The name of this attribute.
30 * @exception java.lang.IllegalArgumentException Thrown if the selected EOModel does not contain an attribute of the specified name in the provided entity.
31 */
32 public GSVAttribute(GSVEntity anEntity, String aName) throws IllegalArgumentException {
33 if(this.validateAttributeForName(anEntity, aName)){
34 entity = anEntity;
35 name = aName;
36 rules = new NSMutableArray();
37 }else{
38 throw new IllegalArgumentException("EOAttribute named '"+ aName +"' does not exist for EOEnity named '"+anEntity.name()+"' in EOModel for path: "+anEntity.model().eomodelPath());
39 }
40 }
41
42 /**
43 * Private validation for checking for valid attributes.
44 */
45 private boolean validateAttributeForName(GSVEntity anEntity, String aName){
46 try{
47 GSVEOModel gsmodel = new GSVEOModel(anEntity.model().eomodelPath());
48 GSVEOEntity eoEntity = (gsmodel).entityNamed(anEntity.name());
49 return ( eoEntity.attributeNamed(aName) != null );
50 }catch(java.io.IOException e){
51 System.out.println(e);
52 return false;
53 }
54 }
55
56 /**
57 * Returns the name of this attribute.
58 *
59 * @return The name of the attribute.
60 * @see #setName
61 */
62 public String name(){
63 return name;
64 }
65
66 /**
67 * Sets the name of the attribute.
68 *
69 * @param newName The new name for the attribute.
70 * @see #name
71 */
72 public void setName(String newName){
73 name = newName;
74 }
75
76 /**
77 * The list of rules assigned to this attribute.
78 *
79 * @return An NSArray of rules.
80 * @see #removeRule
81 * @see #addRule
82 */
83 public NSArray rules(){
84 return rules;
85 }
86
87 /**
88 * Adds the rule to this attribute.
89 *
90 * @param newRule The rule to add.
91 * @see #removeRule
92 * @see #rules
93 */
94 public void addRule(GSVRule newRule){
95 rules.addObject(newRule);
96 }
97
98 /**
99 * Remove the rule from this attribute.
100 *
101 * @param oldRule The rule to remove.
102 * @see #addRule
103 * @see #rules
104 */
105 public void removeRule(GSVRule oldRule){
106 rules.removeObject(oldRule);
107 }
108
109
110 /**
111 * Returns the rule with the provided name.
112 *
113 * @param name The name of the rule you wish to fetch.
114 * @return Returns the rule with the provided name or null if one was not found.
115 */
116 public GSVRule ruleNamed(String name){
117 GSVRule currentrule = null;
118 for(int i=0;i<rules.count();i++){
119 currentrule = (GSVRule)rules.objectAtIndex(i);
120 if(currentrule.ruleName().equals(name))
121 return currentrule;
122 }
123 return null;
124 }
125
126 /**
127 * Returns the parent entity for this attribute.
128 *
129 * @returns The parent entity for this attribute.
130 * @see #setEntity
131 */
132 public GSVEntity entity(){
133 return entity;
134 }
135
136 /**
137 * Sets the parent entity for this attribute to the provided entity.
138 *
139 * @param parentEntity The parent entity for this attribute.
140 * @see #entity
141 */
142 public void setEntity(GSVEntity parentEntity){
143 entity = parentEntity;
144 }
145
146 /******************************** WOXMLCoding Impl ********************************/
147
148 /**
149 * WOXMLCoding Impl
150 *
151 * @param coder WOXMLCoder
152 *
153 * @see #GSVAttribute
154 */
155 public void encodeWithWOXMLCoder(WOXMLCoder coder) {
156 coder.encodeObjectForKey(entity, "Entity");
157 coder.encodeObjectForKey(name, "Name");
158 coder.encodeObjectForKey(new NSArray(rules), "Rules");
159 }
160
161 /**
162 * WOXMLCoding Impl
163 *
164 * @param decoder WOXMLDecoder
165 *
166 * @see #encodeWithWOXMLCoder
167 */
168 public GSVAttribute(WOXMLDecoder decoder) {
169 entity = (GSVEntity)decoder.decodeObjectForKey("Entity");
170 name = (String)decoder.decodeObjectForKey("Name");
171 rules = new NSMutableArray((NSArray)decoder.decodeObjectForKey("Rules"));
172 }
173
174 /**
175 * WOXMLCoding Impl
176 */
177 public Class classForCoder() {
178 try{
179 return Class.forName("com.gammastream.validity.GSVAttribute");
180 }catch(ClassNotFoundException e){
181 return null;
182 }
183 }
184
185
186 }