📄 registrationservlet.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.wstore;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.compiere.model.*;
import org.compiere.util.*;
/**
* Registration Servlet.
*
* @author Jorg Janke
* @version $Id: RegistrationServlet.java,v 1.16 2005/12/13 00:16:59 jjanke Exp $
*/
public class RegistrationServlet extends HttpServlet
{
/** Logging */
private CLogger log = CLogger.getCLogger(getClass());
/** Name */
static public final String NAME = "RegistrationServlet";
/**
* Initialize global variables
*
* @param config Configuration
* @throws ServletException
*/
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
if (!WebEnv.initWeb(config))
throw new ServletException("RegistrationServlet.init");
} // init
/**
* Get Servlet information
* @return Info
*/
public String getServletInfo()
{
return "Compiere Web Registration Servlet";
} // getServletInfo
/**
* Clean up resources
*/
public void destroy()
{
log.fine("");
} // destroy
/*************************************************************************/
public static final String P_REGISTRATION_ID = "A_Registration_ID";
/** Thanks Msg */
private String THANKS = "Thank you for your registration!";
/** Problem Msg */
private String PROBLEM = "Thank you for your registration - We experienced a problem - please let us know!";
/**************************************************************************
* Process the HTTP Get request.
* If not System registration - forward to registration.jsp
*
* @param request request
* @param response response
* @throws ServletException
* @throws IOException
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
log.info("From " + request.getRemoteHost() + " - " + request.getRemoteAddr());
if (!processSystemRegistration(request, response))
{
log.info("Forward to registration.jsp");
response.sendRedirect("registration.jsp");
}
} // doGet
/**
* Process System Registration
* @param request request
* @param response response
* @return true if System Registration
*/
private boolean processSystemRegistration (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// System Info
String name = WebUtil.getParameter (request, "Name");
String userName = WebUtil.getParameter (request, "UserName");
String password = WebUtil.getParameter (request, "Password");
// Not a System registration
if ((name == null || name.length() == 0)&& (userName == null || userName.length() == 0) && (password == null || password.length() == 0))
return false;
log.info ("Name=" + name + ", User=" + userName);
// Registration Info
String description = WebUtil.getParameter (request, "Description");
boolean inProduction = WebUtil.getParameterAsBoolean(request, "IsInProduction", "Y");
Timestamp startDate = WebUtil.getParameterAsDate (request, "StartProductionDate");
if (startDate == null)
startDate = new Timestamp(System.currentTimeMillis());
boolean allowPublish = WebUtil.getParameterAsBoolean (request, "IsAllowPublish", "Y");
boolean registered = WebUtil.getParameterAsBoolean (request, "IsRegistered", "Y");
int Record_ID = WebUtil.getParameterAsInt(request, "Record_ID");
// Find User
Properties ctx = JSPEnv.getCtx(request);
MUser user = null;
int AD_User_ID = DB.getSQLValue(null,
"SELECT AD_User_ID FROM AD_User WHERE EMail=?", userName);
if (AD_User_ID > 0)
user = MUser.get(ctx, AD_User_ID);
else
log.warning("User Not found=" + userName);
// Registration
MRegistration reg = null;
if (Record_ID > 0)
{
reg = new MRegistration (ctx, Record_ID, null);
if (reg.get_ID() != Record_ID)
{
log.warning("Registration Not found=" + Record_ID);
reg = null;
}
else if (user != null)
{
if (reg.getC_BPartner_ID() != user.getC_BPartner_ID())
{
log.warning("Registration for different BP - AD_User_ID="
+ AD_User_ID + "(" + user.getEMail()
+ "), BP RegistrationBP=" + reg.getC_BPartner_ID()
+ "<>UserBP=" + user.getC_BPartner_ID());
reg = null;
}
if (!password.equals(user.getPassword()))
{
log.warning("Password does not match - AD_User_ID="
+ AD_User_ID + "(" + user.getEMail() + ")");
// ??
}
}
}
if (reg == null)
{
log.fine("New Registration");
reg = new MRegistration (ctx, name, allowPublish, inProduction, startDate, null);
Record_ID = 0;
}
// Common Update
reg.setDescription(description);
reg.setRemote_Addr(request.getRemoteAddr());
reg.setRemote_Host(request.getRemoteHost());
// User
if (user != null)
{
reg.setAD_User_ID(user.getAD_User_ID());
reg.setC_BPartner_ID(user.getC_BPartner_ID());
}
if (reg.save())
{
if (Record_ID == 0)
reg.loadAttributeValues(request); // new
else
reg.updateAttributeValues(request); // existing
sendAnswer (response, THANKS + " Record_ID=" + reg.getA_Registration_ID());
}
else
{
log.log(Level.SEVERE, "Registration not saved");
sendAnswer (response, PROBLEM + " Record_ID=0");
}
return true;
} // processSystemRegistration
/**
* Send Answer
* @param response response
* @param answer answer
* @throws IOException
*/
private void sendAnswer (HttpServletResponse response, String answer)
throws IOException
{
response.setHeader("Cache-Control", "no-cache");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter(); // with character encoding support
out.print(answer);
out.flush();
} // sendAnswer
/**************************************************************************
* Process the HTTP Post request
*
* @param request request
* @param response response
* @throws ServletException
* @throws IOException
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
log.info("From " + request.getRemoteHost() + " - " + request.getRemoteAddr());
// Get Session attributes
HttpSession session = request.getSession(true);
session.removeAttribute(WebSessionCtx.HDR_MESSAGE);
//
Properties ctx = JSPEnv.getCtx(request);
WebUser wu = (WebUser)session.getAttribute(WebUser.NAME);
if (wu == null)
{
log.warning("No web user");
response.sendRedirect("loginServlet?ForwardTo=registration.jsp"); // entry
return;
}
int A_Registration_ID = WebUtil.getParameterAsInt(request, P_REGISTRATION_ID);
MRegistration reg = null;
if (A_Registration_ID > 0)
reg = new MRegistration (ctx, A_Registration_ID, null);
if (reg == null)
{
reg = new MRegistration (ctx, 0, null);
A_Registration_ID = 0;
}
//
String name = WebUtil.getParameter (request, "Name");
if (name == null || name.length() == 0)
{
WebUtil.createForwardPage(response, "Name is Mandatory", "registrations.jsp", 4);
return;
}
reg.setName(name);
String description = WebUtil.getParameter (request, "Description");
if (description != null && description.length() > 0)
reg.setDescription(description);
boolean isInProduction = WebUtil.getParameterAsBoolean (request, "IsInProduction");
reg.setIsInProduction(isInProduction);
Timestamp assetServiceDate = WebUtil.getParameterAsDate (request, "AssetServiceDate");
if (assetServiceDate == null)
assetServiceDate = new Timestamp(System.currentTimeMillis());
reg.setAssetServiceDate(assetServiceDate);
boolean isAllowPublish = WebUtil.getParameterAsBoolean (request, "IsAllowPublish");
reg.setIsAllowPublish(isAllowPublish);
if (reg.save())
{
if (A_Registration_ID == 0)
reg.loadAttributeValues(request); // new
else
reg.updateAttributeValues(request); // existing
WebUtil.createForwardPage(response, THANKS, "registrations.jsp", 3);
}
else
{
log.log(Level.SEVERE, "Registration not saved");
WebUtil.createForwardPage(response, PROBLEM, "registrations.jsp", 3);
}
} // doPost
} // RegistrationSerlet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -