📄 mlocation.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 Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.sql.*;
import java.util.*;
import java.math.*;
import java.io.Serializable;
import org.apache.log4j.Logger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.NamePair;
import org.compiere.util.KeyNamePair;
/**
* Address Loaction Lookup Model - not cached.
* <p>
* MLocation maintains a static cache of countries and regions
*
* @author Jorg Janke
* @version $Id: MLocation.java,v 1.7 2003/04/28 04:19:51 jjanke Exp $
*/
public final class MLocation extends Lookup
implements Serializable
{
/**
* Constructor
* @param ctx context
* @param WindowNo window no (to derive AD_Client/Org for new records)
*/
public MLocation(Properties ctx, int WindowNo)
{
m_ctx = ctx;
if (s_countries.size() == 0)
loadCountries(ctx);
m_WindowNo = WindowNo;
mCountry = s_mCountry;
C_Country_ID = mCountry.C_Country_ID;
mRegion = s_mRegion;
if (mRegion != null)
C_Region_ID = mRegion.C_Region_ID;
} // MLocation
/** Logger */
private Logger log = Logger.getLogger (getClass());
private static Logger s_log = Logger.getLogger (MLocation.class);
private Properties m_ctx;
private int m_WindowNo;
// Key
public int C_Location_ID = 0;
//
public boolean IsActive = true;
public String Address1 = "";
public String Address2 = "";
public String City = "";
public String Postal = "";
public String PostalAdd = "";
public int C_Country_ID = 0;
public int C_Region_ID = 0;
public String RegionName = "";
//
public MCountry mCountry;
public MRegion mRegion;
//
private static MCountry s_mCountry; // default
private static MRegion s_mRegion; // default
//
private static HashMap s_countries = new HashMap();
private static HashMap s_regions = new HashMap();
/**
* Get Display for Value (not cached)
* @param value Location_ID
* @return String Value
*/
public String getDisplay (Object value)
{
if (!containsKey (value))
return "<" + value.toString() + ">";
return toString();
} // getDisplay
/**
* Get Object of Key Value
* @param value value
* @return Object or null
*/
public NamePair get (Object value)
{
if (value == null)
return null;
if (!containsKey (value))
return null;
return new KeyNamePair (C_Location_ID, toString());
} // get
/**
* The Lookup contains the key (not cached)
* @param key Location_ID
* @return true if key known
*/
public boolean containsKey (Object key)
{
int intValue = 0;
if (key instanceof Integer)
intValue = ((Integer)key).intValue();
else if (key != null)
intValue = Integer.parseInt(key.toString());
//
return load (intValue);
} // containsKey
/**
* Get Address line 1
* @return address 1
*/
public String getAddress1()
{
return Address1;
} // getAddress1
/**
* Get Address line 2
* @return address 2
*/
public String getAddress2()
{
return Address2;
} // getAddress2
/**
* Get formatted City Region Postal line
* @return City, Region Postal
*/
public String getCityRegionPostal()
{
return parseCRP (mCountry);
} // getCityRegionPostal
/**
* Get City
* @return city
*/
public String getCity()
{
return City;
} // getCity
/**
* Get Postal
* @return Postal
*/
public String getPostal()
{
if (PostalAdd == null || PostalAdd.length() == 0)
return Postal;
else
return Postal + "-" + PostalAdd;
} // getPostal
/**
* Get Country Line
* @param onlyForeign if true only foreign country is returned
* @return country or null
*/
public String getCountry (boolean onlyForeign)
{
if (mCountry == null
|| onlyForeign && mCountry.C_Country_ID == s_mCountry.C_Country_ID)
return null;
return mCountry.Name;
} // getCountryLine
/**
* Return String representation
* @return String
*/
public String toString()
{
if (C_Location_ID == 0)
return ""; // Msg.getMsg("NotFound");
StringBuffer retStr = new StringBuffer(Address1);
if (!Address2.equals(""))
retStr.append(" ").append(Address2);
// City, Region, Postal
retStr.append(", ").append(parseCRP (mCountry));
//
return retStr.toString();
} // toString
/**
* Return String representation with CR at line end
* @return String
*/
public String toStringCR()
{
if (C_Location_ID == 0)
return ""; // Msg.getMsg("NotFound");
StringBuffer retStr = new StringBuffer(Address1);
if (!Address2.equals(""))
retStr.append("\n").append(Address2);
// City, Region, Postal
retStr.append("\n").append(parseCRP (mCountry));
// Add Country would come here
return retStr.toString();
} // toString
/**
* Return detailed String representation
* @return String
*/
public String toStringX()
{
StringBuffer sb = new StringBuffer("MLocation=[");
sb.append("ID=").append(C_Location_ID)
.append(toString())
.append("]");
return sb.toString();
} // toStringX
/**
* Load Location with ID
* @param Location_ID C_Location_ID
* @return true if loaded
*/
public boolean load (int Location_ID)
{
if (Location_ID == 0) // load default
{
Address1 = "";
Address2 = "";
City = "";
Postal = "";
PostalAdd = "";
mCountry = s_mCountry;
C_Country_ID = s_mCountry.C_Country_ID;
mRegion = s_mRegion;
if (mRegion != null)
C_Region_ID = mRegion.C_Region_ID;
C_Location_ID = 0;
return true;
}
if (Location_ID == C_Location_ID) // already loaded
return true;
String SQL = "SELECT IsActive,Address1,Address2,City," // 1..4
+ "C_Country_ID,C_Region_ID,Postal,Postal_Add, RegionName " // 5..9
+ "FROM C_Location "
+ "WHERE C_Location_ID=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(SQL);
pstmt.setInt(1, Location_ID);
ResultSet rs = pstmt.executeQuery();
if (!rs.next())
{
rs.close();
pstmt.close();
C_Location_ID = 0;
return false;
}
// read values
IsActive = rs.getString(1).equals("Y");
Address1 = rs.getString(2);
if (Address1 == null)
Address1 = "";
Address2 = rs.getString(3);
if (Address2 == null)
Address2 = "";
City = rs.getString(4);
if (City == null)
City = "";
//
C_Country_ID = rs.getInt(5);
C_Region_ID = rs.getInt(6);
//
Postal = rs.getString(7);
if (Postal == null)
Postal = "";
PostalAdd = rs.getString(8);
if (PostalAdd == null)
PostalAdd = "";
RegionName = rs.getString(9);
if (RegionName == null)
RegionName = "";
//
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.error("load - " + Location_ID, e);
C_Location_ID = 0;
return false;
}
C_Location_ID = Location_ID;
mCountry = (MCountry)s_countries.get (String.valueOf(C_Country_ID));
mRegion = (MRegion)s_regions.get (String.valueOf(C_Region_ID));
return true;
} // load
/**
* Load Location with ID if Business Partner Location
* @param C_BPartner_Location_ID Business Partner Location
* @return true if loaded
*/
public boolean loadBPLocation (int C_BPartner_Location_ID)
{
if (C_BPartner_Location_ID == 0) // load default
{
Address1 = "";
Address2 = "";
City = "";
Postal = "";
PostalAdd = "";
mCountry = s_mCountry;
C_Country_ID = s_mCountry.C_Country_ID;
mRegion = s_mRegion;
if (mRegion != null)
C_Region_ID = mRegion.C_Region_ID;
RegionName = "";
C_Location_ID = 0;
return true;
}
String SQL = "SELECT l.IsActive,Address1,Address2,City," // 1..4
+ " C_Country_ID,C_Region_ID,Postal,Postal_Add," // 5..8
+ " l.C_Location_ID, l.RegionName " // 9..10
+ "FROM C_BPartner_Location bpl"
+ " INNER JOIN C_Location l ON (bpl.C_Location_ID=l.C_Location_ID) "
+ "WHERE C_BPartner_Location_ID=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(SQL);
pstmt.setInt(1, C_BPartner_Location_ID);
ResultSet rs = pstmt.executeQuery();
if (!rs.next())
{
rs.close();
pstmt.close();
C_Location_ID = 0;
return false;
}
// read values
IsActive = rs.getString(1).equals("Y");
Address1 = rs.getString(2);
if (Address1 == null)
Address1 = "";
Address2 = rs.getString(3);
if (Address2 == null)
Address2 = "";
City = rs.getString(4);
if (City == null)
City = "";
//
C_Country_ID = rs.getInt(5);
C_Region_ID = rs.getInt(6);
//
Postal = rs.getString(7);
if (Postal == null)
Postal = "";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -