/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.laboratory;
import framework.beans.SecuredBean;
import framework.beans.security.BeanRights;
import framework.beans.FindEntity.Field;
import beans.doctor.checkup.Checkup;
import beans.doctor.checkup.CheckupDetails;
import framework.generic.ClipsServerException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import beans.UserRightsSet; import framework.security.RightChecker;
import framework.utils.DateTimeUtils;
/**
* Security - Ok.
* @author vip
*/
@Stateful(mappedName="clips-beans/CheckupSearchBean")
public class CheckupSearchBean extends SecuredBean implements CheckupSearchBeanRemote {
public static int COMMAND_READ_ANALYSE_LIST = 0;
@Override
protected void initBeanRights() {
int [] r = new int[1];
int k = RightPresence(UserRightsSet.READ_MEDICAL_DATA.id);
if (k > 0) {
r[COMMAND_READ_ANALYSE_LIST] = k;
} else {
r[COMMAND_READ_ANALYSE_LIST] = RightPresence(UserRightsSet.READ_ANALYSE_LIST.id);
}
rights = new BeanRights(r);
}
/**
* Поиск в базе чекапов (анализов) по заданным параметрам
* за исключнием чекапов отмененных услуг
* @param laboratoryID чекапы должны принадлежать данной лаборатории != 0
* @param serviceID либо 0 (любые услуги) либо поиск ограничен конкретной услугой
* @param rendered true если только оказанные false если только не оказанные
* @return
*/
@Override
public ArrayList<CheckupDetails> getAnalyseList(int laboratoryID, int serviceID, boolean rendered, Date from, Date to) throws ClipsServerException{
//Приведем from и to к датам
from = DateTimeUtils.getDateOnly(from);
to = DateTimeUtils.getDateOnly(to);
to = DateTimeUtils.getNextDay(to);
ArrayList<CheckupDetails> detailsList = new ArrayList<CheckupDetails>();
checkCommandAccessibility(COMMAND_READ_ANALYSE_LIST);
try {
String sRendered = "";
if (rendered){
sRendered = " AND ch.serviceRender.renderedDate is not null";
}else{
sRendered = " AND ch.serviceRender.renderedDate is null";
}
String sql = "SELECT ch FROM Laboratory lab, IN (lab.checkupTypes) as type, Checkup ch"
+ " WHERE lab.id=:laboratoryID"
+ " AND ch.checkupType = type"
+ " AND ch.checkupType.isAnalyse = true"
+ sRendered
+ " AND ch.serviceRender.cancelled = false"
+ " AND ch.serviceRender.date >= :dateFrom"
+ " AND ch.serviceRender.date < :dateTo";
if (serviceID != 0) {
sql += " AND ch.serviceRender.service.id=:serviceID";
}
Query query = manager.createQuery(sql);
query.setParameter("laboratoryID", laboratoryID);
query.setParameter("dateFrom", from);
query.setParameter("dateTo", to);
if (serviceID != 0) {
query.setParameter("serviceID", serviceID);
}
@SuppressWarnings("unchecked")
List<Checkup> res = query.getResultList();
for(int i=0; i<res.size(); i++) {
detailsList.add(res.get(i).getDetails((RightChecker) this));
}
} catch (NoResultException ex) {
//do nothing
}
return detailsList;
}
@Override
public ArrayList<CheckupDetails> getClientAnalyseList(int clientID) throws ClipsServerException{
checkCommandAccessibility(COMMAND_READ_ANALYSE_LIST);
ArrayList<CheckupDetails> res = new ArrayList<CheckupDetails>();
Field field[] = {
new Field("serviceRender.disease.emc.client.id", clientID),
new Field("serviceRender.cancelled", false),
new Field("checkupType.isAnalyse", true)
};
List list = findEntityList(Checkup.class, field);
Iterator i = list.iterator();
while(i.hasNext()) {
Checkup j = (Checkup) i.next();
res.add(j.getDetails((RightChecker) this));
}
return res;
}
@Override
public ArrayList<CheckupDetails> getClientCheckupList(int clientID) throws ClipsServerException{
checkCommandAccessibility(COMMAND_READ_ANALYSE_LIST);
ArrayList<CheckupDetails> res = new ArrayList<CheckupDetails>();
Field field[] = {
new Field("serviceRender.disease.emc.client.id", clientID),
new Field("serviceRender.cancelled", false),
new Field("checkupType.isAnalyse", false)
};
List list = findEntityList(Checkup.class, field);
Iterator i = list.iterator();
while(i.hasNext()) {
Checkup j = (Checkup) i.next();
res.add(j.getDetails((RightChecker) this));
}
return res;
}
@Override
public ArrayList<CheckupDetails> getFreeAnalysesList() throws ClipsServerException {
checkCommandAccessibility(COMMAND_READ_ANALYSE_LIST);
ArrayList<CheckupDetails> res = new ArrayList<CheckupDetails>();
//не в расписании
Field[] field = {
new Field("serviceRender.renderedDate","", Field.OPERATOR_IS_NULL),
new Field("serviceRender.functions", "", Field.OPERATOR_IS_NULL),
new Field("serviceRender.cancelled", false),
new Field("checkupType.isAnalyse", true),
new Field("checkupShedule", "", Field.OPERATOR_IS_NULL),
};
List list = findEntityList(Checkup.class, field);
Iterator i = list.iterator();
while(i.hasNext()) {
Checkup j = (Checkup) i.next();
res.add(j.getDetails((RightChecker) this));
}
//расписание просрочено
Date current = new Date();
current = DateTimeUtils.getDateOnly(current);
Field[] field1 = {
new Field("serviceRender.renderedDate", "", Field.OPERATOR_IS_NULL),
new Field("serviceRender.functions", "", Field.OPERATOR_IS_NULL),
new Field("serviceRender.cancelled", false),
new Field("checkupType.isAnalyse", true),
new Field("checkupShedule.date", current, Field.OPERATOR_LESS)
};
list = findEntityList(Checkup.class, field1);
i = list.iterator();
while(i.hasNext()) {
Checkup j = (Checkup) i.next();
res.add(j.getDetails((RightChecker) this));
}
return res;
}
}