KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > entityops > EntityData


1 /*
2  * $Id: EntityData.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 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.entityops;
25
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.ofbiz.base.location.FlexibleLocation;
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.base.util.UtilValidate;
33 import org.ofbiz.base.util.string.FlexibleStringExpander;
34 import org.ofbiz.entity.GenericDelegator;
35 import org.ofbiz.entity.util.EntityDataAssert;
36 import org.ofbiz.entity.util.EntitySaxReader;
37 import org.ofbiz.minilang.SimpleMethod;
38 import org.ofbiz.minilang.method.ContextAccessor;
39 import org.ofbiz.minilang.method.MethodContext;
40 import org.ofbiz.minilang.method.MethodOperation;
41 import org.w3c.dom.Element JavaDoc;
42
43 /**
44  * Uses the delegator to find entity values by a primary key
45  *
46  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
47  * @version $Rev: 5462 $
48  * @since 3.1
49  */

50 public class EntityData extends MethodOperation {
51     
52     public static final String JavaDoc module = EntityData.class.getName();
53     
54     protected FlexibleStringExpander locationExdr;
55     protected FlexibleStringExpander delegatorNameExdr;
56     protected FlexibleStringExpander timeoutExdr;
57     protected ContextAccessor errorListAcsr;
58     protected String JavaDoc mode;
59
60     public EntityData(Element JavaDoc element, SimpleMethod simpleMethod) {
61         super(element, simpleMethod);
62         locationExdr = new FlexibleStringExpander(element.getAttribute("location"));
63         delegatorNameExdr = new FlexibleStringExpander(element.getAttribute("delegator-name"));
64         timeoutExdr = new FlexibleStringExpander(element.getAttribute("timeout"));
65         errorListAcsr = new ContextAccessor(element.getAttribute("error-list-name"), "error_list");
66         
67         mode = element.getAttribute("mode");
68         if (UtilValidate.isEmpty(mode)) {
69             mode = "load";
70         }
71     }
72
73     public boolean exec(MethodContext methodContext) {
74         List JavaDoc messages = (List JavaDoc) errorListAcsr.get(methodContext);
75         String JavaDoc location = this.locationExdr.expandString(methodContext.getEnvMap());
76         String JavaDoc delegatorName = this.delegatorNameExdr.expandString(methodContext.getEnvMap());
77
78         GenericDelegator delegator = methodContext.getDelegator();
79         if (delegatorName != null && delegatorName.length() > 0) {
80             delegator = GenericDelegator.getGenericDelegator(delegatorName);
81         }
82
83         URL JavaDoc dataUrl = null;
84         try {
85             dataUrl = FlexibleLocation.resolveLocation(location, methodContext.getLoader());
86         } catch (MalformedURLException JavaDoc e) {
87             messages.add("Could not find Entity Data document in resource: " + location + "; error was: " + e.toString());
88         }
89         if (dataUrl == null) {
90             messages.add("Could not find Entity Data document in resource: " + location);
91         }
92         
93         String JavaDoc timeout = this.timeoutExdr.expandString(methodContext.getEnvMap());
94         int txTimeout = -1;
95         if (UtilValidate.isNotEmpty(timeout)) {
96             try {
97                 txTimeout = Integer.parseInt(timeout);
98             } catch (NumberFormatException JavaDoc e) {
99                 Debug.logWarning("Timeout not formatted properly in entity-data operation, defaulting to container default", module);
100             }
101         }
102
103         if ("assert".equals(mode)) {
104             // load the XML file, read in one element at a time and check it against the database
105
EntityDataAssert.assertData(dataUrl, delegator, messages);
106         } else {
107             // again, default to load
108
try {
109                 EntitySaxReader reader = null;
110                 if (txTimeout > 0) {
111                     reader = new EntitySaxReader(delegator, txTimeout);
112                 } else {
113                     reader = new EntitySaxReader(delegator);
114                 }
115                 long rowsChanged = reader.parse(dataUrl);
116             } catch (Exception JavaDoc e) {
117                 String JavaDoc xmlError = "Error loading XML Resource \"" + dataUrl.toExternalForm() + "\"; Error was: " + e.getMessage();
118                 messages.add(xmlError);
119                 Debug.logError(e, xmlError, module);
120             }
121         }
122         return true;
123     }
124
125     public String JavaDoc rawString() {
126         // TODO: something more than the empty tag
127
return "<entity-data/>";
128     }
129     public String JavaDoc expandedString(MethodContext methodContext) {
130         // TODO: something more than a stub/dummy
131
return this.rawString();
132     }
133 }
134
135
Popular Tags