com.itd.dbmrgdao.TestTime3_backup.java Source code

Java tutorial

Introduction

Here is the source code for com.itd.dbmrgdao.TestTime3_backup.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.itd.dbmrgdao;

import com.itd.common.dao.EmployeeDropDownDao;
import com.itd.common.dao.GennericDao;
import com.itd.regis.db.entity.ScanData;
import com.itd.regis.db.entity.ScanDataKey;
import com.itd.regis.db.entity.ScanRule;
import com.itd.scan.dao.ScanDao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Administrator Test if employee punch either time-in or timeout will
 * credit 8:00 and 17:00
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring/config/BeanLocations.xml")
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@Transactional
public class TestTime3_backup {

    @Autowired
    ScanDao scanDao;

    @Autowired
    GennericDao<ScanData> gennericDao;

    @Ignore
    @org.junit.Test
    public void test() throws ParseException {
        String newTab = "\t";
        String[] sysidLine;

        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt");
        chooser.setFileFilter(filter);
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();

        String filename = f.getAbsolutePath();
        try {
            FileReader reader = new FileReader(filename);
            BufferedReader br = new BufferedReader(reader);
            String strLine;

            String sysid = null, p1Start = null, p1End = null, p2Start = null, p2End = null, otStart = null,
                    otEnd = null;
            Date scdate = null;

            // read from select file
            while ((strLine = br.readLine()) != null) {
                String[] parts = strLine.split(" ");

                // check if not first record or sysid change will save in table scandata 
                if (sysid != null && !sysid.equals(parts[0])) {
                    // rule if whatever employee register will get standard hour 8 hrs
                    if (p1Start != null || p2End != null) {
                        ScanRule scanRule = scanDao.findScanRuleBySysId(sysid);
                        p1Start = scanRule.getP1start();
                        p2End = scanRule.getP2end();
                    }

                    // set up data in entity
                    ScanDataKey scanDataKey = new ScanDataKey(sysid, scdate);
                    ScanData scanData = new ScanData(scanDataKey, "1985", p1Start, p1End, p2Start, p2End, otStart,
                            otEnd);
                    // remove old record (key is sysid and scdate)
                    gennericDao.remove(scanData);
                    gennericDao.create(scanData);

                    //clear 
                    p1Start = null;
                    p1End = null;
                    p2Start = null;
                    p2End = null;
                    otStart = null;
                    otEnd = null;

                }

                sysid = parts[0];
                scdate = new SimpleDateFormat("yyyyMMdd", Locale.US).parse(parts[1]);

                if (parts[6].equals("01")) {
                    p1Start = CompareTime(p1Start, parts[5], "01");
                } else {
                    p2End = CompareTime(p2End, parts[5], "04");
                }

            }
            //last line

            if (p1Start != null || p2End != null) {
                ScanRule scanRule = scanDao.findScanRuleBySysId(sysid);
                p1Start = scanRule.getP1start();
                p2End = scanRule.getP2end();
            }

            ScanDataKey scanDataKey = new ScanDataKey(sysid, scdate);
            ScanData scanData = new ScanData(scanDataKey, "1985", p1Start, p1End, p2Start, p2End, otStart, otEnd);
            gennericDao.remove(scanData);
            gennericDao.create(scanData);

            br.close();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

    }

    protected static String CompareTime(String a, String b, String operator) throws ParseException {

        if (a == null) {
            return b;
        }

        SimpleDateFormat parser1 = new SimpleDateFormat("HHmm");
        Date x = parser1.parse(a);
        Date y = parser1.parse(b);

        try {
            // The Magic happens here i only get the Time out of the Date Object
            SimpleDateFormat parser2 = new SimpleDateFormat("HH:mm");
            x = parser2.parse(parser2.format(x));
            y = parser2.parse(parser2.format(y));

        } catch (ParseException ex) {
            System.err.println(ex);

        }

        switch (operator) {
        case "01":
            return ((x.compareTo(y) <= 0) ? a : b);
        case "04":
            return ((x.compareTo(y) >= 0) ? a : b);
        default:
            throw new IllegalArgumentException("Operator " + operator + " not fould in control!");
        }
    }
}