/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.profcheckup.search;
import beans.directory.lpu.entity.Lpu;
import framework.beans.SecuredBean;
import beans.contract.entity.Contract;
import beans.contract.entity.Polis;
import beans.contract.entity.PolisDetails;
import beans.directory.enterprise.Enterprise;
import beans.directory.enterprise.EnterpriseDetails;
import beans.profcheckup.entity.Profcheckup;
import beans.profcheckup.entity.ProfcheckupDetails;
import beans.profcheckup.entity.ProfcheckupItem;
import beans.profcheckup.entity.ProfcheckupItemDetails;
import beans.doctor.disease.DiseaseDetails;
import beans.user.collaborator.entities.Collaborator;
import beans.user.job.entity.DangerJob;
import framework.generic.ClipsServerException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.Query;
import framework.security.RightChecker;
/**
* @security @todo
* @author petr
*/
@Stateful(mappedName="clips-beans/ProfcheckupSearchBean")
public class ProfcheckupSearchBean extends SecuredBean implements ProfcheckupSearchBeanRemote {
@Override
public List<ProfcheckupDetails> getFullListDetailed() {
ArrayList <ProfcheckupDetails> det = new ArrayList<ProfcheckupDetails>();
Iterator res = findEntityList(Profcheckup.class).iterator();
while(res.hasNext()) {
Profcheckup p = (Profcheckup) res.next();
det.add(p.getDetails((RightChecker) this));
}
return det;
}
@Override
public List<Integer> getFullList() {
ArrayList <Integer> det = new ArrayList<Integer>();
Iterator res = findEntityList(Profcheckup.class).iterator();
while(res.hasNext()) {
Profcheckup p = (Profcheckup) res.next();
det.add(p.getId());
}
return det;
}
@Override
public List<ProfcheckupDetails> getListDetailedBetweenDates(Date begin, Date end) {
Field fields[] = {
new Field("datefrom", begin, Field.OPERATOR_EQUAL_OR_MORE),
new Field("datefrom", end, Field.OPERATOR_EQUAL_OR_LESS)
};
Collection<Profcheckup> serRens = findEntityList(Profcheckup.class, fields);
List<ProfcheckupDetails> res = new ArrayList<ProfcheckupDetails>();
Iterator<Profcheckup> i = serRens.iterator();
while(i.hasNext()) {
Profcheckup j = i.next();
res.add(j.getDetails((RightChecker) this));
}
return res;
}
@Override
public List<ProfcheckupDetails> getListDetailedAfterDate(Date begin) {
Field fields[] = {
new Field("datefrom", begin, Field.OPERATOR_EQUAL_OR_MORE),
};
Collection<Profcheckup> serRens = findEntityList(Profcheckup.class, fields);
List<ProfcheckupDetails> res = new ArrayList<ProfcheckupDetails>();
Iterator<Profcheckup> i = serRens.iterator();
while(i.hasNext()) {
Profcheckup j = i.next();
res.add(j.getDetails((RightChecker) this));
}
return res;
}
@Override
public List<ProfcheckupDetails> getListDetailedBeforeDate(Date end) {
Field fields[] = {
new Field("datefrom", end, Field.OPERATOR_EQUAL_OR_LESS)
};
Collection<Profcheckup> serRens = findEntityList(Profcheckup.class, fields);
List<ProfcheckupDetails> res = new ArrayList<ProfcheckupDetails>();
Iterator<Profcheckup> i = serRens.iterator();
while(i.hasNext()) {
Profcheckup j = i.next();
res.add(j.getDetails((RightChecker) this));
}
return res;
}
@Override
public List<ProfcheckupItemDetails> getProfcheckupItemList(DiseaseDetails diseaseDetails){
ArrayList<ProfcheckupItemDetails> res = new ArrayList<ProfcheckupItemDetails>();
String sql = "SELECT pi FROM ProfcheckupItem pi "+
"WHERE pi.disease.id = :diseaseID";
Query query = manager.createQuery(sql);
// query.setParameter("pcid", pd.id);
query.setParameter("diseaseID", diseaseDetails.id);
Iterator iterator = query.getResultList().iterator();
while (iterator.hasNext()) {
ProfcheckupItem pi = (ProfcheckupItem) iterator.next();
res.add(pi.getDetails(this));
}
return res;
}
@Override
public List<ProfcheckupContractChunk> getUsableContractsM(EnterpriseDetails ed) throws ClipsServerException {
ArrayList<ProfcheckupContractChunk> list = new ArrayList<ProfcheckupContractChunk>();
Lpu lpu = findEntity(Collaborator.class, getCollaboratorId()).getLpu();
Date date = new Date();
String sql = "SELECT pol, dj " +
"FROM Polis pol WHERE pol.contract.enterprise.id IN (SELECT DISTINCT dj.job.enterprise.id FROM DangerJob dj) " +
"AND dj.job.client = pol.client " +
"AND pol.member = true " +
"AND pol.trash = false " +
"AND dj.job.enterprise.id = pol.contract.enterprise.id " +
"AND (dj.job.begin<=current_date OR dj.job.begin IS NULL) " +
"AND (dj.job.end > current_date OR dj.job.end IS NULL) " +
"AND pol.contract.begin <= current_date " +
"AND (pol.contract.end > current_date OR pol.contract.end IS NULL) " +
"AND pol.contract.enterprise.id = :eid " +
"AND (pol.contract.lpu is null or pol.contract.lpu = :lpu)";
Query query = manager.createQuery(sql);
// query.setParameter("dnow", date);
// query.setParameter("dnow1", date);
query.setParameter("eid", ed.id);
query.setParameter("lpu", lpu);
// query.setParameter("eid1", ed.id);
Iterator iterator = query.getResultList().iterator();
while(iterator.hasNext()) {
ProfcheckupContractChunk chunk = new ProfcheckupContractChunk();
Object[] objects = (Object[]) iterator.next();
Polis pol = (Polis)objects[0];
DangerJob d = (DangerJob)objects[1];
chunk.danger = d.getDetails((RightChecker) this);
chunk.polis = pol.getDetails((RightChecker) this);
chunk.contract = pol.getContract().getDetails((RightChecker) this);
chunk.client = pol.getClient().getDetails((RightChecker) this);
System.out.println("danger: " + chunk.danger.dangerId + " job: " + chunk.danger.jobId + " polis: " + chunk.polis.id + " contract: " + chunk.contract.id + " client: " + pol.getClient().getFio());
list.add(chunk);
}
return list;
}
public List<PolisDetails> getActivPolicses(){
ArrayList<PolisDetails> list = new ArrayList<PolisDetails>();
return list;
}
}