* The contents of this file are subject to the Mozilla Public License Version 1.1
* See LICENSE.TXT at the top of this package for the full license terms.
*/
-
package eu.etaxonomy.cdm.persistence.dao.hibernate.term;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
+import java.util.Map;
import java.util.Set;
import java.util.UUID;
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
import eu.etaxonomy.cdm.model.media.Media;
import eu.etaxonomy.cdm.model.media.RightsType;
-import eu.etaxonomy.cdm.model.metadata.NamedAreaSearchField;
+import eu.etaxonomy.cdm.model.metadata.TermSearchField;
import eu.etaxonomy.cdm.model.name.HybridRelationshipType;
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
private static final Logger logger = Logger.getLogger(DefinedTermDaoImpl.class);
- public DefinedTermDaoImpl() {
+ @SuppressWarnings("unchecked")
+ public DefinedTermDaoImpl() {
super(DefinedTermBase.class);
indexedClasses = new Class[25];
indexedClasses[0] = Rank.class;
* Searches by Label
*/
@Override
- public List<DefinedTermBase> findByTitle(String queryString) {
- return findByTitle(queryString, null);
+ public List<DefinedTermBase> findByLabel(String queryString) {
+ return findByLabel(queryString, null);
}
/**
* Searches by Label
*/
@Override
- public List<DefinedTermBase> findByTitle(String queryString, CdmBase sessionObject) {
+ public List<DefinedTermBase> findByLabel(String queryString, CdmBase sessionObject) {
checkNotInPriorView("DefinedTermDaoImpl.findByTitle(String queryString, CdmBase sessionObject)");
Session session = getSession();
if ( sessionObject != null ) {//attache the object to the session, TODO needed?
session.update(sessionObject);
}
- Query query = session.createQuery("select term from DefinedTermBase term join fetch term.representations representation where representation.label = :label");
+ Query query = session.createQuery("SELECT term "
+ + " FROM DefinedTermBase term JOIN FETCH term.representations representation "
+ + " WHERE representation.label = :label");
query.setParameter("label", queryString);
@SuppressWarnings({ "unchecked", "rawtypes" })
- List<DefinedTermBase> result = query.list();
+ List<DefinedTermBase> result = deduplicateResult(query.list());
return result;
-
}
@Override
crit.setMaxResults(pagesize);
int firstItem = (page - 1) * pagesize + 1;
crit.setFirstResult(firstItem);
- List<DefinedTermBase> results = crit.list();
+ @SuppressWarnings("unchecked")
+ List<DefinedTermBase> results = deduplicateResult(crit.list());
return results;
}
addPageSizeAndNumber(criteria, pageSize, pageNumber);
@SuppressWarnings("unchecked")
- List<T> result = criteria.list();
+ List<T> result = deduplicateResult(criteria.list());
return result;
}
addPageSizeAndNumber(criteria, pageSize, pageNumber);
@SuppressWarnings("unchecked")
- List<T> result = criteria.list();
+ List<T> result = deduplicateResult(criteria.list());
return result;
}
addPageSizeAndNumber(criteria, pageSize, pageNumber);
@SuppressWarnings("unchecked")
- List<T> result = criteria.list();
+ List<T> result = deduplicateResult(criteria.list());
return result;
}
String queryStr;
if (isIso639_1){
- queryStr = "from Language where iso639_1 = :isoCode";
+ queryStr = "FROM Language WHERE iso639_1 = :isoCode";
}else{
- queryStr = "from Language where idInVocabulary = :isoCode and vocabulary.uuid = :vocUuid";
+ queryStr = "FROM Language WHERE idInVocabulary = :isoCode AND vocabulary.uuid = :vocUuid";
}
AuditEvent auditEvent = getAuditEventFromContext();
if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
*/
@Override
public List<Language> getLanguagesByIso(List<String> iso639List) {
- List<Language> languages = new ArrayList<Language>(iso639List.size());
+ List<Language> languages = new ArrayList<>(iso639List.size());
for (String iso639 : iso639List) {
languages.add(getLanguageByIso(iso639));
}
@Override
public List<Language> getLanguagesByLocale(Enumeration<Locale> locales) {
- List<Language> languages = new ArrayList<Language>();
+ List<Language> languages = new ArrayList<>();
while(locales.hasMoreElements()) {
Locale locale = locales.nextElement();
languages.add(getLanguageByIso(locale.getLanguage()));
@Override
public List<Media> getMedia(DefinedTermBase definedTerm, Integer pageSize, Integer pageNumber) {
checkNotInPriorView("DefinedTermDaoImpl.getMedia(DefinedTermBase definedTerm, Integer pageSize, Integer pageNumber)");
- Query query = getSession().createQuery("select media from DefinedTermBase definedTerm join definedTerm.media media where definedTerm = :definedTerm");
+ Query query = getSession().createQuery(
+ "SELECT media "
+ + " FROM DefinedTermBase definedTerm "
+ + " JOIN definedTerm.media media "
+ + " WHERE definedTerm = :definedTerm");
query.setParameter("definedTerm", definedTerm);
addPageSizeAndNumber(query, pageSize, pageNumber);
addPageSizeAndNumber(criteria, pageSize, pageNumber);
@SuppressWarnings("unchecked")
- List<NamedArea> result = criteria.list();
+ List<NamedArea> result = deduplicateResult(criteria.list());
return result;
} else {
AuditQuery query = makeAuditQuery(NamedArea.class, auditEvent);
}
@SuppressWarnings("unchecked")
- List<NamedArea> result = query.getResultList();
+ List<NamedArea> result = deduplicateResult(query.getResultList());
return result;
}
}
addOrder(criteria,orderHints);
addPageSizeAndNumber(criteria, pageSize, pageNumber);
- result = criteria.list();
+ result = deduplicateResult(criteria.list());
} else {
AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(NamedArea.class,
if (type != null) {
query.add(AuditEntity.relatedId("type").eq(type.getId()));
}
- result = query.getResultList();
+ result = deduplicateResult(query.getResultList());
}
defaultBeanInitializer.initializeAll(result, propertyPaths);
addPageSizeAndNumber(query, pageSize, pageNumber);
@SuppressWarnings("unchecked")
- List<T> result = query.list();
+ List<T> result = deduplicateResult(query.list());
return result;
} else {
AuditQuery query = makeAuditQuery(DefinedTermBase.class, auditEvent);
addPageSizeAndNumber(query, pageSize, pageNumber);
@SuppressWarnings("unchecked")
- List<T> result = query.getResultList();
+ List<T> result = deduplicateResult(query.getResultList());
return result;
}
}
addPageSizeAndNumber(query, pageSize, pageNumber);
@SuppressWarnings("unchecked")
- List<T> results = query.list();
+ List<T> results = deduplicateResult(query.list());
defaultBeanInitializer.initializeAll(results, propertyPaths);
return results;
} else {
- List<T> result = new ArrayList<T>();
+ List<T> result = new ArrayList<>();
for(T t : partOf) {
AuditQuery query = makeAuditQuery(DefinedTermBase.class, auditEvent);
query.add(AuditEntity.relatedId("partOf").eq(t.getId()));
addPageSizeAndNumber(query, pageSize, pageNumber);
- result.addAll(query.getResultList());
+ result.addAll(deduplicateResult(query.getResultList()));
}
defaultBeanInitializer.initializeAll(result, propertyPaths);
return result;
query.setParameter("termType", termType);
@SuppressWarnings("unchecked")
- List<T> result = query.list();
+ List<T> result = deduplicateResult(query.list());
defaultBeanInitializer.initializeAll(result, propertyPaths);
return result;
Query query = getSession().createQuery("FROM " + clazz.getSimpleName());
@SuppressWarnings("unchecked")
- List<TERM> result = query.list();
+ List<TERM> result = deduplicateResult(query.list());
defaultBeanInitializer.initializeAll(result, propertyPaths);
* @param orginals
* @return
*/
- private <S extends DefinedTermBase<?>> List<S> deduplicateResult(List<S> orginals) {
+ protected static <S extends CdmBase> List<S> deduplicateResult(List<S> orginals) {
List<S> result = new ArrayList<>();
Iterator<S> it = orginals.iterator();
S last = null;
}
@Override
- public List<NamedArea> listNamedArea(List<TermVocabulary> vocs, Integer pageNumber, Integer limit, String pattern, MatchMode matchmode){
+ public <S extends DefinedTermBase> List<S> list(Class<S> clazz, List<TermVocabulary> vocs, Integer limit, String pattern) {
+ return list(clazz, vocs, 0, limit, pattern, MatchMode.BEGINNING);
+ }
+
+ @Override
+ public <S extends DefinedTermBase> List<S> list(Class<S> clazz, List<TermVocabulary> vocs, Integer pageNumber, Integer limit, String pattern, MatchMode matchmode){
Session session = getSession();
-// Query query = null;
-// if (pattern != null){
-// if (vocs != null && !vocs.isEmpty()){
-// query = session.createQuery("from NamedArea where titleCache like :pattern and vocabulary in :vocs ORDER BY titleCache");
-// query.setParameterList("vocs", vocs);
-// }else{
-// query = session.createQuery("from NamedArea where titleCache like :pattern ORDER BY titleCache");
-// }
-// pattern = pattern.replace("*", "%");
-// pattern = pattern.replace("?", "_");
-// pattern = pattern + "%";
-// query.setParameter("pattern", pattern);
-//
-// } else {
-// query = session.createQuery("FROM NamedArea WHERE vocabulary IN :vocs ORDER BY titleCache");
-// query.setParameterList("vocs", vocs);
-// }
-// if (limit != null && limit > 0){
-// query.setMaxResults(limit);
-// }
-
- Criteria crit = getSession().createCriteria(type, "namedArea");
+ if (clazz == null){
+ clazz = (Class)type;
+ }
+ Criteria crit = getSession().createCriteria(clazz, "term");
if (!StringUtils.isBlank(pattern)){
+ crit.createAlias("term.representations", "reps");
+ Disjunction or = Restrictions.disjunction();
if (matchmode == MatchMode.EXACT) {
- crit.add(Restrictions.eq("titleCache", matchmode.queryStringFrom(pattern)));
+ or.add(Restrictions.eq("titleCache", matchmode.queryStringFrom(pattern)));
+ or.add(Restrictions.eq("reps.label", matchmode.queryStringFrom(pattern)));
} else {
- // crit.add(Restrictions.ilike("titleCache", matchmode.queryStringFrom(queryString)));
- crit.add(Restrictions.like("titleCache", matchmode.queryStringFrom(pattern)));
+ or.add(Restrictions.like("titleCache", matchmode.queryStringFrom(pattern)));
+ or.add(Restrictions.like("reps.label", matchmode.queryStringFrom(pattern)));
}
+ crit.add(or);
}
+
if (limit != null && limit >= 0) {
crit.setMaxResults(limit);
}
if (vocs != null &&!vocs.isEmpty()){
- crit.createAlias("namedArea.vocabulary", "voc");
+ crit.createAlias("term.vocabulary", "voc");
Disjunction or = Restrictions.disjunction();
for (TermVocabulary<?> voc: vocs){
Criterion criterion = Restrictions.eq("voc.id", voc.getId());
if (limit == null){
limit = 1;
}
-// int firstItem = (pageNumber - 1) * limit;
-
crit.setFirstResult(0);
@SuppressWarnings("unchecked")
- List<NamedArea> results = crit.list();
+ List<S> results = deduplicateResult(crit.list());
return results;
}
+ @Override
+ public <S extends DefinedTermBase> List<S> listByAbbrev(Class<S> clazz, List<TermVocabulary> vocs, Integer limit, String pattern, TermSearchField type) {
+ return listByAbbrev(clazz, vocs, 0, limit, pattern, MatchMode.BEGINNING, type);
+ }
@Override
- public List<NamedArea> listNamedAreaByAbbrev(List<TermVocabulary> vocs, Integer pageNumber, Integer limit, String pattern, MatchMode matchmode, NamedAreaSearchField abbrevType){
+ public <S extends DefinedTermBase> List<S> listByAbbrev(Class<S> clazz, List<TermVocabulary> vocs, Integer pageNumber, Integer limit, String pattern, MatchMode matchmode, TermSearchField abbrevType){
- Criteria crit = getSession().createCriteria(type, "namedArea");
+ if (clazz == null){
+ clazz = (Class)type;
+ }
+ Criteria crit = getSession().createCriteria(clazz, "type");
if (!StringUtils.isBlank(pattern)){
if (matchmode == MatchMode.EXACT) {
crit.add(Restrictions.eq(abbrevType.getKey(), matchmode.queryStringFrom(pattern)));
} else {
- // crit.add(Restrictions.ilike("titleCache", matchmode.queryStringFrom(queryString)));
crit.add(Restrictions.like(abbrevType.getKey(), matchmode.queryStringFrom(pattern)));
}
}
}
if (vocs != null &&!vocs.isEmpty()){
- crit.createAlias("namedArea.vocabulary", "voc");
+ crit.createAlias("type.vocabulary", "voc");
Disjunction or = Restrictions.disjunction();
for (TermVocabulary<?> voc: vocs){
Criterion criterion = Restrictions.eq("voc.id", voc.getId());
crit.setFirstResult(0);
@SuppressWarnings("unchecked")
- List<NamedArea> results = crit.list();
+ List<S> results = deduplicateResult(crit.list());
return results;
}
- @Override
- public long count(List<TermVocabulary> vocs, String pattern){
- Session session = getSession();
- Query query = null;
- if (pattern != null){
- if (vocs != null && !vocs.isEmpty()){
- query = session.createQuery("SELECT COUNT(*) FROM NamedArea WHERE titleCache LIKE :pattern AND vocabulary IN :vocs");
- query.setParameterList("vocs", vocs);
- }else{
- query = session.createQuery("SELECT COUNT(*) FROM NamedArea WHERE titleCache LIKE :pattern ");
- }
- pattern = pattern.replace("*", "%");
- pattern = pattern.replace("?", "_");
- pattern = pattern + "%";
- query.setParameter("pattern", pattern);
-
- } else {
- query = session.createQuery("SELECT COUNT(*) FROM NamedArea WHERE vocabulary IN :vocs");
- query.setParameterList("vocs", vocs);
- }
-
- Long result = (Long) query.uniqueResult();
- return result;
- }
@Override
public Collection<TermDto> getIncludesAsDto(
}
@Override
- public Collection<TermDto> getKindOfsAsDto(
- TermDto parentTerm) {
+ public Collection<TermDto> getKindOfsAsDto(TermDto parentTerm) {
+
String queryString;
if (parentTerm.getTermType().equals(TermType.NamedArea)){
queryString = TermDto.getTermDtoSelectNamedArea();
return list;
}
+ @Override
+ public TermDto findByUUIDAsDto(UUID uuid) {
+ String queryString = TermDto.getTermDtoSelect()
+ + " where a.uuid like :uuid ";
+
+
+ Query query = getSession().createQuery(queryString);
+ query.setParameter("uuid", uuid);
+
+
+ @SuppressWarnings("unchecked")
+ List<Object[]> result = query.list();
+
+ List<TermDto> list = TermDto.termDtoListFrom(result);
+ if (list.size()== 1){
+ return list.get(0);
+ }else{
+ return null;
+ }
+
+ }
+
+
@Override
public Collection<TermDto> findByTypeAsDto(TermType termType) {
if (termType == null){
+ " where a.termType = :termType ";
Query query = getSession().createQuery(queryString);
- if(termType!=null){
- query.setParameter("termType", termType);
- }
+ query.setParameter("termType", termType);
@SuppressWarnings("unchecked")
List<Object[]> result = query.list();
}
@Override
- public List<NamedArea> listNamedArea(List<TermVocabulary> vocs, Integer limit, String pattern) {
+ public Map<UUID, List<TermDto>> getSupportedStatesForFeature(Set<UUID> featureUuids){
+ Map<UUID, List<TermDto>> map = new HashMap<>();
+ for (UUID featureUuid: featureUuids){
+ List<TermDto> list = new ArrayList<>();
+ String supportedCategoriesQueryString = "SELECT cat.uuid "
+ + "from DefinedTermBase t "
+ + "join t.supportedCategoricalEnumerations as cat "
+ + "where t.uuid = :featureUuid";
+ Query supportedCategoriesQuery = getSession().createQuery(supportedCategoriesQueryString);
+ supportedCategoriesQuery.setParameter("featureUuid", featureUuid);
+ @SuppressWarnings("unchecked")
+ List<UUID> supportedCategories = supportedCategoriesQuery.list();
+ if(supportedCategories.isEmpty()){
+ map.put(featureUuid, list);
+ continue;
+ }
- return listNamedArea(vocs, 0, limit, pattern, MatchMode.BEGINNING);
- }
+ String queryString = TermDto.getTermDtoSelect()
+ + "where v.uuid in (:supportedCategories) "
+ + "order by a.titleCache";
+ Query query = getSession().createQuery(queryString);
+ query.setParameterList("supportedCategories", supportedCategories);
- @Override
- public List<NamedArea> listNamedAreaByAbbrev(List<TermVocabulary> vocs, Integer limit, String pattern, NamedAreaSearchField type) {
+ @SuppressWarnings("unchecked")
+ List<Object[]> result = query.list();
- return listNamedAreaByAbbrev(vocs, 0, limit, pattern, MatchMode.BEGINNING, type);
- }
-
- @Override
- public List<TermDto> getSupportedStatesForFeature(UUID featureUuid){
- List<TermDto> list = new ArrayList<>();
- String supportedCategoriesQueryString = "SELECT cat.uuid "
- + "from DefinedTermBase t "
- + "join t.supportedCategoricalEnumerations as cat "
- + "where t.uuid = :featureUuid";
- Query supportedCategoriesQuery = getSession().createQuery(supportedCategoriesQueryString);
- supportedCategoriesQuery.setParameter("featureUuid", featureUuid);
- @SuppressWarnings("unchecked")
- List<UUID> supportedCategories = supportedCategoriesQuery.list();
- if(supportedCategories.isEmpty()){
- return list;
+ list = TermDto.termDtoListFrom(result);
+ map.put(featureUuid, list);
}
-
- String queryString = TermDto.getTermDtoSelect()
- + "where v.uuid in (:supportedCategories) "
- + "order by a.titleCache";
- Query query = getSession().createQuery(queryString);
- query.setParameterList("supportedCategories", supportedCategories);
-
- @SuppressWarnings("unchecked")
- List<Object[]> result = query.list();
-
- list = TermDto.termDtoListFrom(result);
- return list;
+ return map;
}
@Override
return list;
}
-}
+ @Override
+ public TermDto getTermDto(UUID uuid) {
+ String queryString = TermDto.getTermDtoSelect()
+ + " where a.uuid = :uuid ";
+
+
+ Query query = getSession().createQuery(queryString);
+ query.setParameter("uuid", uuid);
+
+
+
+ @SuppressWarnings("unchecked")
+ List<Object[]> result = query.list();
+ TermDto dto = null;
+ List<TermDto> dtoList = TermDto.termDtoListFrom(result);
+ if (dtoList != null && !dtoList.isEmpty()){
+ dto = dtoList.get(0);
+ }
+ return dto;
+ }
+}
\ No newline at end of file