📄 serviceutil.java
字号:
public static String getErrorMessage(Map result) {
return (String) result.get(ModelService.ERROR_MESSAGE);
}
public static String makeErrorMessage(Map result, String msgPrefix, String msgSuffix, String errorPrefix, String errorSuffix) {
if (result == null) {
Debug.logWarning("A null result map was passed", module);
return null;
}
String errorMsg = (String) result.get(ModelService.ERROR_MESSAGE);
List errorMsgList = (List) result.get(ModelService.ERROR_MESSAGE_LIST);
Map errorMsgMap = (Map) result.get(ModelService.ERROR_MESSAGE_MAP);
StringBuffer outMsg = new StringBuffer();
if (errorMsg != null) {
if (msgPrefix != null) outMsg.append(msgPrefix);
outMsg.append(errorMsg);
if (msgSuffix != null) outMsg.append(msgSuffix);
}
outMsg.append(makeMessageList(errorMsgList, msgPrefix, msgSuffix));
if (errorMsgMap != null) {
Iterator mapIter = errorMsgMap.entrySet().iterator();
while (mapIter.hasNext()) {
Map.Entry entry = (Map.Entry) mapIter.next();
outMsg.append(msgPrefix);
outMsg.append(entry.getKey());
outMsg.append(": ");
outMsg.append(entry.getValue());
outMsg.append(msgSuffix);
}
}
if (outMsg.length() > 0) {
StringBuffer strBuf = new StringBuffer();
if (errorPrefix != null) strBuf.append(errorPrefix);
strBuf.append(outMsg.toString());
if (errorSuffix != null) strBuf.append(errorSuffix);
return strBuf.toString();
} else {
return null;
}
}
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 = (String) iter.next();
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 done = new EntityConditionList(UtilMisc.toList(cancelled, finished), EntityOperator.OR);
EntityCondition main = new EntityConditionList(UtilMisc.toList(done, pool), EntityOperator.AND);
// lookup the jobs
List foundJobs = null;
try {
foundJobs = delegator.findByCondition("JobSandbox", main, null, null);
} catch (GenericEntityException e) {
Debug.logError(e, "Cannot get jobs to purge");
return ServiceUtil.returnError(e.getMessage());
}
if (foundJobs != null && foundJobs.size() > 0) {
Iterator i = foundJobs.iterator();
while (i.hasNext()) {
GenericValue job = (GenericValue) i.next();
try {
job.remove();
} catch (GenericEntityException e) {
Debug.logError(e, "Unable to remove job : " + job, 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");
if (!security.hasPermission("SERVICE_INVOKE_ANY", userLogin)) {
return ServiceUtil.returnError("You do not have permission to run this service");
}
String jobName = (String) context.get("jobName");
Timestamp runTime = (Timestamp) context.get("runTime");
Map fields = UtilMisc.toMap("jobName", jobName, "runTime", runTime);
GenericValue job = null;
try {
job = delegator.findByPrimaryKey("JobSandbox", fields);
if (job != null) {
job.set("cancelDateTime", UtilDateTime.nowTimestamp());
job.store();
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
return ServiceUtil.returnError("Unable to cancel job : " + fields);
}
Timestamp cancelDate = job.getTimestamp("cancelDateTime");
if (cancelDate != null) {
Map result = ServiceUtil.returnSuccess();
result.put("cancelDateTime", cancelDate);
return result;
} else {
return ServiceUtil.returnError("Unable to cancel job : " + job);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -