Java tutorial
/* * Copyright (C) 2010 The Halal Certification Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.mui.certificate.core; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.net.URL; import java.util.Calendar; import java.util.List; import java.util.Locale; import java.util.Vector; import javax.imageio.ImageIO; import org.apache.xmlrpc.client.TimingOutCallback; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.AcroFields; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import com.itextpdf.text.pdf.qrcode.ByteMatrix; import com.itextpdf.text.pdf.qrcode.Encoder; import com.itextpdf.text.pdf.qrcode.ErrorCorrectionLevel; import com.itextpdf.text.pdf.qrcode.Mode; import com.itextpdf.text.pdf.qrcode.QRCode; import com.mui.certificate.core.db.DBConnector; import com.mui.certificate.core.db.HistoryCertificate; import com.mui.certificate.core.db.TempCertNumber; /** * @author bagus * */ public class HalalCertification extends AbstractHalalCertification { private static final int sMagicNumber = 100; private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private TempCertNumber mTmpCertNumber; private HistoryCertificate mHistoryCert; private IUploadScheduler mUploadScheduler; private static HalalCertification sCertification; public static HalalCertification getInstance() throws Exception { if (sCertification == null) sCertification = new HalalCertification(); return sCertification; } private HalalCertification() throws Exception { mTmpCertNumber = new TempCertNumber(DBConnector.getConnection()); mHistoryCert = new HistoryCertificate(DBConnector.getConnection()); mUploadScheduler = new UploadedSchedulerImpl(); } /* * (non-Javadoc) * * @see * com.mui.certificate.core.AbstractHalalCertification#createNewCertificate * () */ @Override public Certificate createNewCertificate() throws Exception { /* * Pseudocode if there is certification number in temporary table : * cert_number = temp_table.getCertNumber(); else cert_number * xmlrpc_client.get_certnumber(); * * Certification certificate = new Certificate(); * certificate.setCertificationNumber(cert_number); * certificate.setCertificateURL(cert_url); return certificate */ Certificate retcert = new Certificate(); // if (!mTmpCertNumber.isEmpty()) { // retcert.setCertificateURL(mTmpCertNumber.getCertURL()); // retcert.setCertificateNumber(mTmpCertNumber.getNumber()); // } else { XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); Object[] result = null; XmlRpcClient client = new XmlRpcClient(); // 100 second timeout TimingOutCallback callback = new TimingOutCallback(100 * 1000); config.setServerURL(new URL("http://localhost:8000/")); client.setConfig(config); client.setTransportFactory(new XmlRpcCommonsTransportFactory(client)); Vector<Integer> params = new Vector<Integer>(); client.executeAsync("create_certificate", params, callback); try { result = (Object[]) callback.waitForResponse(); retcert.setCertificateURL(new URL(result[0].toString())); retcert.setCertificateNumber((Integer) result[1]); } catch (Throwable e) { throw new Exception("Timeout while waiting for server's response"); } // } return retcert; } /* * (non-Javadoc) * * @see * com.mui.certificate.core.AbstractHalalCertification#getCertificate(java * .lang.Integer) */ @Override public Certificate getCertificate(Integer certNumber) throws Exception { return mHistoryCert.getCertificate(certNumber); } /* * (non-Javadoc) * * @see com.mui.certificate.core.AbstractHalalCertification#getHistory() */ @Override public List<Certificate> getHistory() throws Exception { return mHistoryCert.getCertificateList(); } private ByteMatrix generateQRCode(String data) throws Exception { QRCode qrcode = new QRCode(); qrcode.setMode(Mode.BYTE); Encoder.encode(data, ErrorCorrectionLevel.L, qrcode); ByteMatrix matrix = qrcode.getMatrix(); return matrix; } private BufferedImage convertToImage(ByteMatrix matrix, int scale) { BufferedImage buffImage = new BufferedImage(matrix.getHeight() * scale, matrix.getWidth() * scale, BufferedImage.TYPE_INT_ARGB); for (int i = 0; i < matrix.getHeight(); ++i) { for (int j = 0; j < matrix.getWidth(); ++j) { for (int iscale = 0; iscale < scale; ++iscale) { for (int jscale = 0; jscale < scale; ++jscale) { buffImage.setRGB(j * scale + jscale, i * scale + iscale, matrix.get(j, i) == 0 ? WHITE : BLACK); } } } } return buffImage; } @Override public void exportQRCode(Certificate cert, File outputPng) throws Exception { ByteMatrix matrix = this.generateQRCode(cert.getCertificateURL().toString()); BufferedImage img = convertToImage(matrix, 1); ImageIO.write(img, "png", outputPng); } /* * (non-Javadoc) * * @see * com.mui.certificate.core.AbstractHalalCertification#exportToPDF(com.mui * .certificate.core.Certificate, File output) */ @Override public void exportCertificate(Certificate cert, File outputPdf) throws Exception { ByteMatrix matrix = generateQRCode(cert.getCertificateURL().toString()); int scale = sMagicNumber / matrix.getWidth(); BufferedImage buffImage = convertToImage(matrix, scale); File tempOutput = new File("tmp-qrcode.png"); ImageIO.write(buffImage, "png", tempOutput); Image itImage = Image.getInstance(tempOutput.getCanonicalPath()); PdfReader reader = new PdfReader("doc/template_certificate_halal.pdf"); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPdf)); int centerY = 715; int centerX = 135; itImage.setAbsolutePosition(centerX - (itImage.getWidth() / 2), centerY - (itImage.getHeight() / 2)); stamper.getOverContent(1).addImage(itImage); AcroFields form1 = stamper.getAcroFields(); String nameHeader = "topmostSubform[0].Page1[0]."; form1.setField(nameHeader + "no_certificate[0]", cert.getCertificateNumber().toString()); form1.setField(nameHeader + "name_product[0]", cert.getProductName()); form1.setField(nameHeader + "type_product[0]", cert.getProductType()); form1.setField(nameHeader + "name_company[0]", cert.getCompanyName()); form1.setField(nameHeader + "company_address[0]", cert.getCompanyAddress()); Calendar cal = Calendar.getInstance(new Locale("id")); Locale idLocale = new Locale("id"); cal.setTime(cert.getIssuedDate()); form1.setField(nameHeader + "issued_date[0]", String.format(idLocale, "%1$tA, %1$te %1$tB %1$tY", cal)); cal.setTime(cert.getValidDate()); form1.setField(nameHeader + "expired_date[0]", String.format(idLocale, "%1$tA, %1$te %1$tB %1$tY", cal)); stamper.setFormFlattening(true); stamper.close(); tempOutput.delete(); } /* * (non-Javadoc) * * @see * com.mui.certificate.core.AbstractHalalCertification#login(java.lang.String * , java.lang.String) */ @Override public void login(String username, String password) { // TODO Auto-generated method /* * user_table.validateName(username); * user_table.validatePassword(password); * user_table.updateStatus(LOGGED_IN); */ } /* * (non-Javadoc) * * @see com.mui.certificate.core.AbstractHalalCertification#logut() */ @Override public void logut() { // TODO Auto-generated method stub /* * if(user_table.getStatus()==LOGGED_IN) * user_table.updateStatus(LOGGED_OUT); */ } /* * (non-Javadoc) * * @see * com.mui.certificate.core.AbstractHalalCertification#printCertificate( * com.mui.certificate.core.Certificate) */ @Override public void printCertificate(Certificate cert) { // TODO Auto-generated method stub } /* * (non-Javadoc) * * @see * com.mui.certificate.core.AbstractHalalCertification#scheduleUpload(com * .mui.certificate.core.Certificate, * com.mui.certificate.core.IUploadActivity) */ @Override public void scheduleUpload(Certificate cert, IUploadActivity monitor) throws Exception { /* * mUploadScheduler.pushCertificate(cert,monitor) */ mUploadScheduler.pushData(cert); } /* * (non-Javadoc) * * @see * com.mui.certificate.core.AbstractHalalCertification#scheduleUploadPicture * (java.lang.Integer, java.io.File, * com.mui.certificate.core.IUploadActivity) */ @Override public void scheduleUploadPicture(Integer certNumber, File picture, IUploadActivity monitor) { // TODO Auto-generated method stub } /* * (non-Javadoc) * * @see * com.mui.certificate.core.AbstractHalalCertification#updateCertificate * (com.mui.certificate.core.Certificate) */ @Override public void updateCertificate(Certificate cert) { // TODO Auto-generated method stub /* * mUploadScheduler.pushCertificate(cert) */ } @Override public void cancelCertificate(Certificate cert) throws Exception { mTmpCertNumber.setTempCert(cert.getCertificateNumber(), cert.getCertificateURL()); } }