package gui;
import hibernateSwingApi.component.CTable;
import hibernateSwingApi.component.TablePanel;
import hibernateSwingApi.hibernate.HibernateManager;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.SystemColor;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import org.hibernate.Query;
import org.hibernate.Session;
import pojos.UrunSevk;
import pojos.UrunSevkDetay;
import service.PSAUtil;
import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JTextFieldDateEditor;
public class PSASevkHareketleriEkrani extends JFrame {
private static final long serialVersionUID = 1L;
private static PSASevkHareketleriEkrani instance;
private TablePanel tablePanelSevk;
private JLabel jLabelSevkHareketleri;
private JPanel jPanelTarih;
private JLabel jLabelTarih;
private JDateChooser dateChooserIlkTarih;
private JLabel jLabelUrunDetay;
private JScrollPane scrollPaneUrunDetay;
private CTable tableUrunDetay;
private UrunSevk urunSevk;
private UrunSevkDetay urunSevkDetay;
private JDateChooser dateChooserSonTarih;
private List<UrunSevkDetay> stok = new LinkedList<>();
private List<UrunSevk> urunSevkList = new LinkedList<>();
public static PSASevkHareketleriEkrani getInstance() {
instance = new PSASevkHareketleriEkrani();
return instance;
}
public PSASevkHareketleriEkrani() {
initialize();
}
private void initialize() {
setBounds(0, 0, 970, 500);
setTitle("PSA MERKEZ SEVK HAREKETLERİ EKRANI");
setIconImage(PSAUtil.getImageIconViaRelativePath("picture/psaIcon.png")
.getImage());
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[] { 30, 206, 223, 460, 0, 0 };
gridBagLayout.rowHeights = new int[] { 5, 60, 263, 21, 77, 0, 0 };
gridBagLayout.columnWeights = new double[] { 0.0, 1.0, 1.0, 1.0, 0.0,
Double.MIN_VALUE };
gridBagLayout.rowWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
Double.MIN_VALUE };
getContentPane().setLayout(gridBagLayout);
jLabelSevkHareketleri = new JLabel("SEVK HAREKETLERİ");
jLabelSevkHareketleri.setFont(new Font("Tahoma", Font.BOLD, 14));
GridBagConstraints gbc_jLabelSevkHareketleri = new GridBagConstraints();
gbc_jLabelSevkHareketleri.anchor = GridBagConstraints.SOUTH;
gbc_jLabelSevkHareketleri.fill = GridBagConstraints.HORIZONTAL;
gbc_jLabelSevkHareketleri.insets = new Insets(0, 0, 5, 5);
gbc_jLabelSevkHareketleri.gridx = 1;
gbc_jLabelSevkHareketleri.gridy = 1;
getContentPane().add(jLabelSevkHareketleri, gbc_jLabelSevkHareketleri);
jPanelTarih = new JPanel();
jPanelTarih.setBackground(SystemColor.controlShadow);
GridBagConstraints gbc_jPanelTarih = new GridBagConstraints();
gbc_jPanelTarih.fill = GridBagConstraints.BOTH;
gbc_jPanelTarih.insets = new Insets(0, 0, 5, 5);
gbc_jPanelTarih.gridx = 3;
gbc_jPanelTarih.gridy = 1;
getContentPane().add(jPanelTarih, gbc_jPanelTarih);
GridBagLayout gbl_jPanelTarih = new GridBagLayout();
gbl_jPanelTarih.columnWidths = new int[] { 0, 121, 138, 138, 0, 0 };
gbl_jPanelTarih.rowHeights = new int[] { 20, 0 };
gbl_jPanelTarih.columnWeights = new double[] { 0.0, 1.0, 1.0, 1.0, 0.0,
Double.MIN_VALUE };
gbl_jPanelTarih.rowWeights = new double[] { 1.0, Double.MIN_VALUE };
jPanelTarih.setLayout(gbl_jPanelTarih);
jLabelTarih = new JLabel("ARALIK SEÇİNİZ");
jLabelTarih.setFont(new Font("Tahoma", Font.BOLD, 14));
GridBagConstraints gbc_jLabelTarih = new GridBagConstraints();
gbc_jLabelTarih.anchor = GridBagConstraints.WEST;
gbc_jLabelTarih.insets = new Insets(0, 0, 0, 5);
gbc_jLabelTarih.gridx = 1;
gbc_jLabelTarih.gridy = 0;
jPanelTarih.add(jLabelTarih, gbc_jLabelTarih);
dateChooserIlkTarih = new JDateChooser();
GridBagConstraints gbc_dateChooserIlkTarih = new GridBagConstraints();
gbc_dateChooserIlkTarih.fill = GridBagConstraints.HORIZONTAL;
gbc_dateChooserIlkTarih.insets = new Insets(0, 0, 0, 5);
gbc_dateChooserIlkTarih.gridx = 2;
gbc_dateChooserIlkTarih.gridy = 0;
jPanelTarih.add(dateChooserIlkTarih, gbc_dateChooserIlkTarih);
dateChooserIlkTarih.setDate(new Date());
((JTextFieldDateEditor) dateChooserIlkTarih.getDateEditor())
.setEditable(false);
((JTextFieldDateEditor) dateChooserIlkTarih.getDateEditor())
.setFocusable(false);
dateChooserSonTarih = new JDateChooser();
GridBagConstraints gbc_dateChooserSonTarih = new GridBagConstraints();
gbc_dateChooserSonTarih.fill = GridBagConstraints.HORIZONTAL;
gbc_dateChooserSonTarih.insets = new Insets(0, 0, 0, 5);
gbc_dateChooserSonTarih.gridx = 3;
gbc_dateChooserSonTarih.gridy = 0;
jPanelTarih.add(dateChooserSonTarih, gbc_dateChooserSonTarih);
dateChooserSonTarih.setDate(new Date());
((JTextFieldDateEditor) dateChooserSonTarih.getDateEditor())
.setEditable(false);
((JTextFieldDateEditor) dateChooserSonTarih.getDateEditor())
.setFocusable(false);
tablePanelSevk = new TablePanel((String) null, UrunSevk.class);
GridBagConstraints gbc_tablePanelSevk = new GridBagConstraints();
gbc_tablePanelSevk.fill = GridBagConstraints.BOTH;
gbc_tablePanelSevk.insets = new Insets(0, 0, 5, 5);
gbc_tablePanelSevk.gridwidth = 3;
gbc_tablePanelSevk.gridx = 1;
gbc_tablePanelSevk.gridy = 2;
getContentPane().add(tablePanelSevk, gbc_tablePanelSevk);
jLabelUrunDetay = new JLabel("ÜRÜN DETAY");
jLabelUrunDetay.setFont(new Font("Tahoma", Font.BOLD, 14));
GridBagConstraints gbc_jLabelUrunDetay = new GridBagConstraints();
gbc_jLabelUrunDetay.fill = GridBagConstraints.BOTH;
gbc_jLabelUrunDetay.insets = new Insets(0, 0, 5, 5);
gbc_jLabelUrunDetay.gridx = 1;
gbc_jLabelUrunDetay.gridy = 3;
getContentPane().add(jLabelUrunDetay, gbc_jLabelUrunDetay);
scrollPaneUrunDetay = new JScrollPane();
GridBagConstraints gbc_scrollPaneUrunDetay = new GridBagConstraints();
gbc_scrollPaneUrunDetay.insets = new Insets(0, 0, 5, 5);
gbc_scrollPaneUrunDetay.fill = GridBagConstraints.BOTH;
gbc_scrollPaneUrunDetay.gridwidth = 3;
gbc_scrollPaneUrunDetay.gridx = 1;
gbc_scrollPaneUrunDetay.gridy = 4;
getContentPane().add(scrollPaneUrunDetay, gbc_scrollPaneUrunDetay);
tableUrunDetay = new CTable((String) null, UrunSevkDetay.class);
scrollPaneUrunDetay.setViewportView(tableUrunDetay);
dateControl();
initListeners();
}
private void initListeners() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
anaEkranSetEnable();
}
});
tablePanelSevk.getTable().getSelectionModel()
.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
return;
urunSevk = (UrunSevk) tablePanelSevk
.getSelectedObject();
if (urunSevk == null) {
emptyDetayTable();
} else {
fillDetayTable();
}
}
});
dateChooserIlkTarih.getDateEditor().addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("date")) {
dateControl();
}
}
});
dateChooserSonTarih.getDateEditor().addPropertyChangeListener(
new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("date")) {
dateControl();
}
}
});
}
protected void anaEkranSetEnable() {
PSAAnaEkran.getInstance(null).setEnabled(true);
instance.dispose();
}
protected void emptyDetayTable() {
tableUrunDetay.refresh();
}
protected void fillDetayTable() {
stok.clear();
urunSevkDetay = new UrunSevkDetay(urunSevk);
stok.add(urunSevkDetay);
tableUrunDetay.setObjects(stok);
}
@SuppressWarnings("unchecked")
protected void dateControl() {
urunSevkList = (List<UrunSevk>) kasaHqlQuery(
dateChooserIlkTarih.getDate(), dateChooserSonTarih.getDate());
tablePanelSevk.setObjects(urunSevkList);
}
private static List<?> kasaHqlQuery(Date from, Date to) {
String userSystemName = HibernateManager.getInstance().getSystemName();
Calendar cal = Calendar.getInstance();
cal.setTime(from);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
from = cal.getTime();
cal.setTime(to);
cal.set(Calendar.HOUR_OF_DAY, 23);
cal.set(Calendar.MINUTE, 59);
cal.set(Calendar.SECOND, 59);
cal.set(Calendar.MILLISECOND, 99);
to = cal.getTime();
Session openSession = HibernateManager.getInstance().openSession();
openSession.beginTransaction();
StringBuilder sBuilder = new StringBuilder();
sBuilder.append("from UrunSevk where ");
sBuilder.append("tarih >= ").append(":from");
sBuilder.append(" and tarih <= ").append(":to");
sBuilder.append(" and ( urunSevkFrom =").append(
"'" + userSystemName + "'");
sBuilder.append(" or urunSevkTo =")
.append("'" + userSystemName + "' )");
String hql = sBuilder.toString();
Query createQuery = openSession.createQuery(hql);
createQuery.setParameter("from", from);
createQuery.setParameter("to", to);
List<?> list = createQuery.list();
openSession.close();
return list;
}
}