diff --git a/org.adempiere.base/src/org/compiere/model/MArchive.java b/org.adempiere.base/src/org/compiere/model/MArchive.java index 11a3189c40..30eafa656c 100644 --- a/org.adempiere.base/src/org/compiere/model/MArchive.java +++ b/org.adempiere.base/src/org/compiere/model/MArchive.java @@ -48,7 +48,7 @@ public class MArchive extends X_AD_Archive { /** * */ - private static final long serialVersionUID = 6934821005476123632L; + private static final long serialVersionUID = -6343913337999164991L; /** * Get Archives @@ -147,7 +147,8 @@ public class MArchive extends X_AD_Archive { setC_BPartner_ID(info.getC_BPartner_ID()); } // MArchive - public MStorageProvider provider; + protected MStorageProvider provider; + /** * Get the isStoreArchiveOnFileSystem and archivePath for the client. * @@ -175,8 +176,12 @@ public class MArchive extends X_AD_Archive { return sb.toString(); } // toString - public byte[] getBinaryData() { - + /** + * Get data as byte[] from storage provider + * + * @return byte[] or null + */ + public byte[] getBinaryData() { IArchiveStore prov = provider.getArchiveStore(); if (prov != null) return prov.loadLOBData(this,provider); @@ -196,7 +201,7 @@ public class MArchive extends X_AD_Archive { } // getInputStream /** - * Save Binary Data to file system or db. + * Save Binary Data through storage provider * * @param inflatedData * inflated data @@ -258,10 +263,20 @@ public class MArchive extends X_AD_Archive { return path.toString(); } + /** + * Get byte data from BinaryData column. + * Usually, your code should call getBinaryData() instead (using provider). + * @return byte[] + */ public byte[] getByteData(){ return super.getBinaryData(); } + /** + * Store byte data to BinaryData column. + * Usually, your code should call setBinaryData() instead (using provider). + * @param BinaryData + */ public void setByteData(byte[] BinaryData){ super.setBinaryData(BinaryData); } @@ -369,4 +384,60 @@ public class MArchive extends X_AD_Archive { return destZipFile; } + /** + * Get number of document and report archive by table and record id + * + * @param AD_Table_ID + * @param Record_ID + * @param trxName + * @return int[], [0] = report count and [1] = document count + */ + public static int[] getReportAndDocumentCountByRecordId(int AD_Table_ID, int Record_ID, String trxName) { + int reportCount = 0; + int documentCount = 0; + StringBuilder sql = new StringBuilder("SELECT IsReport, COUNT(*) FROM AD_Archive ") + .append("WHERE (AD_Table_ID=? AND Record_ID=?) "); + if (AD_Table_ID == MBPartner.Table_ID) + sql.append(" OR C_BPartner_ID=?"); + sql.append(" GROUP BY IsReport"); + PreparedStatement pstmt = null; + ResultSet rs = null; + try + { + pstmt = DB.prepareStatement (sql.toString(), trxName); + pstmt.setInt(1, AD_Table_ID); + pstmt.setInt(2, Record_ID); + if (AD_Table_ID == MBPartner.Table_ID) + pstmt.setInt(3, Record_ID); + rs = pstmt.executeQuery (); + while (rs.next ()) + { + if ("Y".equals(rs.getString(1))) + reportCount += rs.getInt(2); + else + documentCount += rs.getInt(2); + } + } + catch (Exception e) + { + throw new AdempiereException(e); + } + finally + { + DB.close(rs, pstmt); + } + return new int[] {reportCount, documentCount}; + } + + /** + * Get number of report archive by table id + * + * @param AD_Table_ID + * @param trxName + * @return Number of report archive for AD_Table_ID + */ + public static int getReportCountByTableId(int AD_Table_ID, String trxName) { + String sql = "SELECT COUNT(*) FROM AD_Archive WHERE AD_Table_ID=? AND IsReport='Y'"; + return DB.getSQLValueEx(trxName, sql, AD_Table_ID); + } } // MArchive diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/WArchive.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/WArchive.java index c51d8860cd..51e6fb2b67 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/WArchive.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/WArchive.java @@ -1,17 +1,39 @@ +/*********************************************************************** + * This file is part of iDempiere ERP Open Source * + * http://www.idempiere.org * + * * + * Copyright (C) Contributors * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 2 * + * of the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * + * MA 02110-1301, USA. * + * * + * Contributors: * + * - hengsin * + **********************************************************************/ package org.adempiere.webui; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.util.logging.Level; import org.adempiere.webui.apps.form.WArchiveViewer; import org.adempiere.webui.component.Window; import org.adempiere.webui.panel.ADForm; import org.adempiere.webui.session.SessionManager; +import org.compiere.model.MArchive; import org.compiere.model.MBPartner; import static org.compiere.model.SystemIDs.*; import org.compiere.util.CLogger; -import org.compiere.util.DB; import org.compiere.util.Env; import org.compiere.util.Msg; import org.zkoss.zk.ui.Component; @@ -55,23 +77,19 @@ public class WArchive implements EventListener private Menuitem m_reports = null; private Menuitem m_reportsAll = null; private Menuitem m_documents = null; -// private JPopupMenu m_popup = new JPopupMenu("ArchiveMenu"); /** Where Clause */ - StringBuffer m_where = null; + protected StringBuffer m_where = null; /** Logger */ private static final CLogger log = CLogger.getCLogger (WArchive.class); /** - * Display Request Options - New/Existing. + * Display archive menu * @param invoker button */ private void getArchives(Component invoker) { - int reportCount = 0; - int documentCount = 0; - m_where = new StringBuffer(); m_where.append("(AD_Table_ID=").append(m_AD_Table_ID) .append(" AND Record_ID=").append(m_Record_ID) @@ -80,39 +98,9 @@ public class WArchive implements EventListener if (m_AD_Table_ID == MBPartner.Table_ID) m_where.append(" OR C_BPartner_ID=").append(m_Record_ID); // - StringBuffer sql = new StringBuffer("SELECT IsReport, COUNT(*) FROM AD_Archive ") - .append("WHERE (AD_Table_ID=? AND Record_ID=?) "); - if (m_AD_Table_ID == MBPartner.Table_ID) - sql.append(" OR C_BPartner_ID=?"); - sql.append(" GROUP BY IsReport"); - PreparedStatement pstmt = null; - ResultSet rs = null; - try - { - pstmt = DB.prepareStatement (sql.toString(), null); - pstmt.setInt(1, m_AD_Table_ID); - pstmt.setInt(2, m_Record_ID); - if (m_AD_Table_ID == MBPartner.Table_ID) - pstmt.setInt(3, m_Record_ID); - rs = pstmt.executeQuery (); - while (rs.next ()) - { - if ("Y".equals(rs.getString(1))) - reportCount += rs.getInt(2); - else - documentCount += rs.getInt(2); - } - } - catch (Exception e) - { - log.log(Level.SEVERE, sql.toString(), e); - } - finally - { - DB.close(rs, pstmt); - rs = null; pstmt = null; - } - + int[] counts = MArchive.getReportAndDocumentCountByRecordId(m_AD_Table_ID, m_Record_ID, null); + int reportCount = counts[0]; + int documentCount = counts[1]; // if (documentCount > 0) { @@ -129,8 +117,7 @@ public class WArchive implements EventListener m_popup.appendChild(m_reports); } // All Reports - String sql1 = "SELECT COUNT(*) FROM AD_Archive WHERE AD_Table_ID=? AND IsReport='Y'"; - int allReports = DB.getSQLValue(null, sql1, m_AD_Table_ID); + int allReports = MArchive.getReportCountByTableId(m_AD_Table_ID, null); if (allReports > 0) { m_reportsAll = new Menuitem(Msg.getMsg(Env.getCtx(), "ArchivedReportsAll") @@ -141,7 +128,6 @@ public class WArchive implements EventListener if (documentCount == 0 && reportCount == 0 && allReports == 0) m_popup.appendChild(new Menuitem(Msg.getMsg(Env.getCtx(), "ArchivedNone"))); - // Popup popup = LayoutUtils.findPopup(invoker); if (popup != null) @@ -160,6 +146,7 @@ public class WArchive implements EventListener * Listner * @param e event */ + @Override public void onEvent(Event e) throws Exception { if (e.getTarget() instanceof Menuitem) diff --git a/org.idempiere.test/src/org/idempiere/test/model/MArchiveTest.java b/org.idempiere.test/src/org/idempiere/test/model/MArchiveTest.java new file mode 100644 index 0000000000..1d05b14d7a --- /dev/null +++ b/org.idempiere.test/src/org/idempiere/test/model/MArchiveTest.java @@ -0,0 +1,73 @@ +/*********************************************************************** + * This file is part of iDempiere ERP Open Source * + * http://www.idempiere.org * + * * + * Copyright (C) Contributors * + * * + * This program is free software; you can redistribute it and/or * + * modify it under the terms of the GNU General Public License * + * as published by the Free Software Foundation; either version 2 * + * of the License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the Free Software * + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * + * MA 02110-1301, USA. * + * * + * Contributors: * + * - hengsin * + **********************************************************************/ +package org.idempiere.test.model; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.compiere.model.MArchive; +import org.compiere.model.MProduct; +import org.compiere.util.Env; +import org.idempiere.test.AbstractTestCase; +import org.idempiere.test.DictionaryIDs; +import org.junit.jupiter.api.Test; + +public class MArchiveTest extends AbstractTestCase { + + public MArchiveTest() { + } + + @Test + public void testArchive() { + int allReportCount = MArchive.getReportCountByTableId(MProduct.Table_ID, getTrxName()); + int[] recordCounts = MArchive.getReportAndDocumentCountByRecordId(MProduct.Table_ID, DictionaryIDs.M_Product.AZALEA_BUSH.id, getTrxName()); + + MArchive marchive = new MArchive(Env.getCtx(), 0, getTrxName()); + marchive.setAD_Table_ID(MProduct.Table_ID); + marchive.setIsReport(true); + marchive.setName(getClass().getName()); + marchive.setRecord_ID(DictionaryIDs.M_Product.AZALEA_BUSH.id); + marchive.setBinaryData("test".getBytes()); + marchive.saveEx(); + + int allReportCount1 = MArchive.getReportCountByTableId(MProduct.Table_ID, getTrxName()); + assertEquals(allReportCount+1, allReportCount1, "Unexpected all archive report counts by table"); + + int[] recordCounts1 = MArchive.getReportAndDocumentCountByRecordId(MProduct.Table_ID, DictionaryIDs.M_Product.AZALEA_BUSH.id, getTrxName()); + assertEquals(recordCounts[0]+1, recordCounts1[0], "Unexpected archive report counts by table and record id"); + assertEquals(recordCounts[1], recordCounts1[1], "Unexpected archive document counts by table and record id"); + + marchive = new MArchive(Env.getCtx(), 0, getTrxName()); + marchive.setAD_Table_ID(MProduct.Table_ID); + marchive.setIsReport(false); + marchive.setName(getClass().getName()); + marchive.setRecord_ID(DictionaryIDs.M_Product.AZALEA_BUSH.id); + marchive.setBinaryData("test1".getBytes()); + marchive.saveEx(); + + recordCounts1 = MArchive.getReportAndDocumentCountByRecordId(MProduct.Table_ID, DictionaryIDs.M_Product.AZALEA_BUSH.id, getTrxName()); + assertEquals(recordCounts[0]+1, recordCounts1[0], "Unexpected archive report counts by table and record id"); + assertEquals(recordCounts[1]+1, recordCounts1[1], "Unexpected archive document counts by table and record id"); + } +}