/*
* SheduleExceptionLocal.java
*
*/
package clips.delegate.shedule.exception;
import beans.shedule.exceptions.SheduleExceptionBeanRemote;
import beans.shedule.exceptions.SheduleExceptionDetails;
import cli_fmw.delegate.cache.ExtraDataManager;
import cli_fmw.main.ClipsException;
import beans.shedule.exceptions.SheduleExceptionBean;
import cli_fmw.delegate.DelegateStandart;
import cli_fmw.delegate.lists.DataChunkMapList;
import cli_fmw.utils.Log;
import clips.login.UserInfo;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
/**
* Делегат для работы с исключительными днями.
* @author Axe Ilshat
*/
public class SheduleExceptionLocal extends DelegateStandart<SheduleExceptionBeanRemote> {
private static SheduleExceptionLocal instance;
private static int sessionID;
private ExtraDataManager ecm;
//map year*100 + month(0-11) => DayList
private HashMap <Integer, DayList> daysList = new HashMap<Integer, DayList>();
/**
* Синглетон контсруктор
* @return
* @throws ClipsException
*/
public static SheduleExceptionLocal getInstance() throws ClipsException {
if(instance == null || UserInfo.get().getSessionId() != sessionID) {
instance = new SheduleExceptionLocal();
sessionID = UserInfo.get().getSessionId();
}
return instance;
}
/**
* Creates a new instance of SheduleExceptionLocal
*/
private SheduleExceptionLocal() throws ClipsException {
super(SheduleExceptionBean.class.getSimpleName());
initBean();
ecm = new ExtraDataManager(getBean(), this, false);
}
@Override
public boolean canModify() throws ClipsException {
return getBeanRights().isCommandAccessible(SheduleExceptionBean.COMMAND_MODIFY);
}
/**
* Удаляет исключение.
* @param day день (1-31)
* @param month месяц (1-12)
* @param year год (1900-2100)
* @throws ClipsException
*/
public void removeException(Date day)
throws ClipsException {
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(day);
DayList dayList = get(cal.get(Calendar.MONTH), cal.get(Calendar.YEAR));
SheduleExceptionData data = dayList.get(cal.get(Calendar.DAY_OF_MONTH));
Log.printlnAnsPos(data);
dayList.selector().remove(data);
}
public SheduleExceptionData getException(Date day) throws ClipsException {
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(day);
DayList dayList = get(cal.get(Calendar.MONTH), cal.get(Calendar.YEAR));
return dayList.get(cal.get(Calendar.DAY_OF_MONTH));
}
public void setException(SheduleExceptionData data) throws ClipsException {
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(data.getDay());
DayList dayList = get(cal.get(Calendar.MONTH), cal.get(Calendar.YEAR));
SheduleExceptionData d = dayList.get(cal.get(Calendar.DAY_OF_MONTH));
if(data == d) {
//do nothing
} else if(d == null) {
dayList.selector().append(data);
} else {
throw new ClipsException("You must use existent exception");
}
}
/**
*
* @throws ClipsException
*/
@Override
public void save1() throws ClipsException {
Iterator<DayList> i = daysList.values().iterator();
while(i.hasNext()) {
i.next().save();
}
}
/**
*
*/
@Override
public void restore() {
Iterator<DayList> i = daysList.values().iterator();
while(i.hasNext()) {
i.next().restore();
}
}
/**
* @return
*/
@Override
public boolean isDirty() {
Iterator<DayList> i = daysList.values().iterator();
while(i.hasNext()) {
if(i.next().isDirty()) {
return true;
}
}
return false;
}
/**
*
* @param month 0-11
* @param year
* @return
* @throws ClipsException
*/
private DayList get(int month, int year) throws ClipsException {
int key = year*100 + month;
DayList dl = daysList.get(key);
if(dl == null) {
dl = new DayList(ecm, month, year);
daysList.put(key, dl);
}
return dl;
}
class DayList extends DataChunkMapList<SheduleExceptionData, Integer> {
private int month, year;
/**
*
* @param contaner
* @param month (0-11)
* @param year
*/
public DayList(ExtraDataManager contaner, int month, int year) {
super(contaner);
this.month = month;
this.year = year;
}
@Override
protected void deleteDB(SheduleExceptionData data) throws Exception {
bean().removeException(data.getDay());
}
@Override
protected int saveDB(SheduleExceptionData data) throws Exception {
return bean().setException(data.getDay(), data.isWorking(), data.getDescription(),
data.getBeginTime().getRaw(), data.getDuration());
}
@Override
protected void loadDB() throws Exception {
for(SheduleExceptionDetails d: bean().getExceptionList(month+1, year)) {
initByDetails(new SheduleExceptionData(d));
}
}
}
}