Search in sources :

Example 1 with MProductDownload

use of org.compiere.model.MProductDownload in project adempiere by adempiere.

the class AssetDelivery method deliverIt.

//	sendNoGuaranteeMail
/**************************************************************************
	 * 	Deliver Asset
	 * 	@param A_Asset_ID asset
	 * 	@return message - delivery errors start with **
	 */
private String deliverIt(int A_Asset_ID) {
    log.fine("A_Asset_ID=" + A_Asset_ID);
    long start = System.currentTimeMillis();
    //
    MAsset asset = new MAsset(getCtx(), A_Asset_ID, get_TrxName());
    if (asset.getAD_User_ID() == 0)
        return "** No Asset User";
    MUser user = new MUser(getCtx(), asset.getAD_User_ID(), get_TrxName());
    if (user.getEMail() == null || user.getEMail().length() == 0)
        return "** No Asset User Email";
    if (asset.getProductR_MailText_ID() == 0)
        return "** Product Mail Text";
    if (m_MailText == null || m_MailText.getR_MailText_ID() != asset.getProductR_MailText_ID())
        m_MailText = new MMailText(getCtx(), asset.getProductR_MailText_ID(), get_TrxName());
    if (m_MailText.getMailHeader() == null || m_MailText.getMailHeader().length() == 0)
        return "** No Subject";
    //	Create Mail
    EMail email = m_client.createEMail(user.getEMail(), null, null);
    if (!email.isValid()) {
        asset.setHelp(asset.getHelp() + " - Invalid EMail");
        asset.setIsActive(false);
        return "** Invalid EMail: " + user.getEMail();
    }
    if (m_client.isSmtpAuthorization())
        email.createAuthenticator(m_client.getRequestUser(), m_client.getRequestUserPW());
    m_MailText.setUser(user);
    m_MailText.setPO(asset);
    String message = m_MailText.getMailText(true);
    if (m_MailText.isHtml() || m_AttachAsset)
        email.setMessageHTML(m_MailText.getMailHeader(), message);
    else {
        email.setSubject(m_MailText.getMailHeader());
        email.setMessageText(message);
    }
    if (m_AttachAsset) {
        MProductDownload[] pdls = asset.getProductDownloads();
        if (pdls != null) {
            for (int i = 0; i < pdls.length; i++) {
                URI url = pdls[i].getDownloadURL(m_client.getDocumentDir());
                if (url != null)
                    email.addAttachment(url);
            }
        } else
            log.warning("No DowloadURL for A_Asset_ID=" + A_Asset_ID);
    }
    String msg = email.send();
    new MUserMail(m_MailText, asset.getAD_User_ID(), email).saveEx();
    if (!EMail.SENT_OK.equals(msg))
        return "** Not delivered: " + user.getEMail() + " - " + msg;
    MAssetDelivery ad = confirmDelivery(asset, email, user.getAD_User_ID());
    ad.saveEx();
    asset.saveEx();
    //
    log.fine((System.currentTimeMillis() - start) + " ms");
    //	success
    return user.getEMail() + " - " + asset.getProductVersionNo();
}
Also used : MMailText(org.compiere.model.MMailText) MUserMail(org.compiere.model.MUserMail) MProductDownload(org.compiere.model.MProductDownload) MAssetDelivery(org.compiere.model.MAssetDelivery) MAsset(org.compiere.model.MAsset) EMail(org.compiere.util.EMail) MUser(org.compiere.model.MUser) URI(java.net.URI)

Example 2 with MProductDownload

use of org.compiere.model.MProductDownload in project adempiere by adempiere.

the class MigrateData method release252c.

/**
	 * 	Release 252c
	 */
private void release252c() {
    String sql = "SELECT COUNT(*) FROM M_ProductDownload";
    int no = DB.getSQLValue(null, sql);
    if (no > 0) {
        log.finer("No Need - Downloads #" + no);
        return;
    }
    //
    int count = 0;
    sql = "SELECT AD_Client_ID, AD_Org_ID, M_Product_ID, Name, DownloadURL " + "FROM M_Product " + "WHERE DownloadURL IS NOT NULL";
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        pstmt = DB.prepareStatement(sql, null);
        rs = pstmt.executeQuery();
        while (rs.next()) {
            int AD_Client_ID = rs.getInt(1);
            int AD_Org_ID = rs.getInt(2);
            int M_Product_ID = rs.getInt(3);
            String Name = rs.getString(4);
            String DownloadURL = rs.getString(5);
            //
            Properties ctx = new Properties(Env.getCtx());
            Env.setContext(ctx, "#AD_Client_ID", AD_Client_ID);
            Env.setContext(ctx, "AD_Client_ID", AD_Client_ID);
            Env.setContext(ctx, "#AD_Org_ID", AD_Org_ID);
            Env.setContext(ctx, "AD_Org_ID", AD_Org_ID);
            MProductDownload pdl = new MProductDownload(ctx, 0, null);
            pdl.setM_Product_ID(M_Product_ID);
            pdl.setName(Name);
            pdl.setDownloadURL(DownloadURL);
            if (pdl.save()) {
                count++;
                String sqlUpdate = "UPDATE M_Product SET DownloadURL = NULL WHERE M_Product_ID=" + M_Product_ID;
                int updated = DB.executeUpdate(sqlUpdate, null);
                if (updated != 1)
                    log.warning("Product not updated");
            } else
                log.warning("Product Download not created M_Product_ID=" + M_Product_ID);
        }
    } catch (Exception e) {
        log.log(Level.SEVERE, sql, e);
    } finally {
        DB.close(rs, pstmt);
        rs = null;
        pstmt = null;
    }
    log.info("#" + count);
}
Also used : MProductDownload(org.compiere.model.MProductDownload) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties)

Example 3 with MProductDownload

use of org.compiere.model.MProductDownload in project adempiere by adempiere.

the class CopyProduct method doIt.

@Override
protected String doIt() throws Exception {
    int toMProductID = getRecord_ID();
    log.info("From M_Product_ID=" + m_copyFromId + " to " + toMProductID);
    if (toMProductID == 0)
        throw new IllegalArgumentException("Target M_Product_ID == 0");
    if (m_copyFromId == 0)
        throw new IllegalArgumentException("Source M_Product_ID == 0");
    // Get product price from the source product
    List<MProductPrice> prices = new Query(getCtx(), MProductPrice.Table_Name, "M_Product_ID=?", get_TrxName()).setParameters(new Object[] { m_copyFromId }).setOnlyActiveRecords(true).list();
    // Copy prices
    MProductPrice priceSrc;
    MProductPrice priceDst;
    for (Iterator<MProductPrice> it = prices.iterator(); it.hasNext(); ) {
        priceSrc = it.next();
        priceDst = new MProductPrice(getCtx(), 0, get_TrxName());
        priceDst.setM_Product_ID(toMProductID);
        priceDst.setM_PriceList_Version_ID(priceSrc.getM_PriceList_Version_ID());
        priceDst.setPrices(priceSrc.getPriceList(), priceSrc.getPriceStd(), priceSrc.getPriceLimit());
        priceDst.saveEx(get_TrxName());
    }
    int count = prices.size();
    // Copy substitutes
    List<X_M_Substitute> subs = new Query(getCtx(), X_M_Substitute.Table_Name, "M_Product_ID=? and NOT substitute_ID=?", get_TrxName()).setParameters(new Object[] { m_copyFromId, toMProductID }).setOnlyActiveRecords(true).list();
    X_M_Substitute subSrc;
    X_M_Substitute subDst;
    for (Iterator<X_M_Substitute> it = subs.iterator(); it.hasNext(); ) {
        subSrc = it.next();
        subDst = new X_M_Substitute(getCtx(), 0, get_TrxName());
        subDst.setM_Product_ID(toMProductID);
        subDst.setSubstitute_ID(subSrc.getSubstitute_ID());
        subDst.setName(subSrc.getName());
        subDst.setDescription(subSrc.getDescription());
        subDst.saveEx(get_TrxName());
    }
    count += subs.size();
    // Copy related
    List<X_M_RelatedProduct> related = new Query(getCtx(), X_M_RelatedProduct.Table_Name, "M_Product_ID=? and NOT relatedProduct_ID=?", get_TrxName()).setParameters(new Object[] { m_copyFromId, toMProductID }).setOnlyActiveRecords(true).list();
    X_M_RelatedProduct relatedSrc;
    X_M_RelatedProduct relatedDst;
    for (Iterator<X_M_RelatedProduct> it = related.iterator(); it.hasNext(); ) {
        relatedSrc = it.next();
        relatedDst = new X_M_RelatedProduct(getCtx(), 0, get_TrxName());
        relatedDst.setM_Product_ID(toMProductID);
        relatedDst.setRelatedProduct_ID(relatedSrc.getRelatedProduct_ID());
        relatedDst.setRelatedProductType(relatedSrc.getRelatedProductType());
        relatedDst.setName(relatedSrc.getName());
        relatedDst.setDescription(relatedSrc.getDescription());
        relatedDst.saveEx(get_TrxName());
    }
    count += related.size();
    // Copy replenish
    List<X_M_Replenish> replenish = new Query(getCtx(), X_M_Replenish.Table_Name, "M_Product_ID=?", get_TrxName()).setParameters(new Object[] { m_copyFromId }).setOnlyActiveRecords(true).list();
    X_M_Replenish replenishSrc;
    X_M_Replenish replenishDst;
    for (Iterator<X_M_Replenish> it = replenish.iterator(); it.hasNext(); ) {
        replenishSrc = it.next();
        replenishDst = new X_M_Replenish(getCtx(), 0, get_TrxName());
        replenishDst.setM_Product_ID(toMProductID);
        replenishDst.setM_Warehouse_ID(replenishSrc.getM_Warehouse_ID());
        replenishDst.setM_WarehouseSource_ID(replenishSrc.getM_WarehouseSource_ID());
        replenishDst.setReplenishType(replenishSrc.getReplenishType());
        replenishDst.setM_Locator_ID(replenishSrc.getM_Locator_ID());
        replenishDst.setLevel_Min(replenishSrc.getLevel_Min());
        replenishDst.setLevel_Max(replenishSrc.getLevel_Max());
        replenishDst.saveEx(get_TrxName());
    }
    count += replenish.size();
    // Don't copy purchasing since it demands a unique vendor product no
    /*
		List<MProductPO> poList = new Query(getCtx(), MProductPO.Table_Name, "M_Product_ID=? AND Discontinued='N'", get_TrxName())
									.setParameters(new Object[]{m_copyFromId})
									.setOnlyActiveRecords(true)
									.list();
		MProductPO poSrc;
		MProductPO poDst;
		for (Iterator<MProductPO> it = poList.iterator(); it.hasNext();) {
			poSrc = it.next();
			poDst = new MProductPO(getCtx(), 0, get_TrxName());
			poDst.setM_Product_ID(toMProductID);
			poDst.setC_BPartner_ID(poSrc.getC_BPartner_ID());
			poDst.setC_Currency_ID(poSrc.getC_Currency_ID());
			poDst.setC_UOM_ID(poSrc.getC_UOM_ID());
			poDst.setCostPerOrder(poSrc.getCostPerOrder());
			poDst.setDeliveryTime_Actual(poSrc.getDeliveryTime_Actual());
			poDst.setDeliveryTime_Promised(poSrc.getDeliveryTime_Promised());
			poDst.setIsCurrentVendor(poSrc.isCurrentVendor());
			poDst.setManufacturer(poSrc.getManufacturer());
			poDst.setOrder_Min(poSrc.getOrder_Min());
			poDst.setOrder_Pack(poSrc.getOrder_Pack());
			poDst.setPriceEffective(poSrc.getPriceEffective());
			poDst.setPriceLastInv(poSrc.getPriceLastInv());
			poDst.setPriceLastPO(poSrc.getPriceLastPO());
			poDst.setPriceList(poSrc.getPriceList());
			poDst.setPricePO(poSrc.getPricePO());
			poDst.setQualityRating(poSrc.getQualityRating());
			poDst.setRoyaltyAmt(poSrc.getRoyaltyAmt());
			// Don't set vendor product no or UPC since that's likely to be different
			poDst.setVendorCategory(poSrc.getVendorCategory());
			poDst.saveEx(get_TrxName()); 
		}
		
		count += poList.size();
		 */
    // Copy business partner
    List<MBPartnerProduct> bpList = new Query(getCtx(), MBPartnerProduct.Table_Name, "M_Product_ID=?", get_TrxName()).setParameters(new Object[] { m_copyFromId }).setOnlyActiveRecords(true).list();
    MBPartnerProduct bpSrc;
    MBPartnerProduct bpDst;
    for (Iterator<MBPartnerProduct> it = bpList.iterator(); it.hasNext(); ) {
        bpSrc = it.next();
        bpDst = new MBPartnerProduct(getCtx(), 0, get_TrxName());
        bpDst.setC_BPartner_ID(bpSrc.getC_BPartner_ID());
        bpDst.setDescription(bpSrc.getDescription());
        bpDst.setIsManufacturer(bpSrc.isManufacturer());
        bpDst.setM_Product_ID(toMProductID);
        bpDst.setManufacturer(bpSrc.getManufacturer());
        bpDst.setQualityRating(bpSrc.getQualityRating());
        bpDst.setShelfLifeMinDays(bpSrc.getShelfLifeMinDays());
        bpDst.setShelfLifeMinPct(bpSrc.getShelfLifeMinPct());
        bpDst.setVendorCategory(bpSrc.getVendorCategory());
        bpDst.setVendorProductNo(bpSrc.getVendorProductNo());
        bpDst.saveEx(get_TrxName());
    }
    count += bpList.size();
    // Copy download
    List<MProductDownload> dlList = new Query(getCtx(), MProductDownload.Table_Name, "M_Product_ID=?", get_TrxName()).setParameters(new Object[] { m_copyFromId }).setOnlyActiveRecords(true).list();
    MProductDownload dlSrc;
    MProductDownload dlDst;
    for (Iterator<MProductDownload> it = dlList.iterator(); it.hasNext(); ) {
        dlSrc = it.next();
        dlDst = new MProductDownload(getCtx(), 0, get_TrxName());
        dlDst.setM_Product_ID(toMProductID);
        dlDst.setName(dlSrc.getName());
        dlDst.setDownloadURL(dlSrc.getDownloadURL());
        dlDst.saveEx(get_TrxName());
    }
    count += dlList.size();
    // TODO Auto-generated method stub
    return "@Copied@=" + count;
}
Also used : MBPartnerProduct(org.compiere.model.MBPartnerProduct) X_M_Substitute(org.compiere.model.X_M_Substitute) Query(org.compiere.model.Query) X_M_Replenish(org.compiere.model.X_M_Replenish) MProductDownload(org.compiere.model.MProductDownload) X_M_RelatedProduct(org.compiere.model.X_M_RelatedProduct) MProductPrice(org.compiere.model.MProductPrice)

Example 4 with MProductDownload

use of org.compiere.model.MProductDownload in project adempiere by adempiere.

the class AssetServlet method streamAsset.

//	doPost
/**
	 * 	Stream asset
	 * 	@param request request
	 * 	@param response response
	 * 	@return "" or error message
	 */
protected String streamAsset(HttpServletRequest request, HttpServletResponse response) {
    //	Get Asset ID
    int A_Asset_ID = WebUtil.getParameterAsInt(request, "Asset_ID");
    if (A_Asset_ID == 0) {
        log.fine("No ID)");
        return "No Asset ID";
    }
    byte[] assetInfo = String.valueOf(A_Asset_ID).getBytes();
    //	Get Asset
    Properties ctx = JSPEnv.getCtx(request);
    HttpSession session = request.getSession(true);
    WebEnv.dump(request);
    MAsset asset = new MAsset(ctx, A_Asset_ID, null);
    if (asset.getA_Asset_ID() != A_Asset_ID) {
        log.fine("Asset not found - ID=" + A_Asset_ID);
        return "Asset not found";
    }
    //	Get WebUser & Compare with invoice
    WebUser wu = (WebUser) session.getAttribute(WebUser.NAME);
    if (wu.getC_BPartner_ID() != asset.getC_BPartner_ID()) {
        log.warning("A_Asset_ID=" + A_Asset_ID + " - BP_Invoice=" + asset.getC_BPartner_ID() + " <> BP_User=" + wu.getC_BPartner_ID());
        return "Your asset not found";
    }
    if (!asset.isDownloadable() || wu.isCreditStopHold() || !wu.isEMailVerified())
        return "Asset not downloadable";
    //	Name & URL
    String pd = WebUtil.getParameter(request, "PD");
    String dl_name = null;
    String dl_url = null;
    InputStream in = null;
    int M_ProductDownload_ID = 0;
    if (pd != null && pd.length() > 0) {
        MProductDownload[] pdls = asset.getProductDownloads();
        if (pdls != null) {
            for (int i = 0; i < pdls.length; i++) {
                if (pdls[i].getDownloadURL().indexOf(pd) != -1) {
                    M_ProductDownload_ID = pdls[i].getM_ProductDownload_ID();
                    dl_name = pd;
                    dl_url = pdls[i].getDownloadURL();
                    in = pdls[i].getDownloadStream(ctx.getProperty(WebSessionCtx.CTX_DOCUMENT_DIR));
                    break;
                }
            }
        }
    }
    log.fine(dl_name + " - " + dl_url);
    if (dl_name == null || dl_url == null || in == null)
        return "@NotFound@ @A_Asset_ID@: " + pd;
    /**
		Download SupportContract.pdf for Jorg Janke - jjanke@adempiere.org
		Version = 120 - Lot = . - SerNo = .
		Guarantee Date = 5/30/05
		Thank you for using Adempiere Customer Asset Management
		**/
    String lot = asset.getLot();
    if (lot == null || lot.length() == 0)
        lot = ".";
    String ser = asset.getSerNo();
    if (ser == null || ser.length() == 0)
        ser = ".";
    Object[] args = new Object[] { dl_name, wu.getName() + " - " + wu.getEmail(), asset.getVersionNo(), lot, ser, asset.getGuaranteeDate() };
    String readme = Msg.getMsg(ctx, "AssetDeliveryTemplate", args);
    //	Send File
    MAssetDelivery ad = confirmDelivery(asset, request, wu.getAD_User_ID());
    if (M_ProductDownload_ID != 0)
        ad.setM_ProductDownload_ID(M_ProductDownload_ID);
    ad.setDescription(dl_name);
    //
    float speed = 0;
    try {
        response.setContentType("application/zip");
        response.setHeader("Content-Location", "asset.zip");
        //	response.setContentLength(length);
        //	2k Buffer
        int bufferSize = 2048;
        response.setBufferSize(bufferSize);
        //
        log.fine(in + ", available=" + in.available());
        long time = System.currentTimeMillis();
        //	Zip Output Stream
        ServletOutputStream out = response.getOutputStream();
        //	Servlet out
        ZipOutputStream zip = new ZipOutputStream(out);
        zip.setMethod(ZipOutputStream.DEFLATED);
        zip.setLevel(Deflater.BEST_COMPRESSION);
        zip.setComment(readme);
        //	Readme File
        ZipEntry entry = new ZipEntry("readme.txt");
        entry.setExtra(assetInfo);
        zip.putNextEntry(entry);
        zip.write(readme.getBytes(), 0, readme.length());
        zip.closeEntry();
        //	Payload
        entry = new ZipEntry(dl_name);
        entry.setExtra(assetInfo);
        zip.putNextEntry(entry);
        byte[] buffer = new byte[bufferSize];
        int count = 0;
        int totalSize = 0;
        do {
            //	read delivery
            count = in.read(buffer, 0, bufferSize);
            if (count > 0) {
                totalSize += count;
                //	write zip
                zip.write(buffer, 0, count);
            }
        } while (count != -1);
        zip.closeEntry();
        //	Fini
        zip.finish();
        zip.close();
        in.close();
        time = System.currentTimeMillis() - time;
        speed = ((float) totalSize / 1024) / ((float) time / 1000);
        String msg = (totalSize / 1024) + "kB - " + time + " ms - " + speed + " kB/sec";
        log.fine(msg);
        //	Delivery Record
        ad.setDeliveryConfirmation(msg);
        ad.saveEx();
        asset.saveEx();
    } catch (IOException ex) {
        String msg = ex.getMessage();
        if (msg == null || msg.length() == 0)
            msg = ex.toString();
        log.warning(msg);
        //	Delivery Record
        try {
            if (msg.length() > 120)
                msg = msg.substring(0, 119);
            ad.setDeliveryConfirmation(msg);
            ad.save();
        //	asset.saveEx();	not delivered
        } catch (Exception ex1) {
            log.log(Level.SEVERE, "2 - " + ex);
        }
        //	nned to differentiate error message as response committed
        return "** Streaming error; Please Retry";
    }
    //
    return null;
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) HttpSession(javax.servlet.http.HttpSession) InputStream(java.io.InputStream) MProductDownload(org.compiere.model.MProductDownload) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Properties(java.util.Properties) MAsset(org.compiere.model.MAsset) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) ZipOutputStream(java.util.zip.ZipOutputStream) MAssetDelivery(org.compiere.model.MAssetDelivery) WebUser(org.compiere.util.WebUser)

Aggregations

MProductDownload (org.compiere.model.MProductDownload)4 Properties (java.util.Properties)2 MAsset (org.compiere.model.MAsset)2 MAssetDelivery (org.compiere.model.MAssetDelivery)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 ServletException (javax.servlet.ServletException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpSession (javax.servlet.http.HttpSession)1 MBPartnerProduct (org.compiere.model.MBPartnerProduct)1 MMailText (org.compiere.model.MMailText)1 MProductPrice (org.compiere.model.MProductPrice)1 MUser (org.compiere.model.MUser)1 MUserMail (org.compiere.model.MUserMail)1 Query (org.compiere.model.Query)1