KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > ModelServiceReader


1 /*
2  * $Id: ModelServiceReader.java 7104 2006-03-29 13:57:20Z 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  */

25 package org.ofbiz.service;
26
27 import java.io.IOException JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Set JavaDoc;
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36
37 import javolution.util.FastList;
38 import javolution.util.FastMap;
39
40 import org.ofbiz.base.config.GenericConfigException;
41 import org.ofbiz.base.config.ResourceHandler;
42 import org.ofbiz.base.util.Debug;
43 import org.ofbiz.base.util.GeneralException;
44 import org.ofbiz.base.util.UtilTimer;
45 import org.ofbiz.base.util.UtilValidate;
46 import org.ofbiz.base.util.UtilXml;
47 import org.ofbiz.base.util.cache.UtilCache;
48 import org.ofbiz.entity.GenericDelegator;
49 import org.ofbiz.entity.GenericEntityException;
50 import org.ofbiz.entity.model.ModelEntity;
51 import org.ofbiz.entity.model.ModelField;
52 import org.ofbiz.entity.model.ModelFieldType;
53
54 import org.apache.commons.collections.map.LinkedMap;
55 import org.w3c.dom.Document JavaDoc;
56 import org.w3c.dom.Element JavaDoc;
57 import org.w3c.dom.Node JavaDoc;
58 import org.w3c.dom.NodeList JavaDoc;
59 import org.xml.sax.SAXException JavaDoc;
60
61 /**
62  * Generic Service - Service Definition Reader
63  *
64  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
65  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
66  * @version $Rev: 7104 $
67  * @since 2.0
68  */

69
70 public class ModelServiceReader implements Serializable JavaDoc {
71
72     public static final String JavaDoc module = ModelServiceReader.class.getName();
73
74     protected static UtilCache readersUrl = new UtilCache("service.ModelServiceReader.ByURL", 0, 0);
75     protected static UtilCache readersLoader = new UtilCache("service.ModelServiceReader.ByResourceLoader", 0, 0);
76
77     /** is either from a URL or from a ResourceLoader (through the ResourceHandler) */
78     protected boolean isFromURL;
79     protected URL JavaDoc readerURL = null;
80     protected ResourceHandler handler = null;
81     protected Map JavaDoc modelServices = null;
82     protected DispatchContext dctx = null;
83
84     public static ModelServiceReader getModelServiceReader(URL JavaDoc readerURL, DispatchContext dctx) {
85         ModelServiceReader reader = null;
86
87         // if ( readersUrl.containsKey(readerURL) ) <-- this is unnecessary as it will return null below if not found
88
reader = (ModelServiceReader) readersUrl.get(readerURL);
89         if (reader == null) { // don't want to block here
90
synchronized (ModelServiceReader.class) {
91                 // must check if null again as one of the blocked threads can still enter
92
reader = (ModelServiceReader) readersUrl.get(readerURL);
93                 if (reader == null) {
94                     // if (Debug.infoOn()) Debug.logInfo("[Creating reader]: " + readerURL.toExternalForm(), module);
95
reader = new ModelServiceReader(readerURL, dctx);
96                     readersUrl.put(readerURL, reader);
97                 }
98             }
99         }
100         return reader;
101     }
102
103     public static ModelServiceReader getModelServiceReader(ResourceHandler handler, DispatchContext dctx) {
104         ModelServiceReader reader = null;
105
106         reader = (ModelServiceReader) readersLoader.get(handler);
107         if (reader == null) { // don't want to block here
108
synchronized (ModelServiceReader.class) {
109                 // must check if null again as one of the blocked threads can still enter
110
reader = (ModelServiceReader) readersLoader.get(handler);
111                 if (reader == null) {
112                     // if (Debug.infoOn()) Debug.logInfo("[Creating reader]: " + handler, module);
113
reader = new ModelServiceReader(handler, dctx);
114                     readersLoader.put(handler, reader);
115                 }
116             }
117         }
118         return reader;
119     }
120
121     protected ModelServiceReader(URL JavaDoc readerURL, DispatchContext dctx) {
122         this.isFromURL = true;
123         this.readerURL = readerURL;
124         this.handler = null;
125         this.dctx = dctx;
126         // preload models...
127
getModelServices();
128     }
129
130     protected ModelServiceReader(ResourceHandler handler, DispatchContext dctx) {
131         this.isFromURL = false;
132         this.readerURL = null;
133         this.handler = handler;
134         this.dctx = dctx;
135         // preload models...
136
getModelServices();
137     }
138
139     public Map JavaDoc getModelServices() {
140         if (modelServices == null) { // don't want to block here
141
synchronized (ModelServiceReader.class) {
142                 // must check if null again as one of the blocked threads can still enter
143
if (modelServices == null) { // now it's safe
144
modelServices = FastMap.newInstance();
145
146                     UtilTimer utilTimer = new UtilTimer();
147
148                     Document JavaDoc document = null;
149
150                     if (this.isFromURL) {
151                         // utilTimer.timerString("Before getDocument in file " + readerURL);
152
document = getDocument(readerURL);
153
154                         if (document == null) {
155                             modelServices = null;
156                             return null;
157                         }
158                     } else {
159                         // utilTimer.timerString("Before getDocument in " + handler);
160
try {
161                             document = handler.getDocument();
162                         } catch (GenericConfigException e) {
163                             Debug.logError(e, "Error getting XML document from resource", module);
164                             return null;
165                         }
166                     }
167
168                     if (this.isFromURL) {// utilTimer.timerString("Before getDocumentElement in file " + readerURL);
169
} else {// utilTimer.timerString("Before getDocumentElement in " + handler);
170
}
171
172                     Element JavaDoc docElement = document.getDocumentElement();
173                     if (docElement == null) {
174                         modelServices = null;
175                         return null;
176                     }
177
178                     docElement.normalize();
179
180                     int i = 0;
181                     Node JavaDoc curChild = docElement.getFirstChild();
182                     if (curChild != null) {
183                         if (this.isFromURL) {
184                             utilTimer.timerString("Before start of service loop in file " + readerURL);
185                         } else {
186                             utilTimer.timerString("Before start of service loop in " + handler);
187                         } ;
188
189                         do {
190                             if (curChild.getNodeType() == Node.ELEMENT_NODE && "service".equals(curChild.getNodeName())) {
191                                 i++;
192                                 Element JavaDoc curService = (Element JavaDoc) curChild;
193                                 String JavaDoc serviceName = UtilXml.checkEmpty(curService.getAttribute("name"));
194
195                                 // check to see if service with same name has already been read
196
if (modelServices.containsKey(serviceName)) {
197                                     Debug.logWarning("WARNING: Service " + serviceName + " is defined more than once, " +
198                                         "most recent will over-write previous definition(s)", module);
199                                 }
200
201                                 // utilTimer.timerString(" After serviceName -- " + i + " --");
202
ModelService service = createModelService(curService);
203
204                                 // utilTimer.timerString(" After createModelService -- " + i + " --");
205
if (service != null) {
206                                     modelServices.put(serviceName, service);
207                                     // utilTimer.timerString(" After modelServices.put -- " + i + " --");
208
/*
209                                     int reqIn = service.getParameterNames(ModelService.IN_PARAM, false).size();
210                                     int optIn = service.getParameterNames(ModelService.IN_PARAM, true).size() - reqIn;
211                                     int reqOut = service.getParameterNames(ModelService.OUT_PARAM, false).size();
212                                     int optOut = service.getParameterNames(ModelService.OUT_PARAM, true).size() - reqOut;
213
214                                     if (Debug.verboseOn()) {
215                                         String msg = "-- getModelService: # " + i + " Loaded service: " + serviceName +
216                                             " (IN) " + reqIn + "/" + optIn + " (OUT) " + reqOut + "/" + optOut;
217
218                                         Debug.logVerbose(msg, module);
219                                     }
220                                     */

221                                 } else {
222                                     Debug.logWarning(
223                                         "-- -- SERVICE ERROR:getModelService: Could not create service for serviceName: " +
224                                         serviceName, module);
225                                 }
226
227                             }
228                         } while ((curChild = curChild.getNextSibling()) != null);
229                     } else {
230                         Debug.logWarning("No child nodes found.", module);
231                     }
232                     if (this.isFromURL) {
233                         utilTimer.timerString("Finished file " + readerURL + " - Total Services: " + i + " FINISHED");
234                         Debug.logImportant("Loaded " + i + " Service definitions from " + readerURL, module);
235                     } else {
236                         utilTimer.timerString("Finished document in " + handler + " - Total Services: " + i + " FINISHED");
237                         if (Debug.importantOn()) {
238                             String JavaDoc resourceLocation = handler.getLocation();
239                             try {
240                                 resourceLocation = handler.getURL().toExternalForm();
241                             } catch (GenericConfigException e) {
242                                 Debug.logError(e, "Could not get resource URL", module);
243                             }
244                             Debug.logImportant("Loaded " + i + " Service definitions from " + resourceLocation, module);
245                         }
246                     }
247                 }
248             }
249         }
250         return modelServices;
251     }
252
253     /**
254      * Gets an Service object based on a definition from the specified XML Service descriptor file.
255      * @param serviceName The serviceName of the Service definition to use.
256      * @return An Service object describing the specified service of the specified descriptor file.
257      */

258     public ModelService getModelService(String JavaDoc serviceName) {
259         Map JavaDoc ec = getModelServices();
260
261         if (ec != null)
262             return (ModelService) ec.get(serviceName);
263         else
264             return null;
265     }
266
267     /**
268      * Creates a Iterator with the serviceName of each Service defined in the specified XML Service Descriptor file.
269      * @return A Iterator of serviceName Strings
270      */

271     public Iterator JavaDoc getServiceNamesIterator() {
272         Collection JavaDoc collection = getServiceNames();
273
274         if (collection != null) {
275             return collection.iterator();
276         } else {
277             return null;
278         }
279     }
280
281     /**
282      * Creates a Collection with the serviceName of each Service defined in the specified XML Service Descriptor file.
283      * @return A Collection of serviceName Strings
284      */

285     public Collection JavaDoc getServiceNames() {
286         Map JavaDoc ec = getModelServices();
287
288         return ec.keySet();
289     }
290
291     protected ModelService createModelService(Element JavaDoc serviceElement) {
292         ModelService service = new ModelService();
293
294         service.name = UtilXml.checkEmpty(serviceElement.getAttribute("name"));
295         service.engineName = UtilXml.checkEmpty(serviceElement.getAttribute("engine"));
296         service.location = UtilXml.checkEmpty(serviceElement.getAttribute("location"));
297         service.invoke = UtilXml.checkEmpty(serviceElement.getAttribute("invoke"));
298         service.defaultEntityName = UtilXml.checkEmpty(serviceElement.getAttribute("default-entity-name"));
299         
300         // these default to true; if anything but true, make false
301
service.auth = "true".equalsIgnoreCase(serviceElement.getAttribute("auth"));
302         service.export = "true".equalsIgnoreCase(serviceElement.getAttribute("export"));
303         service.debug = "true".equalsIgnoreCase(serviceElement.getAttribute("debug"));
304         
305         // this defaults to true; if anything but false, make it true
306
service.validate = !"false".equalsIgnoreCase(serviceElement.getAttribute("validate"));
307         service.useTransaction = !"false".equalsIgnoreCase(serviceElement.getAttribute("use-transaction"));
308         service.requireNewTransaction = !"false".equalsIgnoreCase(serviceElement.getAttribute("require-new-transaction"));
309
310         // set the max retry field
311
String JavaDoc maxRetryStr = UtilXml.checkEmpty(serviceElement.getAttribute("max-retry"));
312         int maxRetry = -1;
313         if (!UtilValidate.isEmpty(maxRetryStr)) {
314             try {
315                 maxRetry = Integer.parseInt(maxRetryStr);
316             } catch (NumberFormatException JavaDoc e) {
317                 Debug.logWarning(e, "Setting maxRetry to -1 (default)", module);
318                 maxRetry = -1;
319             }
320         }
321         service.maxRetry = maxRetry;
322         
323         // get the timeout and convert to int
324
String JavaDoc timeoutStr = UtilXml.checkEmpty(serviceElement.getAttribute("transaction-timout"));
325         int timeout = 0;
326         if (!UtilValidate.isEmpty(timeoutStr)) {
327             try {
328                 timeout = Integer.parseInt(timeoutStr);
329             } catch (NumberFormatException JavaDoc e) {
330                 Debug.logWarning(e, "Setting timeout to 0 (default)", module);
331                 timeout = 0;
332             }
333         }
334         service.transactionTimeout = timeout;
335                        
336         service.description = getCDATADef(serviceElement, "description");
337         service.nameSpace = getCDATADef(serviceElement, "namespace");
338               
339         service.contextInfo = FastMap.newInstance();
340         this.createPermGroups(serviceElement, service);
341         this.createImplDefs(serviceElement, service);
342         this.createAutoAttrDefs(serviceElement, service);
343         this.createAttrDefs(serviceElement, service);
344         this.createOverrideDefs(serviceElement, service);
345                
346         return service;
347     }
348
349     protected String JavaDoc getCDATADef(Element JavaDoc baseElement, String JavaDoc tagName) {
350         String JavaDoc value = "";
351         NodeList JavaDoc nl = baseElement.getElementsByTagName(tagName);
352
353         // if there are more then one decriptions we will use only the first one
354
if (nl.getLength() > 0) {
355             Node JavaDoc n = nl.item(0);
356             NodeList JavaDoc childNodes = n.getChildNodes();
357
358             if (childNodes.getLength() > 0) {
359                 Node JavaDoc cdata = childNodes.item(0);
360
361                 value = UtilXml.checkEmpty(cdata.getNodeValue());
362             }
363         }
364         return value;
365     }
366
367     protected void createPermGroups(Element JavaDoc baseElement, ModelService model) {
368         List JavaDoc permGroups = UtilXml.childElementList(baseElement, "required-permissions");
369         Iterator JavaDoc permIter = permGroups.iterator();
370
371         while (permIter.hasNext()) {
372             Element JavaDoc element = (Element JavaDoc) permIter.next();
373             ModelPermGroup group = new ModelPermGroup();
374             group.joinType = element.getAttribute("join-type");
375             createPermissions(element, group, model);
376             model.permissionGroups.add(group);
377         }
378     }
379
380     protected void createPermissions(Element JavaDoc baseElement, ModelPermGroup group, ModelService service) {
381         List JavaDoc permElements = UtilXml.childElementList(baseElement, "check-permission");
382         List JavaDoc rolePermElements = UtilXml.childElementList(baseElement, "check-role-member");
383
384         // create the simple permissions
385
Iterator JavaDoc si = permElements.iterator();
386         while (si.hasNext()) {
387             Element JavaDoc element = (Element JavaDoc) si.next();
388             ModelPermission perm = new ModelPermission();
389             perm.nameOrRole = element.getAttribute("permission");
390             perm.action = element.getAttribute("action");
391             if (perm.action != null && perm.action.length() > 0) {
392                 perm.permissionType = ModelPermission.ENTITY_PERMISSION;
393             } else {
394                 perm.permissionType = ModelPermission.PERMISSION;
395             }
396             perm.serviceModel = service;
397             group.permissions.add(perm);
398         }
399
400         // create the role member permissions
401
Iterator JavaDoc ri = rolePermElements.iterator();
402         while (ri.hasNext()) {
403             Element JavaDoc element = (Element JavaDoc) ri.next();
404             ModelPermission perm = new ModelPermission();
405             perm.permissionType = ModelPermission.ROLE_MEMBER;
406             perm.nameOrRole = element.getAttribute("role-type");
407             perm.serviceModel = service;
408             group.permissions.add(perm);
409         }
410     }
411
412     protected void createImplDefs(Element JavaDoc baseElement, ModelService service) {
413         List JavaDoc implElements = UtilXml.childElementList(baseElement, "implements");
414         Iterator JavaDoc implIter = implElements.iterator();
415                 
416         while (implIter.hasNext()) {
417             Element JavaDoc implement = (Element JavaDoc) implIter.next();
418             String JavaDoc serviceName = UtilXml.checkEmpty(implement.getAttribute("service"));
419             if (serviceName.length() > 0)
420                 service.implServices.add(serviceName);
421         }
422     }
423     
424     protected void createAutoAttrDefs(Element JavaDoc baseElement, ModelService service) {
425         List JavaDoc autoElement = UtilXml.childElementList(baseElement, "auto-attributes");
426         Iterator JavaDoc autoIter = autoElement.iterator();
427         
428         while (autoIter.hasNext()) {
429             Element JavaDoc element = (Element JavaDoc) autoIter.next();
430             createAutoAttrDef(element, service);
431         }
432     }
433     
434     protected void createAutoAttrDef(Element JavaDoc autoElement, ModelService service) {
435         // get the entity name; first from the auto-attributes then from the service def
436
String JavaDoc entityName = UtilXml.checkEmpty(autoElement.getAttribute("entity-name"));
437         if (entityName == null || entityName.length() == 0) {
438             entityName = service.defaultEntityName;
439             if (entityName == null || entityName.length() == 0) {
440                 Debug.logWarning("Auto-Attribute does not specify an entity-name; not default-entity on service definition", module);
441             }
442         }
443         
444         // get the include type 'pk|nonpk|all'
445
String JavaDoc includeType = UtilXml.checkEmpty(autoElement.getAttribute("include"));
446         boolean includePk = "pk".equals(includeType) || "all".equals(includeType);
447         boolean includeNonPk = "nonpk".equals(includeType) || "all".equals(includeType);
448         
449         // need a delegator for this
450
GenericDelegator delegator = dctx.getDelegator();
451         if (delegator == null) {
452             Debug.logWarning("Cannot use auto-attribute fields with a null delegator", module);
453         }
454         
455         if (delegator != null && entityName != null) {
456             Map JavaDoc modelParamMap = new LinkedMap();
457             try {
458                 ModelEntity entity = delegator.getModelEntity(entityName);
459                 if (entity == null) {
460                     throw new GeneralException("Could not find entity with name [" + entityName + "]");
461                 }
462                 Iterator JavaDoc fieldsIter = entity.getFieldsIterator();
463                 if (fieldsIter != null) {
464                     while (fieldsIter.hasNext()) {
465                         ModelField field = (ModelField) fieldsIter.next();
466                         if ((!field.getIsAutoCreatedInternal()) && ((field.getIsPk() && includePk) || (!field.getIsPk() && includeNonPk))) {
467                             ModelFieldType fieldType = delegator.getEntityFieldType(entity, field.getType());
468                             if (fieldType == null) {
469                                 throw new GeneralException("Null field type from delegator for entity [" + entityName + "]");
470                             }
471                             ModelParam param = new ModelParam();
472                             param.entityName = entityName;
473                             param.fieldName = field.getName();
474                             param.name = field.getName();
475                             param.type = fieldType.getJavaType();
476                             param.mode = UtilXml.checkEmpty(autoElement.getAttribute("mode"));
477                             param.optional = "true".equalsIgnoreCase(autoElement.getAttribute("optional")); // default to true
478
param.formDisplay = !"false".equalsIgnoreCase(autoElement.getAttribute("form-display")); // default to false
479
modelParamMap.put(field.getName(), param);
480                         }
481                     }
482                     
483                     // get the excludes list; and remove those from the map
484
List JavaDoc excludes = UtilXml.childElementList(autoElement, "exclude");
485                     if (excludes != null) {
486                         Iterator JavaDoc excludesIter = excludes.iterator();
487                         while (excludesIter.hasNext()) {
488                             Element JavaDoc exclude = (Element JavaDoc) excludesIter.next();
489                             modelParamMap.remove(UtilXml.checkEmpty(exclude.getAttribute("field-name")));
490                         }
491                     }
492                     
493                     // now add in all the remaining params
494
Set JavaDoc keySet = modelParamMap.keySet();
495                     Iterator JavaDoc setIter = keySet.iterator();
496                     while (setIter.hasNext()) {
497                         ModelParam thisParam = (ModelParam) modelParamMap.get(setIter.next());
498                         //Debug.logInfo("Adding Param to " + service.name + ": " + thisParam.name + " [" + thisParam.mode + "] " + thisParam.type + " (" + thisParam.optional + ")", module);
499
service.addParam(thisParam);
500                     }
501                 }
502             } catch (GenericEntityException e) {
503                 Debug.logError(e, "Problem loading auto-attributes [" + entityName + "] for " + service.name, module);
504             } catch (GeneralException e) {
505                 Debug.logError(e, "Cannot load auto-attributes : " + e.getMessage() + " for " + service.name, module);
506             }
507         }
508     }
509             
510     protected void createAttrDefs(Element JavaDoc baseElement, ModelService service) {
511         // Add in the defined attributes (override the above defaults if specified)
512
List JavaDoc paramElements = UtilXml.childElementList(baseElement, "attribute");
513         Iterator JavaDoc paramIter = paramElements.iterator();
514
515         while (paramIter.hasNext()) {
516             Element JavaDoc attribute = (Element JavaDoc) paramIter.next();
517             ModelParam param = new ModelParam();
518
519             param.name = UtilXml.checkEmpty(attribute.getAttribute("name"));
520             param.type = UtilXml.checkEmpty(attribute.getAttribute("type"));
521             param.mode = UtilXml.checkEmpty(attribute.getAttribute("mode"));
522             param.entityName = UtilXml.checkEmpty(attribute.getAttribute("entity-name"));
523             param.fieldName = UtilXml.checkEmpty(attribute.getAttribute("field-name"));
524             param.stringMapPrefix = UtilXml.checkEmpty(attribute.getAttribute("string-map-prefix"));
525             param.stringListSuffix = UtilXml.checkEmpty(attribute.getAttribute("string-list-suffix"));
526             param.formLabel = attribute.hasAttribute("form-label")?attribute.getAttribute("form-label"):null;
527             param.optional = "true".equalsIgnoreCase(attribute.getAttribute("optional")); // default to true
528
param.formDisplay = !"false".equalsIgnoreCase(attribute.getAttribute("form-display")); // default to false
529

530             // set the entity name to the default if not specified
531
if (param.entityName.length() == 0) {
532                 param.entityName = service.defaultEntityName;
533             }
534             
535             // set the field-name to the name if entity name is specified but no field-name
536
if (param.fieldName.length() == 0 && param.entityName.length() > 0) {
537                 param.fieldName = param.name;
538             }
539
540             // set the validators
541
this.addValidators(attribute, param);
542             service.addParam(param);
543         }
544
545         // Add the default optional parameters
546
ModelParam def = null;
547
548         // responseMessage
549
def = new ModelParam();
550         def.name = ModelService.RESPONSE_MESSAGE;
551         def.type = "String";
552         def.mode = "OUT";
553         def.optional = true;
554         def.internal = true;
555         service.addParam(def);
556         // errorMessage
557
def = new ModelParam();
558         def.name = ModelService.ERROR_MESSAGE;
559         def.type = "String";
560         def.mode = "OUT";
561         def.optional = true;
562         def.internal = true;
563         service.addParam(def);
564         // errorMessageList
565
def = new ModelParam();
566         def.name = ModelService.ERROR_MESSAGE_LIST;
567         def.type = "java.util.List";
568         def.mode = "OUT";
569         def.optional = true;
570         def.internal = true;
571         service.addParam(def);
572         // successMessage
573
def = new ModelParam();
574         def.name = ModelService.SUCCESS_MESSAGE;
575         def.type = "String";
576         def.mode = "OUT";
577         def.optional = true;
578         def.internal = true;
579         service.addParam(def);
580         // successMessageList
581
def = new ModelParam();
582         def.name = ModelService.SUCCESS_MESSAGE_LIST;
583         def.type = "java.util.List";
584         def.mode = "OUT";
585         def.optional = true;
586         def.internal = true;
587         service.addParam(def);
588         // userLogin
589
def = new ModelParam();
590         def.name = "userLogin";
591         def.type = "org.ofbiz.entity.GenericValue";
592         def.mode = "INOUT";
593         def.optional = true;
594         def.internal = true;
595         service.addParam(def);
596         // Locale
597
def = new ModelParam();
598         def.name = "locale";
599         def.type = "java.util.Locale";
600         def.mode = "INOUT";
601         def.optional = true;
602         def.internal = true;
603         service.addParam(def);
604     }
605     
606     protected void createOverrideDefs(Element JavaDoc baseElement, ModelService service) {
607         List JavaDoc paramElements = UtilXml.childElementList(baseElement, "override");
608         Iterator JavaDoc paramIter = paramElements.iterator();
609
610         while (paramIter.hasNext()) {
611             Element JavaDoc attribute = (Element JavaDoc) paramIter.next();
612             String JavaDoc name = UtilXml.checkEmpty(attribute.getAttribute("name"));
613             ModelParam param = service.getParam(name);
614             boolean directToParams = true;
615             if (param == null) {
616                 if (service.implServices.size() > 0 && !service.inheritedParameters) {
617                     // create a temp def to place in the ModelService
618
// this will get read when we read implemented services
619
directToParams = false;
620                     param = new ModelParam();
621                     param.name = name;
622                 } else {
623                     Debug.logWarning("No parameter found for override parameter named: " + name + " in service " + service.name, module);
624                 }
625             }
626             
627             if (param != null) {
628                 // set only modified values
629
if (attribute.getAttribute("type") != null && attribute.getAttribute("type").length() > 0) {
630                     param.type = UtilXml.checkEmpty(attribute.getAttribute("type"));
631                 }
632                 if (attribute.getAttribute("mode") != null && attribute.getAttribute("mode").length() > 0) {
633                     param.mode = UtilXml.checkEmpty(attribute.getAttribute("mode"));
634                 }
635                 if (attribute.getAttribute("entity-name") != null && attribute.getAttribute("entity-name").length() > 0) {
636                    param.entityName = UtilXml.checkEmpty(attribute.getAttribute("entity-name"));
637                 }
638                 if (attribute.getAttribute("field-name") != null && attribute.getAttribute("field-name").length() > 0) {
639                     param.fieldName = UtilXml.checkEmpty(attribute.getAttribute("field-name"));
640                 }
641                 if (attribute.getAttribute("form-label") != null && attribute.getAttribute("form-label").length() > 0) {
642                     param.formLabel = UtilXml.checkEmpty(attribute.getAttribute("form-label"));
643                 }
644                 if (attribute.getAttribute("optional") != null && attribute.getAttribute("optional").length() > 0) {
645                     param.optional = "true".equalsIgnoreCase(attribute.getAttribute("optional")); // default to true
646
param.overrideOptional = true;
647                 }
648                 if (attribute.getAttribute("form-display") != null && attribute.getAttribute("form-display").length() > 0) {
649                     param.formDisplay = !"false".equalsIgnoreCase(attribute.getAttribute("form-display")); // default to false
650
param.overrideFormDisplay = true;
651                 }
652
653                 // override validators
654
this.addValidators(attribute, param);
655
656                 if (directToParams) {
657                     service.addParam(param);
658                 } else {
659                     service.overrideParameters.add(param);
660                 }
661             }
662         }
663     }
664
665     protected void addValidators(Element JavaDoc attribute, ModelParam param) {
666         List JavaDoc validateElements = UtilXml.childElementList(attribute, "type-validate");
667         if (validateElements != null && validateElements.size() > 0) {
668             // always clear out old ones; never append
669
param.validators = FastList.newInstance();
670
671             Iterator JavaDoc i = validateElements.iterator();
672             Element JavaDoc validate = (Element JavaDoc) i.next();
673             String JavaDoc methodName = validate.getAttribute("method");
674             String JavaDoc className = validate.getAttribute("class");
675
676             Element JavaDoc fail = UtilXml.firstChildElement(validate, "fail-message");
677             if (fail != null) {
678                 String JavaDoc message = fail.getAttribute("message");
679                 param.addValidator(className, methodName, message);
680             } else {
681                 fail = UtilXml.firstChildElement(validate, "fail-property");
682                 if (fail != null) {
683                     String JavaDoc resource = fail.getAttribute("resource");
684                     String JavaDoc property = fail.getAttribute("property");
685                     param.addValidator(className, methodName, resource, property);
686                 }
687             }
688         }
689     }
690
691     protected Document JavaDoc getDocument(URL JavaDoc url) {
692         if (url == null)
693             return null;
694         Document JavaDoc document = null;
695
696         try {
697             document = UtilXml.readXmlDocument(url, true);
698         } catch (SAXException JavaDoc sxe) {
699             // Error generated during parsing)
700
Exception JavaDoc x = sxe;
701
702             if (sxe.getException() != null)
703                 x = sxe.getException();
704             x.printStackTrace();
705         } catch (ParserConfigurationException JavaDoc pce) {
706             // Parser with specified options can't be built
707
pce.printStackTrace();
708         } catch (IOException JavaDoc ioe) {
709             ioe.printStackTrace();
710         }
711
712         return document;
713     }
714 }
715
Popular Tags