001: /*
002: * $Id: CommonServices.java,v 1.8 2003/12/12 16:16: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: */
025: package org.ofbiz.common;
026:
027: import java.sql.Timestamp;
028: import java.util.*;
029:
030: import javax.transaction.xa.XAException;
031:
032: import org.ofbiz.base.util.Debug;
033: import org.ofbiz.base.util.UtilDateTime;
034: import org.ofbiz.base.util.UtilMisc;
035: import org.ofbiz.entity.GenericDelegator;
036: import org.ofbiz.entity.GenericEntityException;
037: import org.ofbiz.entity.GenericValue;
038: import org.ofbiz.entity.model.ModelEntity;
039: import org.ofbiz.service.DispatchContext;
040: import org.ofbiz.service.ModelService;
041: import org.ofbiz.service.ServiceUtil;
042: import org.ofbiz.service.ServiceXaWrapper;
043:
044: /**
045: * Common Services
046: *
047: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
048: * @version $Revision: 1.8 $
049: * @since 2.0
050: */
051: public class CommonServices {
052:
053: public final static String module = CommonServices.class.getName();
054:
055: /**
056: * Generic Test Service
057: *@param dctx The DispatchContext that this service is operating in
058: *@param context Map containing the input parameters
059: *@return Map with the result of the service, the output parameters
060: */
061: public static Map testService(DispatchContext dctx, Map context) {
062: Map response = new HashMap();
063:
064: if (context.size() > 0) {
065: Iterator i = context.keySet().iterator();
066:
067: while (i.hasNext()) {
068: Object cKey = i.next();
069: Object value = context.get(cKey);
070:
071: System.out.println("---- SVC-CONTEXT: " + cKey + " => "
072: + value);
073: }
074: }
075: if (!context.containsKey("message")) {
076: response.put("resp", "no message found");
077: } else {
078: System.out.println("-----SERVICE TEST----- : "
079: + (String) context.get("message"));
080: response.put("resp", "service done");
081: }
082:
083: System.out.println("----- SVC: " + dctx.getName() + " -----");
084: return response;
085: }
086:
087: public static Map testWorkflowCondition(DispatchContext dctx,
088: Map context) {
089: Map result = new HashMap();
090: result.put("evaluationResult", new Boolean(true));
091: return result;
092: }
093:
094: public static Map testRollbackListener(DispatchContext dctx,
095: Map context) {
096: ServiceXaWrapper xar = new ServiceXaWrapper(dctx);
097: xar.setRollbackService("testScv", context);
098: try {
099: xar.enlist();
100: } catch (XAException e) {
101: Debug.logError(e, module);
102: }
103: return ServiceUtil.returnError("Rolling back!");
104: }
105:
106: public static Map testCommitListener(DispatchContext dctx,
107: Map context) {
108: ServiceXaWrapper xar = new ServiceXaWrapper(dctx);
109: xar.setCommitService("testScv", context);
110: try {
111: xar.enlist();
112: } catch (XAException e) {
113: Debug.logError(e, module);
114: }
115: return ServiceUtil.returnSuccess();
116: }
117:
118: /**
119: * Create Note Record
120: *@param ctx The DispatchContext that this service is operating in
121: *@param context Map containing the input parameters
122: *@return Map with the result of the service, the output parameters
123: */
124: public static Map createNote(DispatchContext ctx, Map context) {
125: GenericDelegator delegator = ctx.getDelegator();
126: GenericValue userLogin = (GenericValue) context
127: .get("userLogin");
128: Timestamp now = UtilDateTime.nowTimestamp();
129: String partyId = (String) context.get("partyId");
130: String noteName = (String) context.get("noteName");
131: String note = (String) context.get("note");
132: String noteId = null;
133:
134: // create the note id
135: Long newId = delegator.getNextSeqId("NoteData");
136:
137: if (newId == null) {
138: return ServiceUtil
139: .returnError("ERROR: Could not create note data (id generation failure)");
140: } else {
141: noteId = newId.toString();
142: }
143:
144: // check for a party id
145: if (partyId == null) {
146: if (userLogin != null && userLogin.get("partyId") != null)
147: partyId = userLogin.getString("partyId");
148: }
149:
150: Map fields = UtilMisc.toMap("noteId", noteId, "noteName",
151: noteName, "noteInfo", note, "noteParty", partyId,
152: "noteDateTime", now);
153:
154: try {
155: GenericValue newValue = delegator.makeValue("NoteData",
156: fields);
157:
158: delegator.create(newValue);
159: } catch (GenericEntityException e) {
160: return ServiceUtil
161: .returnError("Could update note data (write failure): "
162: + e.getMessage());
163: }
164: Map result = ServiceUtil.returnSuccess();
165:
166: result.put("noteId", noteId);
167: return result;
168: }
169:
170: /**
171: * Service for setting debugging levels.
172: *@param dctx The DispatchContext that this service is operating in
173: *@param context Map containing the input parameters
174: *@return Map with the result of the service, the output parameters
175: */
176: public static Map setDebugLevels(DispatchContext dctx, Map context) {
177: Boolean verbose = (Boolean) context.get("verbose");
178: Boolean timing = (Boolean) context.get("timing");
179: Boolean info = (Boolean) context.get("info");
180: Boolean important = (Boolean) context.get("important");
181: Boolean warning = (Boolean) context.get("warning");
182: Boolean error = (Boolean) context.get("error");
183: Boolean fatal = (Boolean) context.get("fatal");
184:
185: if (verbose != null)
186: Debug.set(Debug.VERBOSE, verbose.booleanValue());
187: else
188: Debug.set(Debug.VERBOSE, false);
189: if (timing != null)
190: Debug.set(Debug.TIMING, timing.booleanValue());
191: else
192: Debug.set(Debug.TIMING, false);
193: if (info != null)
194: Debug.set(Debug.INFO, info.booleanValue());
195: else
196: Debug.set(Debug.INFO, false);
197: if (important != null)
198: Debug.set(Debug.IMPORTANT, important.booleanValue());
199: else
200: Debug.set(Debug.IMPORTANT, false);
201: if (warning != null)
202: Debug.set(Debug.WARNING, warning.booleanValue());
203: else
204: Debug.set(Debug.WARNING, false);
205: if (error != null)
206: Debug.set(Debug.ERROR, error.booleanValue());
207: else
208: Debug.set(Debug.ERROR, false);
209: if (fatal != null)
210: Debug.set(Debug.FATAL, fatal.booleanValue());
211: else
212: Debug.set(Debug.FATAL, false);
213:
214: return ServiceUtil.returnSuccess();
215: }
216:
217: /**
218: * Echo service; returns exactly what was sent.
219: * This service does not have required parameters and does not validate
220: */
221: public static Map echoService(DispatchContext dctx, Map context) {
222: context.put(ModelService.RESPONSE_MESSAGE,
223: ModelService.RESPOND_SUCCESS);
224: return context;
225: }
226:
227: /**
228: * Return Error Service; Used for testing error handling
229: */
230: public static Map returnErrorService(DispatchContext dctx,
231: Map context) {
232: return ServiceUtil
233: .returnError("Return Error Service : Returning Error");
234: }
235:
236: /** Cause a Referential Integrity Error */
237: public static Map entityFailTest(DispatchContext dctx, Map context) {
238: GenericDelegator delegator = dctx.getDelegator();
239:
240: // attempt to create a DataSource entity w/ an invalid dataSourceTypeId
241: GenericValue newEntity = delegator
242: .makeValue("DataSource", null);
243: newEntity.set("dataSourceId", "ENTITY_FAIL_TEST");
244: newEntity.set("dataSourceTypeId", "ENTITY_FAIL_TEST");
245: newEntity.set("description",
246: "Entity Fail Test - Delete me if I am here");
247: try {
248: delegator.create(newEntity);
249: } catch (GenericEntityException e) {
250: Debug.logError(e, module);
251: return ServiceUtil
252: .returnError("Unable to create test entity");
253: }
254:
255: /*
256: try {
257: newEntity.remove();
258: } catch(GenericEntityException e) {
259: Debug.logError(e, module);
260: }
261: */
262:
263: return ServiceUtil.returnSuccess();
264: }
265:
266: /** Test entity sorting */
267: public static Map entitySortTest(DispatchContext dctx, Map context) {
268: GenericDelegator delegator = dctx.getDelegator();
269: Set set = new TreeSet();
270:
271: set.add(delegator.getModelEntity("Person"));
272: set.add(delegator.getModelEntity("PartyRole"));
273: set.add(delegator.getModelEntity("Party"));
274: set.add(delegator.getModelEntity("ContactMech"));
275: set.add(delegator.getModelEntity("PartyContactMech"));
276: set.add(delegator.getModelEntity("OrderHeader"));
277: set.add(delegator.getModelEntity("OrderItem"));
278: set.add(delegator.getModelEntity("OrderContactMech"));
279: set.add(delegator.getModelEntity("OrderRole"));
280: set.add(delegator.getModelEntity("Product"));
281: set.add(delegator.getModelEntity("RoleType"));
282:
283: Iterator i = set.iterator();
284: while (i.hasNext()) {
285: Debug.log(((ModelEntity) i.next()).getEntityName(), module);
286: }
287: return ServiceUtil.returnSuccess();
288: }
289: }
|