1 25 package org.ofbiz.service; 26 27 import java.sql.Timestamp ; 28 import java.util.Calendar ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Locale ; 33 import java.util.Map ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.transaction.Transaction ; 36 37 import javolution.util.FastMap; 38 import javolution.util.FastList; 39 40 import org.ofbiz.base.util.Debug; 41 import org.ofbiz.base.util.UtilDateTime; 42 import org.ofbiz.base.util.UtilMisc; 43 import org.ofbiz.base.util.UtilProperties; 44 import org.ofbiz.base.util.UtilValidate; 45 import org.ofbiz.entity.GenericDelegator; 46 import org.ofbiz.entity.GenericEntityException; 47 import org.ofbiz.entity.GenericValue; 48 import org.ofbiz.entity.transaction.TransactionUtil; 49 import org.ofbiz.entity.transaction.GenericTransactionException; 50 import org.ofbiz.entity.condition.EntityCondition; 51 import org.ofbiz.entity.condition.EntityConditionList; 52 import org.ofbiz.entity.condition.EntityExpr; 53 import org.ofbiz.entity.condition.EntityOperator; 54 import org.ofbiz.entity.util.EntityFindOptions; 55 import org.ofbiz.entity.util.EntityListIterator; 56 import org.ofbiz.security.Security; 57 import org.ofbiz.service.config.ServiceConfigUtil; 58 59 66 public class ServiceUtil { 67 68 public static final String module = ServiceUtil.class.getName(); 69 public static final String resource = "ServiceErrorUiLabels"; 70 71 72 public static boolean isError(Map results) { 73 if (results == null || results.get(ModelService.RESPONSE_MESSAGE) == null) { 74 return false; 75 } 76 return ModelService.RESPOND_ERROR.equals(results.get(ModelService.RESPONSE_MESSAGE)); 77 } 78 79 public static boolean isFailure(Map results) { 80 if (results == null || results.get(ModelService.RESPONSE_MESSAGE) == null) { 81 return false; 82 } 83 return ModelService.RESPOND_FAIL.equals(results.get(ModelService.RESPONSE_MESSAGE)); 84 } 85 86 87 public static Map returnError(String errorMessage) { 88 return returnProblem(ModelService.RESPOND_ERROR, errorMessage, null, null, null); 89 } 90 91 92 public static Map returnError(String errorMessage, List errorMessageList) { 93 return returnProblem(ModelService.RESPOND_ERROR, errorMessage, errorMessageList, null, null); 94 } 95 96 97 public static Map returnError(List errorMessageList) { 98 return returnProblem(ModelService.RESPOND_ERROR, null, errorMessageList, null, null); 99 } 100 101 public static Map returnFailure(String errorMessage) { 102 return returnProblem(ModelService.RESPOND_FAIL, errorMessage, null, null, null); 103 } 104 105 public static Map returnFailure(List errorMessageList) { 106 return returnProblem(ModelService.RESPOND_FAIL, null, errorMessageList, null, null); 107 } 108 109 110 public static Map returnError(String errorMessage, List errorMessageList, Map errorMessageMap, Map nestedResult) { 111 return returnProblem(ModelService.RESPOND_ERROR, errorMessage, errorMessageList, errorMessageMap, nestedResult); 112 } 113 114 public static Map returnProblem(String returnType, String errorMessage, List errorMessageList, Map errorMessageMap, Map nestedResult) { 115 Map result = FastMap.newInstance(); 116 result.put(ModelService.RESPONSE_MESSAGE, returnType); 117 if (errorMessage != null) { 118 result.put(ModelService.ERROR_MESSAGE, errorMessage); 119 } 120 121 List errorList = new LinkedList (); 122 if (errorMessageList != null) { 123 errorList.addAll(errorMessageList); 124 } 125 126 Map errorMap = FastMap.newInstance(); 127 if (errorMessageMap != null) { 128 errorMap.putAll(errorMessageMap); 129 } 130 131 if (nestedResult != null) { 132 if (nestedResult.get(ModelService.ERROR_MESSAGE) != null) { 133 errorList.add(nestedResult.get(ModelService.ERROR_MESSAGE)); 134 } 135 if (nestedResult.get(ModelService.ERROR_MESSAGE_LIST) != null) { 136 errorList.addAll((List ) nestedResult.get(ModelService.ERROR_MESSAGE_LIST)); 137 } 138 if (nestedResult.get(ModelService.ERROR_MESSAGE_MAP) != null) { 139 errorMap.putAll((Map ) nestedResult.get(ModelService.ERROR_MESSAGE_MAP)); 140 } 141 } 142 143 if (errorList.size() > 0) { 144 result.put(ModelService.ERROR_MESSAGE_LIST, errorList); 145 } 146 if (errorMap.size() > 0) { 147 result.put(ModelService.ERROR_MESSAGE_MAP, errorMap); 148 } 149 return result; 150 } 151 152 153 public static Map returnSuccess(String successMessage) { 154 return returnMessage(ModelService.RESPOND_SUCCESS, successMessage); 155 } 156 157 158 public static Map returnSuccess() { 159 return returnMessage(ModelService.RESPOND_SUCCESS, null); 160 } 161 162 166 public static Map returnMessage(String code, String message) { 167 Map result = FastMap.newInstance(); 168 if (code != null) result.put(ModelService.RESPONSE_MESSAGE, code); 169 if (message != null) result.put(ModelService.SUCCESS_MESSAGE, message); 170 return result; 171 } 172 173 176 public static String getPartyIdCheckSecurity(GenericValue userLogin, Security security, Map context, Map result, String secEntity, String secOperation) { 177 String partyId = (String ) context.get("partyId"); 178 Locale locale = getLocale(context); 179 if (partyId == null || partyId.length() == 0) { 180 partyId = userLogin.getString("partyId"); 181 } 182 183 if (partyId == null || partyId.length() == 0) { 185 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 186 String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.party_id_missing", locale) + "."; 187 result.put(ModelService.ERROR_MESSAGE, errMsg); 188 return partyId; 189 } 190 191 if (!partyId.equals(userLogin.getString("partyId"))) { 193 if (!security.hasEntityPermission(secEntity, secOperation, userLogin)) { 194 result.put(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR); 195 String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.no_permission_to_operation", locale) + "."; 196 result.put(ModelService.ERROR_MESSAGE, errMsg); 197 return partyId; 198 } 199 } 200 return partyId; 201 } 202 203 public static void setMessages(HttpServletRequest request, String errorMessage, String eventMessage, String defaultMessage) { 204 if (UtilValidate.isNotEmpty(errorMessage)) 205 request.setAttribute("_ERROR_MESSAGE_", errorMessage); 206 207 if (UtilValidate.isNotEmpty(eventMessage)) 208 request.setAttribute("_EVENT_MESSAGE_", eventMessage); 209 210 if (UtilValidate.isEmpty(errorMessage) && UtilValidate.isEmpty(eventMessage) && UtilValidate.isNotEmpty(defaultMessage)) 211 request.setAttribute("_EVENT_MESSAGE_", defaultMessage); 212 213 } 214 215 public static void getMessages(HttpServletRequest request, Map result, String defaultMessage) { 216 getMessages(request, result, defaultMessage, null, null, null, null, null, null); 217 } 218 219 public static void getMessages(HttpServletRequest request, Map result, String defaultMessage, 220 String msgPrefix, String msgSuffix, String errorPrefix, String errorSuffix, String successPrefix, String successSuffix) { 221 String errorMessage = ServiceUtil.makeErrorMessage(result, msgPrefix, msgSuffix, errorPrefix, errorSuffix); 222 String successMessage = ServiceUtil.makeSuccessMessage(result, msgPrefix, msgSuffix, successPrefix, successSuffix); 223 setMessages(request, errorMessage, successMessage, defaultMessage); 224 } 225 226 public static String getErrorMessage(Map result) { 227 StringBuffer errorMessage = new StringBuffer (); 228 229 if (result.get(ModelService.ERROR_MESSAGE) != null) errorMessage.append((String ) result.get(ModelService.ERROR_MESSAGE)); 230 231 if (result.get(ModelService.ERROR_MESSAGE_LIST) != null) { 232 List errors = (List ) result.get(ModelService.ERROR_MESSAGE_LIST); 233 Iterator errorIter = errors.iterator(); 234 while (errorIter.hasNext()) { 235 String curMessage = errorIter.next().toString(); 237 if (errorMessage.length() > 0) { 238 errorMessage.append(", "); 239 } 240 errorMessage.append(curMessage); 241 } 242 } 243 244 return errorMessage.toString(); 245 } 246 247 public static String makeErrorMessage(Map result, String msgPrefix, String msgSuffix, String errorPrefix, String errorSuffix) { 248 if (result == null) { 249 Debug.logWarning("A null result map was passed", module); 250 return null; 251 } 252 String errorMsg = (String ) result.get(ModelService.ERROR_MESSAGE); 253 List errorMsgList = (List ) result.get(ModelService.ERROR_MESSAGE_LIST); 254 Map errorMsgMap = (Map ) result.get(ModelService.ERROR_MESSAGE_MAP); 255 StringBuffer outMsg = new StringBuffer (); 256 257 if (errorMsg != null) { 258 if (msgPrefix != null) outMsg.append(msgPrefix); 259 outMsg.append(errorMsg); 260 if (msgSuffix != null) outMsg.append(msgSuffix); 261 } 262 263 outMsg.append(makeMessageList(errorMsgList, msgPrefix, msgSuffix)); 264 265 if (errorMsgMap != null) { 266 Iterator mapIter = errorMsgMap.entrySet().iterator(); 267 268 while (mapIter.hasNext()) { 269 Map.Entry entry = (Map.Entry ) mapIter.next(); 270 271 outMsg.append(msgPrefix); 272 outMsg.append(entry.getKey()); 273 outMsg.append(": "); 274 outMsg.append(entry.getValue()); 275 outMsg.append(msgSuffix); 276 } 277 } 278 279 if (outMsg.length() > 0) { 280 StringBuffer strBuf = new StringBuffer (); 281 282 if (errorPrefix != null) strBuf.append(errorPrefix); 283 strBuf.append(outMsg.toString()); 284 if (errorSuffix != null) strBuf.append(errorSuffix); 285 return strBuf.toString(); 286 } else { 287 return null; 288 } 289 } 290 291 public static String makeSuccessMessage(Map result, String msgPrefix, String msgSuffix, String successPrefix, String successSuffix) { 292 if (result == null) { 293 return ""; 294 } 295 String successMsg = (String ) result.get(ModelService.SUCCESS_MESSAGE); 296 List successMsgList = (List ) result.get(ModelService.SUCCESS_MESSAGE_LIST); 297 StringBuffer outMsg = new StringBuffer (); 298 299 outMsg.append(makeMessageList(successMsgList, msgPrefix, msgSuffix)); 300 301 if (successMsg != null) { 302 if (msgPrefix != null) outMsg.append(msgPrefix); 303 outMsg.append(successMsg); 304 if (msgSuffix != null) outMsg.append(msgSuffix); 305 } 306 307 if (outMsg.length() > 0) { 308 StringBuffer strBuf = new StringBuffer (); 309 if (successPrefix != null) strBuf.append(successPrefix); 310 strBuf.append(outMsg.toString()); 311 if (successSuffix != null) strBuf.append(successSuffix); 312 return strBuf.toString(); 313 } else { 314 return null; 315 } 316 } 317 318 public static String makeMessageList(List msgList, String msgPrefix, String msgSuffix) { 319 StringBuffer outMsg = new StringBuffer (); 320 if (msgList != null && msgList.size() > 0) { 321 Iterator iter = msgList.iterator(); 322 while (iter.hasNext()) { 323 String curMsg = iter.next().toString(); 324 if (msgPrefix != null) outMsg.append(msgPrefix); 325 outMsg.append(curMsg); 326 if (msgSuffix != null) outMsg.append(msgSuffix); 327 } 328 } 329 return outMsg.toString(); 330 } 331 332 public static Map purgeOldJobs(DispatchContext dctx, Map context) { 333 String sendPool = ServiceConfigUtil.getSendPool(); 334 int daysToKeep = ServiceConfigUtil.getPurgeJobDays(); 335 GenericDelegator delegator = dctx.getDelegator(); 336 337 Timestamp now = UtilDateTime.nowTimestamp(); 338 Calendar cal = Calendar.getInstance(); 339 cal.setTimeInMillis(now.getTime()); 340 cal.add(Calendar.DAY_OF_YEAR, daysToKeep * -1); 341 Timestamp purgeTime = new Timestamp (cal.getTimeInMillis()); 342 343 EntityCondition pool = new EntityExpr("poolId", EntityOperator.EQUALS, sendPool); 345 346 List finExp = UtilMisc.toList(new EntityExpr("finishDateTime", EntityOperator.NOT_EQUAL, null)); 347 finExp.add(new EntityExpr("finishDateTime", EntityOperator.LESS_THAN, purgeTime)); 348 349 List canExp = UtilMisc.toList(new EntityExpr("cancelDateTime", EntityOperator.NOT_EQUAL, null)); 350 canExp.add(new EntityExpr("cancelDateTime", EntityOperator.LESS_THAN, purgeTime)); 351 352 EntityCondition cancelled = new EntityConditionList(canExp, EntityOperator.AND); 353 EntityCondition finished = new EntityConditionList(finExp, EntityOperator.AND); 354 355 EntityCondition doneCond = new EntityConditionList(UtilMisc.toList(cancelled, finished), EntityOperator.OR); 356 EntityCondition mainCond = new EntityConditionList(UtilMisc.toList(doneCond, pool), EntityOperator.AND); 357 358 EntityFindOptions findOptions = new EntityFindOptions(); 360 findOptions.setResultSetType(EntityFindOptions.TYPE_SCROLL_INSENSITIVE); 361 findOptions.setMaxRows(1000); 362 363 Transaction parent = null; 365 try { 366 if (TransactionUtil.getStatus() != TransactionUtil.STATUS_NO_TRANSACTION) { 367 parent = TransactionUtil.suspend(); 368 } 369 370 boolean noMoreResults = false; 373 boolean beganTx1 = false; 374 while (!noMoreResults) { 375 List curList = null; 377 try { 378 beganTx1 = TransactionUtil.begin(); 380 381 EntityListIterator foundJobs = delegator.findListIteratorByCondition("JobSandbox", mainCond, null, null, null, findOptions); 382 curList = foundJobs.getPartialList(1, 1000); 383 foundJobs.close(); 384 385 } catch (GenericEntityException e) { 386 Debug.logError(e, "Cannot obtain job data from datasource", module); 387 try { 388 TransactionUtil.rollback(beganTx1, e.getMessage(), e); 389 } catch (GenericTransactionException e1) { 390 Debug.logWarning(e1, module); 391 } 392 return ServiceUtil.returnError(e.getMessage()); 393 } finally { 394 try { 395 TransactionUtil.commit(beganTx1); 396 } catch (GenericTransactionException e) { 397 Debug.logWarning(e, module); 398 } 399 } 400 401 if (curList != null && curList.size() > 0) { 403 List runtimeToDelete = FastList.newInstance(); 405 406 Iterator curIter = curList.iterator(); 407 while (curIter.hasNext()) { 408 GenericValue job = (GenericValue) curIter.next(); 409 String jobId = job.getString("jobId"); 410 boolean beganTx2 = false; 411 try { 412 beganTx2 = TransactionUtil.begin(); 413 job.remove(); 414 } catch (GenericEntityException e) { 415 Debug.logInfo("Cannot remove job data for ID: " + jobId, module); 416 try { 417 TransactionUtil.rollback(beganTx2, e.getMessage(), e); 418 } catch (GenericTransactionException e1) { 419 Debug.logWarning(e1, module); 420 } 421 } finally { 422 try { 423 TransactionUtil.commit(beganTx2); 424 } catch (GenericTransactionException e) { 425 Debug.logWarning(e, module); 426 } 427 } 428 } 429 430 if (runtimeToDelete.size() > 0) { 434 Iterator delIter = runtimeToDelete.iterator(); 435 while (delIter.hasNext()) { 436 String runtimeId = (String ) delIter.next(); 437 boolean beganTx3 = false; 438 try { 439 beganTx3 = TransactionUtil.begin(); 440 delegator.removeByAnd("RuntimeData", UtilMisc.toMap("runtimeDataId", runtimeId)); 441 442 } catch (GenericEntityException e) { 443 Debug.logInfo("Cannot remove runtime data for ID: " + runtimeId, module); 444 try { 445 TransactionUtil.rollback(beganTx3, e.getMessage(), e); 446 } catch (GenericTransactionException e1) { 447 Debug.logWarning(e1, module); 448 } 449 } finally { 450 try { 451 TransactionUtil.commit(beganTx3); 452 } catch (GenericTransactionException e) { 453 Debug.logWarning(e, module); 454 } 455 } 456 } 457 } 458 } else { 459 noMoreResults = true; 460 } 461 } 462 } catch (GenericTransactionException e) { 463 Debug.logError(e, "Unable to suspend transaction; cannot purge jobs!", module); 464 return ServiceUtil.returnError(e.getMessage()); 465 } finally { 466 if (parent != null) { 467 try { 468 TransactionUtil.resume(parent); 469 } catch (GenericTransactionException e) { 470 Debug.logWarning(e, module); 471 } 472 } 473 } 474 475 return ServiceUtil.returnSuccess(); 476 } 477 478 public static Map cancelJob(DispatchContext dctx, Map context) { 479 GenericDelegator delegator = dctx.getDelegator(); 480 Security security = dctx.getSecurity(); 481 GenericValue userLogin = (GenericValue) context.get("userLogin"); 482 Locale locale = getLocale(context); 483 484 if (!security.hasPermission("SERVICE_INVOKE_ANY", userLogin)) { 485 String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.no_permission_to_run", locale) + "."; 486 return ServiceUtil.returnError(errMsg); 487 } 488 489 String jobId = (String ) context.get("jobId"); 490 Map fields = UtilMisc.toMap("jobId", jobId); 491 492 GenericValue job = null; 493 try { 494 job = delegator.findByPrimaryKey("JobSandbox", fields); 495 if (job != null) { 496 job.set("cancelDateTime", UtilDateTime.nowTimestamp()); 497 job.set("statusId", "SERVICE_CANCELLED"); 498 job.store(); 499 } 500 } catch (GenericEntityException e) { 501 Debug.logError(e, module); 502 String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.unable_to_cancel_job", locale) + " : " + fields; 503 return ServiceUtil.returnError(errMsg); 504 } 505 506 Timestamp cancelDate = job.getTimestamp("cancelDateTime"); 507 if (cancelDate != null) { 508 Map result = ServiceUtil.returnSuccess(); 509 result.put("cancelDateTime", cancelDate); 510 return result; 511 } else { 512 String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.unable_to_cancel_job", locale) + " : " + job; 513 return ServiceUtil.returnError(errMsg); 514 } 515 } 516 517 public static Map cancelJobRetries(DispatchContext dctx, Map context) { 518 GenericDelegator delegator = dctx.getDelegator(); 519 Security security = dctx.getSecurity(); 520 GenericValue userLogin = (GenericValue) context.get("userLogin"); 521 Locale locale = getLocale(context); 522 if (!security.hasPermission("SERVICE_INVOKE_ANY", userLogin)) { 523 String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.no_permission_to_run", locale) + "."; 524 return ServiceUtil.returnError(errMsg); 525 } 526 527 String jobId = (String ) context.get("jobId"); 528 Map fields = UtilMisc.toMap("jobId", jobId); 529 530 GenericValue job = null; 531 try { 532 job = delegator.findByPrimaryKey("JobSandbox", fields); 533 if (job != null) { 534 job.set("maxRetry", new Long (0)); 535 job.store(); 536 } 537 } catch (GenericEntityException e) { 538 Debug.logError(e, module); 539 String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.unable_to_cancel_job_retries", locale) + " : " + fields; 540 return ServiceUtil.returnError(errMsg); 541 } 542 543 Timestamp cancelDate = job.getTimestamp("cancelDateTime"); 544 if (cancelDate != null) { 545 return ServiceUtil.returnSuccess(); 546 } else { 547 String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.unable_to_cancel_job_retries", locale) + " : " + job; 548 return ServiceUtil.returnError(errMsg); 549 } 550 } 551 552 public static GenericValue getUserLogin(DispatchContext dctx, Map context, String runAsUser) { 553 GenericValue userLogin = (GenericValue) context.get("userLogin"); 554 GenericDelegator delegator = dctx.getDelegator(); 555 if (runAsUser != null) { 556 try { 557 GenericValue runAs = delegator.findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", runAsUser)); 558 if (runAs != null) { 559 userLogin = runAs; 560 } 561 } catch (GenericEntityException e) { 562 Debug.logError(e, module); 563 } 564 } 565 return userLogin; 566 } 567 568 private static Locale getLocale(Map context) { 569 Locale locale = (Locale ) context.get("locale"); 570 if (locale == null) { 571 locale = Locale.getDefault(); 572 } 573 return locale; 574 } 575 } 576 | Popular Tags |