Java Source Code: org.compiere.model.MArchive


   1: /******************************************************************************
   2:  * The contents of this file are subject to the   Compiere License  Version 1.1
   3:  * ("License"); You may not use this file except in compliance with the License
   4:  * You may obtain a copy of the License at http://www.compiere.org/license.html
   5:  * Software distributed under the License is distributed on an  "AS IS"  basis,
   6:  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
   7:  * the specific language governing rights and limitations under the License.
   8:  * The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
   9:  * Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
  10:  * are Copyright (C) 1999-2005 Jorg Janke.
  11:  * All parts are Copyright (C) 1999-2005 ComPiere, Inc.  All Rights Reserved.
  12:  * Contributor(s): ______________________________________.
  13:  *****************************************************************************/
  14: package org.compiere.model;
  15: 
  16: import java.io.*;
  17: import java.sql.*;
  18: import java.util.*;
  19: import java.util.logging.*;
  20: import java.util.zip.*;
  21: import org.compiere.util.*;
  22: 
  23: 
  24: /**
  25:  *    Archive Model
  26:  *    
  27:  *  @author Jorg Janke
  28:  *  @version $Id: MArchive.java,v 1.8 2005/10/26 00:38:17 jjanke Exp $
  29:  */
  30: public class MArchive extends X_AD_Archive
  31:	  {
  32:    /**
  33:     *     Get Archives
  34:     *    @param ctx context
  35:     *    @param whereClause optional where clause (starting with AND)
  36:     *    @return archives
  37:     */
  38:    public static MArchive[] get (Properties ctx, String whereClause)
  39:	      {
  40:        ArrayList<MArchive> list = new ArrayList<MArchive>();
  41:        PreparedStatement pstmt = null;
  42:        String sql = "SELECT * FROM AD_Archive WHERE AD_Client_ID=?";
  43:        if (whereClause != null && whereClause.length() > 0)
  44:            sql += whereClause;
  45:        sql += " ORDER BY Created";
  46:        
  47:        try
  48:	          {
  49:            pstmt = DB.prepareStatement (sql, null);
  50:            pstmt.setInt (1, Env.getAD_Client_ID(ctx));
  51:            ResultSet rs = pstmt.executeQuery ();
  52:            while (rs.next ())
  53:                list.add (new MArchive(ctx, rs, null));
  54:            rs.close ();
  55:            pstmt.close ();
  56:            pstmt = null;
  57:        }
  58:        catch (Exception e)
  59:	          {
  60:            s_log.log(Level.SEVERE, sql, e);
  61:        }
  62:        try
  63:	          {
  64:            if (pstmt != null)
  65:                pstmt.close ();
  66:            pstmt = null;
  67:        }
  68:        catch (Exception e)
  69:	          {
  70:            pstmt = null;
  71:        }
  72:        if (list.size() == 0)
  73:            s_log.fine(sql);
  74:        else
  75:            s_log.finer(sql);
  76:        //
  77:        MArchive[] retValue = new MArchive[list.size()];
  78:        list.toArray(retValue);
  79:        return retValue;
  80:    }    //    get
  81:
  82:    /**    Logger            */
  83:    private static CLogger s_log = CLogger.getCLogger(MArchive.class);
  84:    
  85:    private Integer        m_inflated = null;
  86:    private Integer        m_deflated = null;
  87:    
  88:    
  89:    /**************************************************************************
  90:     *     Standard Constructor
  91:     *    @param ctx context
  92:     *    @param AD_Archive_ID id
  93:     */
  94:    public MArchive (Properties ctx, int AD_Archive_ID, String trxName)
  95:	      {
  96:        super (ctx, AD_Archive_ID, trxName);
  97:    }    //    MArchive
  98:
  99:    /**
 100:     *     Load Constructor
 101:     *    @param ctx context
 102:     *    @param rs result set
 103:     */
 104:    public MArchive (Properties ctx, ResultSet rs, String trxName)
 105:	      {
 106:        super(ctx, rs, trxName);
 107:    }    //    MArchive
 108:
 109:    /**
 110:     *     Constructor
 111:     *    @param ctx context
 112:     *    @param info print info
 113:     */
 114:    public MArchive (Properties ctx, PrintInfo info, String trxName)
 115:	      {
 116:        this (ctx, 0, trxName);
 117:        setName(info.getName());
 118:        setIsReport(info.isReport());
 119:        //
 120:        setAD_Process_ID(info.getAD_Process_ID());
 121:        setAD_Table_ID(info.getAD_Table_ID());
 122:        setRecord_ID(info.getRecord_ID());
 123:        setC_BPartner_ID(info.getC_BPartner_ID());
 124:    }    //    MArchive
 125:
 126:    
 127:    
 128:    /**
 129:     *     String Representation
 130:     *    @return info
 131:     */
 132:    public String toString()
 133:	      {
 134:        StringBuffer sb = new StringBuffer("MArchive[");
 135:        sb.append(get_ID()).append(",Name=").append(getName());
 136:        if (m_inflated != null)
 137:            sb.append(",Inflated=" + m_inflated);
 138:        if (m_deflated != null)
 139:            sb.append(",Deflated=" + m_deflated);
 140:        sb.append("]");
 141:        return sb.toString();
 142:    }    //    toString
 143:
 144:    /**
 145:     *     Get Binary Data.
 146:     *     (inflate)
 147:     *    @return inflated data
 148:     */
 149:    public byte[] getBinaryData()
 150:	      {
 151:        byte[] deflatedData = super.getBinaryData();
 152:        m_deflated = null;
 153:        m_inflated = null;
 154:        if (deflatedData == null)
 155:            return null;
 156:        //
 157:        log.fine("ZipSize=" + deflatedData.length);
 158:        m_deflated = new Integer(deflatedData.length);
 159:        if (deflatedData.length == 0)
 160:            return null;
 161:
 162:        byte[] inflatedData = null;
 163:        try
 164:	          {
 165:            ByteArrayInputStream in = new ByteArrayInputStream(deflatedData);
 166:            ZipInputStream zip = new ZipInputStream (in);
 167:            ZipEntry entry = zip.getNextEntry();
 168:            if (entry != null)    //    just one entry
 169:	              {
 170:                ByteArrayOutputStream out = new ByteArrayOutputStream();
 171:                byte[] buffer = new byte[2048];
 172:                int length = zip.read(buffer);
 173:                while (length != -1)
 174:	                  {
 175:                    out.write(buffer, 0, length);
 176:                    length = zip.read(buffer);
 177:                }
 178:                //
 179:                inflatedData = out.toByteArray();
 180:                log.fine("Size=" + inflatedData.length + " - zip="
 181:                    + entry.getCompressedSize() + "(" + entry.getSize() + ") "
 182:                    + (entry.getCompressedSize()*100/entry.getSize())+ "%");
 183:                m_inflated = new Integer(inflatedData.length);
 184:            }
 185:        }
 186:        catch (Exception e)
 187:	          {
 188:            log.log(Level.SEVERE, "loadLOBData", e);
 189:            inflatedData = null;
 190:        }
 191:        return inflatedData;
 192:    }    //    getBinaryData
 193:    
 194:    /**
 195:     *     Get Data as Input Stream
 196:     *    @return input stream or null
 197:     */
 198:    public InputStream getInputStream()
 199:	      {
 200:        byte[] inflatedData = getBinaryData();
 201:        if (inflatedData == null)
 202:            return null;
 203:        return new ByteArrayInputStream(inflatedData);
 204:    }    //    getInputStream
 205:    
 206:    /**
 207:     *     Save Binary Data.
 208:     *     (deflate)
 209:     *    @param inflatedData inflated data 
 210:     */
 211:    public void setBinaryData (byte[] inflatedData)
 212:	      {
 213:        if (inflatedData == null || inflatedData.length == 0)
 214:            throw new IllegalArgumentException("InflatedData is NULL");
 215:        m_inflated = new Integer(inflatedData.length);
 216:        ByteArrayOutputStream out = new ByteArrayOutputStream(); 
 217:        ZipOutputStream zip = new ZipOutputStream(out);
 218:        zip.setMethod(ZipOutputStream.DEFLATED);
 219:        zip.setLevel(Deflater.BEST_COMPRESSION);
 220:        zip.setComment("compiere");
 221:        //
 222:        byte[] deflatedData = null;
 223:        try
 224:	          {
 225:            ZipEntry entry = new ZipEntry("CompiereArchive");
 226:            entry.setTime(System.currentTimeMillis());
 227:            entry.setMethod(ZipEntry.DEFLATED);
 228:            zip.putNextEntry(entry);
 229:            zip.write (inflatedData, 0, inflatedData.length);
 230:            zip.closeEntry();
 231:            log.fine(entry.getCompressedSize() + " (" + entry.getSize() + ") "
 232:                    + (entry.getCompressedSize()*100/entry.getSize())+ "%");
 233:            //
 234:        //    zip.finish();
 235:            zip.close();
 236:            deflatedData = out.toByteArray();
 237:            log.fine("Length=" +  inflatedData.length);
 238:            m_deflated = new Integer(deflatedData.length);
 239:        }
 240:        catch (Exception e)
 241:	          {
 242:            log.log(Level.SEVERE, "saveLOBData", e);
 243:            deflatedData = null;
 244:            m_deflated = null;
 245:        }
 246:        super.setBinaryData (deflatedData);
 247:    }    //    setBinaryData
 248:    
 249:    /**
 250:     *     Get Created By (User) Name
 251:     *    @return name
 252:     */
 253:    public String getCreatedByName()
 254:	      {
 255:        String name = "?";
 256:        String sql = "SELECT Name FROM AD_User WHERE AD_User_ID=?";
 257:        PreparedStatement pstmt = null;
 258:        try
 259:	          {
 260:            pstmt = DB.prepareStatement (sql, null);
 261:            pstmt.setInt (1, getCreatedBy());
 262:            ResultSet rs = pstmt.executeQuery ();
 263:            if (rs.next ())
 264:                name = rs.getString(1);
 265:            rs.close ();
 266:            pstmt.close ();
 267:            pstmt = null;
 268:        }
 269:        catch (Exception e)
 270:	          {
 271:            log.log(Level.SEVERE, sql, e);
 272:        }
 273:        try
 274:	          {
 275:            if (pstmt != null)
 276:                pstmt.close ();
 277:            pstmt = null;
 278:        }
 279:        catch (Exception e)
 280:	          {
 281:            pstmt = null;
 282:        }
 283:        return name;
 284:    }    //    getCreatedByName
 285:    
 286:    
 287:    /**
 288:     *     Before Save
 289:     *    @param newRecord new
 290:     *    @returntrue if can be saved
 291:     */
 292:    protected boolean beforeSave (boolean newRecord)
 293:	      {
 294:        //    Binary Data is Mandatory
 295:        byte[] data = super.getBinaryData();
 296:        if (data == null || data.length == 0)
 297:            return false;
 298:        //
 299:        log.fine(toString());
 300:        return true;
 301:    }    //    beforeSave
 302:    
 303:}    //    MArchive