use of org.compiere.util.NamePair in project adempiere by adempiere.
the class PrintDataElement method toStringX.
// hasKey
/**
* String representation with key info
* @return info
*/
public String toStringX() {
if (m_value instanceof NamePair) {
NamePair pp = (NamePair) m_value;
StringBuffer sb = new StringBuffer(m_columnName);
sb.append("(").append(pp.getID()).append(")").append("=").append(pp.getName());
if (m_isPKey)
sb.append("(PK)");
return sb.toString();
} else
return toString();
}
use of org.compiere.util.NamePair in project adempiere by adempiere.
the class MLookup method getData.
// getData
/**
* Return data as Array containing Value/KeyNamePair
* @param mandatory if not mandatory, an additional empty value is inserted
* @param onlyValidated only validated
* @param onlyActive only active
* @param temporary force load for temporary display
* @return list
*/
public ArrayList<Object> getData(boolean mandatory, boolean onlyValidated, boolean onlyActive, boolean temporary) {
// create list
ArrayList<Object> list = getData(onlyValidated, true);
// Remove inactive choices
if (onlyActive && m_hasInactive) {
// list from the back
for (int i = list.size(); i > 0; i--) {
Object o = list.get(i - 1);
if (o != null) {
String s = o.toString();
if (s.startsWith(INACTIVE_S) && s.endsWith(INACTIVE_E))
list.remove(i - 1);
}
}
}
// Add Optional (empty) selection
if (!mandatory) {
NamePair p = null;
if (m_info.KeyColumn != null && m_info.KeyColumn.endsWith("_ID"))
p = new KeyNamePair(-1, "");
else
p = new ValueNamePair("", "");
list.add(0, p);
}
return list;
}
use of org.compiere.util.NamePair in project adempiere by adempiere.
the class MLookup method get.
// loadComplete
/**
* Get value (name) for key.
* If not found return null;
* @param key key (Integer for Keys or String for Lists)
* @return value
*/
public NamePair get(Object key) {
if (// indicator for null
key == null || MINUS_ONE.equals(key))
return null;
//auto refresh parent lookup
if (m_info.IsParent) {
if (m_nextRead > 0 && m_nextRead < System.currentTimeMillis()) {
m_lookup.clear();
if (m_lookupDirect != null)
m_lookupDirect.clear();
}
// 5 sec
m_nextRead = System.currentTimeMillis() + 5000;
}
// try cache
NamePair retValue = (NamePair) m_lookup.get(key);
if (retValue != null)
return retValue;
// Not found and waiting for loader
if (m_loader != null && m_loader.isAlive()) {
log.finer((m_info.KeyColumn == null ? "ID=" + m_info.Column_ID : m_info.KeyColumn) + ": waiting for Loader");
loadComplete();
// is most current
retValue = (NamePair) m_lookup.get(key);
if (retValue != null)
return retValue;
}
// Always check for parents - not if we SQL was validated and completely loaded
if (!m_info.IsParent && m_info.IsValidated && m_allLoaded) {
log.finer(// + "(" + key.getClass()
m_info.KeyColumn + ": <NULL> - " + key + "; Size=" + m_lookup.size());
// cache locally
return getDirect(key, false, true);
}
log.finest(m_info.KeyColumn + ": " + key + "; Size=" + m_lookup.size() + "; Validated=" + m_info.IsValidated + "; All Loaded=" + m_allLoaded + "; HasInactive=" + m_hasInactive);
// never loaded
if (!m_allLoaded && m_lookup.size() == 0 && !m_info.IsCreadedUpdatedBy && !m_info.IsParent && getDisplayType() != DisplayType.Search) {
m_loader = new MLoader();
// sync!
m_loader.run();
retValue = (NamePair) m_lookup.get(key);
if (retValue != null)
return retValue;
}
// Try to get it directly
boolean cacheLocal = m_info.IsValidated;
// do NOT cache
return getDirect(key, false, cacheLocal);
}
use of org.compiere.util.NamePair in project adempiere by adempiere.
the class MLookup method getDirect.
/**
* Get Data Direct from Table.
* @param key key
* @param saveInCache save in cache for r/w
* @param cacheLocal cache locally for r/o
* @return value
*/
public NamePair getDirect(Object key, boolean saveInCache, boolean cacheLocal) {
// Nothing to query
if (key == null || m_info.QueryDirect == null || m_info.QueryDirect.length() == 0)
return null;
if (key.equals(m_directNullKey))
return null;
//
NamePair directValue = null;
if (// Lookup cache
m_lookupDirect != null) {
directValue = (NamePair) m_lookupDirect.get(key);
if (directValue != null)
return directValue;
}
log.finer(m_info.KeyColumn + ": " + key + ", SaveInCache=" + saveInCache + ",Local=" + cacheLocal);
boolean isNumber = m_info.KeyColumn.endsWith("_ID");
try {
// SELECT Key, Value, Name FROM ...
PreparedStatement pstmt = DB.prepareStatement(m_info.QueryDirect, null);
if (isNumber)
pstmt.setInt(1, Integer.parseInt(key.toString()));
else
pstmt.setString(1, key.toString());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
String name = rs.getString(3);
if (isNumber) {
int keyValue = rs.getInt(1);
KeyNamePair p = new KeyNamePair(keyValue, name);
if (// save if
saveInCache)
m_lookup.put(new Integer(keyValue), p);
directValue = p;
} else {
String value = rs.getString(2);
ValueNamePair p = new ValueNamePair(value, name);
if (// save if
saveInCache)
m_lookup.put(value, p);
directValue = p;
}
if (rs.next())
log.log(Level.SEVERE, m_info.KeyColumn + ": Not unique (first returned) for " + key + " SQL=" + m_info.QueryDirect);
} else {
m_directNullKey = key;
directValue = null;
}
rs.close();
pstmt.close();
if (CLogMgt.isLevelFinest())
log.finest(m_info.KeyColumn + ": " + directValue + " - " + m_info);
} catch (Exception e) {
log.log(Level.SEVERE, m_info.KeyColumn + ": SQL=" + m_info.QueryDirect + "; Key=" + key, e);
directValue = null;
}
// Cache Local if not added to R/W cache
if (cacheLocal && !saveInCache && directValue != null) {
if (m_lookupDirect == null)
m_lookupDirect = new HashMap<Object, Object>();
m_lookupDirect.put(key, directValue);
}
m_hasInactive = true;
return directValue;
}
use of org.compiere.util.NamePair in project adempiere by adempiere.
the class MLocatorLookup method getDirect.
// containsKey
/**
* Get Data Direct from Table
* @param keyValue integer key value
* @param saveInCache save in cache
* @param trxName transaction
* @return Object directly loaded
*/
public NamePair getDirect(Object keyValue, boolean saveInCache, String trxName) {
MLocator loc = getMLocator(keyValue, trxName);
if (loc == null)
return null;
//
int key = loc.getM_Locator_ID();
if (saveInCache)
m_lookup.put(new Integer(key), loc);
NamePair retValue = new KeyNamePair(key, loc.toString());
return retValue;
}
Aggregations