?? serviceutil.java
字號(hào):
} public static String makeSuccessMessage(Map result, String msgPrefix, String msgSuffix, String successPrefix, String successSuffix) { if (result == null) { return ""; } String successMsg = (String) result.get(ModelService.SUCCESS_MESSAGE); List successMsgList = (List) result.get(ModelService.SUCCESS_MESSAGE_LIST); StringBuffer outMsg = new StringBuffer(); outMsg.append(makeMessageList(successMsgList, msgPrefix, msgSuffix)); if (successMsg != null) { if (msgPrefix != null) outMsg.append(msgPrefix); outMsg.append(successMsg); if (msgSuffix != null) outMsg.append(msgSuffix); } if (outMsg.length() > 0) { StringBuffer strBuf = new StringBuffer(); if (successPrefix != null) strBuf.append(successPrefix); strBuf.append(outMsg.toString()); if (successSuffix != null) strBuf.append(successSuffix); return strBuf.toString(); } else { return null; } } public static String makeMessageList(List msgList, String msgPrefix, String msgSuffix) { StringBuffer outMsg = new StringBuffer(); if (msgList != null && msgList.size() > 0) { Iterator iter = msgList.iterator(); while (iter.hasNext()) { String curMsg = iter.next().toString(); if (msgPrefix != null) outMsg.append(msgPrefix); outMsg.append(curMsg); if (msgSuffix != null) outMsg.append(msgSuffix); } } return outMsg.toString(); } public static Map purgeOldJobs(DispatchContext dctx, Map context) { String sendPool = ServiceConfigUtil.getSendPool(); int daysToKeep = ServiceConfigUtil.getPurgeJobDays(); GenericDelegator delegator = dctx.getDelegator(); Timestamp now = UtilDateTime.nowTimestamp(); Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(now.getTime()); cal.add(Calendar.DAY_OF_YEAR, daysToKeep * -1); Timestamp purgeTime = new Timestamp(cal.getTimeInMillis()); // create the conditions to query EntityCondition pool = new EntityExpr("poolId", EntityOperator.EQUALS, sendPool); List finExp = UtilMisc.toList(new EntityExpr("finishDateTime", EntityOperator.NOT_EQUAL, null)); finExp.add(new EntityExpr("finishDateTime", EntityOperator.LESS_THAN, purgeTime)); List canExp = UtilMisc.toList(new EntityExpr("cancelDateTime", EntityOperator.NOT_EQUAL, null)); canExp.add(new EntityExpr("cancelDateTime", EntityOperator.LESS_THAN, purgeTime)); EntityCondition cancelled = new EntityConditionList(canExp, EntityOperator.AND); EntityCondition finished = new EntityConditionList(finExp, EntityOperator.AND); EntityCondition doneCond = new EntityConditionList(UtilMisc.toList(cancelled, finished), EntityOperator.OR); EntityCondition mainCond = new EntityConditionList(UtilMisc.toList(doneCond, pool), EntityOperator.AND); // configure the find options EntityFindOptions findOptions = new EntityFindOptions(); findOptions.setResultSetType(EntityFindOptions.TYPE_SCROLL_INSENSITIVE); findOptions.setMaxRows(1000); // always suspend the current transaction; use the one internally Transaction parent = null; try { if (TransactionUtil.getStatus() != TransactionUtil.STATUS_NO_TRANSACTION) { parent = TransactionUtil.suspend(); } // lookup the jobs - looping 1000 at a time to avoid problems with cursors // also, using unique transaction to delete as many as possible even with errors boolean noMoreResults = false; boolean beganTx1 = false; while (!noMoreResults) { // current list of records List curList = null; try { // begin this transaction beganTx1 = TransactionUtil.begin(); EntityListIterator foundJobs = delegator.findListIteratorByCondition("JobSandbox", mainCond, null, null, null, findOptions); curList = foundJobs.getPartialList(1, 1000); foundJobs.close(); } catch (GenericEntityException e) { Debug.logError(e, "Cannot obtain job data from datasource", module); try { TransactionUtil.rollback(beganTx1, e.getMessage(), e); } catch (GenericTransactionException e1) { Debug.logWarning(e1, module); } return ServiceUtil.returnError(e.getMessage()); } finally { try { TransactionUtil.commit(beganTx1); } catch (GenericTransactionException e) { Debug.logWarning(e, module); } } // remove each from the list in its own transaction if (curList != null && curList.size() > 0) { // list of runtime data IDs to attempt to delete List runtimeToDelete = FastList.newInstance(); Iterator curIter = curList.iterator(); while (curIter.hasNext()) { GenericValue job = (GenericValue) curIter.next(); String jobId = job.getString("jobId"); boolean beganTx2 = false; try { beganTx2 = TransactionUtil.begin(); job.remove(); } catch (GenericEntityException e) { Debug.logInfo("Cannot remove job data for ID: " + jobId, module); try { TransactionUtil.rollback(beganTx2, e.getMessage(), e); } catch (GenericTransactionException e1) { Debug.logWarning(e1, module); } } finally { try { TransactionUtil.commit(beganTx2); } catch (GenericTransactionException e) { Debug.logWarning(e, module); } } } // delete the runtime data - in a new transaction for each delete // we do this so that the ones which cannot be deleted to not cause // the entire group to rollback; some may be attached to multiple jobs. if (runtimeToDelete.size() > 0) { Iterator delIter = runtimeToDelete.iterator(); while (delIter.hasNext()) { String runtimeId = (String) delIter.next(); boolean beganTx3 = false; try { beganTx3 = TransactionUtil.begin(); delegator.removeByAnd("RuntimeData", UtilMisc.toMap("runtimeDataId", runtimeId)); } catch (GenericEntityException e) { Debug.logInfo("Cannot remove runtime data for ID: " + runtimeId, module); try { TransactionUtil.rollback(beganTx3, e.getMessage(), e); } catch (GenericTransactionException e1) { Debug.logWarning(e1, module); } } finally { try { TransactionUtil.commit(beganTx3); } catch (GenericTransactionException e) { Debug.logWarning(e, module); } } } } } else { noMoreResults = true; } } } catch (GenericTransactionException e) { Debug.logError(e, "Unable to suspend transaction; cannot purge jobs!", module); return ServiceUtil.returnError(e.getMessage()); } finally { if (parent != null) { try { TransactionUtil.resume(parent); } catch (GenericTransactionException e) { Debug.logWarning(e, module); } } } return ServiceUtil.returnSuccess(); } public static Map cancelJob(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = getLocale(context); if (!security.hasPermission("SERVICE_INVOKE_ANY", userLogin)) { String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.no_permission_to_run", locale) + "."; return ServiceUtil.returnError(errMsg); } String jobId = (String) context.get("jobId"); Map fields = UtilMisc.toMap("jobId", jobId); GenericValue job = null; try { job = delegator.findByPrimaryKey("JobSandbox", fields); if (job != null) { job.set("cancelDateTime", UtilDateTime.nowTimestamp()); job.set("statusId", "SERVICE_CANCELLED"); job.store(); } } catch (GenericEntityException e) { Debug.logError(e, module); String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.unable_to_cancel_job", locale) + " : " + fields; return ServiceUtil.returnError(errMsg); } Timestamp cancelDate = job.getTimestamp("cancelDateTime"); if (cancelDate != null) { Map result = ServiceUtil.returnSuccess(); result.put("cancelDateTime", cancelDate); return result; } else { String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.unable_to_cancel_job", locale) + " : " + job; return ServiceUtil.returnError(errMsg); } } public static Map cancelJobRetries(DispatchContext dctx, Map context) { GenericDelegator delegator = dctx.getDelegator(); Security security = dctx.getSecurity(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = getLocale(context); if (!security.hasPermission("SERVICE_INVOKE_ANY", userLogin)) { String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.no_permission_to_run", locale) + "."; return ServiceUtil.returnError(errMsg); } String jobId = (String) context.get("jobId"); Map fields = UtilMisc.toMap("jobId", jobId); GenericValue job = null; try { job = delegator.findByPrimaryKey("JobSandbox", fields); if (job != null) { job.set("maxRetry", new Long(0)); job.store(); } } catch (GenericEntityException e) { Debug.logError(e, module); String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.unable_to_cancel_job_retries", locale) + " : " + fields; return ServiceUtil.returnError(errMsg); } Timestamp cancelDate = job.getTimestamp("cancelDateTime"); if (cancelDate != null) { return ServiceUtil.returnSuccess(); } else { String errMsg = UtilProperties.getMessage(ServiceUtil.resource, "serviceUtil.unable_to_cancel_job_retries", locale) + " : " + job; return ServiceUtil.returnError(errMsg); } } public static GenericValue getUserLogin(DispatchContext dctx, Map context, String runAsUser) { GenericValue userLogin = (GenericValue) context.get("userLogin"); GenericDelegator delegator = dctx.getDelegator(); if (runAsUser != null) { try { GenericValue runAs = delegator.findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", runAsUser)); if (runAs != null) { userLogin = runAs; } } catch (GenericEntityException e) { Debug.logError(e, module); } } return userLogin; } private static Locale getLocale(Map context) { Locale locale = (Locale) context.get("locale"); if (locale == null) { locale = Locale.getDefault(); } return locale; }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -