KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: FindByPrimaryKey.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.entityops;
25
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.base.util.UtilValidate;
32 import org.ofbiz.entity.GenericDelegator;
33 import org.ofbiz.entity.GenericEntity;
34 import org.ofbiz.entity.GenericEntityException;
35 import org.ofbiz.minilang.SimpleMethod;
36 import org.ofbiz.minilang.method.ContextAccessor;
37 import org.ofbiz.minilang.method.MethodContext;
38 import org.ofbiz.minilang.method.MethodOperation;
39 import org.w3c.dom.Element JavaDoc;
40
41 /**
42  * Uses the delegator to find an entity value by its primary key
43  *
44  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
45  * @version $Rev: 5462 $
46  * @since 2.0
47  */

48 public class FindByPrimaryKey extends MethodOperation {
49     
50     public static final String JavaDoc module = FindByPrimaryKey.class.getName();
51     
52     ContextAccessor valueAcsr;
53     String JavaDoc entityName;
54     ContextAccessor mapAcsr;
55     String JavaDoc delegatorName;
56     String JavaDoc useCacheStr;
57     ContextAccessor fieldsToSelectListAcsr;
58
59     public FindByPrimaryKey(Element JavaDoc element, SimpleMethod simpleMethod) {
60         super(element, simpleMethod);
61         valueAcsr = new ContextAccessor(element.getAttribute("value-name"));
62         entityName = element.getAttribute("entity-name");
63         mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
64         fieldsToSelectListAcsr = new ContextAccessor(element.getAttribute("fields-to-select-list"));
65         delegatorName = element.getAttribute("delegator-name");
66         useCacheStr = element.getAttribute("use-cache");
67     }
68
69     public boolean exec(MethodContext methodContext) {
70         String JavaDoc entityName = methodContext.expandString(this.entityName);
71         String JavaDoc delegatorName = methodContext.expandString(this.delegatorName);
72         String JavaDoc useCacheStr = methodContext.expandString(this.useCacheStr);
73         
74         boolean useCache = "true".equals(useCacheStr);
75
76         GenericDelegator delegator = methodContext.getDelegator();
77         if (delegatorName != null && delegatorName.length() > 0) {
78             delegator = GenericDelegator.getGenericDelegator(delegatorName);
79         }
80
81         Map JavaDoc inMap = (Map JavaDoc) mapAcsr.get(methodContext);
82         if (UtilValidate.isEmpty(entityName) && inMap instanceof GenericEntity) {
83             GenericEntity inEntity = (GenericEntity) inMap;
84             entityName = inEntity.getEntityName();
85         }
86         
87         List JavaDoc fieldsToSelectList = null;
88         if (!fieldsToSelectListAcsr.isEmpty()) {
89             fieldsToSelectList = (List JavaDoc) fieldsToSelectListAcsr.get(methodContext);
90         }
91         
92         try {
93             if (fieldsToSelectList != null) {
94                 valueAcsr.put(methodContext, delegator.findByPrimaryKeyPartial(delegator.makePK("Product", inMap), new HashSet JavaDoc(fieldsToSelectList)));
95             } else {
96                 if (useCache) {
97                     valueAcsr.put(methodContext, delegator.findByPrimaryKeyCache(entityName, inMap));
98                 } else {
99                     valueAcsr.put(methodContext, delegator.findByPrimaryKey(entityName, inMap));
100                 }
101             }
102         } catch (GenericEntityException e) {
103             Debug.logError(e, module);
104             String JavaDoc errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem finding the " + entityName + " entity: " + e.getMessage() + "]";
105             methodContext.setErrorReturn(errMsg, simpleMethod);
106             return false;
107         }
108         return true;
109     }
110
111     public String JavaDoc rawString() {
112         // TODO: something more than the empty tag
113
return "<find-by-primary-key/>";
114     }
115     public String JavaDoc expandedString(MethodContext methodContext) {
116         // TODO: something more than a stub/dummy
117
return this.rawString();
118     }
119 }
120
Popular Tags