package open.dolphin.ejb;
import java.util.*;
import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import open.dolphin.infomodel.DocumentModel;
import open.dolphin.infomodel.ModuleModel;
import org.apache.log4j.Logger;
import org.hibernate.CacheMode;
import org.hibernate.search.MassIndexer;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;
import org.jboss.ejb3.annotation.SecurityDomain;
@Stateless
@SecurityDomain("openDolphin")
@RolesAllowed("user")
@Remote({RemotePnsService.class})
/**
* いろいろやってみる remote service
* @author pns
*/
public class RemotePnsServiceImpl extends DolphinService implements RemotePnsService {
private static final long serialVersionUID = 1L;
private Logger logger = Logger.getLogger("CONSOLE");
@Resource
private SessionContext ctx;
@PersistenceContext
private EntityManager em;
/**
* patient_id から,今日のカルテ内容の module のリストを返す カルテがなければ null
* @param patientId
* @return
*/
@Override
public List<ModuleModel> peekKarte(Long patientId) {
try {
Long karteId = (Long) em.createQuery("select k.id from KarteBean k where k.patient.id = :patientId")
.setParameter("patientId", patientId)
.getSingleResult();
GregorianCalendar today = new GregorianCalendar();
today.set(Calendar.HOUR_OF_DAY, 0);
List docIdList = em.createQuery("select d.id from DocumentModel d where d.karte.id = :karteId and (d.status ='F' or d.status='T') and d.started >= :fromDate")
.setParameter("karteId", karteId)
.setParameter("fromDate", today.getTime())
.getResultList();
if (docIdList.isEmpty()) return null;
else {
Long docId = (Long) docIdList.get(0);
// m.document で DocumentModel がとれて,document.id で doc_id がとれる
List<ModuleModel> modules = em.createQuery("from ModuleModel m where m.document.id = :id")
.setParameter("id", docId)
.getResultList();
return modules;
}
} catch (Exception e) {
logger.info(e.getMessage(), e.getCause());
}
return null;
}
/**
* hibernate search のインデックスを作る
* standalone.xml のトランザクション default-timeout 変更必要
* ex) 4 hrs = 14400 secs
* coordinator-environment default-timeout="14400"
*/
@Override
public void makeInitialIndex() {
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
MassIndexer massIndexer = fullTextEntityManager.createIndexer(DocumentModel.class);
massIndexer.purgeAllOnStart(true)
.batchSizeToLoadObjects(30)
.threadsForSubsequentFetching(8)
.threadsToLoadObjects(4)
.cacheMode(CacheMode.NORMAL);
massIndexer.start();
}
}