Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/gammastream/validity/GSVRule.java


1   package com.gammastream.validity;
2   
3   import com.webobjects.appserver.xml.*;
4   import com.webobjects.foundation.*;
5   
6   import java.lang.*;
7   import java.net.*;
8   import java.math.*;
9   
10  /**
11   *  This structure stores the various information which defines each rule.
12   *  Information like the name, where the rule is located, error messages, 
13   *  when to execute, etc.
14   *
15   *  @author GammaStream Technologies, Inc.
16   */
17  
18  public final class GSVRule extends Object implements WOXMLCoding {
19  
20      //definitions
21      private String ruleName = null;
22      private String cName = null;
23      private String mName = null;
24      private String errorMessage = null;
25      private String documentation = null;
26  
27      //execution mutators
28      private boolean negate = false;
29      private boolean failIfNULL = true;
30      private boolean stopIfFails = false;
31      private boolean continueIfNULL = false;
32      
33      //when to execute
34      private boolean onSave = true;
35      private boolean onInsert = true;
36      private boolean onDelete = true;
37      private boolean onUpdate = true;
38      
39      //key-value list of parameters
40      private NSMutableDictionary parameters = null;
41      
42     /**
43       *   Creates a new GSVRule with the provided parameters.
44       *
45       * @param    rName    The name you wish to give the rule.
46       * @param    cName2    The fully qualified class name in which the rule's method is located.
47       * @param    mName2    The name of the method to be executed.
48       * @param    eMessage  An error message to provide the user in the event the rule fails.
49       * @param    doc       Optional documentation as to the rules function.
50       * @exception java.lang.IllegalArgumentException  Thrown should the class name or method name appear to be invalid.
51       */
52      public GSVRule(String rName, String cName2, String mName2, String eMessage, String doc) throws IllegalArgumentException {
53          
54          if(!this.validateClassName(cName2)){
55              throw new IllegalArgumentException("Invalid class named '"+ cName2 +"'");
56          }
57          
58          if(!this.validateMethodName(mName2)){
59              throw new IllegalArgumentException("Invalid method named '"+ mName2 +"'");
60          }
61          ruleName = rName;
62          cName = cName2;
63          mName = mName2;
64          errorMessage = eMessage;
65          documentation = doc;
66          parameters = new NSMutableDictionary();
67      }
68  
69      /**
70       *  Private
71       *  Determines whether the provided class name appears to be valid.
72       */
73      private boolean validateClassName(String cName){
74          return true;
75      }
76      
77      /**
78       *  Private
79       *  Determines whether the provided method name appears to be valid.
80       */
81      private boolean validateMethodName(String mName){
82          return true;
83      }
84  
85      /**
86       *  Returns the name of this rule.
87       *
88       *  @return    The name of the rule.
89       *  @see #setRuleName
90       */
91      public String ruleName(){
92          return ruleName;
93      }
94  
95      /**
96       *  Sets the name of this rule.
97       *
98       *  @see #ruleName
99       */
100     public void setRuleName(String newRule) throws IllegalArgumentException {
101         ruleName = newRule;
102     }
103 
104     /**
105      *  Returns the fully qualified class name of the class in which the method used 
106      *  in this rule is located. :-)
107      *
108      *  @return    The name of the rule.
109      *  @see #setCName
110      */
111     public String cName(){
112         return cName;
113     }
114 
115     /**
116      *  Set the class name for this rule.
117      *
118      *  @param    The fully qualified name of the class.
119      *   @exception java.lang.IllegalArgumentException  Thrown should the class name appear to be invalid.
120      *                          Currently the validation logic here always returns true.
121      *  @see #cName
122      */
123     public void setCName(String newClass)  throws IllegalArgumentException {
124         if(!this.validateClassName(newClass)){
125             throw new IllegalArgumentException("Invalid class named '"+ newClass +"'");
126         } else {
127             cName = newClass;
128         }
129     }
130     
131     /**
132      *  Returns the method name used for this rule.
133      *
134      *  @return    The method name.
135      *  @see #setMName
136      */
137     public String mName(){
138         return mName;
139     }
140 
141     /**
142      *  Set the method name for this rule.
143      *
144      *  @param    The name for the method.
145      *   @exception java.lang.IllegalArgumentException  Thrown should the method name appear to be invalid.
146      *                          Currently the validation logic here always returns true.
147      *  @see #mName
148      */
149     public void setMName(String newMethod)  throws IllegalArgumentException {
150         if(!this.validateMethodName(newMethod)){
151             throw new IllegalArgumentException("Invalid method named '"+ newMethod +"'");
152         } else {
153             mName = newMethod;
154         }
155     }
156 
157     /**
158      *  Returns the error message that should be displayed to the user
159      *  when this rule fails to be validated.
160      *
161      *  @return    The error message.
162      *  @see #setErrorMessage
163      */
164     public String errorMessage(){
165         return errorMessage;
166     }
167     
168     /**
169      *  Set the error message.
170      *
171      *  @param    The error message
172      *
173      *  @see #errorMessage
174      */
175     public void setErrorMessage(String newMessage){
176         errorMessage = newMessage;
177     }
178     
179     /**
180      *  Returns the documentation for this rule.
181      *
182      *  @return    The documentation.
183      *  @see #setDocumentation
184      */
185     public String documentation(){
186         return documentation;
187     }
188     
189     /**
190      *  Set the documentation to the provided <code>String</code>.
191      *
192      *  @param    The documentation.
193      *
194      *  @see #documentation
195      */
196     public void setDocumentation(String doc){
197         documentation = doc;
198     }
199 
200     /**
201      *  Returns a dictionary of key-value pairs used for providing parameters
202      *  to the validation rule's method. This parameter dictionary is
203      *  passed to the method defined by <code>mName()</code>.
204      *
205      *  @return    The key-values pairs.
206      *  @see #setParameters
207      */
208     public NSMutableDictionary parameters(){
209         return parameters;
210     }
211 
212     /**
213      *  Assigns a key-value pair dictionary to this rule.
214      *
215      *  @param    A dictionary of key-value pairs.
216      *
217      *  @see #parameters
218      */
219     public void setParameters(NSMutableDictionary newParameters){
220         parameters = newParameters;
221     }
222     
223     /**
224      *  Should the outcome of this rule be negated (reversed).
225      *  This might be useful if you have a method which returns <code>true</code> if you have a <code>String</code>
226      *  which is empty or <code>null</code>. Your rule might indicate that you want this attribute to be
227      *  required. If the method returns <code>true</code> if it is, you will want to reverse the outcome. Did that 
228      *  make any sense?
229      *
230      *  @return  Whether the initial return value should be negated (reversed).
231      *  @see #setNegate
232      */
233     public boolean negate(){
234         return negate;
235     }
236 
237     /**
238      *  Assigns a key-value pair dictionary to this rule.
239      *
240      *  @param    A dictionary of key-value pairs.
241      *
242      *  @see #parameters
243      */
244     public void setNegate(boolean z){
245         negate = z;
246     }
247     
248     public boolean failIfNULL(){
249         return failIfNULL;
250     }
251 
252     public void setFailIfNULL(boolean z){
253         failIfNULL = z;
254     }
255     
256     public boolean continueIfNULL(){
257         return continueIfNULL;
258     }
259 
260     public void setContinueIfNULL(boolean z){
261         continueIfNULL = z;
262     }
263     
264     public boolean stopIfFails(){
265         return stopIfFails;
266     }
267 
268     public void setStopIfFails(boolean z){
269         stopIfFails = z;
270     }
271 
272     public boolean onSave(){
273         return onSave;
274     }
275 
276     public void setOnSave(boolean z){
277         onSave = z;
278     }
279 
280     public boolean onUpdate(){
281         return onUpdate;
282     }
283 
284     public void setOnUpdate(boolean z){
285         onUpdate = z;
286     }
287     
288     
289     public boolean onInsert(){
290         return onInsert;
291     }
292 
293     public void setOnInsert(boolean z){
294         onInsert = z;
295     }
296     
297     public boolean onDelete(){
298         return onDelete;
299     }
300 
301     public void setOnDelete(boolean z){
302         onDelete = z;
303     }
304 
305     /********************************  WOXMLCoding Impl  ********************************/
306     
307     /**
308      *  WOXMLCoding Impl
309      *  
310      *  @param  coder  WOXMLCoder
311      *
312      *  @see #GSVRule
313      */
314     public void encodeWithWOXMLCoder(WOXMLCoder coder) {
315         coder.encodeObjectForKey(ruleName, "RuleName");
316         coder.encodeObjectForKey(cName, "ClassName");
317         coder.encodeObjectForKey(mName, "MethodName");
318         coder.encodeObjectForKey(errorMessage, "ErrorMessage");
319         coder.encodeObjectForKey(documentation, "Documentation");
320         coder.encodeObjectForKey(new NSDictionary(parameters), "Parameters");
321         coder.encodeBooleanForKey(negate, "Negate");
322         coder.encodeBooleanForKey(failIfNULL, "FailIfNULL");
323         coder.encodeBooleanForKey(continueIfNULL, "ContinueIfNULL");
324         coder.encodeBooleanForKey(stopIfFails, "StopIfFails");
325         coder.encodeBooleanForKey(onSave, "OnSave");
326         coder.encodeBooleanForKey(onInsert, "OnInsert");
327         coder.encodeBooleanForKey(onUpdate, "OnUpdate");
328         coder.encodeBooleanForKey(onDelete, "OnDelete");
329    }
330     
331     /**
332      *  WOXMLCoding Impl
333      *
334      *  @param  decoder  WOXMLDecoder
335      *
336      *  @see #encodeWithWOXMLCoder
337      */
338     public GSVRule(WOXMLDecoder decoder) {
339         ruleName = (String)decoder.decodeObjectForKey("RuleName");
340         cName = (String)decoder.decodeObjectForKey("ClassName");
341         mName = (String)decoder.decodeObjectForKey("MethodName");
342         errorMessage = (String)decoder.decodeObjectForKey("ErrorMessage");
343         documentation = (String)decoder.decodeObjectForKey("Documentation");
344         parameters = new NSMutableDictionary((NSDictionary)decoder.decodeObjectForKey("Parameters"));
345         negate = decoder.decodeBooleanForKey("Negate");
346         failIfNULL = decoder.decodeBooleanForKey("FailIfNULL");
347         continueIfNULL = decoder.decodeBooleanForKey("ContinueIfNULL");
348         stopIfFails = decoder.decodeBooleanForKey("StopIfFails");
349         onSave = decoder.decodeBooleanForKey("OnSave");
350         onInsert = decoder.decodeBooleanForKey("OnInsert");
351         onUpdate = decoder.decodeBooleanForKey("OnUpdate");
352         onDelete = decoder.decodeBooleanForKey("OnDelete");
353     }
354     
355     /**
356    *  WOXMLCoding Impl
357    */
358     public Class classForCoder() {
359         try {
360             return Class.forName("com.gammastream.validity.GSVRule");
361         } catch(ClassNotFoundException e) {
362             return null;
363         }
364     }
365 }