* @param response HttpServletResponse
* @return Response code string
*/
public static String scheduleService(HttpServletRequest request, HttpServletResponse response) {
GenericValue userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
Authorization authz = (Authorization) request.getAttribute("authz");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
//Delegator delegator = (Delegator) request.getAttribute("delegator");
Locale locale = UtilHttp.getLocale(request);
TimeZone timeZone = UtilHttp.getTimeZone(request);
Map<String, Object> params = UtilHttp.getParameterMap(request);
// get the schedule parameters
String jobName = (String) params.remove("JOB_NAME");
String serviceName = (String) params.remove("SERVICE_NAME");
String poolName = (String) params.remove("POOL_NAME");
String serviceTime = (String) params.remove("SERVICE_TIME");
String serviceEndTime = (String) params.remove("SERVICE_END_TIME");
String serviceFreq = (String) params.remove("SERVICE_FREQUENCY");
String serviceIntr = (String) params.remove("SERVICE_INTERVAL");
String serviceCnt = (String) params.remove("SERVICE_COUNT");
String retryCnt = (String) params.remove("SERVICE_MAXRETRY");
// the frequency map
Map<String, Integer> freqMap = FastMap.newInstance();
freqMap.put("SECONDLY", Integer.valueOf(1));
freqMap.put("MINUTELY", Integer.valueOf(2));
freqMap.put("HOURLY", Integer.valueOf(3));
freqMap.put("DAILY", Integer.valueOf(4));
freqMap.put("WEEKLY", Integer.valueOf(5));
freqMap.put("MONTHLY", Integer.valueOf(6));
freqMap.put("YEARLY", Integer.valueOf(7));
// some defaults
long startTime = (new Date()).getTime();
long endTime = 0;
int maxRetry = -1;
int count = 1;
int interval = 1;
int frequency = RecurrenceRule.DAILY;
StringBuilder errorBuf = new StringBuilder();
// make sure we passed a service
if (serviceName == null) {
String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.must_specify_service", locale);
request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);
return "error";
}
// lookup the service definition to see if this service is externally available, if not require the SERVICE_INVOKE_ANY permission
ModelService modelService = null;
try {
modelService = dispatcher.getDispatchContext().getModelService(serviceName);
} catch (GenericServiceException e) {
Debug.logError(e, "Error looking up ModelService for serviceName [" + serviceName + "]", module);
String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.error_modelservice_for_srv_name", locale);
request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + " [" + serviceName + "]: " + e.toString());
return "error";
}
if (modelService == null) {
String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.service_name_not_find", locale);
request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg + " [" + serviceName + "]");
return "error";
}
// make the context valid; using the makeValid method from ModelService
Map<String, Object> serviceContext = FastMap.newInstance();
Iterator<String> ci = modelService.getInParamNames().iterator();
while (ci.hasNext()) {
String name = ci.next();
// don't include userLogin, that's taken care of below
if ("userLogin".equals(name)) continue;
// don't include locale, that is also taken care of below
if ("locale".equals(name)) continue;
Object value = request.getParameter(name);
// if the parameter wasn't passed and no other value found, don't pass on the null
if (value == null) {
value = request.getAttribute(name);
}
if (value == null) {
value = request.getSession().getAttribute(name);
}
if (value == null) {
// still null, give up for this one
continue;
}
if (value instanceof String && ((String) value).length() == 0) {
// interpreting empty fields as null values for each in back end handling...
value = null;
}
// set even if null so that values will get nulled in the db later on
serviceContext.put(name, value);
}
serviceContext = modelService.makeValid(serviceContext, ModelService.IN_PARAM, true, null, timeZone, locale);
if (userLogin != null) {
serviceContext.put("userLogin", userLogin);
}
if (locale != null) {
serviceContext.put("locale", locale);
}
if (!modelService.export && !authz.hasPermission(request.getSession(), "SERVICE_INVOKE_ANY", null)) {
String errMsg = UtilProperties.getMessage(CoreEvents.err_resource, "coreEvents.not_authorized_to_call", locale);
request.setAttribute("_ERROR_MESSAGE_", "<li>" + errMsg);
return "error";
}