com.iaito.atsws.services.ClientServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.iaito.atsws.services.ClientServiceImpl.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.iaito.atsws.services;

import com.iaito.atsws.bean.in.Entry;
import com.iaito.atsws.bean.in.EntryData;
import com.iaito.atsws.bean.in.TagMovement;
import com.iaito.atsws.bean.out.SkippedTag;
import com.iaito.atsws.bean.out.TagListToTab;
import com.iaito.atsws.entity.Reader;
import com.iaito.atsws.entity.TabPosition;
import com.iaito.atsws.entity.TagEntry;
import com.iaito.atsws.entity.TagLog;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Service;

/**
 *
 * @author habib
 */
@Service
public class ClientServiceImpl implements ClientService {

    @Override
    public List<Reader> getReaderList() {
        Transaction tx;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            Query query = session.createQuery("from Reader");
            List<Reader> readerList = (List<Reader>) query.list();
            session.flush();
            session.clear();
            tx.commit();

            return readerList;
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(
                    "Exception from -------------ClientServiceImpl.java----getReaderList()--" + e.getMessage());
        }
        return null;
    }

    @Override
    public List<TagLog> getTagLogs(String reader) {
        Transaction tx;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            String s = "select * from tag_log w where at_reader='" + reader
                    + "' and arrival=1 and departure=0 and entry_done=0";
            Query query = session.createSQLQuery(s).addEntity(TagLog.class);
            List<TagLog> readerList = (List<TagLog>) query.list();
            session.flush();
            session.clear();
            tx.commit();

            return readerList;
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(
                    "Exception from -------------ClientServiceImpl.java----getTagLogs()--" + e.getMessage());
        }
        return null;
    }

    //    @Override
    //    public String postTags(Entry[] entries) {
    //        Transaction tx;
    //        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    //         try
    //         {
    //            tx = session.beginTransaction();
    //            
    //            TagEntry tagentry = new TagEntry(new Date().toString(), "habib", entries.length);
    //            
    //            Integer tagentryId = (Integer)session.save(tagentry);
    //            
    //            TagEntry tEntry = (TagEntry) session.get(TagEntry.class, tagentryId);
    //             
    //               for(Entry e : entries)
    //                {
    //                TagLog tagLog = (TagLog) session.get(TagLog.class, Integer.parseInt(e.getTaglogId()));
    //                tagLog.setEntryDone(1);
    //                tagLog.setTagEntry(tEntry);
    //                session.update(tagLog);
    //                }
    //            session.flush();
    //            session.clear();
    //            tx.commit();
    //           
    //            return "successfully entered";
    //       }
    //           catch(Exception e)
    //           {
    //            session.getTransaction().rollback();
    //            System.out.println("Exception from -------------ClientServiceImpl.java----postTags()--"+e.getMessage());
    //           }
    //        return "fail";
    //    }

    @Override
    public String tagArrived(TagMovement movement) {

        Transaction tx;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();
            TagLog tagLog = new TagLog(movement.getTagData(), movement.getReader_id(), 1, movement.getTime(), 0,
                    "null", 0, 0);
            session.save(tagLog);
            session.flush();
            session.clear();
            tx.commit();
            return "successfully entered";
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(
                    "Exception from -------------ClientServiceImpl.java----tagArrived()--" + e.getMessage());
        }
        return "fail";
    }

    @Override
    public String tagDeparted(TagMovement movement) {
        Transaction tx;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();

            String s = "select * from tag_log w where tag_data='" + movement.getTagData() + "' and at_reader='"
                    + movement.getReader_id() + "' and arrival=1 and departure=0";

            Query query = session.createSQLQuery(s).addEntity(TagLog.class);

            List<TagLog> tagLogList = (List<TagLog>) query.list();

            for (TagLog t : tagLogList) {
                t.setDeparture(1);
                t.setDepartureTime(movement.getTime());
                session.update(t);
            }

            session.flush();
            session.clear();
            tx.commit();

            return "successfully entered";
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(
                    "Exception from -------------ClientServiceImpl.java----tagDeparted()--" + e.getMessage());
        }
        return "fail";
    }

    @Override
    public String tabArrived(TagMovement movement) {
        Transaction tx;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();

            String s = "select * from tab_position w where tab_mac_address='" + movement.getTagData() + "'";
            Query query = session.createSQLQuery(s).addEntity(TabPosition.class);
            List<TabPosition> tabPositionList = (List<TabPosition>) query.list();

            if (tabPositionList.isEmpty()) {
                TabPosition tabPosition = new TabPosition(movement.getTagData(), movement.getReader_id());
                session.save(tabPosition);
            } else {
                for (TabPosition tb : tabPositionList) {
                    tb.setReader(movement.getReader_id());
                    session.update(tb);
                }
            }
            session.flush();
            session.clear();
            tx.commit();

            return "success";
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(
                    "Exception from -------------ClientServiceImpl.java----tabArrived()--" + e.getMessage());
        }
        return "fail";
    }

    @Override
    public String tabDeparted(TagMovement movement) {
        Transaction tx;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();

            String s = "select * from tab_position w where tab_mac_address='" + movement.getTagData() + "'";
            Query query = session.createSQLQuery(s).addEntity(TabPosition.class);
            List<TabPosition> tabPositionList = (List<TabPosition>) query.list();

            if (tabPositionList.isEmpty()) {
                TabPosition tabPosition = new TabPosition(movement.getTagData(), "null");
                session.save(tabPosition);
            } else {
                for (TabPosition tb : tabPositionList) {
                    tb.setReader("null");
                    session.update(tb);
                }
            }
            session.flush();
            session.clear();
            tx.commit();

            return "success";
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(
                    "Exception from -------------ClientServiceImpl.java----tabDeparted()--" + e.getMessage());
        }
        return "fail";
    }

    //    @Override
    //    public List<TagLog> getTagsAtTab(String reader) {
    //        Transaction tx;
    //        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    //         try
    //         {
    //            tx = session.beginTransaction();
    //            String s="select * from tag_log w where at_reader='"+reader+"' and arrival=1 and departure=0 and entry_done=0";
    //            Query query=session.createSQLQuery(s).addEntity(TagLog.class);
    //            List<TagLog> readerList =(List<TagLog>)query.list(); 
    //            session.flush();
    //            session.clear();
    //            tx.commit();
    //           
    //            return readerList;
    //       }
    //           catch(Exception e)
    //           {
    //            session.getTransaction().rollback();
    //            System.out.println("Exception from -------------ClientServiceImpl.java----getTagsAtTab()--"+e.getMessage());
    //           }
    //        return null;
    //    }

    //    @Override
    //        public TagListToTab getTagsAtTabNew(String tab) {
    //        Transaction tx;
    //        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    //        TagListToTab tagListTotab=null;
    //        List<TagLog> tagLogList=null;
    //        List<TagLog> entryTagLogList=new ArrayList<>();
    //        List<TagLog> exitTagLogList=new ArrayList<>();
    //         try
    //         {
    //            tx = session.beginTransaction();
    //            String s="select * from tab_position w where tab_mac_address='"+tab+"'";
    //            Query query=session.createSQLQuery(s).addEntity(TabPosition.class);
    //            List<TabPosition> tabPositionList =(List<TabPosition>)query.list();
    //            if(tabPositionList==null || tabPositionList.isEmpty())
    //            {
    //            tagLogList=null;
    //            }
    //            else
    //            {
    //            TabPosition tabPosition = tabPositionList.get(tabPositionList.size()-1);
    //            
    //            String s1="select * from tag_log w where at_reader='"+tabPosition.getReader()+"' and arrival=1 and departure=0 and entry_done=0";
    //            Query query1=session.createSQLQuery(s1).addEntity(TagLog.class);
    //            tagLogList =(List<TagLog>)query1.list();
    //            
    //                for(TagLog taglog : tagLogList)
    //                {
    //                     String s2="select * from tag_log w where tag_data='"+taglog.getTagData()+"' and arrival=1 and departure=1 and entry_done!=0 ORDER BY arrival_time desc limit 1";
    //                     Query query2=session.createSQLQuery(s2).addEntity(TagLog.class);
    //                     List<TagLog> tagLogList2 = (List<TagLog>)query2.list();
    //                     if(tagLogList2!=null && !tagLogList2.isEmpty())
    //                     {
    //                         TagLog tagLog2 = tagLogList2.get(tagLogList2.size()-1);
    //                         if(tagLog2.getAtReader().equals(taglog.getAtReader()))
    //                         {
    //                             if(tagLog2.getEntryDone()==1)
    //                             {
    //                                 exitTagLogList.add(taglog);
    //                             }
    //                             else if(tagLog2.getEntryDone()==2)
    //                             {
    //                                 // Again entering at same reader position
    //                                 entryTagLogList.add(taglog);
    //                             }
    //                         }
    //                         else if(!tagLog2.getAtReader().equals(taglog.getAtReader()))
    //                         {
    //                             if(tagLog2.getEntryDone()==1)
    //                             {
    //                                 //Should exit from tagLog2.getAtReader() first
    //                                 entryTagLogList.add(taglog);
    //                             }
    //                             else if(tagLog2.getEntryDone()==2)
    //                             {
    //                                 entryTagLogList.add(taglog);
    //                             }
    //                         }
    //                     }
    //                     else
    //                     {
    //                      // This arrived tags never enter or exited before now.
    //                         entryTagLogList.add(taglog);
    //                     }
    //                }
    //                
    //               tagListTotab = new TagListToTab(tab, tabPosition.getReader());
    //               tagListTotab.setEntryTagLogList(entryTagLogList);
    //               tagListTotab.setExitTagLogList(exitTagLogList);
    //            }
    //            
    //            session.flush();
    //            session.clear();
    //            tx.commit();
    //            
    //            
    //           
    //            return tagListTotab;
    //       }
    //           catch(Exception e)
    //           {
    //            session.getTransaction().rollback();
    //            System.out.println("Exception from -------------ClientServiceImpl.java----getTagsAtTabNew()--"+e.getMessage());
    //           }
    //        return null;
    //    }

    @Override
    public TagListToTab getTagsAtTabNew(String tab) {
        Transaction tx;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        TagListToTab tagListTotab = null;
        List<TagLog> tagLogList = null;
        List<TagLog> entryTagLogList = new ArrayList<>();
        List<TagLog> exitTagLogList = new ArrayList<>();
        List<SkippedTag> skippedTagLogList = new ArrayList<>();

        try {
            tx = session.beginTransaction();
            String s = "select * from tab_position w where tab_mac_address='" + tab + "'";
            Query query = session.createSQLQuery(s).addEntity(TabPosition.class);
            List<TabPosition> tabPositionList = (List<TabPosition>) query.list();
            if (tabPositionList == null || tabPositionList.isEmpty()) {
                tagLogList = null;
            } else {
                TabPosition tabPosition = tabPositionList.get(tabPositionList.size() - 1);

                String s1 = "select * from tag_log w where at_reader='" + tabPosition.getReader()
                        + "' and arrival=1 and departure=0 and entry_done=0";
                Query query1 = session.createSQLQuery(s1).addEntity(TagLog.class);
                tagLogList = (List<TagLog>) query1.list();

                for (TagLog taglog : tagLogList) {
                    String s2 = "select * from tag_log w where tag_data='" + taglog.getTagData()
                            + "' and arrival=1 and departure=1 and entry_done!=0 ORDER BY arrival_time desc limit 1";
                    Query query2 = session.createSQLQuery(s2).addEntity(TagLog.class);
                    List<TagLog> tagLogList2 = (List<TagLog>) query2.list();
                    if (tagLogList2 != null && !tagLogList2.isEmpty()) {
                        TagLog tagLog2 = tagLogList2.get(tagLogList2.size() - 1);
                        if (tagLog2.getAtReader().equals(taglog.getAtReader())) {
                            if (tagLog2.getEntryDone() == 1) {
                                exitTagLogList.add(taglog);
                            } else if (tagLog2.getEntryDone() == 2) {
                                // Again entering at same reader position
                                entryTagLogList.add(taglog);
                            }
                        } else if (!tagLog2.getAtReader().equals(taglog.getAtReader())) {
                            if (tagLog2.getEntryDone() == 1) {
                                //Should exit from tagLog2.getAtReader() first
                                skippedTagLogList.add(new SkippedTag(tagLog2.getAtReader(), "2", tagLog2));
                            } else if (tagLog2.getEntryDone() == 2) {
                                entryTagLogList.add(taglog);
                            }
                        }
                    } else {
                        // This arrived tags never enter or exited before now.
                        entryTagLogList.add(taglog);
                    }
                }

                tagListTotab = new TagListToTab(tab, tabPosition.getReader());
                tagListTotab.setEntryTagLogList(entryTagLogList);
                tagListTotab.setExitTagLogList(exitTagLogList);
                tagListTotab.setSkippedTagLogList(skippedTagLogList);
            }

            session.flush();
            session.clear();
            tx.commit();

            return tagListTotab;
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out.println(
                    "Exception from -------------ClientServiceImpl.java----getTagsAtTabNew()--" + e.getMessage());
        }
        return null;
    }

    //    @Override
    //    public List<TabPosition> getTabAtReader(String reader) {
    //                 Transaction tx;
    //        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    //         try
    //         {
    //            tx = session.beginTransaction();      
    //            String s="select * from tab_position w where reader='"+reader+"'";
    //            Query query=session.createSQLQuery(s).addEntity(TabPosition.class);
    //            List<TabPosition> tabPositions =(List<TabPosition>)query.list(); 
    //            session.flush();
    //            session.clear();
    //            tx.commit();
    //           
    //            return tabPositions;
    //       }
    //           catch(Exception e)
    //           {
    //            session.getTransaction().rollback();
    //            System.out.println("Exception from -------------ClientServiceImpl.java----getTabAtReader()--"+e.getMessage());
    //           }
    //        return null;
    //    }

    //    @Override
    //    public List<TagLog> newTagDeparted(TagMovement movement) {
    //        Transaction tx;
    //        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    //         try
    //         {
    //            tx = session.beginTransaction();
    //            String s="select * from tag_log w where tag_data='"+movement.getTagData()+"' and at_reader='"+movement.getReader_id()+"' and arrival=1 and departure=0";
    //            Query query=session.createSQLQuery(s).addEntity(TagLog.class);
    //            List<TagLog> tagLogList =(List<TagLog>)query.list();
    //            for(TagLog t : tagLogList)
    //            {
    //             t.setDeparture(1);
    //             t.setDepartureTime(movement.getTime());
    //             session.update(t);
    //            }
    //            String s1="select * from tag_log w where at_reader='"+movement.getReader_id()+"' and arrival=1 and departure=0 and entry_done=0";
    //            Query query1=session.createSQLQuery(s1).addEntity(TagLog.class);
    //            List<TagLog> tagLogListNew =(List<TagLog>)query1.list(); 
    //            session.flush();
    //            session.clear();
    //            tx.commit();
    //            return tagLogListNew;
    //       }
    //           catch(Exception e)
    //           {
    //            session.getTransaction().rollback();
    //            System.out.println("Exception from -------------ClientServiceImpl.java----newTagDeparted()--"+e.getMessage());
    //           }
    //        return null;
    //    }

    //    @Override
    //    public List<TagLog> newTagArrived(TagMovement movement) {
    //           Transaction tx;
    //        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    //         try
    //         {
    //            tx = session.beginTransaction();
    //            TagLog tagLog = new TagLog(null,movement.getTagData(),movement.getReader_id(),1,movement.getTime(),0,"null",0 );
    //            session.save(tagLog);
    //            String s="select * from tag_log w where at_reader='"+movement.getReader_id()+"' and arrival=1 and departure=0 and entry_done=0";
    //            Query query=session.createSQLQuery(s).addEntity(TagLog.class);
    //            List<TagLog> tagLogList =(List<TagLog>)query.list(); 
    //            session.flush();
    //            session.clear();
    //            tx.commit();
    //            return tagLogList;
    //       }
    //           catch(Exception e)
    //           {
    //            session.getTransaction().rollback();
    //            System.out.println("Exception from -------------ClientServiceImpl.java----tagArrived()--"+e.getMessage());
    //           }
    //        return null;
    //    }

    //       @Override
    //        public String checkInOrOut(TagMovement movement) {
    //        Transaction tx;
    //        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    //         try
    //         {
    //            tx = session.beginTransaction();
    //            
    //            String s="select * from tag_log w where tag_data='"+movement.getTagData()+"' and at_reader='"+movement.getReader_id()+"' and arrival=1 and entry_done!=0";
    //            Query query=session.createSQLQuery(s).addEntity(TagLog.class);
    //            List<TagLog> tagLogList =(List<TagLog>)query.list();
    //            session.flush();
    //            session.clear();
    //            tx.commit();
    //            
    //            TagLog taglog =null;
    //            
    //            if(!tagLogList.isEmpty())
    //            {    
    //            taglog =tagLogList.get(tagLogList.size()-1);
    //            
    //                if(taglog.getEntryDone()==1)
    //                {
    //                return "out";
    //                }
    //                else if(taglog.getEntryDone()==2)
    //                {
    //                return "in";
    //                }
    //            }
    //           
    //            return "in";
    //       }
    //           catch(Exception e)
    //           {
    //            session.getTransaction().rollback();
    //            System.out.println("Exception from -------------ClientServiceImpl.java----checkInOrOut()--"+e.getMessage());
    //           }
    //        return "fail";
    //    }

    @Override
    public String postTags(EntryData entryData) {
        Transaction tx;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        try {
            tx = session.beginTransaction();

            TagEntry tagentry = new TagEntry(new Date().toString(), entryData.getEntry_by(),
                    entryData.getEntries().length, entryData.getTab(), entryData.getEntry_type());

            Integer tagentryId = (Integer) session.save(tagentry);

            TagEntry tEntry = (TagEntry) session.get(TagEntry.class, tagentryId);

            for (Entry e : entryData.getEntries()) {
                TagLog tagLog = (TagLog) session.get(TagLog.class, Integer.parseInt(e.getTaglogId()));
                tagLog.setEntryDone(entryData.getEntry_type());
                tagLog.setTagEntryId(tagentryId.intValue());
                session.update(tagLog);
            }
            session.flush();
            session.clear();
            tx.commit();

            return "successfully entered";
        } catch (Exception e) {
            session.getTransaction().rollback();
            System.out
                    .println("Exception from -------------ClientServiceImpl.java----postTags()--" + e.getMessage());
        }
        return "fail";
    }

}