/**
*
* NORD POS is a fork of Openbravo POS.
*
* Copyright (C) 2009-2016 Nord Trading Ltd. <http://www.nordpos.com>
*
* This file is part of NORD POS.
*
* NORD POS 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 3 of the License, or (at your option) any later
* version.
*
* NORD POS 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
* NORD POS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nordpos.sync.panel;
import com.nordpos.sync.kettle.TransVariable;
import com.openbravo.pos.forms.JPanelView;
import com.openbravo.pos.forms.AppView;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.pos.forms.AppProperties;
import com.openbravo.pos.forms.BeanFactoryApp;
import com.openbravo.pos.forms.BeanFactoryException;
import com.openbravo.pos.forms.DataLogicSystem;
import java.awt.Color;
import java.util.List;
import java.util.Properties;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.pentaho.di.core.KettleEnvironment;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.logging.KettleLogStore;
import org.pentaho.di.core.logging.LoggingBuffer;
import org.pentaho.di.core.logging.LogLevel;
import org.pentaho.di.core.util.EnvUtil;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
/**
*
* @author Andrey Svininykh <svininykh@gmail.com>
* @version NORD POS 3
*/
public abstract class JPanelTransformation extends JPanel implements JPanelView, BeanFactoryApp {
protected AppView app;
// private DataLogicSystem dlSystem;
// private Properties hostProp;
protected Trans trans;
public JPanelTransformation() {
initComponents();
}
@Override
public void init(AppView app) throws BeanFactoryException {
this.app = app;
// dlSystem = (DataLogicSystem) app.getBean(DataLogicSystem.class.getName());
// hostProp = dlSystem.getResourceAsProperties(app.getProperties() + "/properties");
this.app.waitCursorBegin();
try {
KettleEnvironment.init(false);
EnvUtil.environmentInit();
} catch (KettleException ex) {
MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.syncerror"), ex);
msg.show(this);
}
this.app.waitCursorEnd();
}
@Override
public Object getBean() {
return this;
}
protected abstract String getTransformation();
protected abstract List<TransVariable> getTransVaribles();
@Override
public JComponent getComponent() {
return this;
}
@Override
public void activate() throws BasicException {
jPanelRows.setVisible(false);
}
@Override
public boolean deactivate() {
jMessage.setText(null);
jMessage.setBackground(Color.GRAY);
jLogArea.setText(null);
jPanelRows.setVisible(false);
return true;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanelHeader = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jPanelRows = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jMessage = new javax.swing.JLabel();
m_jScrollTableRow = new javax.swing.JScrollPane();
jLogArea = new javax.swing.JTextArea();
setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
setLayout(new java.awt.BorderLayout());
jPanelHeader.setLayout(new java.awt.BorderLayout());
jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/applications-accessories.png"))); // NOI18N
jButton1.setText(AppLocal.getIntString("label.RunTransformation")); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1);
jPanelHeader.add(jPanel1, java.awt.BorderLayout.CENTER);
add(jPanelHeader, java.awt.BorderLayout.NORTH);
jPanelRows.setLayout(new java.awt.BorderLayout());
jPanel2.setLayout(new java.awt.BorderLayout());
jMessage.setBackground(java.awt.Color.white);
jMessage.setFont(jMessage.getFont().deriveFont(jMessage.getFont().getStyle() | java.awt.Font.BOLD));
jMessage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jMessage.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), javax.swing.BorderFactory.createEmptyBorder(4, 1, 4, 1)));
jMessage.setEnabled(false);
jMessage.setOpaque(true);
jPanel2.add(jMessage, java.awt.BorderLayout.CENTER);
jPanelRows.add(jPanel2, java.awt.BorderLayout.NORTH);
jLogArea.setColumns(20);
jLogArea.setRows(5);
m_jScrollTableRow.setViewportView(jLogArea);
jPanelRows.add(m_jScrollTableRow, java.awt.BorderLayout.CENTER);
add(jPanelRows, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
this.app.waitCursorBegin();
jPanelRows.setVisible(true);
Trans t = runTransformationFromResource(getTransformation(), getTransVaribles());
// retrieve logging appender
LoggingBuffer appender = KettleLogStore.getAppender();
// retrieve logging lines for job
String logText = appender.getBuffer(t.getLogChannelId(), true).toString();
if (t.nrActiveSteps() == 0) {
t.cleanup();
}
if (t.isFinished()) {
jMessage.setEnabled(true);
int errors = t.getErrors();
if (errors > 0) {
jMessage.setText(AppLocal.getIntString("label.TransformationError"));
jMessage.setBackground(Color.RED);
} else {
jMessage.setText(AppLocal.getIntString("label.TransformationComplete"));
jMessage.setBackground(Color.GREEN);
}
jLogArea.setText(logText);
appender.clear();
appender.close();
}
this.app.waitCursorEnd();
}//GEN-LAST:event_jButton1ActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JTextArea jLogArea;
private javax.swing.JLabel jMessage;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanelHeader;
private javax.swing.JPanel jPanelRows;
private javax.swing.JScrollPane m_jScrollTableRow;
// End of variables declaration//GEN-END:variables
public Trans runTransformationFromResource(String resource, List<TransVariable> listVaribale) {
try {
// load latest revision of the transformation
// The TransMeta object is the programmatic representation of a transformation definition.
TransMeta transMeta = new TransMeta(getClass().getResourceAsStream(resource), null, false, null, null);
// Creating a transformation object which is the programmatic representation of a transformation
// A transformation object can be executed, report success, etc.
Trans transformation = new Trans(transMeta);
if (listVaribale != null) {
for (TransVariable variable : listVaribale) {
transformation.setVariable(variable.getName(), variable.getValue());
}
}
// adjust the log level
transformation.setLogLevel(LogLevel.BASIC);
// starting the transformation, which will execute asynchronously
transformation.execute(null);
// waiting for the transformation to finish
transformation.waitUntilFinished();
return transformation;
} catch (KettleException ex) {
MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.syncerror"), ex);
msg.show(this);
return null;
}
}
}