KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > callops > AddError


1 /*
2  * $Id: AddError.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.callops;
25
26 import java.util.*;
27
28 import org.w3c.dom.*;
29 import org.ofbiz.base.util.*;
30 import org.ofbiz.minilang.*;
31 import org.ofbiz.minilang.method.*;
32
33 /**
34  * Adds the fail-message or fail-property value to the error-list.
35  *
36  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
37  * @version $Rev: 5462 $
38  * @since 2.0
39  */

40 public class AddError extends MethodOperation {
41     String JavaDoc message = null;
42     String JavaDoc propertyResource = null;
43     boolean isProperty = false;
44
45     ContextAccessor errorListAcsr;
46
47     public AddError(Element element, SimpleMethod simpleMethod) {
48         super(element, simpleMethod);
49         errorListAcsr = new ContextAccessor(element.getAttribute("error-list-name"), "error_list");
50
51         Element failMessage = UtilXml.firstChildElement(element, "fail-message");
52         Element failProperty = UtilXml.firstChildElement(element, "fail-property");
53
54         if (failMessage != null) {
55             this.message = failMessage.getAttribute("message");
56             this.isProperty = false;
57         } else if (failProperty != null) {
58             this.propertyResource = failProperty.getAttribute("resource");
59             this.message = failProperty.getAttribute("property");
60             this.isProperty = true;
61         }
62     }
63
64     public boolean exec(MethodContext methodContext) {
65         boolean hasPermission = false;
66
67         List messages = (List) errorListAcsr.get(methodContext);
68         if (messages == null) {
69             messages = new LinkedList();
70             errorListAcsr.put(methodContext, messages);
71         }
72
73         this.addMessage(messages, methodContext.getLoader(), methodContext);
74         return true;
75     }
76
77     public void addMessage(List messages, ClassLoader JavaDoc loader, MethodContext methodContext) {
78         String JavaDoc message = methodContext.expandString(this.message);
79         String JavaDoc propertyResource = methodContext.expandString(this.propertyResource);
80         
81         if (!isProperty && message != null) {
82             messages.add(message);
83             // if (Debug.infoOn()) Debug.logInfo("[SimpleMapOperation.addMessage] Adding message: " + message, module);
84
} else if (isProperty && propertyResource != null && message != null) {
85             //String propMsg = UtilProperties.getPropertyValue(UtilURL.fromResource(propertyResource, loader), message);
86
String JavaDoc propMsg = UtilProperties.getMessage(propertyResource, message, methodContext.getEnvMap(), methodContext.getLocale());
87
88             if (propMsg == null || propMsg.length() == 0) {
89                 messages.add("Simple Method error occurred, but no message was found, sorry.");
90             } else {
91                 messages.add(methodContext.expandString(propMsg));
92             }
93             // if (Debug.infoOn()) Debug.logInfo("[SimpleMapOperation.addMessage] Adding property message: " + propMsg, module);
94
} else {
95             messages.add("Simple Method error occurred, but no message was found, sorry.");
96             // if (Debug.infoOn()) Debug.logInfo("[SimpleMapOperation.addMessage] ERROR: No message found", module);
97
}
98     }
99
100     public String JavaDoc rawString() {
101         // TODO: something more than the empty tag
102
return "<add-error/>";
103     }
104     public String JavaDoc expandedString(MethodContext methodContext) {
105         // TODO: something more than a stub/dummy
106
return this.rawString();
107     }
108 }
109
Popular Tags