001: /*
002: * $Id: GroupServiceModel.java,v 1.2 2003/09/25 21:52:58 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.service.group;
025:
026: import java.util.HashMap;
027: import java.util.List;
028: import java.util.Map;
029:
030: import org.ofbiz.service.DispatchContext;
031: import org.ofbiz.service.GenericServiceException;
032: import org.ofbiz.service.ModelService;
033: import org.ofbiz.service.ServiceDispatcher;
034: import org.ofbiz.base.util.Debug;
035: import org.w3c.dom.Element;
036:
037: /**
038: * GroupServiceModel.java
039: *
040: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
041: * @version $Revision: 1.2 $
042: * @since 2.0
043: */
044: public class GroupServiceModel {
045:
046: public static final String module = GroupServiceModel.class
047: .getName();
048:
049: private String serviceName, serviceMode;
050: private boolean resultToContext = false;
051:
052: /**
053: * Constructor using DOM element
054: * @param service DOM element for the service
055: */
056: public GroupServiceModel(Element service) {
057: this .serviceName = service.getAttribute("name");
058: this .serviceMode = service.getAttribute("mode");
059: this .resultToContext = service
060: .getAttribute("result-to-context").equalsIgnoreCase(
061: "true") ? true : false;
062: }
063:
064: /**
065: * Basic constructor
066: * @param serviceName name of the service
067: * @param serviceMode service invocation mode (sync|async)
068: */
069: public GroupServiceModel(String serviceName, String serviceMode) {
070: this .serviceName = serviceName;
071: this .serviceMode = serviceMode;
072: }
073:
074: /**
075: * Getter for the service mode
076: * @return String
077: */
078: public String getMode() {
079: return this .serviceMode;
080: }
081:
082: /**
083: * Getter for the service name
084: * @return String
085: */
086: public String getName() {
087: return this .serviceName;
088: }
089:
090: /**
091: * Returns true if the results of this service are to go back into the context
092: * @return boolean
093: */
094: public boolean resultToContext() {
095: return this .resultToContext;
096: }
097:
098: /**
099: * Invoker method to invoke this service
100: * @param dispatcher ServiceDispatcher used for this invocation
101: * @param localName Name of the LocalDispatcher used
102: * @param context Context for this service (will use only valid parameters)
103: * @return Map result Map
104: * @throws GenericServiceException
105: */
106: public Map invoke(ServiceDispatcher dispatcher, String localName,
107: Map context) throws GenericServiceException {
108: DispatchContext dctx = dispatcher.getLocalContext(localName);
109: ModelService model = dctx.getModelService(getName());
110: if (model == null)
111: throw new GenericServiceException("Group defined service ("
112: + getName() + ") is not a defined service.");
113:
114: Map this Context = model.makeValid(context,
115: ModelService.IN_PARAM);
116: Debug.logInfo("Running grouped service [" + serviceName + "]",
117: module);
118: if (getMode().equals("async")) {
119: List requiredOut = model.getParameterNames(
120: ModelService.OUT_PARAM, false);
121: if (requiredOut.size() > 0) {
122: Debug
123: .logWarning(
124: "Grouped service ("
125: + getName()
126: + ") requested 'async' invocation; running sync because of required OUT parameters.",
127: module);
128: return dispatcher
129: .runSync(localName, model, this Context);
130: } else {
131: dispatcher.runAsync(localName, model, this Context,
132: false);
133: return new HashMap();
134: }
135: } else {
136: return dispatcher.runSync(localName, model, this Context);
137: }
138: }
139:
140: /**
141: * @see java.lang.Object#toString()
142: */
143: public String toString() {
144: StringBuffer str = new StringBuffer();
145: str.append(getName());
146: str.append("::");
147: str.append(getMode());
148: str.append("::");
149: return str.toString();
150: }
151: }
|