Revision a51ab35e
Added by Katja Luther over 6 years ago
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/UuidAndTitleCacheTaxonComparator.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2017 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
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. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.model.taxon; |
|
10 |
|
|
11 |
import java.io.Serializable; |
|
12 |
import java.util.Comparator; |
|
13 |
import java.util.UUID; |
|
14 |
|
|
15 |
import eu.etaxonomy.cdm.model.name.Rank; |
|
16 |
|
|
17 |
/** |
|
18 |
* @author k.luther |
|
19 |
* @date 06.12.2017 |
|
20 |
* |
|
21 |
*/ |
|
22 |
public class UuidAndTitleCacheTaxonComparator implements Serializable, Comparator<Object[]>{ |
|
23 |
|
|
24 |
|
|
25 |
private static final long serialVersionUID = 6000794425983318091L; |
|
26 |
|
|
27 |
|
|
28 |
@Override |
|
29 |
public int compare(Object[] o1, Object[] o2) { |
|
30 |
//same UUID |
|
31 |
|
|
32 |
if (o1[0].equals(o2[0])){ |
|
33 |
return 0; |
|
34 |
} |
|
35 |
|
|
36 |
//Rank |
|
37 |
Rank rankTax1 = (Rank)o1[3]; |
|
38 |
Rank rankTax2 = (Rank)o2[3]; |
|
39 |
|
|
40 |
String titleCache1 = (String)o1[2]; |
|
41 |
String titleCache2 = (String)o2[2]; |
|
42 |
//first compare ranks, if ranks are equal (or both null) compare names or taxon title cache if names are null |
|
43 |
if (rankTax1 == null && rankTax2 != null){ |
|
44 |
return 1; |
|
45 |
}else if(rankTax2 == null && rankTax1 != null){ |
|
46 |
return -1; |
|
47 |
}else if (rankTax1 != null && rankTax1.isHigher(rankTax2)){ |
|
48 |
return -1; |
|
49 |
}else if (rankTax1 == null && rankTax2 == null || rankTax1.equals(rankTax2)) { |
|
50 |
if (titleCache1 != null && titleCache2 != null){ |
|
51 |
//same rank, order by titleCache |
|
52 |
int result = titleCache1.compareTo(titleCache2); |
|
53 |
if (result == 0){ |
|
54 |
return ((UUID)o1[0]).compareTo((UUID)o2[0]); |
|
55 |
}else{ |
|
56 |
return result; |
|
57 |
} |
|
58 |
} |
|
59 |
}else{ |
|
60 |
//rankTax2.isHigher(rankTax1) |
|
61 |
return 1; |
|
62 |
} |
|
63 |
return 0; |
|
64 |
} |
|
65 |
|
|
66 |
} |
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/taxon/TaxonDaoHibernateImpl.java | ||
---|---|---|
26 | 26 |
import org.hibernate.FetchMode; |
27 | 27 |
import org.hibernate.Hibernate; |
28 | 28 |
import org.hibernate.Query; |
29 |
import org.hibernate.Session; |
|
29 | 30 |
import org.hibernate.criterion.Criterion; |
30 | 31 |
import org.hibernate.criterion.Order; |
31 | 32 |
import org.hibernate.criterion.Projections; |
... | ... | |
60 | 61 |
import eu.etaxonomy.cdm.model.taxon.TaxonNode; |
61 | 62 |
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship; |
62 | 63 |
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType; |
64 |
import eu.etaxonomy.cdm.model.taxon.UuidAndTitleCacheTaxonComparator; |
|
63 | 65 |
import eu.etaxonomy.cdm.model.view.AuditEvent; |
64 | 66 |
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.IdentifiableDaoBase; |
65 | 67 |
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao; |
... | ... | |
1854 | 1856 |
return results; |
1855 | 1857 |
} |
1856 | 1858 |
|
1859 |
@Override |
|
1860 |
public List<UuidAndTitleCache<TaxonBase>> getUuidAndTitleCache(Integer limit, String pattern){ |
|
1861 |
Session session = getSession(); |
|
1862 |
Query query = null; |
|
1863 |
if (pattern != null){ |
|
1864 |
query = session.createQuery("select taxonBase.uuid, taxonBase.id, taxonBase.titleCache, taxonBase.name.rank from TaxonBase as taxonBase where taxonBase.titleCache like :pattern"); |
|
1865 |
pattern = pattern.replace("*", "%"); |
|
1866 |
pattern = pattern.replace("?", "_"); |
|
1867 |
pattern = pattern + "%"; |
|
1868 |
query.setParameter("pattern", pattern); |
|
1869 |
} else { |
|
1870 |
query = session.createQuery("select uuid, id, titleCache, taxonBase.name.rank from TaxonBase " ); |
|
1871 |
} |
|
1872 |
if (limit != null){ |
|
1873 |
query.setMaxResults(limit); |
|
1874 |
} |
|
1875 |
|
|
1876 |
return getUuidAndTitleCache(query); |
|
1877 |
} |
|
1878 |
|
|
1879 |
@Override |
|
1880 |
protected List<UuidAndTitleCache<TaxonBase>> getUuidAndTitleCache(Query query){ |
|
1881 |
List<UuidAndTitleCache<TaxonBase>> list = new ArrayList<UuidAndTitleCache<TaxonBase>>(); |
|
1882 |
|
|
1883 |
List<Object[]> result = query.list(); |
|
1884 |
if (result != null && !result.isEmpty()){ |
|
1885 |
if (result.iterator().next().length == 4){ |
|
1886 |
Collections.sort(result, new UuidAndTitleCacheTaxonComparator()); |
|
1887 |
} |
|
1888 |
} |
|
1889 |
|
|
1890 |
for(Object[] object : result){ |
|
1891 |
list.add(new UuidAndTitleCache<TaxonBase>((UUID) object[0],(Integer) object[1], (String) object[2])); |
|
1892 |
} |
|
1893 |
return list; |
|
1894 |
} |
|
1895 |
|
|
1857 | 1896 |
|
1858 | 1897 |
} |
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/taxon/TaxonNodeDaoHibernateImpl.java | ||
---|---|---|
12 | 12 |
import java.math.BigInteger; |
13 | 13 |
import java.util.ArrayList; |
14 | 14 |
import java.util.Collection; |
15 |
import java.util.Collections; |
|
15 | 16 |
import java.util.HashMap; |
16 | 17 |
import java.util.HashSet; |
17 | 18 |
import java.util.Iterator; |
... | ... | |
38 | 39 |
import eu.etaxonomy.cdm.model.taxon.TaxonBase; |
39 | 40 |
import eu.etaxonomy.cdm.model.taxon.TaxonNode; |
40 | 41 |
import eu.etaxonomy.cdm.model.taxon.TaxonNodeAgentRelation; |
42 |
import eu.etaxonomy.cdm.model.taxon.UuidAndTitleCacheTaxonComparator; |
|
41 | 43 |
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.AnnotatableDaoImpl; |
42 | 44 |
import eu.etaxonomy.cdm.persistence.dao.taxon.IClassificationDao; |
43 | 45 |
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao; |
... | ... | |
155 | 157 |
|
156 | 158 |
@Override |
157 | 159 |
public List<UuidAndTitleCache<TaxonNode>> getUuidAndTitleCache(Integer limit, String pattern, UUID classificationUuid) { |
158 |
String queryString = "select tn.uuid, tn.id, tx.titleCache from TaxonNode tn " |
|
160 |
String queryString = "select tn.uuid, tn.id, tx.titleCache, tx.name.rank from TaxonNode tn "
|
|
159 | 161 |
+ "INNER JOIN tn.taxon as tx " |
160 | 162 |
+ "INNER JOIN tn.classification as cls " |
161 | 163 |
+ "WHERE tx.titleCache like :pattern "; |
... | ... | |
172 | 174 |
@SuppressWarnings("unchecked") |
173 | 175 |
List<Object[]> result = query.list(); |
174 | 176 |
|
177 |
if (result != null){ |
|
178 |
Collections.sort(result, new UuidAndTitleCacheTaxonComparator()); |
|
179 |
} |
|
175 | 180 |
for(Object[] object : result){ |
176 | 181 |
list.add(new UuidAndTitleCache<TaxonNode>((UUID) object[0],(Integer) object[1], (String) object[2])); |
177 | 182 |
} |
Also available in: Unified diff
ref #7078: add uuidAndTitleCacheComparator to sort by rank and name