📄 coreevents.java
字号:
/*
* $Id: CoreEvents.java,v 1.5 2003/12/05 21:03:55 ajzeneski Exp $
*
* Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.ofbiz.content.webapp.event;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilHttp;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.content.webapp.control.RequestHandler;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.security.Security;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ModelService;
import org.ofbiz.service.ServiceDispatcher;
import org.ofbiz.service.WebAppDispatcher;
import org.ofbiz.service.calendar.RecurrenceRule;
/**
* CoreEvents - WebApp Events Related To CORE components
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
* @version $Revision: 1.5 $
* @since 2.0
*/
public class CoreEvents {
public static final String module = CoreEvents.class.getName();
/**
* Return success event. Used as a place holder for events.
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return Response code string
*/
public static String returnSuccess(HttpServletRequest request, HttpServletResponse response) {
return "success";
}
/**
* Return error event. Used as a place holder for events.
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return Response code string
*/
public static String returnError(HttpServletRequest request, HttpServletResponse response) {
return "error";
}
/**
* Return null event. Used as a place holder for events.
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return Response code string
*/
public static String returnNull(HttpServletRequest request, HttpServletResponse response) {
return null;
}
/**
* Change delegator event. Changes the delegator for the current session
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return Response code string
*/
public static String changeDelegator(HttpServletRequest request, HttpServletResponse response) {
String delegatorName = request.getParameter("delegator");
Security security = (Security) request.getAttribute("security");
if (!security.hasPermission("ENTITY_MAINT", request.getSession())) {
request.setAttribute("_ERROR_MESSAGE_", "<li>You are not authorized to use this function.");
return "error";
}
if (delegatorName == null) {
request.setAttribute("_ERROR_MESSAGE_", "<li>Required parameter 'delegator' not passed.");
return "error";
}
GenericDelegator delegator = GenericDelegator.getGenericDelegator(delegatorName);
if (delegator == null) {
request.setAttribute("_ERROR_MESSAGE_", "<li>No delegator defined by that name.");
return "error";
}
// now change the dispatcher to use this delegator
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
DispatchContext dctx = dispatcher.getDispatchContext();
String dispatcherName = dispatcher.getName();
if (dispatcherName == null) {
request.setAttribute("_ERROR_MESSAGE_", "<li>Dispatcher name is null.");
return "error";
}
if (dctx == null) {
request.setAttribute("_ERROR_MESSAGE_", "<li>Dispatch context is null.");
return "error";
}
ServiceDispatcher sd = ServiceDispatcher.getInstance(dispatcherName, delegator);
if (sd == null)
dispatcher = new WebAppDispatcher(dctx, delegator);
else
dispatcher = sd.getLocalContext(dispatcherName).getDispatcher();
request.getSession().setAttribute("delegator", delegator);
request.getSession().setAttribute("dispatcher", dispatcher);
return "success";
}
/**
* Change dispatcher event. Changes the dispatch for the current session
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return Response code string
*/
public static String changeDispatcher(HttpServletRequest request, HttpServletResponse response) {
String dispatcherName = request.getParameter("dispatcher");
Security security = (Security) request.getAttribute("security");
if (!security.hasPermission("ENTITY_MAINT", request.getSession())) {
request.setAttribute("_ERROR_MESSAGE_", "<li>You are not authorized to use this function.");
return "error";
}
if (dispatcherName == null) {
request.setAttribute("_ERROR_MESSAGE_", "<li>Required parameter 'dispatcher' not passed.");
return "error";
}
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
ServiceDispatcher sd = ServiceDispatcher.getInstance(dispatcherName, delegator);
if (sd == null) {
request.setAttribute("_ERROR_MESSAGE_", "<li>No dispatcher with that name has been registered.");
return "error";
}
LocalDispatcher dispatcher = sd.getLocalContext(dispatcherName).getDispatcher();
request.getSession().setAttribute("dispatcher", dispatcher);
return "success";
}
/**
* Schedule a service for a specific time or recurrence
* Request Parameters which are used for this service:
*
* SERVICE_NAME - Name of the service to invoke
* SERVICE_TIME - First time the service will occur
* SERVICE_FREQUENCY - The type of recurrence (SECONDLY,MINUTELY,DAILY,etc)
* SERVICE_INTERVAL - The interval of the frequency (every 5 minutes, etc)
*
* @param request HttpServletRequest
* @param response HttpServletResponse
* @return Response code string
*/
public static String scheduleService(HttpServletRequest request, HttpServletResponse response) {
Security security = (Security) request.getAttribute("security");
LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
Map params = UtilHttp.getParameterMap(request);
// get the schedule parameters
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");
// the frequency map
Map freqMap = new HashMap();
freqMap.put("SECONDLY", new Integer(1));
freqMap.put("MINUTELY", new Integer(2));
freqMap.put("HOURLY", new Integer(3));
freqMap.put("DAILY", new Integer(4));
freqMap.put("WEEKLY", new Integer(5));
freqMap.put("MONTHLY", new Integer(6));
freqMap.put("YEARLY", new Integer(7));
// some defaults
long startTime = (new Date()).getTime();
long endTime = 0;
int count = 1;
int interval = 1;
int frequency = RecurrenceRule.DAILY;
StringBuffer errorBuf = new StringBuffer();
// make sure we passed a service
if (serviceName == null) {
request.setAttribute("_ERROR_MESSAGE_", "<li>You must specify a 'SERVICE_NAME' (other parameters include: SERVICE_TIME, SERVICE_FREQUENCY, SERVICE_INTERVAL, SERVICE_COUNT).");
return "error";
}
Timestamp ts = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -