-// $Id$
/**
* Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy
+* European Distributed Institute of Taxonomy
* http://www.e-taxonomy.eu
-*
+*
* 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.taxeditor.store;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
+import java.util.HashSet;
import java.util.List;
+import java.util.Set;
import eu.etaxonomy.cdm.api.service.ITermService;
-import eu.etaxonomy.cdm.api.service.IVocabularyService;
-import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
-import eu.etaxonomy.cdm.model.common.AnnotationType;
-import eu.etaxonomy.cdm.model.common.DefinedTermBase;
-import eu.etaxonomy.cdm.model.common.ExtensionType;
-import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
+import eu.etaxonomy.cdm.model.term.DefinedTermBase;
import eu.etaxonomy.cdm.model.common.MarkerType;
-import eu.etaxonomy.cdm.model.common.OrderedTermBase;
-import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
-import eu.etaxonomy.cdm.model.common.TermVocabulary;
-import eu.etaxonomy.cdm.model.common.VocabularyEnum;
-import eu.etaxonomy.cdm.model.description.AbsenceTerm;
+import eu.etaxonomy.cdm.model.term.TermType;
+import eu.etaxonomy.cdm.model.term.TermVocabulary;
import eu.etaxonomy.cdm.model.description.Feature;
-import eu.etaxonomy.cdm.model.description.MeasurementUnit;
-import eu.etaxonomy.cdm.model.description.Modifier;
-import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
-import eu.etaxonomy.cdm.model.description.PresenceTerm;
-import eu.etaxonomy.cdm.model.description.Scope;
-import eu.etaxonomy.cdm.model.description.Stage;
-import eu.etaxonomy.cdm.model.description.State;
-import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
-import eu.etaxonomy.cdm.model.location.NamedAreaType;
-import eu.etaxonomy.cdm.model.location.ReferenceSystem;
-import eu.etaxonomy.cdm.model.media.RightsTerm;
-import eu.etaxonomy.cdm.model.name.NameRelationshipType;
-import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
-import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
+import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
import eu.etaxonomy.cdm.model.name.Rank;
-import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
-import eu.etaxonomy.cdm.model.occurrence.DeterminationModifier;
-import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
+import eu.etaxonomy.taxeditor.model.DefaultTermComparator;
+import eu.etaxonomy.taxeditor.model.TaxonRelationshipTypeInverseContainer;
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
/**
* @version 1.0
*/
public class TermStore {
-
- /**
- * <p>getSpecimenTypeDesignationStatus</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<SpecimenTypeDesignationStatus> getSpecimenTypeDesignationStatus() {
- return getTerms(SpecimenTypeDesignationStatus.class);
- }
-
/**
- * <p>getNameTypeDesignationStatus</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<NameTypeDesignationStatus> getNameTypeDesignationStatus() {
- return getTerms(NameTypeDesignationStatus.class);
- }
-
- /**
- * <p>getNamedAreaTypes</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<NamedAreaType> getNamedAreaTypes(){
- return getTerms(NamedAreaType.class);
- }
-
- /**
- * <p>getAnnotationTypes</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<AnnotationType> getAnnotationTypes(){
- return getTerms(AnnotationType.class);
- }
-
- /**
- * <p>getTaxonRelationshipTypes</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<TaxonRelationshipType> getTaxonRelationshipTypes(){
- return getTerms(TaxonRelationshipType.class);
- }
-
-
- /**
- * <p>getLanguages</p>
+ * Returns an alphabetically sorted list of terms (anything that extends {@link DefinedTermBase}) of the given type currently available in the system.
*
- * @return a {@link java.util.List} object.
+ * @param clazz a class that inherits from {@link DefinedTermBase}
+ * @return an alphabetically sorted list of terms
*/
- public static List<Language> getLanguages(){
- return getTerms(Language.class);
+ public static <TERM extends DefinedTermBase> List<TERM> getTerms(Class<TERM> clazz){
+ return getTerms(clazz, null, true);
}
-
- /**
- * <p>getFeatures</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<Feature> getFeatures() {
- List<Feature> features = getTerms(Feature.class);
- features.remove(Feature.IMAGE());
- return features;
- }
-
- /**
- * <p>getNameRelationshipTypes</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<NameRelationshipType> getNameRelationshipTypes() {
- return getTerms(NameRelationshipType.class);
- }
-
/**
- * <p>getNomenclaturalStatusTypes</p>
+ * Returns an alphabetically sorted list of terms (anything that extends {@link DefinedTermBase}) of the given {@link TermType})
+ * currently available in the system.
*
- * @return a {@link java.util.List} object.
+ * @param termType the term type
+ * @return an alphabetically sorted list of terms
*/
- public static List<NomenclaturalStatusType> getNomenclaturalStatusTypes(){
- return getTerms(NomenclaturalStatusType.class);
+ public static <TERM extends DefinedTermBase> List<TERM> getTerms(TermType termType, Comparator<TERM> comparator){
+ return getTermsFromService(termType, comparator);
}
-
/**
- * <p>getNonTechnicalMarkerTypes</p>
+ * Returns an alphabetically sorted list of terms (anything that extends {@link DefinedTermBase}) of the given {@link TermType})
+ * currently available in the system.
*
- * @return a {@link java.util.List} object.
+ * @param termType the term type
+ * @return an alphabetically sorted list of terms
*/
- public static List<MarkerType> getNonTechnicalMarkerTypes() {
- List<MarkerType> nonTechnicalMarkerTypes = new ArrayList<MarkerType>();
- List<MarkerType> markerTypes = getTerms(MarkerType.class);
-
- for (Object type : markerTypes) {
- if (((MarkerType) type).isTechnical() == false) {
- nonTechnicalMarkerTypes.add((MarkerType) type);
- }
- }
-
- return nonTechnicalMarkerTypes;
+ public static <TERM extends DefinedTermBase> Set<TERM> getTerms(TermVocabulary<TERM> termVocabulary, Comparator<DefinedTermBase<?>> comparator){
+ return termVocabulary.getTerms();
}
-
/**
- * <p>getExtensionTypes</p>
+ * Returns a list of terms of the given type currently available in the system.
+ * If <code>filtered</code> is set to <code>true</code>, all terms for certain types will be
+ * gathered from edge cases.
*
- * @return a {@link java.util.List} object.
+ * @param clazz a class that inherits from {@link DefinedTermBase}
+ * @param comparator a {@link Comparator} that defines the sorting algorithm. If set to null, {@link DefaultTermComparator} will be used
+ * @param filtered if set to true, some terms are filtered from the result (according to definition in {@link ITermStoreEdgeCase}s)
+ * @return a sorted list of terms
*/
- public static List<ExtensionType> getExtensionTypes() {
- return getTerms(ExtensionType.class);
- }
-
- /**
- * <p>getRightsTypes</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<RightsTerm> getRightsTypes() {
- return getTerms(RightsTerm.class);
- }
-
-
- /**
- * Retrieve all ranks from data store.
- *
- * @return a {@link java.util.List} object.
- */
- public static List<Rank> getRanks(){
- if(PreferencesUtil.getSortRanksHierarchichally()){
- return getTerms(Rank.class, new Comparator<Rank>(){
-
- @Override
- public int compare(Rank o1, Rank o2) {
- return o1.compareTo(o2);
- }
-
- });
+ public static <TERM extends DefinedTermBase> List<TERM> getTerms(Class<TERM> clazz, Comparator<TERM> comparator, boolean filtered){
+ List<TERM> terms = new ArrayList<TERM>();
+
+ ITermStoreEdgeCase<TERM> edgeCase = getEdgeCase(clazz);
+ if(filtered && edgeCase != null){
+ terms = edgeCase.getTerms();
}else{
- return getTerms(Rank.class);
+ terms = getTermsFromService(clazz, comparator);
}
- }
-
-
- /**
- * Retrieve all presence and absence terms from data store.
- *
- * @return a {@link java.util.List} object.
- */
- public static List<PresenceAbsenceTermBase> getPresenceAbsenceTerms() {
- List presenceAbsenceTerms = getTerms(PresenceTerm.class);
- presenceAbsenceTerms.addAll(getTerms(AbsenceTerm.class));
- return presenceAbsenceTerms;
- }
+ return terms;
- /**
- * Retrieve all stages from data store.
- *
- * @return a {@link java.util.List} object.
- */
- public static List<Stage> getStages(){
- return getTerms(Stage.class);
- }
-
- /**
- * Retrieve all states from data store.
- *
- * @return a {@link java.util.List} object.
- */
- public static List<State> getStates() {
- return getTerms(State.class);
}
-
- /**
- * Retrieve all preservation methods from data store.
- *
- * @return a {@link java.util.List} object.
- */
- public static List<PreservationMethod> getPreservationMethods(){
- return getTerms(PreservationMethod.class);
- }
-
-
/**
- * <p>getMeasurementUnits</p>
*
- * @return a {@link java.util.List} object.
+ * @param termClass
+ * @param comparator
+ * @return
*/
- public static List<MeasurementUnit> getMeasurementUnits() {
- return getTerms(MeasurementUnit.class);
- }
-
+ private static <T extends DefinedTermBase> List<T> getTermsFromService(TermType termType, Comparator<T> comparator){
+ if (comparator == null){
+ comparator = new DefaultTermComparator<T>();
+ }
- /**
- * <p>getModifiers</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<Modifier> getModifiers() {
- return getTerms(Modifier.class);
+ List<T> terms = CdmStore.getService(ITermService.class).listByTermType(termType, null, null, null, null);
+ Collections.sort(terms, comparator);
+ return terms;
}
-
- /**
- * <p>getStatisticalMeasures</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<StatisticalMeasure> getStatisticalMeasures() {
- return getTerms(StatisticalMeasure.class);
- }
-
- /**
- * <p>getScopes</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<Scope> getScopes() {
- return getTerms(Scope.class);
- }
-
/**
- * <p>getDeterminationModifiers</p>
*
- * @return a {@link java.util.List} object.
+ * @param termClass
+ * @param comparator
+ * @return
*/
- public static List<DeterminationModifier> getDeterminationModifiers() {
- return getTerms(DeterminationModifier.class);
- }
-
- /**
- * <p>getReferenceSystems</p>
- *
- * @return a {@link java.util.List} object.
- */
- public static List<ReferenceSystem> getReferenceSystems() {
- return getTerms(ReferenceSystem.class);
+ private static <T extends DefinedTermBase> List<T> getTermsFromService(Class<T> termClass, Comparator<T> comparator){
+ if (comparator == null){
+ comparator = new DefaultTermComparator<T>();
+ }
+
+ List<T> terms = CdmStore.getService(ITermService.class).list(termClass, null, null, null, null);
+ Collections.sort(terms, comparator);
+ return terms;
}
-
+
/**
- ************* save methods *****************************
- *
- * @param term a {@link eu.etaxonomy.cdm.model.common.DefinedTermBase} object.
+ * @param term a {@link eu.etaxonomy.cdm.model.term.DefinedTermBase} object.
*/
public static void saveTerm(DefinedTermBase term){
- getTermService().saveOrUpdate(term);
+ CdmStore.getService(ITermService.class).saveOrUpdate(term);
}
-
+
/**
* <p>delete</p>
*
- * @param selected a {@link eu.etaxonomy.cdm.model.common.DefinedTermBase} object.
+ * @param selected a {@link eu.etaxonomy.cdm.model.term.DefinedTermBase} object.
+ * @throws ReferencedObjectUndeletableException
*/
- public static void delete(DefinedTermBase selected) {
- getTermService().delete(selected);
+ public static void delete(DefinedTermBase selected) throws ReferencedObjectUndeletableException {
+ CdmStore.getService(ITermService.class).delete(selected);
}
-
+
/**
* Save a vacabulary to data store
*
- * @param term a {@link eu.etaxonomy.cdm.model.common.DefinedTermBase} object.
+ * @param term a {@link eu.etaxonomy.cdm.model.term.DefinedTermBase} object.
*/
public static void updateVocabulary(DefinedTermBase term) {
- getTermService().saveOrUpdate(term);
- }
-
-
-
- /*************** internal methods **************************/
-
- private static Language getDefaultLanguage(){
- return CdmStore.getDefaultLanguage();
+ CdmStore.getService(ITermService.class).saveOrUpdate(term);
}
-
- private static OrderedTermVocabulary<? extends OrderedTermBase> getOrderedTermVocabulary(VocabularyEnum vocabularyType){
- TermVocabulary<DefinedTermBase> vocabulary = getTermVocabulary(vocabularyType);
- return HibernateProxyHelper.deproxy(vocabulary, OrderedTermVocabulary.class);
- }
-
+
/**
- * <p>getTermVocabulary</p>
- *
- * @param vocabularyType a {@link eu.etaxonomy.cdm.model.common.VocabularyEnum} object.
- * @return a {@link eu.etaxonomy.cdm.model.common.TermVocabulary} object.
+ * Handingling of special cases
*/
- public static TermVocabulary<DefinedTermBase> getTermVocabulary(VocabularyEnum vocabularyType){
-
- return CdmStore.getService(IVocabularyService.class).getVocabulary(vocabularyType);
- }
-
- private static <TERM extends DefinedTermBase> List<TERM> getTerms(Class<TERM> clazz){
- Comparator<TERM> comparator = new Comparator<TERM>() {
+
+ private static Set<ITermStoreEdgeCase<? extends DefinedTermBase>> termStoreEdgeCases = new HashSet<ITermStoreEdgeCase<? extends DefinedTermBase>>();
+
+ static {
+ termStoreEdgeCases.add(new ITermStoreEdgeCase<Feature>() {
+
+ @Override
+ public Class<Feature> getTermClass() {
+ return Feature.class;
+ }
+
+ @Override
+ public List<Feature> getTerms() {
+ List<Feature> features = TermStore.getTermsFromService(Feature.class, null);
+ features.remove(Feature.IMAGE());
+ return features;
+ }
+ });
+ termStoreEdgeCases.add(new ITermStoreEdgeCase<MarkerType>(){
+
+ @Override
+ public Class<MarkerType> getTermClass() {
+ return MarkerType.class;
+ }
+
+ @Override
+ public List<MarkerType> getTerms() {
+ // filter out non technical markers
+ List<MarkerType> nonTechnicalMarkerTypes = new ArrayList<MarkerType>();
+ List<MarkerType> markerTypes = TermStore.getTermsFromService(MarkerType.class, null);
+
+ for (Object type : markerTypes) {
+ if (((MarkerType) type).isTechnical() == false) {
+ nonTechnicalMarkerTypes.add((MarkerType) type);
+ }
+ }
+
+ return nonTechnicalMarkerTypes;
+ }
+
+ });
+ termStoreEdgeCases.add(new ITermStoreEdgeCase<Rank>() {
+
@Override
- public int compare(TERM o1, TERM o2) {
- String label1 = o1.getLabel(getDefaultLanguage()) != null ? o1.getLabel(getDefaultLanguage()) : o1.getTitleCache();
- String label2 = o2.getLabel(getDefaultLanguage()) != null ? o2.getLabel(getDefaultLanguage()) : o2.getTitleCache();
- return label1.compareTo(label2);
+ public Class<Rank> getTermClass() {
+ return Rank.class;
}
- };
-
- return getTerms(clazz, comparator);
+
+ @Override
+ public List<Rank> getTerms() {
+ if(PreferencesUtil.getSortRanksHierarchichally()){
+ return TermStore.getTermsFromService(Rank.class, new Comparator<Rank>(){
+
+ @Override
+ public int compare(Rank o1, Rank o2) {
+ return o1.compareTo(o2);
+ }
+
+ });
+ }else{
+ return TermStore.getTermsFromService(Rank.class, null);
+ }
+ }
+
+ });
+ termStoreEdgeCases.add(new ITermStoreEdgeCase<PresenceAbsenceTerm>() {
+
+ @Override
+ public Class<PresenceAbsenceTerm> getTermClass() {
+ return PresenceAbsenceTerm.class;
+ }
+
+ @Override
+ public List<PresenceAbsenceTerm> getTerms() {
+ List<PresenceAbsenceTerm> presenceAbsenceTerms = TermStore.getTermsFromService(PresenceAbsenceTerm.class, null);
+
+ return presenceAbsenceTerms;
+ }
+ });
+ termStoreEdgeCases.add(new ITermStoreEdgeCase<TaxonRelationshipTypeInverseContainer>() {
+
+ @Override
+ public Class<TaxonRelationshipTypeInverseContainer> getTermClass() {
+ return TaxonRelationshipTypeInverseContainer.class;
+ }
+
+ @Override
+ public List<TaxonRelationshipTypeInverseContainer> getTerms() {
+ List<TaxonRelationshipType> excludeTaxonRelationshipTypes = Arrays.asList(new TaxonRelationshipType[]{
+ TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
+ TaxonRelationshipType.MISAPPLIED_NAME_FOR(),
+ TaxonRelationshipType.ALL_RELATIONSHIPS()
+ });
+
+ List<TaxonRelationshipTypeInverseContainer> relationshipTypeInverseContainers = new ArrayList<TaxonRelationshipTypeInverseContainer>();
+
+ List<TaxonRelationshipType> relationshipTypes = TermStore.getTerms(TaxonRelationshipType.class);
+
+ relationshipTypes.removeAll(excludeTaxonRelationshipTypes);
+
+ for (TaxonRelationshipType relationshipType : relationshipTypes){
+ if(!relationshipType.isSymmetric()){
+ TaxonRelationshipTypeInverseContainer inverseContainer = new TaxonRelationshipTypeInverseContainer(relationshipType, true);
+ relationshipTypeInverseContainers.add(inverseContainer);
+ }
+ TaxonRelationshipTypeInverseContainer container = new TaxonRelationshipTypeInverseContainer(relationshipType, false);
+ relationshipTypeInverseContainers.add(container);
+ }
+
+ return relationshipTypeInverseContainers;
+ }
+ });
}
-
-
- private static <TERM extends DefinedTermBase> List<TERM> getTerms(Class<TERM> clazz, Comparator<TERM> comparator){
-
- List<TERM> terms = new ArrayList<TERM>();
- if(getTermService() != null){
- terms = getTermService().listByTermClass(clazz, null, null, null, null);
-
- Collections.sort(terms, comparator);
+ private static <T extends DefinedTermBase> ITermStoreEdgeCase<T> getEdgeCase(Class<T> termClass) {
+
+ for (ITermStoreEdgeCase termStoreEdgeCase : termStoreEdgeCases){
+ if (termStoreEdgeCase.getTermClass().equals(termClass)){
+ return termStoreEdgeCase;
+ }
}
-
- return terms;
-
+
+ return null;
}
-
- private static ITermService getTermService(){
- return CdmStore.getService(ITermService.class);
+
+ private interface ITermStoreEdgeCase<TERM> {
+
+ public abstract Class<TERM> getTermClass();
+
+ public abstract List<TERM> getTerms();
+
}
}