📄 mclientshare.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.model;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import org.compiere.util.*;
/**
* Client Share Info
*
* @author Jorg Janke
* @version $Id: MClientShare.java,v 1.2 2005/11/25 21:58:30 jjanke Exp $
*/
public class MClientShare extends X_AD_ClientShare
{
/**
* Is Table Client Level Only
* @param AD_Client_ID client
* @param AD_Table_ID table
* @return true if client level only (default false)
*/
public static boolean isClientLevelOnly (int AD_Client_ID, int AD_Table_ID)
{
Boolean share = isShared(AD_Client_ID, AD_Table_ID);
if (share != null)
return share.booleanValue();
return false;
} // isClientLevel
/**
* Is Table Org Level Only
* @param AD_Client_ID client
* @param AD_Table_ID table
* @return true if Org level only (default false)
*/
public static boolean isOrgLevelOnly (int AD_Client_ID, int AD_Table_ID)
{
Boolean share = isShared(AD_Client_ID, AD_Table_ID);
if (share != null)
return !share.booleanValue();
return false;
} // isOrgLevel
/**
* Is Table Shared for Client
* @param AD_Client_ID client
* @param AD_Table_ID table
* @return info or null
*/
private static Boolean isShared (int AD_Client_ID, int AD_Table_ID)
{
// Load
if (s_shares.isEmpty())
{
String sql = "SELECT AD_Client_ID, AD_Table_ID, ShareType "
+ "FROM AD_ClientShare WHERE ShareType<>'x' AND IsActive='Y'";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, null);
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
{
int Client_ID = rs.getInt(1);
int Table_ID = rs.getInt(2);
String key = Client_ID + "_" + Table_ID;
String ShareType = rs.getString(3);
if (ShareType.equals(SHARETYPE_ClientAllShared))
s_shares.put(key, Boolean.TRUE);
else if (ShareType.equals(SHARETYPE_OrgNotShared))
s_shares.put(key, Boolean.FALSE);
}
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
s_log.log (Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
if (s_shares.isEmpty()) // put in something
s_shares.put("0_0", Boolean.TRUE);
} // load
String key = AD_Client_ID + "_" + AD_Table_ID;
return s_shares.get(key);
} // load
/** Shared Info */
private static CCache<String,Boolean> s_shares
= new CCache<String,Boolean>("AD_ClientShare", 10, 120); // 2h
/** Logger */
private static CLogger s_log = CLogger.getCLogger (MClientShare.class);
/**************************************************************************
* Default Constructor
* @param ctx context
* @param AD_ClientShare_ID id
* @param trxName trx
*/
public MClientShare (Properties ctx, int AD_ClientShare_ID, String trxName)
{
super (ctx, AD_ClientShare_ID, trxName);
} // MClientShare
/**
* Load Constructor
* @param ctx context
* @param rs result set
* @param trxName trx
*/
public MClientShare (Properties ctx, ResultSet rs, String trxName)
{
super (ctx, rs, trxName);
} // MClientShare
/** The Table */
private M_Table m_table = null;
/**
* Is Client Level Only
* @return true if client level only (shared)
*/
public boolean isClientLevelOnly()
{
return getShareType().equals(SHARETYPE_ClientAllShared);
} // isClientLevelOnly
/**
* Is Org Level Only
* @return true if org level only (not shared)
*/
public boolean isOrgLevelOnly()
{
return getShareType().equals(SHARETYPE_OrgNotShared);
} // isOrgLevelOnly
/**
* Get Table model
* @return table
*/
public M_Table getTable()
{
if (m_table == null)
m_table = M_Table.get(getCtx(), getAD_Table_ID());
return m_table;
} // getTable
/**
* Get Table Name
* @return table name
*/
public String getTableName()
{
return getTable().getTableName();
} // getTableName
/**
* After Save
* @param newRecord new
* @param success success
* @return true
*/
protected boolean afterSave (boolean newRecord, boolean success)
{
if (isActive())
{
setDataToLevel();
listChildRecords();
}
return true;
} // afterSave
/**
* Set Data To Level
* @return info
*/
public String setDataToLevel()
{
String info = "-";
if (isClientLevelOnly())
{
StringBuffer sql = new StringBuffer("UPDATE ")
.append(getTableName())
.append(" SET AD_Org_ID=0 WHERE AD_Org_ID<>0 AND AD_Client_ID=?");
int no = DB.executeUpdate(sql.toString(), getAD_Client_ID(), get_TrxName());
info = getTableName() + " set to Shared #" + no;
log.info(info);
}
else if (isOrgLevelOnly())
{
StringBuffer sql = new StringBuffer("SELECT COUNT(*) FROM ")
.append(getTableName())
.append(" WHERE AD_Org_ID=0 WHERE AD_Client_ID=?");
int no = DB.getSQLValue(get_TrxName(), sql.toString(), getAD_Client_ID());
info = getTableName() + " Shared records #" + no;
log.info(info);
}
return info;
} // setDataToLevel
/**
* List Child Tables
* @return child tables
*/
public String listChildRecords()
{
StringBuffer info = new StringBuffer();
String sql = "SELECT AD_Table_ID, TableName "
+ "FROM AD_Table t "
+ "WHERE AccessLevel=3 AND IsView='N'"
+ " AND EXISTS (SELECT * FROM AD_Column c "
+ "WHERE t.AD_Table_ID=c.AD_Table_ID"
+ " AND c.IsParent='Y'"
+ " AND c.ColumnName=(SELECT ColumnName FROM AD_Column cc "
+ "WHERE cc.IsKey='Y' AND cc.AD_Table_ID=?))";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, null);
pstmt.setInt (1, getAD_Table_ID());
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
{
int AD_Table_ID = rs.getInt(1);
String TableName = rs.getString(2);
if (info.length() != 0)
info.append(", ");
info.append(TableName);
}
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log (Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
log.info(info.toString());
return info.toString();
} // listChildRecords
/**
* Before Save
* @param newRecord new
* @return true
*/
protected boolean beforeSave (boolean newRecord)
{
if (getAD_Org_ID() != 0)
setAD_Org_ID(0);
return true;
} // beforeSave
} // MClientShare
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -