/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.service.search;
import beans.UserRightsSet;
import beans.doctor.disease.Disease;
import beans.doctor.disease.DiseaseDetails;
import beans.service.ServiceRender;
import beans.service.ServiceRenderDetails;
import beans.user.collaborator.entities.Collaborator;
import framework.beans.SecuredBean;
import framework.beans.security.BeanRights;
import framework.generic.ClipsServerException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateful;
import javax.persistence.Query;
/**
*
* @author lacoste
*/
@Stateful(mappedName="clips-beans/SerRenSearchBean")
public class SerRenSearchBean extends SecuredBean implements SerRenSearchBeanRemote {
public static int COMMAND_READ = 0;
@Override
protected void initBeanRights() {
int[] r = new int[1];
r[COMMAND_READ] = RightPresence(UserRightsSet.READ_MEDICAL_DATA.id);
rights = new BeanRights(r);
}
@Override
public ArrayList<ServiceRenderDetails> getFilteredSerRenList(SerRenFilter filter) throws ClipsServerException {
checkCommandAccessibility(COMMAND_READ);
ArrayList<Field> fs = new ArrayList<Field>();
if (filter.dateFrom != null){
fs.add(new Field("renderedDate", filter.dateFrom, Field.OPERATOR_EQUAL_OR_MORE));
}
if (filter.dateTill != null){
fs.add(new Field("renderedDate", filter.dateTill, Field.OPERATOR_LESS));
}
if (filter.rendered != null){
fs.add(new Field("rendered", filter.rendered));
}
if (filter.contratId != 0){
fs.add(new Field("polis.contract.id", filter.contratId));
}
if (filter.serviceId != 0){
fs.add(new Field("service.id", filter.serviceId));
}
Iterator<Field> it = fs.iterator();
Field fields[] = new Field[fs.size()];
int i = 0;
while (it.hasNext()){
fields[i] = it.next();
i++;
}
List<ServiceRender> srl = findEntityList(ServiceRender.class, fields);
ArrayList<ServiceRenderDetails> res = new ArrayList<ServiceRenderDetails>();
for (int j = 0; j < srl.size(); j++) {
ServiceRender serviceRender = srl.get(j);
res.add(serviceRender.getDetails(this));
}
return res;
}
/**
* Возвращает список неоказанных услуг в по сотруднику в указанном диапазоне
* @param collabID может быть 0
* @param from может быть null
* @param till может быть null
* @return
* @throws ClipsServerException
*/
@Override
public Set<DiseaseChunk> getOpenedDiseases(int collabID, Date from, Date till) throws ClipsServerException {
checkCommandAccessibility(COMMAND_READ);
String sql = "SELECT r.disease FROM ServiceRender r" +
" WHERE r.disease.closed IS NULL";
if (collabID != 0) {
sql += " AND (r.director.id = :collabID OR r.disease.collaborator.id = :collabID)";
}
if (from != null) {
sql += " AND r.date >= :dateFrom";
}
if (till != null) {
sql += " AND r.date < :dateTill";
}
Query query = manager.createQuery(sql);
if (collabID != 0) {
query.setParameter("collabID", collabID);
}
if (from != null) {
query.setParameter("dateFrom", from);
}
if (till != null) {
query.setParameter("dateTill", till);
}
List<Disease> resultList = query.getResultList();
HashSet<DiseaseChunk> target = new HashSet<DiseaseChunk>();
for (Disease disease : resultList) {
DiseaseChunk dc = new DiseaseChunk(
disease.getEmc().getClient().getId(),
disease.getEmc().getClient().getFio(),
disease.getDetails(this));
target.add(dc);
}
return target;
}
}