/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.delegate.job;
import beans.user.job.JobBean;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import beans.user.job.JobBeanRemote;
import clips.delegate.client.ClientLocal;
import cli_fmw.delegate.DelegateLine2;
import cli_fmw.delegate.cache.ExtraDataManager;
import cli_fmw.delegate.lists.DataChunkList;
import cli_fmw.main.ClipsException;
import cli_fmw.delegate.utils.TimeLocal;
import cli_fmw.main.DirectoryItemReplacedException;
import cli_fmw.utils.Selector;
import cli_fmw.utils.SelectorEditable;
import clips.delegate.DEC.DECClient;
import clips.delegate.directory.complex.DirectoryEnterprise;
import clips.delegate.directory.complex.DirectoryEnterpriseItem;
import clips.delegate.directory.simple.appointment.DirectoryAppointment;
import clips.delegate.directory.simple.appointment.DirectoryAppointmentItem;
import clips.delegate.directory.simple.profession.DirectoryProfession;
import clips.delegate.directory.simple.profession.DirectoryProfessionItem;
import beans.user.job.entity.DangerJobDetails;
import framework.beans.ModificationInfo;
import java.util.Iterator;
import framework.utils.Converter;
import beans.user.job.entity.JobDetails;
import cli_fmw.delegate.AuditListener;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
/**
* Делегат, предоставляющий доступ к одной записи в таблице job (работа)
* @author ViP
*/
public class JobLocal extends DelegateLine2<JobBeanRemote, JobDetails> {
private DECClient client = new DECClient(this, "информацию о пациенте", getAuditListener());
private DangersInfo dangers = new DangersInfo(getEDM());
private class DangersInfo extends DataChunkList<DangerExamChunk>{
public DangersInfo(ExtraDataManager contaner) {
super(contaner);
}
@Override
protected void deleteDB(DangerExamChunk data) throws Exception {
System.out.println("delete DANGER CHUNK: job: " + data.getDetails().jobId);
ModificationInfo info = getBean().removeDanger(data.getDetails());
auditDetailsList.addAll(info.getAudit());
fireAuditEvent();
}
@Override
protected int saveDB(DangerExamChunk data) throws Exception {
System.out.println("save DANGER CHUNK: job: " + data.getDetails().jobId);
ModificationInfo info = getBean().addDanger(data.getDetails());
auditDetailsList.addAll(info.getAudit());
fireAuditEvent();
return info.getId();
}
@Override
protected void loadDB() throws Exception {
if (!isNewlyCreated()){
Iterator<DangerJobDetails> it = getBean().getDangerList().iterator();
while (it.hasNext()) {
DangerJobDetails djd = it.next();
DangerExamChunk chunk = new DangerExamChunk(djd);
initByDetails(chunk);
}
}
}
}
public JobLocal(int id, AuditListener al) throws ClipsException {
super(id, al);
}
public JobLocal(JobDetails details, AuditListener al) throws ClipsException {
super(details, al);
}
public JobLocal(ClientLocal c, AuditListener al) throws ClipsException {
super(0, al);
client.initBy(c);
}
@Override
protected JobDetails getNewDetails() {
return new JobDetails();
}
public DirectoryProfessionItem getProfession() throws ClipsException {
DirectoryProfession profList = DirectoryLocator.getDirectory(DirectoryProfession.class);
try {
return profList.getItemFromID(getDetails().prof);
} catch (DirectoryItemReplacedException ex) {
reload();
return profList.getItemFromID(getDetails().prof);
}
}
public void setProfession(DirectoryProfessionItem prof) throws ClipsException {
getDetails().prof = prof.getID();
fireContentStateEvent();
}
public DirectoryAppointmentItem getAppointment() throws ClipsException {
DirectoryAppointment appointments = DirectoryLocator.getDirectory(DirectoryAppointment.class);
try {
return appointments.getItemFromID(getDetails().appoint);
} catch (DirectoryItemReplacedException ex) {
reload();
return appointments.getItemFromID(getDetails().appoint);
}
}
public void setAppointment(DirectoryAppointmentItem appoint) throws ClipsException {
getDetails().appoint = appoint.getID();
fireContentStateEvent();
}
public SelectorEditable<DangerExamChunk> dangerFactors() throws ClipsException{
return this.dangers.selector();
}
public Date getBegin() throws ClipsException {
return getDetails().begin;
}
public void setBegin(Date begin) throws ClipsException {
getDetails().begin = begin;
fireContentStateEvent();
}
public Date getEnd() throws ClipsException {
return getDetails().end;
}
public void setEnd(Date end) throws ClipsException {
getDetails().end = end;
fireContentStateEvent();
}
public DirectoryEnterpriseItem getEnterprise() throws ClipsException {
DirectoryEnterprise enterprises = DirectoryLocator.getDirectory(DirectoryEnterprise.class, false);
return enterprises.getItemFromID(getDetails().enterpriseID);
}
public void setEnterprise(DirectoryEnterpriseItem enterprise) throws ClipsException {
getDetails().enterpriseID = enterprise.getID();
fireContentStateEvent();
}
/**
* Уволен с работы или нет
* @return
* @throws ClipsException
*/
public boolean isFired() throws ClipsException {
if (getDetails().end == null) {
return false;
}
Calendar cal = TimeLocal.getCurrentTime();
if (cal.getTime().after(getDetails().end)) {
return true;
}
return false;
}
public HashMap<String, Object> getPrintFields() throws ClipsException {
HashMap<String, Object> extraFields = new HashMap<String, Object>();
extraFields.put("enterpriseTitle", getEnterprise().getFullTitle(false));
extraFields.put("enterpriseAddress", getEnterprise().getAddress().toString());
extraFields.put("dateIn", Converter.dateToString(getBegin()));
extraFields.put("dateOut", getEnd() == null ? "" : Converter.dateToString(getEnd()));
extraFields.put("jobProfession", getProfession().getTitle());
extraFields.put("jobAppointment", getAppointment().getTitle());
Selector<DangerExamChunk> dfs = dangerFactors();
String df = "";
for (int i = 0; i < dfs.size(); i++) {
df += " - " + dfs.get(i).getDanger().getTitle() + "\n";
}
if (df.isEmpty()) {
df = " ---";
}
extraFields.put("jobDanger", df);
return extraFields;
}
@Override
protected String getBeanName() {
return JobBean.class.getSimpleName();
}
}