/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.followup.search;
import framework.beans.SecuredBean;
import framework.beans.security.BeanRights;
import beans.followup.entity.Followup;
import framework.generic.ESecurity;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.Query;
import beans.UserRightsSet;
import beans.followup.entity.FollowupEvent;
import beans.user.client.entity.Client;
/**
*
* @author lacoste
*/
@Stateless(mappedName="clips-beans/FollowUpFactoryBean")
public class FollowUpFactoryBean extends SecuredBean implements FollowUpFactoryBeanRemote {
public static int COMMAND_READ = 0;
@Override
protected void initBeanRights() {
int [] r = new int[1];
r[COMMAND_READ] = RightPresence(UserRightsSet.READ_FOLLOWUP_EVENT.id);
rights = new BeanRights(r);
}
@Override
public ArrayList<FollowupData> getFollowUpsComing(Date from, Date till, boolean all, int clientID) throws ESecurity {
String sql = "SELECT f, e, f.serrenUp.disease.emc.client FROM Followup f, FollowupEvent e WHERE f.id = e.follup.id ";
sql += clientID == 0 ? "" : "AND f.serrenUp.disease.emc.client.id=:clientID ";
sql += "AND e.date in (SELECT MAX(ev.date) FROM FollowupEvent ev WHERE ev.follup.id=f.id) " +
"AND e.date >= :dateFrom AND e.date <= :dateTill";
sql += all ? "" : " AND e.disease IS NULL";
Query query = manager.createQuery(sql);
query.setParameter("dateFrom", from);
query.setParameter("dateTill", till);
if (clientID != 0) {
query.setParameter("clientID", clientID);
}
@SuppressWarnings("unchecked")
List<Object[]> res = query.getResultList();
ArrayList<FollowupData> target = new ArrayList<FollowupData>();
for (int i = 0; i < res.size(); i++) {
Object[] line = res.get(i);
Followup f = (Followup) line[0];
FollowupEvent e = (FollowupEvent) line[1];
Client c = (Client) line [2];
FollowupData d = new FollowupData();
d.details = f.getDetails(this);
d.fio = c.getFio();
d.clientID = c.getId();
d.lastEventDetails = e.getDetails(this);
target.add(d);
}
return target;
}
}