📄 webuser.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 and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.wstore;
import java.util.*;
import java.sql.*;
import org.apache.log4j.Logger;
import org.compiere.model.*;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.www.*;
/**
* Web User Info
*
* @author Jorg Janke
* @version $Id: WebUser.java,v 1.14 2003/04/28 04:20:25 jjanke Exp $
*/
public class WebUser
{
/**
* Get unconditional
* @param ctx context
* @param email email
* @return web user
*/
public static WebUser get (Properties ctx, String email)
{
return get (ctx, email, null);
} // get
/**
* Get unconditional
* @param ctx context
* @param email email
* @param password optional password
* @return web user
*/
public static WebUser get (Properties ctx, String email, String password)
{
if (s_cache != null && email != null && email.equals(s_cache.getEmail()))
{
// if password is null, don't check it
if (password == null || password.equals(s_cache.getPassword()))
return s_cache;
s_cache.setPasswordOK(false, null);
return s_cache;
}
s_cache = new WebUser (ctx, email, password);
return s_cache;
} // get
private static WebUser s_cache = null;
/*************************************************************************/
/** Attribute Name - also in JSPs */
public static final String NAME = "webUser";
/** Logging */
private Logger log = Logger.getLogger(getClass());
/**
* Load User with password
* @param ctx context
* @param email email
* @param password password
*/
private WebUser (Properties ctx, String email, String password)
{
m_ctx = ctx;
m_AD_Client_ID = Env.getContextAsInt(ctx, "#AD_Client_ID");
load (email, password);
} // WebUser
private Properties m_ctx;
//
private MBPartner m_bp;
private MBPartner_Contact m_bpc;
private MBPartner_Location m_bpl;
private MLocation m_loc;
//
private boolean m_passwordOK = false;
private String m_passwordMessage;
private String m_saveErrorMessage;
//
private int m_AD_Client_ID = 0;
private boolean m_loggedIn = false;
/**
* Load Contact
* @param email email
* @param password optional password
*/
private void load (String email, String password)
{
log.info("load " + email + ", AD_Client_ID=" + m_AD_Client_ID);
String sql = "SELECT * "
+ "FROM C_BPartner_Contact "
+ "WHERE AD_Client_ID=?"
+ " AND EMail=?";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, m_AD_Client_ID);
pstmt.setString(2, email);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
m_bpc = new MBPartner_Contact (m_ctx, rs);
log.debug("load - found BPC=" + m_bpc);
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.error("load", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
// Check Password
m_passwordOK = false;
if (m_bpc != null && password != null && password.equals(m_bpc.getPassword()))
m_passwordOK = true;
if (m_passwordOK || m_bpc == null)
m_passwordMessage = null;
else
setPasswordOK (false, password);
// Load BPartner
if (m_bpc != null)
{
m_bp = new MBPartner (m_ctx, m_bpc.getC_BPartner_ID ());
log.debug("load - found BP=" + m_bp);
}
else
m_bp = null;
// Load Loacation
if (m_bpc != null)
{
if (m_bpc.getC_BPartner_Location_ID() != 0)
{
m_bpl = new MBPartner_Location (m_ctx, m_bpc.getC_BPartner_Location_ID ());
log.debug("load - found BPL=" + m_bpl);
}
else
{
MBPartner_Location[] bpls = m_bp.getLocations();
if (bpls != null && bpls.length > 0)
{
m_bpl = bpls[0];
log.debug ("load - found BPL=" + m_bpl);
}
}
if (m_bpl != null)
{
m_loc = new MLocation (m_ctx, 0);
m_loc.load(m_bpl.getC_Location_ID());
log.debug ("load - found LOC=" + m_loc);
}
else
m_loc = null;
}
else
{
m_bpl = null;
m_loc = null;
}
// Make sure that all entities exist
if (m_bpc == null)
{
m_bpc = new MBPartner_Contact (m_ctx, 0, 0);
m_bpc.setEmail(email);
m_bpc.setPassword(password);
}
if (m_bp == null)
m_bp = new MBPartner (m_ctx); // template
if (m_bpl == null)
m_bpl = new MBPartner_Location (m_ctx, 0, 0);
if (m_loc == null)
{
m_loc = new MLocation (m_ctx, 0);
m_loc.load(0);
}
//
log.info("load - " + m_bp + " - " + m_bpc);
} // load
/**
* Return Valid.
* @return return true if found
*/
public boolean isValid()
{
if (m_bpc == null)
return false;
boolean ok = m_bpc.getC_BPartner_Contact_ID() != 0;
return ok;
} // isValid
/**
* Return Email Validation.
* @return return true if email is valid
*/
public boolean isEMailValid()
{
if (m_bpc == null || !WUtil.exists(getEmail()))
{
log.debug("isEMailValid - " + getEmail() + "bpc=" + m_bpc);
return false;
}
//
boolean ok = m_bpc.getC_BPartner_Contact_ID() != 0
&& m_bpc.isOnline()
&& m_bpc.isEMailValid();
if (!ok)
log.debug("isEMailValid - " + getEmail()
+ ", ID=" + m_bpc.getC_BPartner_Contact_ID()
+ ", Online=" + m_bpc.isOnline()
+ ", EMailValid=" + m_bpc.isEMailValid());
return ok;
} // isEMailValid
/**
* Return Email Validation and set error message
* @return return true if email is valid
*/
public boolean setEMailValid()
{
if (isEMailValid())
return true;
m_passwordMessage = "EMail invalid";
return false;
} // setEMailValid
/**
* Info
* @return info
*/
public String toString ()
{
StringBuffer sb = new StringBuffer("WebUser[");
sb.append(getEmail())
.append(",LoggedIn=").append(m_loggedIn)
.append(",").append(m_bpc)
.append(",PasswordOK=").append(m_passwordOK)
.append(",Valid=").append(isValid())
.append("]");
return sb.toString();
} // toString
/*************************************************************************/
/**
* Save BPartner Objects
* @return true if saved
*/
public boolean save()
{
m_saveErrorMessage = null;
log.info("save - BP.Value=" + m_bp.getValue() + ", Name=" + m_bp.getName());
try
{
// check if BPartner exists ***********************************
if (m_bp.getC_BPartner_ID() == 0)
{
String sql = "SELECT * FROM C_BPartner WHERE AD_Client_ID=? AND Value=?";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql);
pstmt.setInt (1, m_AD_Client_ID);
pstmt.setString (2, m_bp.getValue());
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
m_bp = new MBPartner (m_ctx, m_bpc.getC_BPartner_ID ());
log.debug ("save - BP loaded =" + m_bp);
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
log.error("save-check", e);
}
finally
{
try
{
if (pstmt != null)
pstmt.close ();
}
catch (Exception e)
{}
pstmt = null;
}
}
// save BPartner ***************************************
if (m_bp.getName () == null || m_bp.getName().length() == 0)
m_bp.setName (m_bpc.getName());
if (m_bp.getValue() == null || m_bp.getValue().length() == 0)
m_bp.setValue(m_bpc.getEmail());
log.debug ("save - BP=" + m_bp);
if (!m_bp.save ())
{
m_saveErrorMessage = "Could not save Business Partner";
return false;
}
// save Location ***************************************
log.debug ("save - LOC=" + m_loc);
m_loc.save ();
// save BP Location ***************************************
if (m_bpl.getC_BPartner_ID () != m_bp.getC_BPartner_ID())
m_bpl.setC_BPartner_ID (m_bp.getC_BPartner_ID());
if (m_bpl.getC_Location_ID () != m_loc.C_Location_ID)
m_bpl.setC_Location_ID (m_loc.C_Location_ID);
log.debug ("save - BPL=" + m_bpl);
if (!m_bpl.save ())
{
m_saveErrorMessage = "Could not save Location";
return false;
}
// save Contact ***************************************
if (m_bpc.getC_BPartner_ID () != m_bp.getC_BPartner_ID())
m_bpc.setC_BPartner_ID (m_bp.getC_BPartner_ID());
if (m_bpc.getC_BPartner_Location_ID () != m_bpl.getC_BPartner_Location_ID ())
m_bpc.setC_BPartner_Location_ID (m_bpl.getC_BPartner_Location_ID ());
log.debug ("save - BPC=" + m_bpc);
if (!m_bpc.save ())
{
m_saveErrorMessage = "Could not save Contact";
return false;
}
}
catch (Exception ex)
{
log.error("save", ex);
m_saveErrorMessage = ex.toString();
return false;
}
//
return true;
} // save
public void setSaveErrorMessage(String msg)
{
m_saveErrorMessage = msg;
}
public String getSaveErrorMessage()
{
return m_saveErrorMessage;
}
/*************************************************************************/
/**
* Get EMail address
* @return email address of contact
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -