1 25 package org.ofbiz.common; 26 27 import java.io.FileNotFoundException ; 28 import java.io.IOException ; 29 import java.io.RandomAccessFile ; 30 import java.sql.Timestamp ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Locale ; 34 import java.util.Map ; 35 import java.util.Set ; 36 import java.util.TreeSet ; 37 38 import javax.transaction.xa.XAException ; 39 import javax.mail.internet.MimeMessage ; 40 import javax.mail.Address ; 41 42 import org.ofbiz.base.util.Debug; 43 import org.ofbiz.base.util.UtilDateTime; 44 import org.ofbiz.base.util.UtilMisc; 45 import org.ofbiz.entity.GenericDelegator; 46 import org.ofbiz.entity.GenericEntityException; 47 import org.ofbiz.entity.GenericValue; 48 import org.ofbiz.entity.model.ModelEntity; 49 import org.ofbiz.entity.transaction.TransactionUtil; 50 import org.ofbiz.entity.util.ByteWrapper; 51 import org.ofbiz.service.DispatchContext; 52 import org.ofbiz.service.GenericServiceException; 53 import org.ofbiz.service.LocalDispatcher; 54 import org.ofbiz.service.ModelService; 55 import org.ofbiz.service.ServiceUtil; 56 import org.ofbiz.service.ServiceXaWrapper; 57 import org.ofbiz.service.mail.MimeMessageWrapper; 58 59 66 public class CommonServices { 67 68 public final static String module = CommonServices.class.getName(); 69 70 76 public static Map testService(DispatchContext dctx, Map context) { 77 Map response = ServiceUtil.returnSuccess(); 78 79 if (context.size() > 0) { 80 Iterator i = context.keySet().iterator(); 81 82 while (i.hasNext()) { 83 Object cKey = i.next(); 84 Object value = context.get(cKey); 85 86 System.out.println("---- SVC-CONTEXT: " + cKey + " => " + value); 87 } 88 } 89 if (!context.containsKey("message")) { 90 response.put("resp", "no message found"); 91 } else { 92 System.out.println("-----SERVICE TEST----- : " + (String ) context.get("message")); 93 response.put("resp", "service done"); 94 } 95 96 System.out.println("----- SVC: " + dctx.getName() + " -----"); 97 return response; 98 } 99 100 public static Map blockingTestService(DispatchContext dctx, Map context) { 101 System.out.println("-----SERVICE BLOCKING----- : 30 seconds"); 102 try { 103 Thread.sleep(30000); 104 } catch (InterruptedException e) { 105 } 106 return CommonServices.testService(dctx, context); 107 } 108 109 public static Map testWorkflowCondition(DispatchContext dctx, Map context) { 110 Map result = new HashMap (); 111 result.put("evaluationResult", new Boolean (true)); 112 return result; 113 } 114 115 public static Map testRollbackListener(DispatchContext dctx, Map context) { 116 ServiceXaWrapper xar = new ServiceXaWrapper(dctx); 117 xar.setRollbackService("testScv", context); 118 try { 119 xar.enlist(); 120 } catch (XAException e) { 121 Debug.logError(e, module); 122 } 123 return ServiceUtil.returnError("Rolling back!"); 124 } 125 126 public static Map testCommitListener(DispatchContext dctx, Map context) { 127 ServiceXaWrapper xar = new ServiceXaWrapper(dctx); 128 xar.setCommitService("testScv", context); 129 try { 130 xar.enlist(); 131 } catch (XAException e) { 132 Debug.logError(e, module); 133 } 134 return ServiceUtil.returnSuccess(); 135 } 136 137 143 public static Map createNote(DispatchContext ctx, Map context) { 144 GenericDelegator delegator = ctx.getDelegator(); 145 GenericValue userLogin = (GenericValue) context.get("userLogin"); 146 Timestamp noteDate = (Timestamp ) context.get("noteDate"); 147 String partyId = (String ) context.get("partyId"); 148 String noteName = (String ) context.get("noteName"); 149 String note = (String ) context.get("note"); 150 String noteId = delegator.getNextSeqId("NoteData"); 151 if (noteDate == null) { 152 noteDate = UtilDateTime.nowTimestamp(); 153 } 154 155 156 if (partyId == null) { 158 if (userLogin != null && userLogin.get("partyId") != null) 159 partyId = userLogin.getString("partyId"); 160 } 161 162 Map fields = UtilMisc.toMap("noteId", noteId, "noteName", noteName, "noteInfo", note, 163 "noteParty", partyId, "noteDateTime", noteDate); 164 165 try { 166 GenericValue newValue = delegator.makeValue("NoteData", fields); 167 168 delegator.create(newValue); 169 } catch (GenericEntityException e) { 170 return ServiceUtil.returnError("Could update note data (write failure): " + e.getMessage()); 171 } 172 Map result = ServiceUtil.returnSuccess(); 173 174 result.put("noteId", noteId); 175 return result; 176 } 177 178 184 public static Map setDebugLevels(DispatchContext dctx, Map context) { 185 Boolean verbose = (Boolean ) context.get("verbose"); 186 Boolean timing = (Boolean ) context.get("timing"); 187 Boolean info = (Boolean ) context.get("info"); 188 Boolean important = (Boolean ) context.get("important"); 189 Boolean warning = (Boolean ) context.get("warning"); 190 Boolean error = (Boolean ) context.get("error"); 191 Boolean fatal = (Boolean ) context.get("fatal"); 192 193 if (verbose != null) 194 Debug.set(Debug.VERBOSE, verbose.booleanValue()); 195 else 196 Debug.set(Debug.VERBOSE, false); 197 if (timing != null) 198 Debug.set(Debug.TIMING, timing.booleanValue()); 199 else 200 Debug.set(Debug.TIMING, false); 201 if (info != null) 202 Debug.set(Debug.INFO, info.booleanValue()); 203 else 204 Debug.set(Debug.INFO, false); 205 if (important != null) 206 Debug.set(Debug.IMPORTANT, important.booleanValue()); 207 else 208 Debug.set(Debug.IMPORTANT, false); 209 if (warning != null) 210 Debug.set(Debug.WARNING, warning.booleanValue()); 211 else 212 Debug.set(Debug.WARNING, false); 213 if (error != null) 214 Debug.set(Debug.ERROR, error.booleanValue()); 215 else 216 Debug.set(Debug.ERROR, false); 217 if (fatal != null) 218 Debug.set(Debug.FATAL, fatal.booleanValue()); 219 else 220 Debug.set(Debug.FATAL, false); 221 222 return ServiceUtil.returnSuccess(); 223 } 224 225 public static Map forceGc(DispatchContext dctx, Map context) { 226 System.gc(); 227 return ServiceUtil.returnSuccess(); 228 } 229 230 234 public static Map echoService(DispatchContext dctx, Map context) { 235 context.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_SUCCESS); 236 return context; 237 } 238 239 242 public static Map returnErrorService(DispatchContext dctx, Map context) { 243 return ServiceUtil.returnError("Return Error Service : Returning Error"); 244 } 245 246 249 public static Map conditionTrueService(DispatchContext dctx, Map context) { 250 Map result = ServiceUtil.returnSuccess(); 251 result.put("conditionReply", Boolean.TRUE); 252 return result; 253 } 254 255 258 public static Map conditionFalseService(DispatchContext dctx, Map context) { 259 Map result = ServiceUtil.returnSuccess(); 260 result.put("conditionReply", Boolean.FALSE); 261 return result; 262 } 263 264 265 public static Map entityFailTest(DispatchContext dctx, Map context) { 266 GenericDelegator delegator = dctx.getDelegator(); 267 268 GenericValue newEntity = delegator.makeValue("DataSource", null); 270 newEntity.set("dataSourceId", "ENTITY_FAIL_TEST"); 271 newEntity.set("dataSourceTypeId", "ENTITY_FAIL_TEST"); 272 newEntity.set("description", "Entity Fail Test - Delete me if I am here"); 273 try { 274 delegator.create(newEntity); 275 } catch (GenericEntityException e) { 276 Debug.logError(e, module); 277 return ServiceUtil.returnError("Unable to create test entity"); 278 } 279 280 287 288 return ServiceUtil.returnSuccess(); 289 } 290 291 292 public static Map entitySortTest(DispatchContext dctx, Map context) { 293 GenericDelegator delegator = dctx.getDelegator(); 294 Set set = new TreeSet (); 295 296 set.add(delegator.getModelEntity("Person")); 297 set.add(delegator.getModelEntity("PartyRole")); 298 set.add(delegator.getModelEntity("Party")); 299 set.add(delegator.getModelEntity("ContactMech")); 300 set.add(delegator.getModelEntity("PartyContactMech")); 301 set.add(delegator.getModelEntity("OrderHeader")); 302 set.add(delegator.getModelEntity("OrderItem")); 303 set.add(delegator.getModelEntity("OrderContactMech")); 304 set.add(delegator.getModelEntity("OrderRole")); 305 set.add(delegator.getModelEntity("Product")); 306 set.add(delegator.getModelEntity("RoleType")); 307 308 Iterator i = set.iterator(); 309 while (i.hasNext()) { 310 Debug.log(((ModelEntity)i.next()).getEntityName(), module); 311 } 312 return ServiceUtil.returnSuccess(); 313 } 314 315 public static Map makeALotOfVisits(DispatchContext dctx, Map context) { 316 GenericDelegator delegator = dctx.getDelegator(); 317 int count = ((Integer ) context.get("count")).intValue(); 318 319 for (int i = 0; i < count; i++ ) { 320 GenericValue v = delegator.makeValue("Visit", null); 321 String seqId = delegator.getNextSeqId("Visit").toString(); 322 323 v.set("visitId", seqId); 324 v.set("userCreated", "N"); 325 v.set("sessionId", "NA-" + seqId); 326 v.set("serverIpAddress", "127.0.0.1"); 327 v.set("serverHostName", "localhost"); 328 v.set("webappName", "webtools"); 329 v.set("initialLocale", "en_US"); 330 v.set("initialRequest", "http://localhost:8080/webtools/control/main"); 331 v.set("initialReferrer", "http://localhost:8080/webtools/control/main"); 332 v.set("initialUserAgent", "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/124 (KHTML, like Gecko) Safari/125.1"); 333 v.set("clientIpAddress", "127.0.0.1"); 334 v.set("clientHostName", "localhost"); 335 v.set("fromDate", UtilDateTime.nowTimestamp()); 336 337 try { 338 delegator.create(v); 339 } catch (GenericEntityException e) { 340 Debug.logError(e, module); 341 } 342 } 343 344 return ServiceUtil.returnSuccess(); 345 } 346 347 public static Map displayXaDebugInfo(DispatchContext dctx, Map context) { 348 if (TransactionUtil.debugResources) { 349 if (TransactionUtil.debugResMap != null && TransactionUtil.debugResMap.size() > 0) { 350 TransactionUtil.logRunningTx(); 351 } else { 352 Debug.log("No running transaction to display.", module); 353 } 354 } else { 355 Debug.log("Debug resources is disabled.", module); 356 } 357 358 return ServiceUtil.returnSuccess(); 359 } 360 361 public static Map byteWrapperTest(DispatchContext dctx, Map context) { 362 ByteWrapper wrapper1 = (ByteWrapper) context.get("byteWrapper1"); 363 ByteWrapper wrapper2 = (ByteWrapper) context.get("byteWrapper2"); 364 String fileName1 = (String ) context.get("saveAsFileName1"); 365 String fileName2 = (String ) context.get("saveAsFileName2"); 366 String ofbizHome = System.getProperty("ofbiz.home"); 367 String outputPath1 = ofbizHome + (fileName1.startsWith("/") ? fileName1 : "/" + fileName1); 368 String outputPath2 = ofbizHome + (fileName2.startsWith("/") ? fileName2 : "/" + fileName2); 369 370 try { 371 RandomAccessFile file1 = new RandomAccessFile (outputPath1, "rw"); 372 RandomAccessFile file2 = new RandomAccessFile (outputPath2, "rw"); 373 file1.write(wrapper1.getBytes()); 374 file2.write(wrapper2.getBytes()); 375 } catch (FileNotFoundException e) { 376 Debug.logError(e, module); 377 } catch (IOException e) { 378 Debug.logError(e, module); 379 } 380 381 return ServiceUtil.returnSuccess(); 382 } 383 384 public static Map uploadTest(DispatchContext dctx, Map context) { 385 LocalDispatcher dispatcher = dctx.getDispatcher(); 386 GenericValue userLogin = (GenericValue) context.get("userLogin"); 387 388 ByteWrapper wrapper = (ByteWrapper) context.get("uploadFile"); 389 String fileName = (String ) context.get("_uploadFile_fileName"); 390 String contentType = (String ) context.get("_uploadFile_contentType"); 391 392 Map createCtx = new HashMap (); 393 createCtx.put("binData", wrapper); 394 createCtx.put("dataResourceTypeId", "OFBIZ_FILE"); 395 createCtx.put("dataResourceName", fileName); 396 createCtx.put("dataCategoryId", "PERSONAL"); 397 createCtx.put("statusId", "CTNT_PUBLISHED"); 398 createCtx.put("mimeTypeId", contentType); 399 createCtx.put("userLogin", userLogin); 400 401 Map createResp = null; 402 try { 403 createResp = dispatcher.runSync("createFile", createCtx); 404 } catch (GenericServiceException e) { 405 Debug.logError(e, module); 406 return ServiceUtil.returnError(e.getMessage()); 407 } 408 if (ServiceUtil.isError(createResp)) { 409 return ServiceUtil.returnError(ServiceUtil.getErrorMessage(createResp)); 410 } 411 412 GenericValue dataResource = (GenericValue) createResp.get("dataResource"); 413 if (dataResource != null) { 414 Map contentCtx = new HashMap (); 415 contentCtx.put("dataResourceId", dataResource.getString("dataResourceId")); 416 contentCtx.put("localeString", ((Locale ) context.get("locale")).toString()); 417 contentCtx.put("contentTypeId", "DOCUMENT"); 418 contentCtx.put("mimeTypeId", contentType); 419 contentCtx.put("contentName", fileName); 420 contentCtx.put("statusId", "CTNT_PUBLISHED"); 421 contentCtx.put("userLogin", userLogin); 422 423 Map contentResp = null; 424 try { 425 contentResp = dispatcher.runSync("createContent", contentCtx); 426 } catch (GenericServiceException e) { 427 Debug.logError(e, module); 428 return ServiceUtil.returnError(e.getMessage()); 429 } 430 if (ServiceUtil.isError(contentResp)) { 431 return ServiceUtil.returnError(ServiceUtil.getErrorMessage(contentResp)); 432 } 433 } 434 435 return ServiceUtil.returnSuccess(); 436 } 437 438 public static Map mcaTest(DispatchContext dctx, Map context) { 439 MimeMessageWrapper wrapper = (MimeMessageWrapper) context.get("messageWrapper"); 440 MimeMessage message = wrapper.getMessage(); 441 try { 442 if (message.getAllRecipients() != null) { 443 Debug.log("To: " + UtilMisc.toListArray(message.getAllRecipients()), module); 444 } 445 if (message.getFrom() != null) { 446 Debug.log("From: " + UtilMisc.toListArray(message.getFrom()), module); 447 } 448 Debug.log("Subject: " + message.getSubject(), module); 449 if (message.getSentDate() != null) { 450 Debug.log("Sent: " + message.getSentDate().toString(), module); 451 } 452 if (message.getReceivedDate() != null) { 453 Debug.log("Received: " + message.getReceivedDate().toString(), module); 454 } 455 } catch (Exception e) { 456 Debug.logError(e, module); 457 } 458 return ServiceUtil.returnSuccess(); 459 } 460 461 public static Map ping(DispatchContext dctx, Map context) { 462 GenericDelegator delegator = dctx.getDelegator(); 463 String message = (String ) context.get("message"); 464 if (message == null) { 465 message = "PONG"; 466 } 467 468 long count = -1; 469 try { 470 count = delegator.findCountByAnd("SequenceValueItem", null); 471 } catch (GenericEntityException e) { 472 Debug.logError(e.getMessage(), module); 473 return ServiceUtil.returnError("Unable to connect to datasource!"); 474 } 475 476 if (count > 0) { 477 Map result = ServiceUtil.returnSuccess(); 478 result.put("message", message); 479 return result; 480 } else { 481 return ServiceUtil.returnError("Invalid count returned from database"); 482 } 483 } 484 } 485 486 | Popular Tags |