package com.dal.employee;
import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import com.dal.Util;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.Db4o;
import com.db4o.config.Configuration;
import com.db4o.constraints.UniqueFieldValueConstraint;
import com.db4o.query.Predicate;
public class EmployeeDAL
{
private ObjectContainer db;
public EmployeeDAL()
{
//new File(Util.DB4OFILENAME).delete();
db = Db4o.openFile(Util.DB4OFILENAME);
}
public static void main(String args[])
{
EmployeeDAL ed = new EmployeeDAL();
try
{
ed.insertEmployee(101,2000,"sai","MALE","TL",25000);
//ed.insertEmployee(101,2000,"saisaran","MALE","TL",25000);
ed.updateAddressToEmployee(101,"117/17","Rayaji Street","Nellore","AP","INDIA",524001);
ed.updateContactToEmployee(101,916590423,0,0);
ed.updateSkillsToEmployee(101, "employee", 2);
ed.insertDependentToEmployee(101, "xxx","male" ,20, "yyy");
ed.insertDependentToEmployee(101, "ttt", "male", 19, "zzz");
ed.addinTimeToEmployee(101);
ed.addoutTimeToEmployee(101);
Util.retrieveAll(ed.db);
}
catch(Exception e)
{
System.out.println("This is"+e);
ed.db.rollback();
Util.retrieveAll(ed.db);
}
finally
{
ed.db.close();
}
}
public void insertEmployee(final int empid, int ssn, String name, String gender, String designation, float salary)
{
Employee emp = new Employee(empid, ssn, name, gender, designation, salary);
List<Employee> result = db.query(new Predicate<Employee>(){
public boolean match(Employee emp)
{
return emp.getEmpid() == empid;
}
});
if(result.size() != 0)
{
System.out.println("This employee already exists..");
return;
}
db.store(emp);
System.out.println("Stored: " + emp);
}
public void updateAddressToEmployee(final int empid,String dno, String street, String city, String state, String country, int zip)
{
Address address = new Address( dno, street, city, state, country, zip);
Employee found = update(empid).get(0);
if(found != null)
{
db.delete(found.getAddress());
found.setAddress(address);
db.store(found);
}
}
public void updateContactToEmployee(final int empid,long mobileno, long landline, long emergencyno)
{
Contact contact = new Contact(mobileno, landline, emergencyno);
Employee found = update(empid).get(0);
if(found != null)
{
db.delete(found.getContact());
found.setContact(contact);
db.store(found);
}
}
public void insertDependentToEmployee(final int empid, String dname, String gender, int age, String relationship)
{
Dependent dependent = new Dependent(empid, dname, gender, age, relationship);
Employee found = update(empid).get(0);
if(found != null)
{
db.store(dependent);
}
else
{
System.out.println("Employee doesnot exist");
}
}
public void updateSkillsToEmployee(final int empid,String proficiency, double experience)
{
Skills skills = new Skills(proficiency, experience);
Employee found = update(empid).get(0);
if(found != null)
{
db.delete(found.getSkills());
found.setSkills(skills);
db.store(found);
}
}
public void addinTimeToEmployee(int empid)
{
String date = getDate();
String inTime = getTime();
Attendence attendence = new Attendence(empid, date, inTime, null);
Employee found = update(empid).get(0);
if(found != null)
{
db.store(attendence);
}
}
public void addoutTimeToEmployee(final int empid)
{
String date = getDate();
String outTime = getTime();
List<Attendence> result = db.query(new Predicate<Attendence>(){
public boolean match(Attendence att)
{
return att.getEmpid() == empid;
}
});
for(int i = 0; i < result.size(); i++)
{
Attendence attendence = result.get(i);
if(attendence.getDate().equals(date) && attendence.getOutTime().equals(null))
{
attendence.setOutTime(outTime);
db.store(attendence);
}
}
}
private String getTime()
{
Calendar calendar = new GregorianCalendar();
String am_pm, time;
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
if(calendar.get(Calendar.AM_PM) == 0)
am_pm = "AM";
else
am_pm = "PM";
time = hour + ":" + minute + ":" + second + " " + am_pm;
return time;
}
private String getDate()
{
String date;
Calendar cal = new GregorianCalendar();
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
int day = cal.get(Calendar.DAY_OF_MONTH);
date = day + "/" + (month + 1) + "/" + year;
return date;
}
public List<Employee> update(final int empid)
{
List<Employee> result = db.query(new Predicate<Employee>(){
public boolean match(Employee emp)
{
return emp.getEmpid() == empid;
}
});
return result;
}
}