/** * Is standard Period closed for all Document Base Types * * @param ctx context for AD_Client * @param DateAcct accounting date * @return true if closed */ public static boolean isClosed(Ctx ctx, Timestamp DateAcct) { if (DateAcct == null) return false; MAcctSchema as = MClient.get(ctx, ctx.getAD_Client_ID()).getAcctSchema(); if (as.isAutoPeriodControl()) return !as.isAutoPeriodControlOpen(DateAcct); // Get all Calendars in line with Organizations MClientInfo cInfo = MClientInfo.get(ctx, ctx.getAD_Client_ID(), null); ArrayList<Integer> calendars = new ArrayList<Integer>(); MOrg[] orgs = MOrg.getOfClient(cInfo); for (MOrg org : orgs) { MOrgInfo info = MOrgInfo.get(ctx, org.getAD_Org_ID(), null); int C_Calendar_ID = info.getC_Calendar_ID(); if (C_Calendar_ID == 0) C_Calendar_ID = cInfo.getC_Calendar_ID(); if (!calendars.contains(C_Calendar_ID)) calendars.add(C_Calendar_ID); } // Should not happen if (calendars.size() == 0) throw new IllegalArgumentException("@NotFound@ @C_Calendar_ID@"); // For all Calendars get Periods for (int i = 0; i < calendars.size(); i++) { int C_Calendar_ID = calendars.get(i); MPeriod period = MPeriod.getOfCalendar(ctx, C_Calendar_ID, DateAcct); // Period not found if (period == null) return false; if (!period.isClosed()) return false; } return true; // closed } // isClosed
public ChangeVO updateRow( int windowNo, int AD_Tab_ID, int queryResultID, int relRowNo, Map<String, String> context, boolean force) { if (context == null || context.size() == 0) return new ChangeVO(true, Msg.translate(m_context, "NoContext")); ArrayList<String[]> data = m_results.get(queryResultID); if (data == null || data.size() == 0) return new ChangeVO(true, Msg.translate(m_context, "CachedDataNotFound")); UITab tab = getTab(AD_Tab_ID); if (tab == null) { log.config("Not found AD_Tab_ID=" + AD_Tab_ID); return new ChangeVO(true, Msg.translate(m_context, "@NotFound@ @AD_Tab_ID@=" + AD_Tab_ID)); } CContext ctx = new CContext(m_context.entrySet()); ctx.addWindow(windowNo, context); ChangeVO retValue; if (force) retValue = tab.saveRow(ctx, windowNo, false, null); else retValue = tab.saveRow(ctx, windowNo, false, data.get(relRowNo)); if (retValue.hasError()) return retValue; // Update Results String[] dataRow = retValue.rowData.clone(); data.set(relRowNo, dataRow); postProcessChangeVO(retValue, windowNo, context, dataRow, tab); retValue.trxInfo = GridTab.getTrxInfo(tab.getTableName(), ctx, windowNo, tab.getTabNo()); if (retValue.isRefreshAll()) {} return retValue; }
/** * Get Field Lookup Value Direct * * @param windowNo Window * @param AD_Field_ID * @param keyValues array of id values * @param cache * @return list of display values */ public ArrayList<NamePair> getLookupValueDirect( int AD_Field_ID, ArrayList<String> keyValues, boolean cache) { int windowNo = 0; // No Context ArrayList<NamePair> displayValues = new ArrayList<NamePair>(); UIField field = getField(AD_Field_ID, windowNo); // if (cache && field.isLookup()) // field.getLookup().removeAllElements(); if (field == null) log.warning("Cannot find AD_Field_ID=" + AD_Field_ID); // for (int i = 0; i < keyValues.size(); i++) { String key = keyValues.get(i); String value = null; if (field != null) value = field.getLookupDisplay(m_context, windowNo, key, cache); if (value == null) { /* * if(key == null) value = ""; else value = "<" + key + ">"; */ value = ""; } NamePair pp = new ValueNamePair(key, value); displayValues.add(pp); } return displayValues; } // getLookupValueDirect
/** * Save (Insert new) Row of Tab * * @param windowNo relative window * @param AD_Tab_ID tab * @param curRow insert after relative row number in results * @param context current (relevant) context of new row * @return error message or null */ public ChangeVO insertRow( int windowNo, int AD_Tab_ID, int queryResultID, int curRow, Map<String, String> context) { if (context == null || context.size() == 0) return new ChangeVO(true, "No Context"); UITab tab = getTab(AD_Tab_ID); if (tab == null) { log.config("Not found AD_Tab_ID=" + AD_Tab_ID); return new ChangeVO(true, "@NotFound@ @AD_Tab_ID@=" + AD_Tab_ID); } log.info("Line Amt:" + context.get("LineNetAmt")); CContext ctx = new CContext(m_context.entrySet()); ctx.addWindow(windowNo, context); ChangeVO retValue = tab.saveRow(ctx, windowNo, true); if (retValue.hasError()) return retValue; // Update Results ArrayList<String[]> data = m_results.get(queryResultID); if (data == null) retValue.addError("Data Not Found"); else { String[] dataRow = retValue.rowData; if (curRow >= data.size()) data.add(dataRow); else data.add(curRow, dataRow); retValue.trxInfo = GridTab.getTrxInfo(tab.getTableName(), ctx, windowNo, tab.getTabNo()); } return retValue; } // insertRow
public String[][] getTabSearchResults(int searchResultID, int rowCount) { ArrayList<String[]> matches = m_results.get(searchResultID); if (matches != null) { if (matches.size() < rowCount) { rowCount = matches.size(); } String[][] result = new String[rowCount][]; int i = 0; for (String[] row : matches) { result[i++] = row; if (i == rowCount) break; } return result; } else { return new String[0][]; } }
/** * Execute Query for Tab * * @param AD_Tab_ID tab * @param queryVO optional query * @param context record context for link columns and other variables * @param queryResultID stored query identifier provided by client * @return number of records or -1 if error */ public int executeQuery( int AD_Tab_ID, QueryVO queryVO, HashMap<String, String> context, int queryResultID) { UITab tab = getTab(AD_Tab_ID); if (tab == null) { log.config("Not found AD_Tab_ID=" + AD_Tab_ID); return -1; } ArrayList<String[]> result = tab.executeQueryString(queryVO, context, m_context); if (result == null) { log.config("Not Result for AD_Tab_ID=" + AD_Tab_ID); return -1; } MRole role = getRole(); // return -1 to indicate query exceeds if (role.isQueryMax(result.size())) { m_results.put(queryResultID, new ArrayList<String[]>()); return -1; } m_results.put(queryResultID, result); return result.size(); } // executeQuery
/** Fill Tab and Field arrays */ private void fillTabsFieldsAndInitFieldsAndCreateDependencyRelations(UIWindow win, int windowNO) { ArrayList<UITab> tabs = win.getTabs(); for (int j = 0; j < tabs.size(); j++) { UITab winTab = tabs.get(j); Integer tabKey = Integer.valueOf(winTab.getAD_Tab_ID()); Integer ReferencetabKey = Integer.valueOf(winTab.getReferenced_Tab_ID()); m_tabs.put(tabKey, winTab); m_referencetabs.put(ReferencetabKey, winTab); // ArrayList<UIField> fields = winTab.getFields(); for (int k = 0; k < fields.size(); k++) { UIField field = fields.get(k); field.initialize(m_context, windowNO); Integer fieldKey = Integer.valueOf(field.getAD_Field_ID()); // set the correct value if (field.isLookup()) field.getLookup().setContext(m_context, windowNO); m_fields.put(fieldKey, field); } winTab.createDependencyRelations(); } } // fillTabsFields
/** * gets all Periods in the Range * * @param startPeriod * @param endPeriod * @param calendar_ID * @return MPeriod[] */ public static MPeriod[] getAllPeriodsInRange( MPeriod startPeriod, MPeriod endPeriod, int calendar_ID, Ctx ctx, Trx trx) { if ((startPeriod.getC_Calendar_ID() != calendar_ID) || (endPeriod.getC_Calendar_ID() != calendar_ID)) { log.saveError("Error", "Periods do not belong to the calendar"); return null; } ArrayList<MPeriod> periods = new ArrayList<MPeriod>(); String sql = "SELECT * FROM C_Period WHERE " + "C_Period.IsActive='Y' AND PeriodType='S' " + "AND C_Period.C_Year_ID IN " + "(SELECT C_Year_ID FROM C_Year WHERE C_Year.C_Calendar_ID = ? ) " + // calendar_ID "AND ((C_Period.C_Year_ID * 1000) + C_Period.PeriodNo) BETWEEN" + " (? * 1000 + ?) AND (? * 1000 + ? )" + // start Period year ID, Period Number , End Period Year ID, Period Number " ORDER BY C_Period.C_Year_ID ASC, C_Period.PeriodNo ASC"; PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = DB.prepareStatement(sql, trx); pstmt.setInt(1, calendar_ID); pstmt.setInt(2, startPeriod.getC_Year_ID()); pstmt.setInt(3, startPeriod.getPeriodNo()); pstmt.setInt(4, endPeriod.getC_Year_ID()); pstmt.setInt(5, endPeriod.getPeriodNo()); rs = pstmt.executeQuery(); while (rs.next()) periods.add(new MPeriod(ctx, rs, trx)); } catch (Exception e) { s_log.log(Level.SEVERE, sql, e); } finally { DB.closeResultSet(rs); DB.closeStatement(pstmt); } MPeriod[] retValue = new MPeriod[periods.size()]; periods.toArray(retValue); return retValue; }
/** * Get Field Lookup Value Direct * * @param windowNo Window * @param AD_Field_ID * @param keyValues array of id values * @param cache * @return list of display values */ public ArrayList<String> getLookupValueOnlyDirect( int AD_Field_ID, ArrayList<String> keyValues, boolean cache) { int windowNo = 0; // No Context ArrayList<String> displayValues = new ArrayList<String>(); UIField field = getField(AD_Field_ID, windowNo); if (field == null) log.warning("Cannot find AD_Field_ID=" + AD_Field_ID); // for (int i = 0; i < keyValues.size(); i++) { String key = keyValues.get(i); String value = null; if (field != null) value = field.getLookupDisplay(m_context, windowNo, key, cache); if (value == null) { /* * if(key == null) value = ""; else value = "<" + key + ">"; */ value = ""; } displayValues.add(value); } return displayValues; } // getLookupValueDirect
/** * Get Period Control * * @param requery requery * @return period controls */ public MPeriodControl[] getPeriodControls(boolean requery) { if (m_controls != null && !requery) return m_controls; // ArrayList<MPeriodControl> list = new ArrayList<MPeriodControl>(); String sql = "SELECT * FROM C_PeriodControl " + "WHERE C_Period_ID=?"; PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = DB.prepareStatement(sql, get_Trx()); pstmt.setInt(1, getC_Period_ID()); rs = pstmt.executeQuery(); while (rs.next()) list.add(new MPeriodControl(getCtx(), rs, get_Trx())); } catch (Exception e) { log.log(Level.SEVERE, sql, e); } finally { DB.closeResultSet(rs); DB.closeStatement(pstmt); } m_controls = new MPeriodControl[list.size()]; list.toArray(m_controls); return m_controls; } // getPeriodControls
/** * Retrieve results for Tab. If the from/to range does not exist, it returns existing rows * * @param queryResultID stored query identifier provided by client * @param fromRow from row first is 0 * @param noRows number of rows * @return array of rows of array of field values or null if error. You get the columnNames via * String[] columns = uiTab.getColumnNames(); */ public String[][] getResults(int queryResultID, int fromRow, int noRows) { if (noRows < 0) { log.config("Invalid: fromRow=" + fromRow + ",noRows" + noRows); } else if (noRows == 0) return new String[][] {}; // ArrayList<String[]> resultAll = m_results.get(queryResultID); if (resultAll == null) { log.config("No Results for queryResultID=" + queryResultID); return null; } if (resultAll.size() < fromRow) { log.config( "Insufficient Results for queryResultID=" + queryResultID + ", Length=" + resultAll.size() + ", fromRow=" + fromRow); return null; } // copy if (resultAll.size() < noRows) { log.config( "Insufficient Rows for queryResultID=" + queryResultID + ", Length=" + resultAll.size() + ", fromRow=" + fromRow + ", noRows=" + noRows); noRows = resultAll.size(); } String[][] result = new String[noRows][]; for (int i = 0; i < noRows; i++) { int index = i + fromRow; if (index >= resultAll.size()) break; result[i] = resultAll.get(index); } return result; } // getResult
/** * Is standard Period Open for specified orgs for the client. For best performance, ensure that * the list of orgs does not contain duplicates. * * @param ctx * @param AD_Client_ID * @param orgs * @param DateAcct accounting date * @param DocBaseType document base type * @return error message or null */ public static String isOpen( Ctx ctx, int AD_Client_ID, ArrayList<Integer> orgs, Timestamp DateAcct, String DocBaseType) { if (DateAcct == null) return "@NotFound@ @DateAcct@"; if (DocBaseType == null) return "@NotFound@ @DocBaseType@"; MAcctSchema as = MClient.get(ctx, AD_Client_ID).getAcctSchema(); if (as == null) return "@NotFound@ @C_AcctSchema_ID@ for AD_Client_ID=" + AD_Client_ID; if (as.isAutoPeriodControl()) { if (as.isAutoPeriodControlOpen(DateAcct)) return null; else return "@PeriodClosed@ - @AutoPeriodControl@"; } // Get all Calendars in line with Organizations MClientInfo clientInfo = MClientInfo.get(ctx, AD_Client_ID, null); ArrayList<Integer> orgCalendars = new ArrayList<Integer>(); ArrayList<Integer> calendars = new ArrayList<Integer>(); for (int org : orgs) { MOrgInfo orgInfo = MOrgInfo.get(ctx, org, null); int C_Calendar_ID = orgInfo.getC_Calendar_ID(); if (C_Calendar_ID == 0) C_Calendar_ID = clientInfo.getC_Calendar_ID(); orgCalendars.add(C_Calendar_ID); if (!calendars.contains(C_Calendar_ID)) calendars.add(C_Calendar_ID); } // Should not happen if (calendars.size() == 0) return "@NotFound@ @C_Calendar_ID@"; // For all Calendars get Periods for (int i = 0; i < calendars.size(); i++) { int C_Calendar_ID = calendars.get(i); MPeriod period = MPeriod.getOfCalendar(ctx, C_Calendar_ID, DateAcct); // First Org for Calendar int AD_Org_ID = 0; for (int j = 0; j < orgCalendars.size(); j++) { if (orgCalendars.get(j) == C_Calendar_ID) { AD_Org_ID = orgs.get(j); break; } } if (period == null) { MCalendar cal = MCalendar.get(ctx, C_Calendar_ID); String date = DisplayType.getDateFormat(DisplayTypeConstants.Date).format(DateAcct); if (cal != null) return "@NotFound@ @C_Period_ID@: " + date + " - " + MOrg.get(ctx, AD_Org_ID).getName() + " -> " + cal.getName(); else return "@NotFound@ @C_Period_ID@: " + date + " - " + MOrg.get(ctx, AD_Org_ID).getName() + " -> C_Calendar_ID=" + C_Calendar_ID; } String error = period.isOpen(DocBaseType, DateAcct); if (error != null) return error + " - " + MOrg.get(ctx, AD_Org_ID).getName() + " -> " + MCalendar.get(ctx, C_Calendar_ID).getName(); } return null; // open } // isOpen
// Method to return a list of matches according to fields for a tab, using // the cached results // this does not store the result in the cache - this behavior is deferred // to the caller private int searchTabResults( int WindowNo, UITab tab, List<Integer> fieldIds, int queryResultID, int searchResultID, String query, int rowCount) { ArrayList<String[]> results = m_results.get(queryResultID); if (query.trim().equals("")) { m_results.put(searchResultID, results); return results.size(); } ScoreStrategy scorer = new ScoreStrategy(query); ScoreCell[] scores = new ScoreCell[results.size()]; // first initialize score cells int j = 0; for (String[] result : results) { // initialize score cells ScoreCell score = new ScoreCell(); score.row = result; score.score = scorer.createScore(); scores[j++] = score; } for (int id : fieldIds) { UIField field = getField(id, WindowNo); final int idx = tab.getFieldIndex(id); if (field.isLookup()) { ArrayList<String> fieldValues = new ArrayList<String>(results.size()); for (String[] row : results) { fieldValues.add(row[idx]); } ArrayList<String> sorts = getLookupValueOnlyDirect(id, fieldValues, true); int i = 0; for (String value : sorts) { scorer.getScore(value, scores[i].score); i++; } } else { int i = 0; for (String[] row : results) { String value = row[idx]; scorer.getScore(value, scores[i].score); i++; } } } ArrayList<ScoreCell> matchingScores = new ArrayList<ScoreCell>(); for (ScoreCell cell : scores) { if (cell.score.isMatch) { matchingScores.add(cell); } } Collections.sort(matchingScores, scorer); ArrayList<String[]> matches = new ArrayList<String[]>(); for (ScoreCell score : matchingScores) { matches.add(score.row); } m_results.put(searchResultID, matches); return matches.size(); }
public void sortResults( int WindowNo, int AD_Tab_ID, int AD_Field_ID, int queryResultID, final boolean ascending) { class SortCell { String[] row; String sort; } ArrayList<String[]> results = m_results.get(queryResultID); if (results == null) log.severe("cannot sort. results non-existent for queryResultID:" + queryResultID); UITab tab = getTab(AD_Tab_ID); final UIField field = getField(AD_Field_ID, WindowNo); final int displayType = field.getAD_Reference_ID(); final int idx = tab.getFieldIndex(AD_Field_ID); // if not a lookup, directly sort if (!field.isLookup()) { if (FieldType.isNumeric(displayType)) { Collections.sort( results, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { if (o1[idx] == null) o1[idx] = ""; if (o2[idx] == null) o2[idx] = ""; BigDecimal s1 = new BigDecimal(o1[idx].equals("") ? "-1e-10" : o1[idx]); BigDecimal s2 = new BigDecimal(o2[idx].equals("") ? "-1e-10" : o2[idx]); return ascending ? s1.compareTo(s2) : s2.compareTo(s1); } }); } else if (FieldType.isDate(displayType)) { Collections.sort( results, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { if (o1[idx] == null) o1[idx] = ""; if (o2[idx] == null) o2[idx] = ""; Long s1 = new Long(o1[idx].equals("") ? "-1000000" : o1[idx]); Long s2 = new Long(o2[idx].equals("") ? "-1000000" : o2[idx]); return ascending ? s1.compareTo(s2) : s2.compareTo(s1); } }); } else { Collections.sort( results, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { if (o1[idx] == null) o1[idx] = ""; if (o2[idx] == null) o2[idx] = ""; String s1 = o1[idx]; String s2 = o2[idx]; return ascending ? s1.compareTo(s2) : s2.compareTo(s1); } }); } return; } Comparator<SortCell> c = new Comparator<SortCell>() { public int compare(SortCell o1, SortCell o2) { if (ascending) return o1.sort.compareTo(o2.sort); else return o2.sort.compareTo(o1.sort); } }; // for look up, first get id values ArrayList<String> fieldValues = new ArrayList<String>(results.size()); for (String[] row : results) { fieldValues.add(row[idx]); } // then translate into real values ArrayList<String> sorts = getLookupValueOnlyDirect(AD_Field_ID, fieldValues, true); ArrayList<SortCell> toBeSorteds = new ArrayList<SortCell>(sorts.size()); for (int i = 0; i < sorts.size(); i++) { SortCell toBeSorted = new SortCell(); toBeSorted.row = results.get(i); toBeSorted.sort = sorts.get(i); toBeSorteds.add(toBeSorted); } // sort Collections.sort(toBeSorteds, c); // after sorting, replace col with original values int i = 0; for (SortCell toBeSorted : toBeSorteds) { results.set(i, toBeSorted.row); i++; } }