2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.agent
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
14 import java
.util
.UUID
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.apache
.lucene
.analysis
.SimpleAnalyzer
;
18 import org
.apache
.lucene
.queryParser
.ParseException
;
19 import org
.apache
.lucene
.queryParser
.QueryParser
;
20 import org
.hibernate
.Criteria
;
21 import org
.hibernate
.Query
;
22 import org
.hibernate
.Session
;
23 import org
.hibernate
.criterion
.Restrictions
;
24 import org
.hibernate
.envers
.query
.AuditEntity
;
25 import org
.hibernate
.envers
.query
.AuditQuery
;
26 import org
.hibernate
.search
.FullTextSession
;
27 import org
.hibernate
.search
.Search
;
28 import org
.hibernate
.search
.SearchFactory
;
29 import org
.springframework
.stereotype
.Repository
;
31 import eu
.etaxonomy
.cdm
.model
.agent
.Address
;
32 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
33 import eu
.etaxonomy
.cdm
.model
.agent
.Institution
;
34 import eu
.etaxonomy
.cdm
.model
.agent
.InstitutionalMembership
;
35 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
36 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
37 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
38 import eu
.etaxonomy
.cdm
.model
.common
.UuidAndTitleCache
;
39 import eu
.etaxonomy
.cdm
.model
.view
.AuditEvent
;
40 import eu
.etaxonomy
.cdm
.persistence
.dao
.QueryParseException
;
41 import eu
.etaxonomy
.cdm
.persistence
.dao
.agent
.IAgentDao
;
42 import eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
.IdentifiableDaoBase
;
43 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
47 public class AgentDaoImpl
extends IdentifiableDaoBase
<AgentBase
> implements IAgentDao
{
49 private String defaultField
= "titleCache";
50 private Class
<?
extends AgentBase
> indexedClasses
[];
53 @SuppressWarnings("unused")
54 private static final Logger logger
= Logger
.getLogger(AgentDaoImpl
.class);
56 public AgentDaoImpl() {
57 super(AgentBase
.class);
58 indexedClasses
= new Class
[3];
59 indexedClasses
[0] = Institution
.class;
60 indexedClasses
[1] = Person
.class;
61 indexedClasses
[2] = Team
.class;
64 public List
<Institution
> getInstitutionByCode(String code
) {
65 AuditEvent auditEvent
= getAuditEventFromContext();
66 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
67 Criteria crit
= getSession().createCriteria(Institution
.class);
68 crit
.add(Restrictions
.eq("code", code
));
69 return (List
<Institution
>)crit
.list();
71 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(Institution
.class,auditEvent
.getRevisionNumber());
72 query
.add(AuditEntity
.property("code").eq(code
));
73 return (List
<Institution
>)query
.getResultList();
77 public int countInstitutionalMemberships(Person person
) {
78 AuditEvent auditEvent
= getAuditEventFromContext();
79 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
80 Query query
= getSession().createQuery("select count(institutionalMembership) from InstitutionalMembership institutionalMembership where institutionalMembership.person = :person");
81 query
.setParameter("person", person
);
82 return ((Long
)query
.uniqueResult()).intValue();
84 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(InstitutionalMembership
.class,auditEvent
.getRevisionNumber());
85 query
.add(AuditEntity
.relatedId("person").eq(person
.getId()));
86 query
.addProjection(AuditEntity
.id().count("id"));
87 return ((Long
)query
.getSingleResult()).intValue();
91 public int countMembers(Team team
) {
92 checkNotInPriorView("AgentDaoImpl.countMembers(Team team)");
93 Query query
= getSession().createQuery("select count(teamMember) from Team team join team.teamMembers teamMember where team = :team");
94 query
.setParameter("team", team
);
95 return ((Long
)query
.uniqueResult()).intValue();
98 public List
<InstitutionalMembership
> getInstitutionalMemberships(Person person
, Integer pageSize
, Integer pageNumber
) {
99 AuditEvent auditEvent
= getAuditEventFromContext();
100 if(auditEvent
.equals(AuditEvent
.CURRENT_VIEW
)) {
101 Query query
= getSession().createQuery("select institutionalMembership from InstitutionalMembership institutionalMembership left join fetch institutionalMembership.institute where institutionalMembership.person = :person");
102 query
.setParameter("person", person
);
103 setPagingParameter(query
, pageSize
, pageNumber
);
104 return (List
<InstitutionalMembership
>)query
.list();
106 AuditQuery query
= getAuditReader().createQuery().forEntitiesAtRevision(InstitutionalMembership
.class,auditEvent
.getRevisionNumber());
107 query
.add(AuditEntity
.relatedId("person").eq(person
.getId()));
108 setPagingParameter(query
, pageSize
, pageNumber
);
109 return (List
<InstitutionalMembership
>)query
.getResultList();
113 public List
<Person
> getMembers(Team team
, Integer pageSize
, Integer pageNumber
) {
114 checkNotInPriorView("AgentDaoImpl.getMembers(Team team, Integer pageSize, Integer pageNumber)");
115 Query query
= getSession().createQuery("select teamMember from Team team join team.teamMembers teamMember where team = :team order by sortindex");
116 query
.setParameter("team", team
);
117 //query.addOrder( Order.asc("sortindex") );
118 setPagingParameter(query
, pageSize
, pageNumber
);
119 return (List
<Person
>)query
.list();
122 public Integer
countAddresses(AgentBase agent
) {
123 checkNotInPriorView("AgentDaoImpl.countAddresses(AgentBase agent)");
124 Query query
= getSession().createQuery("select count(address) from AgentBase agent join agent.contact.addresses address where agent = :agent");
125 query
.setParameter("agent", agent
);
126 return ((Long
)query
.uniqueResult()).intValue();
129 public List
<Address
> getAddresses(AgentBase agent
, Integer pageSize
,Integer pageNumber
) {
130 checkNotInPriorView("AgentDaoImpl.getAddresses(AgentBase agent, Integer pageSize,Integer pageNumber)");
131 Query query
= getSession().createQuery("select address from AgentBase agent join agent.contact.addresses address where agent = :agent");
132 query
.setParameter("agent", agent
);
133 setPagingParameter(query
, pageSize
, pageNumber
);
134 return (List
<Address
>)query
.list();
139 public int count(Class
<?
extends AgentBase
> clazz
, String queryString
) {
140 checkNotInPriorView("AgentDaoHibernateImpl.count(String queryString, Boolean accepted)");
141 QueryParser queryParser
= new QueryParser(defaultField
, new SimpleAnalyzer());
144 org
.apache
.lucene
.search
.Query query
= queryParser
.parse(queryString
);
146 FullTextSession fullTextSession
= Search
.getFullTextSession(this.getSession());
147 org
.hibernate
.search
.FullTextQuery fullTextQuery
= null;
150 fullTextQuery
= fullTextSession
.createFullTextQuery(query
, type
);
152 fullTextQuery
= fullTextSession
.createFullTextQuery(query
, clazz
);
155 Integer result
= fullTextQuery
.getResultSize();
158 } catch (ParseException e
) {
159 throw new QueryParseException(e
, queryString
);
163 public void purgeIndex() {
164 FullTextSession fullTextSession
= Search
.getFullTextSession(getSession());
165 for(Class clazz
: indexedClasses
) {
166 fullTextSession
.purgeAll(clazz
); // remove all taxon base from indexes
168 fullTextSession
.flushToIndexes();
171 public void rebuildIndex() {
172 FullTextSession fullTextSession
= Search
.getFullTextSession(getSession());
174 for(AgentBase agentBase
: list(null,null)) { // re-index all agents
175 fullTextSession
.index(agentBase
);
177 fullTextSession
.flushToIndexes();
180 public List
<AgentBase
> search(Class
<?
extends AgentBase
> clazz
, String queryString
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
181 checkNotInPriorView("AgentDaoHibernateImpl.searchTaxa(String queryString, Boolean accepted, Integer pageSize, Integer pageNumber)");
182 QueryParser queryParser
= new QueryParser(defaultField
, new SimpleAnalyzer());
183 List
<AgentBase
> results
= new ArrayList
<AgentBase
>();
186 org
.apache
.lucene
.search
.Query query
= queryParser
.parse(queryString
);
188 FullTextSession fullTextSession
= Search
.getFullTextSession(getSession());
189 org
.hibernate
.search
.FullTextQuery fullTextQuery
= null;
192 fullTextQuery
= fullTextSession
.createFullTextQuery(query
, type
);
194 fullTextQuery
= fullTextSession
.createFullTextQuery(query
, clazz
);
197 addOrder(fullTextQuery
,orderHints
);
199 if(pageSize
!= null) {
200 fullTextQuery
.setMaxResults(pageSize
);
201 if(pageNumber
!= null) {
202 fullTextQuery
.setFirstResult(pageNumber
* pageSize
);
204 fullTextQuery
.setFirstResult(0);
208 List
<AgentBase
> result
= (List
<AgentBase
>)fullTextQuery
.list();
209 defaultBeanInitializer
.initializeAll(result
, propertyPaths
);
212 } catch (ParseException e
) {
213 throw new QueryParseException(e
, queryString
);
217 public String
suggestQuery(String string
) {
218 throw new UnsupportedOperationException("suggestQuery is not supported for AgentBase");
221 public void optimizeIndex() {
222 FullTextSession fullTextSession
= Search
.getFullTextSession(getSession());
223 SearchFactory searchFactory
= fullTextSession
.getSearchFactory();
224 for(Class clazz
: indexedClasses
) {
225 searchFactory
.optimize(clazz
); // optimize the indices ()
227 fullTextSession
.flushToIndexes();
231 * @see eu.etaxonomy.cdm.persistence.dao.agent.IAgentDao#getTeamOrPersonBaseUuidAndNomenclaturalTitle()
233 public List
<UuidAndTitleCache
<TeamOrPersonBase
>> getTeamOrPersonBaseUuidAndNomenclaturalTitle() {
234 List
<UuidAndTitleCache
<TeamOrPersonBase
>> list
= new ArrayList
<UuidAndTitleCache
<TeamOrPersonBase
>>();
235 Session session
= getSession();
237 Query query
= session
.createQuery("select uuid, nomenclaturalTitle from " + type
.getSimpleName() + " where dtype = 'Person' or dtype = 'Team'");
239 List
<Object
[]> result
= query
.list();
241 for(Object
[] object
: result
){
242 list
.add(new UuidAndTitleCache
<TeamOrPersonBase
>(TeamOrPersonBase
.class, (UUID
) object
[0], (String
) object
[1]));
249 * @see eu.etaxonomy.cdm.persistence.dao.agent.IAgentDao#getTeamUuidAndTitleCache()
251 public List
<UuidAndTitleCache
<Person
>> getPersonUuidAndNomenclaturalTitle() {
252 List
<UuidAndTitleCache
<Person
>> list
= new ArrayList
<UuidAndTitleCache
<Person
>>();
253 Session session
= getSession();
255 Query query
= session
.createQuery("select uuid, nomenclaturalTitle from " + type
.getSimpleName() + " where dtype = 'Person'");
257 List
<Object
[]> result
= query
.list();
259 for(Object
[] object
: result
){
260 list
.add(new UuidAndTitleCache
<Person
>(Person
.class, (UUID
) object
[0], (String
) object
[1]));