KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > finder > PrimaryKeyFinder


1 /*
2  * $Id: PrimaryKeyFinder.java 5720 2005-09-13 03:10:59Z 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.entity.finder;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import org.ofbiz.base.util.Debug;
33 import org.ofbiz.base.util.UtilValidate;
34 import org.ofbiz.base.util.GeneralException;
35 import org.ofbiz.base.util.collections.FlexibleMapAccessor;
36 import org.ofbiz.base.util.string.FlexibleStringExpander;
37 import org.ofbiz.entity.GenericDelegator;
38 import org.ofbiz.entity.GenericEntityException;
39 import org.ofbiz.entity.GenericPK;
40 import org.ofbiz.entity.GenericValue;
41 import org.ofbiz.entity.model.ModelEntity;
42 import org.w3c.dom.Element JavaDoc;
43
44 /**
45  * Uses the delegator to find entity values by a condition
46  *
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @version $Rev: 5720 $
49  * @since 3.1
50  */

51 public class PrimaryKeyFinder implements Serializable JavaDoc {
52     public static final String JavaDoc module = PrimaryKeyFinder.class.getName();
53     
54     protected FlexibleStringExpander entityNameExdr;
55     protected FlexibleMapAccessor valueNameAcsr;
56     protected FlexibleStringExpander useCacheExdr;
57     protected FlexibleStringExpander autoFieldMapExdr;
58     protected Map JavaDoc fieldMap;
59     protected List JavaDoc selectFieldExpanderList;
60
61     public PrimaryKeyFinder(Element JavaDoc entityOneElement) {
62         this.entityNameExdr = new FlexibleStringExpander(entityOneElement.getAttribute("entity-name"));
63         if (UtilValidate.isNotEmpty(entityOneElement.getAttribute("value-name")))
64             this.valueNameAcsr = new FlexibleMapAccessor(entityOneElement.getAttribute("value-name"));
65         this.useCacheExdr = new FlexibleStringExpander(entityOneElement.getAttribute("use-cache"));
66         this.autoFieldMapExdr = new FlexibleStringExpander(entityOneElement.getAttribute("auto-field-map"));
67
68         // process field-map
69
this.fieldMap = EntityFinderUtil.makeFieldMap(entityOneElement);
70
71         // process select-field
72
selectFieldExpanderList = EntityFinderUtil.makeSelectFieldExpanderList(entityOneElement);
73     }
74
75     public void runFind(Map JavaDoc context, GenericDelegator delegator) throws GeneralException {
76         String JavaDoc entityName = this.entityNameExdr.expandString(context);
77         ModelEntity modelEntity = delegator.getModelEntity(entityName);
78         
79         String JavaDoc useCacheString = this.useCacheExdr.expandString(context);
80         // default to false
81
boolean useCacheBool = "true".equals(useCacheString);
82
83         String JavaDoc autoFieldMapString = this.autoFieldMapExdr.expandString(context);
84         // default to true
85
boolean autoFieldMapBool = !"false".equals(autoFieldMapString);
86
87         // assemble the field map
88
Map JavaDoc entityContext = new HashMap JavaDoc();
89         if (autoFieldMapBool) {
90             GenericValue tempVal = delegator.makeValue(entityName, null);
91
92             // try a map called "parameters", try it first so values from here are overriden by values in the main context
93
Object JavaDoc parametersObj = context.get("parameters");
94             if (parametersObj != null && parametersObj instanceof Map JavaDoc) {
95                 tempVal.setAllFields((Map JavaDoc) parametersObj, true, null, Boolean.TRUE);
96             }
97
98             // just get the primary keys, and hopefully will get all of them, if not they must be manually filled in below in the field-maps
99
tempVal.setAllFields(context, true, null, Boolean.TRUE);
100
101             entityContext.putAll(tempVal);
102         }
103         EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, entityContext);
104         //Debug.logInfo("PrimaryKeyFinder: entityContext=" + entityContext, module);
105
// then convert the types...
106
modelEntity.convertFieldMapInPlace(entityContext, delegator);
107         
108         // get the list of fieldsToSelect from selectFieldExpanderList
109
Set JavaDoc fieldsToSelect = EntityFinderUtil.makeFieldsToSelect(selectFieldExpanderList, context);
110         
111         //if fieldsToSelect != null and useCacheBool is true, throw an error
112
if (fieldsToSelect != null && useCacheBool) {
113             throw new IllegalArgumentException JavaDoc("Error in entity-one definition, cannot specify select-field elements when use-cache is set to true");
114         }
115         
116         try {
117             GenericValue valueOut = null;
118             GenericPK entityPK = delegator.makePK(entityName, entityContext);
119
120             // make sure we have a full primary key, if any field is null then just log a warning and return null instead of blowing up
121
if (entityPK.containsPrimaryKey(true)) {
122                 if (useCacheBool) {
123                     valueOut = delegator.findByPrimaryKeyCache(entityPK);
124                 } else {
125                     if (fieldsToSelect != null) {
126                         valueOut = delegator.findByPrimaryKeyPartial(entityPK, fieldsToSelect);
127                     } else {
128                         valueOut = delegator.findByPrimaryKey(entityPK);
129                     }
130                 }
131             } else {
132                 if (Debug.infoOn()) Debug.logInfo("Returning null because found incomplete primary key in find: " + entityPK, module);
133             }
134             
135             //Debug.logInfo("PrimaryKeyFinder: valueOut=" + valueOut, module);
136
//Debug.logInfo("PrimaryKeyFinder: going into=" + this.valueNameAcsr.getOriginalName(), module);
137
if (valueNameAcsr != null) {
138                this.valueNameAcsr.put(context, valueOut);
139             } else {
140                if (valueOut != null) {
141                    context.putAll(valueOut);
142                }
143             }
144         } catch (GenericEntityException e) {
145             String JavaDoc errMsg = "Error finding entity value by primary key with entity-one: " + e.toString();
146             Debug.logError(e, errMsg, module);
147             throw new IllegalArgumentException JavaDoc(errMsg);
148         }
149     }
150 }
151
152
Popular Tags