/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package beans.contract.search;
import framework.beans.SecuredBean;
import framework.beans.security.BeanRights;
import beans.contract.entity.Contract;
import beans.contract.entity.ContractDetails;
import framework.generic.ClipsServerException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateful;
import beans.UserRightsSet;
import beans.directory.enterprise.Enterprise;
import beans.directory.lpu.entity.Lpu;
import framework.security.RightChecker;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.persistence.EntityManager;
import javax.persistence.Query;
/**
* Security - Ok.
* @author lacoste
*/
@Stateful(mappedName="clips-beans/ContractFactoryBean")
public class ContractFactoryBean extends SecuredBean implements ContractFactoryBeanRemote {
public static int COMMAND_READ = 0;
@Override
protected void initBeanRights() {
int [] r = new int[1];
r[COMMAND_READ] = RightPresence(UserRightsSet.READ_CONTRACT.id);
rights = new BeanRights(r);
}
@Override
public List<ContractDetails> getContractList(ContractFilter filter) throws ClipsServerException{
if (filter.lpuID == 0) {
throw new ClipsServerException("В фильтре договоров отсутствует поликлиника");
}
checkCommandAccessibility(COMMAND_READ);
String sql = "select a from " + Contract.class.getSimpleName() + " a";
ArrayList<String> fieldList = new ArrayList<String>();
if (filter.enterpriseId != -1) {
if (filter.enterpriseId == 0) {
fieldList.add("(a.enterprise is null)");
}
else {
fieldList.add("(a.enterprise = :ent)");
}
}
if (!filter.index.isEmpty()) {
fieldList.add("(a.index like :idx)");
}
if (filter.onlyOpen){
fieldList.add("(a.begin is not null) and (a.begin <= :datebegin) and ((a.end > :dateend) or (a.end is null))");
}
if (filter.onlyOpenForAll){
fieldList.add("(a.openForAll = true)");
}
fieldList.add("(a.lpu is null) or (a.lpu = :lpu)");
boolean isFirst = true;
for (String what : fieldList) {
if (isFirst){
sql += " where ";
isFirst = false;
}
else{
sql += " and ";
}
sql += what;
}
Query query = manager.createQuery(sql);
if (filter.enterpriseId != -1 && filter.enterpriseId != 0) {
Enterprise enterprise = findEntity(Enterprise.class, filter.enterpriseId);
query.setParameter("ent", enterprise);
}
if (!filter.index.isEmpty()) {
query.setParameter("idx", filter.index += "%");
}
if (filter.onlyOpen){
Date date = new Date();
query.setParameter("datebegin", date);
query.setParameter("dateend", date);
}
query.setParameter("lpu", findEntity(Lpu.class, filter.lpuID));
@SuppressWarnings("unchecked")
List<Contract> list = query.getResultList();
List<ContractDetails> res = new ArrayList<ContractDetails>();
for (Contract contract : list) {
res.add(contract.getDetails((RightChecker) this));
}
return res;
}
@Override
public List<ContractDetails> getContractsOMI() {
Query q = manager.createQuery("SELECT a FROM Contract a WHERE "
+ "(a.type = :ctype) "
+ "AND (a.begin < :current) "
+ "AND ((a.end IS NULL) OR (a.end > :current))");
q.setParameter("ctype", Contract.TYPE_OMI);
q.setParameter("current", GregorianCalendar.getInstance().getTime());
List list = q.getResultList();
List<ContractDetails> res = new ArrayList<ContractDetails>();
Iterator i = list.iterator();
while(i.hasNext()) {
Contract j = (Contract) i.next();
res.add(j.getDetails(this));
}
return res;
}
/**
* Возвращает все контракты ОМС у которых не истек срок действия, но возможно
* срок начала еще не наступил
* @return
*/
@Override
public List<ContractDetails> getContractsOMIForUpdate() {
Query q = manager.createQuery("SELECT a FROM Contract a WHERE "
+ "(a.type = :ctype) "
+ "AND ((a.end IS NULL) OR (a.end > :current))");
q.setParameter("ctype", Contract.TYPE_OMI);
q.setParameter("current", GregorianCalendar.getInstance().getTime());
List list = q.getResultList();
List<ContractDetails> res = new ArrayList<ContractDetails>();
Iterator i = list.iterator();
while(i.hasNext()) {
Contract j = (Contract) i.next();
res.add(j.getDetails(this));
}
return res;
}
}