/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.user.job;
import beans.directory.enterprise.Enterprise;
import beans.directory.simple.entities.Appointment;
import beans.directory.simple.entities.Profession;
import beans.user.client.entity.Client;
import beans.user.job.entity.JobDetails;
import framework.beans.EntityDetails;
import framework.beans.FacadeBean;
import framework.beans.security.BeanRights;
import beans.user.job.entity.DangerJob;
import beans.user.job.entity.DangerJobDetails;
import beans.user.job.entity.DangerJobPK;
import beans.user.job.entity.Job;
import framework.generic.ClipsServerException;
import framework.generic.EDataIntegrity;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.ejb.EJBException;
import javax.ejb.Stateful;
import beans.UserRightsSet;
import beans.directory.danger.entity.Danger;
import framework.audit.AuditDoc;
import framework.beans.ModificationInfo;
import framework.security.UserRight;
/**
* @security - Ok.
* @author axe
*/
@Stateful(mappedName="clips-beans/JobBean")
public class JobBean extends FacadeBean<Job>
implements JobBeanRemote {
public static final UserRight RIGHT_CREATION = UserRightsSet.WRITE_CLIENT_PUBLIC_INFO;
public JobBean() {
super(Job.class);
}
@Override
protected void initBeanRights() {
int[] r = new int[4];
r[COMMAND_READ] = RightPresence(UserRightsSet.READ_CLIENT_PUBLIC_INFO.id);
r[COMMAND_CREATE] = RightPresence(RIGHT_CREATION.id);
int cmr = RightPresence(UserRightsSet.WRITE_CLIENT_PUBLIC_INFO.id);
r[COMMAND_MODIFY] = cmr;
r[COMMAND_REMOVE] = cmr;
rights = new BeanRights(r);
}
/**
* Обновляет данные сущности.
* @param details новые детали сущности
* @throws EJBException в случае если обновление отвергнуто системой
* безопастности либо произошла ошибка
* @return идентификатор сущности
*/
@Override
protected void onUpdate(Job entity, EntityDetails details,
AuditDoc auditDoc, List<AuditDoc> auditDocList) throws ClipsServerException {
JobDetails d = (JobDetails) details;
if (entity.getId() == 0) {
entity.setClient(findEntity(Client.class, d.clientID));
} else if (d.clientID != entity.getClient().getId()) {
throw new ClipsServerException("Попытка записи данных о работе другому пациенту");
}
if (d.enterpriseID == 0) {
throw new ClipsServerException("Не указано предприятие");
}
if (d.begin != null) {
entity.setBegin(d.begin);
} else {
throw new ClipsServerException("Не указана дата принятия на работу");
}
entity.setEnd(d.end == null ? null : d.end);
entity.setEnterprise(findEntity(Enterprise.class, d.enterpriseID));
entity.setProfession(d.prof == 0 ? null : findEntity(Profession.class, d.prof));
entity.setAppointment(d.appoint == 0 ? null : findEntity(Appointment.class, d.appoint));
}
@Override
protected void onRemove(Job entity, List<AuditDoc> auditDocList) throws ClipsServerException {
deleteEntityList2(DangerJob.class,
new Field[]{new Field("job", entity)},
auditDocList
);
}
@Override
public ModificationInfo addDanger(DangerJobDetails details) throws ClipsServerException{
checkCommandAccessibility(COMMAND_MODIFY);
Job entity = getExistentEntity();
int jobId = entity.getId();
if (details.jobId != 0 && jobId != details.jobId){
throw new EDataIntegrity("Запрещено подменять работу опасному фактору! job(entity): "+jobId + " job(details): " + details.jobId);
}
Field f[] = {
new Field("key.job", jobId),
new Field("key.danger", details.dangerId)
};
List list = findEntityList(DangerJob.class, f);
DangerJob dangerJob = null;
AuditDoc<DangerJob> auditDJ;
if (!list.isEmpty()){
dangerJob = findEntityList(DangerJob.class, f).get(0);//Должна быть одна
auditDJ = new AuditDoc<DangerJob>(dangerJob, getCollaborator());
dangerJob.setLastExam(details.lastExam);
dangerJob.setExamManually(true);///Если заюзано из бина значит руками
}else {
auditDJ = new AuditDoc<DangerJob>(null, getCollaborator());
dangerJob = new DangerJob();
dangerJob.setKey(new DangerJobPK(details.dangerId, jobId));
dangerJob.setDanger(findEntity(Danger.class, details.dangerId));
dangerJob.setJob(findEntity(Job.class, jobId));
dangerJob.setLastExam(details.lastExam);
dangerJob.setExamManually(details.lastExamManually);
}
int id = saveEntity(dangerJob);
auditDJ.check(dangerJob);
return new ModificationInfo(id, persistAudit(auditDJ));
}
@Override
public Set<DangerJobDetails> getDangerList() throws ClipsServerException {
checkCommandAccessibility(COMMAND_READ);
Job entity = getExistentEntity();
int jobId = entity.getId();
if (jobId == 0) {
throw new EDataIntegrity("");
}
Iterator list = findEntityList(DangerJob.class, "key.job", jobId).iterator();
Set<DangerJobDetails> res = new HashSet<DangerJobDetails>();
while (list.hasNext()) {
DangerJob ds = (DangerJob) list.next();
DangerJobDetails chunk = new DangerJobDetails();
chunk.jobId = jobId;
chunk.dangerId = ds.getKey().getDanger();
chunk.lastExam = ds.getLastExam();
chunk.lastExamManually = ds.isExamManually();
res.add(chunk);
}
return res;
}
@Override
public ModificationInfo removeDanger(DangerJobDetails details) throws ClipsServerException {
checkCommandAccessibility(COMMAND_REMOVE);
Job entity = getExistentEntity();
int jobId = entity.getId();
if (jobId != details.jobId){
throw new EDataIntegrity("Вредный фактор относится к другой работе. Удаление невозможно!");
}
checkEntityExist();
Field f[] = {
new Field("key.job", jobId),
new Field("key.danger", details.dangerId)
};
DangerJob dangerJob = findEntityList(DangerJob.class, f).get(0);//Должна быть одна
checkTheSame(findEntity(Job.class, dangerJob.getKey().getJob()));
AuditDoc<DangerJob> auditDJ = new AuditDoc<DangerJob>(dangerJob, getCollaborator());
manager.remove(dangerJob);
auditDJ.check(null);
return new ModificationInfo(0, persistAudit(auditDJ));
}
}