Project

General

Profile

« Previous | Next » 

Revision 63ff77ee

Added by Andreas Kohlbecker about 12 years ago

harmonizing method names

View differences:

cdmlib-ext/src/main/java/eu/etaxonomy/cdm/ext/geo/EditGeoService.java
120 120
			String backLayer,
121 121
			List<Language> langs) {
122 122
		
123
		List<TaxonDescription> taxonDescriptions = dao.getTaxonDescriptions(taxon, null, null, null, null, null, null);
123
		List<TaxonDescription> taxonDescriptions = dao.listTaxonDescriptions(taxon, null, null, null, null, null, null);
124 124
		
125 125
		Set<Distribution> distCollection = new HashSet<Distribution>();
126 126
		// get descriptions elements for each description
cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/name/TypeDesignationBase.java
1 1
/**
2 2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
3
* European Distributed Institute of Taxonomy
4 4
* http://www.e-taxonomy.eu
5
* 
5
*
6 6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7 7
* See LICENSE.TXT at the top of this package for the full license terms.
8 8
*/
......
40 40
 * can only be typified by specimens (a {@link SpecimenTypeDesignation specimen type designation}), but taxon
41 41
 * names with a higher rank might be typified by an other taxon name with
42 42
 * rank "species" or "genus" (a {@link NameTypeDesignation name type designation}).
43
 * 
43
 *
44 44
 * @see		TaxonNameBase
45 45
 * @see		NameTypeDesignation
46 46
 * @see		SpecimenTypeDesignation
......
56 56
    "typeStatus"
57 57
})
58 58
@XmlSeeAlso({
59
	NameTypeDesignation.class,
60
	SpecimenTypeDesignation.class
59
    NameTypeDesignation.class,
60
    SpecimenTypeDesignation.class
61 61
})
62 62
@Entity
63 63
@Audited
64 64
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
65 65
public abstract class TypeDesignationBase<T extends TypeDesignationStatusBase> extends ReferencedEntityBase implements ITypeDesignation {
66
	private static final long serialVersionUID = 8622351017235131355L;
66
    private static final long serialVersionUID = 8622351017235131355L;
67 67

  
68
	@SuppressWarnings("unused")
69
	private static final Logger logger = Logger.getLogger(TypeDesignationBase.class);
68
    @SuppressWarnings("unused")
69
    private static final Logger logger = Logger.getLogger(TypeDesignationBase.class);
70 70

  
71
	@XmlElement(name = "IsNotDesignated")
72
	private boolean notDesignated;
73
	
74
	@XmlElementWrapper(name = "TypifiedNames")
75
	@XmlElement(name = "TypifiedName")
76
	@XmlIDREF
77
	@XmlSchemaType(name = "IDREF")
71
    @XmlElement(name = "IsNotDesignated")
72
    private boolean notDesignated;
73

  
74
    @XmlElementWrapper(name = "TypifiedNames")
75
    @XmlElement(name = "TypifiedName")
76
    @XmlIDREF
77
    @XmlSchemaType(name = "IDREF")
78 78
    @ManyToMany(fetch = FetchType.LAZY , mappedBy="typeDesignations")
79
	@Cascade({CascadeType.SAVE_UPDATE})
79
    @Cascade({CascadeType.SAVE_UPDATE})
80 80
    private Set<TaxonNameBase> typifiedNames = new HashSet<TaxonNameBase>();
81
	
82
	@XmlElement(name = "HomotypicalGroup")
83
	@XmlIDREF
84
	@XmlSchemaType(name = "IDREF")
85
	@ManyToOne(fetch = FetchType.LAZY)
86
	@Cascade(CascadeType.SAVE_UPDATE)
87
	private HomotypicalGroup homotypicalGroup;
88 81

  
89
	@XmlElement(name = "TypeStatus")
90
	@XmlIDREF
91
	@XmlSchemaType(name = "IDREF")
92
	@ManyToOne(fetch = FetchType.LAZY, targetEntity = TypeDesignationStatusBase.class)
93
	private T typeStatus;
82
    @XmlElement(name = "HomotypicalGroup")
83
    @XmlIDREF
84
    @XmlSchemaType(name = "IDREF")
85
    @ManyToOne(fetch = FetchType.LAZY)
86
    @Cascade(CascadeType.SAVE_UPDATE)
87
    private HomotypicalGroup homotypicalGroup;
88

  
89
    @XmlElement(name = "TypeStatus")
90
    @XmlIDREF
91
    @XmlSchemaType(name = "IDREF")
92
    @ManyToOne(fetch = FetchType.LAZY, targetEntity = TypeDesignationStatusBase.class)
93
    private T typeStatus;
94 94

  
95 95
// **************** CONSTRUCTOR *************************************/
96 96

  
97
	/** 
98
	 * Class constructor: creates a new empty type designation.
99
	 * 
100
	 * @see	#TypeDesignationBase(Reference, String, String, Boolean)
101
	 */
102
	protected TypeDesignationBase(){
103
		super();
104
	}
105
	
106
	/**
107
	 * Class constructor: creates a new type designation
108
	 * (including its {@link Reference reference source} and eventually
109
	 * the taxon name string originally used by this reference when establishing
110
	 * the former designation).
111
	 * 
112
	 * @param citation				the reference source for the new designation
113
	 * @param citationMicroReference	the string with the details describing the exact localisation within the reference
114
	 * @param originalNameString	the taxon name string used originally in the reference source for the new designation
115
	 * @see							#TypeDesignationBase()
116
	 * @see							#isNotDesignated()
117
	 * @see							TaxonNameBase#getTypeDesignations()
118
	 */
119
	protected TypeDesignationBase(Reference citation, String citationMicroReference, String originalNameString) {
120
		this(citation, citationMicroReference, originalNameString, false);
121
	}
97
    /**
98
     * Class constructor: creates a new empty type designation.
99
     *
100
     * @see	#TypeDesignationBase(Reference, String, String, Boolean)
101
     */
102
    protected TypeDesignationBase(){
103
        super();
104
    }
105

  
106
    /**
107
     * Class constructor: creates a new type designation
108
     * (including its {@link Reference reference source} and eventually
109
     * the taxon name string originally used by this reference when establishing
110
     * the former designation).
111
     *
112
     * @param citation				the reference source for the new designation
113
     * @param citationMicroReference	the string with the details describing the exact localisation within the reference
114
     * @param originalNameString	the taxon name string used originally in the reference source for the new designation
115
     * @see							#TypeDesignationBase()
116
     * @see							#isNotDesignated()
117
     * @see							TaxonNameBase#getTypeDesignations()
118
     */
119
    protected TypeDesignationBase(Reference citation, String citationMicroReference, String originalNameString) {
120
        this(citation, citationMicroReference, originalNameString, false);
121
    }
122

  
123
    /**
124
     * Class constructor: creates a new type designation
125
     * (including its {@link Reference reference source} and eventually
126
     * the taxon name string originally used by this reference when establishing
127
     * the former designation).
128
     *
129
     * @param citation				the reference source for the new designation
130
     * @param citationMicroReference	the string with the details describing the exact localisation within the reference
131
     * @param originalNameString	the taxon name string used originally in the reference source for the new designation
132
     * @param isNotDesignated		the boolean flag indicating whether there is no type at all for
133
     * 								<i>this</i> type designation
134
     * @see							#TypeDesignationBase()
135
     * @see							#isNotDesignated()
136
     * @see							TaxonNameBase#getTypeDesignations()
137
     */
138
    protected TypeDesignationBase(Reference citation, String citationMicroReference, String originalNameString, boolean notDesignated){
139
        super(citation, citationMicroReference, originalNameString);
140
        this.notDesignated = notDesignated;
141
    }
142

  
122 143

  
123
	/**
124
	 * Class constructor: creates a new type designation
125
	 * (including its {@link Reference reference source} and eventually
126
	 * the taxon name string originally used by this reference when establishing
127
	 * the former designation).
128
	 * 
129
	 * @param citation				the reference source for the new designation
130
	 * @param citationMicroReference	the string with the details describing the exact localisation within the reference
131
	 * @param originalNameString	the taxon name string used originally in the reference source for the new designation
132
	 * @param isNotDesignated		the boolean flag indicating whether there is no type at all for 
133
	 * 								<i>this</i> type designation
134
	 * @see							#TypeDesignationBase()
135
	 * @see							#isNotDesignated()
136
	 * @see							TaxonNameBase#getTypeDesignations()
137
	 */
138
	protected TypeDesignationBase(Reference citation, String citationMicroReference, String originalNameString, boolean notDesignated){
139
		super(citation, citationMicroReference, originalNameString);
140
		this.notDesignated = notDesignated;
141
	}
142
	
143
	
144 144
// **************** METHODS *************************************/
145 145

  
146 146

  
147
	/** 
148
	 * Returns the {@link TypeDesignationStatusBase type designation status} for <i>this</i> specimen type
149
	 * designation. This status describes which of the possible categories of
150
	 * types like "holotype", "neotype", "syntype" or "isotype" applies to <i>this</i>
151
	 * specimen type designation.
152
	 */
153
	public T getTypeStatus(){
154
		return this.typeStatus;
155
	}
156
	/**
157
	 * @see  #getTypeStatus()
158
	 */
159
	public void setTypeStatus(T typeStatus){
160
		this.typeStatus = typeStatus;
161
	}
147
    /**
148
     * Returns the {@link TypeDesignationStatusBase type designation status} for <i>this</i> specimen type
149
     * designation. This status describes which of the possible categories of
150
     * types like "holotype", "neotype", "syntype" or "isotype" applies to <i>this</i>
151
     * specimen type designation.
152
     */
153
    public T getTypeStatus(){
154
        return this.typeStatus;
155
    }
156
    /**
157
     * @see  #getTypeStatus()
158
     */
159
    public void setTypeStatus(T typeStatus){
160
        this.typeStatus = typeStatus;
161
    }
162

  
163
    /* (non-Javadoc)
164
     * @see eu.etaxonomy.cdm.model.name.ITypeDesignation#getHomotypicalGroup()
165
     */
166
    /**
167
     * Returns the {@link HomotypicalGroup homotypical group} to which all (in <i>this</i>
168
     * type designation) typified {@link TaxonNameBase taxon names} belong.
169
     *
170
     * @see   #getTypifiedNames()
171
     * @deprecated homotypical group can not be set and always seems to be <code>null</code>.
172
     * Probably it is a relict of an old version.
173
     */
174
    @Deprecated
175
    public HomotypicalGroup getHomotypicalGroup() {
176
        return homotypicalGroup;
177
    }
162 178

  
163
	/* (non-Javadoc)
164
	 * @see eu.etaxonomy.cdm.model.name.ITypeDesignation#getHomotypicalGroup()
165
	 */
166
	/** 
167
	 * Returns the {@link HomotypicalGroup homotypical group} to which all (in <i>this</i>
168
	 * type designation) typified {@link TaxonNameBase taxon names} belong.
169
	 *  
170
	 * @see   #getTypifiedNames()
171
	 * @deprecated homotypical group can not be set and always seems to be <code>null</code>.
172
	 * Probably it is a relict of an old version.
173
	 */
174
	@Deprecated
175
	public HomotypicalGroup getHomotypicalGroup() {
176
		return homotypicalGroup;
177
	}
179
    /* (non-Javadoc)
180
     * @see eu.etaxonomy.cdm.model.name.ITypeDesignation#getTypifiedNames()
181
     */
182
    /**
183
     * Returns the set of {@link TaxonNameBase taxon names} typified in <i>this</i>
184
     * type designation. This is a subset of the taxon names belonging to the
185
     * corresponding {@link #getHomotypicalGroup() homotypical group}.
186
     */
187
    public Set<TaxonNameBase> getTypifiedNames() {
188
        return typifiedNames;
189
    }
178 190

  
179
	/* (non-Javadoc)
180
	 * @see eu.etaxonomy.cdm.model.name.ITypeDesignation#getTypifiedNames()
181
	 */
182
	/** 
183
	 * Returns the set of {@link TaxonNameBase taxon names} typified in <i>this</i>
184
	 * type designation. This is a subset of the taxon names belonging to the
185
	 * corresponding {@link #getHomotypicalGroup() homotypical group}.
186
	 */
187
	public Set<TaxonNameBase> getTypifiedNames() {
188
		return typifiedNames;
189
	}
191
    /**
192
     * Returns the boolean value "true" if it is known that a type does not
193
     * exist and therefore the {@link TaxonNameBase taxon name} to which <i>this</i>
194
     * type designation is assigned must still be typified. Two
195
     * cases must be differentiated: <BR><ul>
196
     * <li> a) it is unknown whether a type exists and
197
     * <li> b) it is known that no type exists
198
     *  </ul>
199
     * If a) is true there should be no TypeDesignation instance at all
200
     * assigned to the "typified" taxon name.<BR>
201
     * If b) is true there should be a TypeDesignation instance with the
202
     * flag isNotDesignated set. The typeName attribute, in case of a
203
     * {@link NameTypeDesignation name type designation}, or the typeSpecimen attribute,
204
     * in case of a {@link SpecimenTypeDesignation specimen type designation}, should then be "null".
205
     */
206
    public boolean isNotDesignated() {
207
        return notDesignated;
208
    }
190 209

  
191
	/**
192
	 * Returns the boolean value "true" if it is known that a type does not
193
	 * exist and therefore the {@link TaxonNameBase taxon name} to which <i>this</i>
194
	 * type designation is assigned must still be typified. Two
195
	 * cases must be differentiated: <BR><ul> 
196
	 * <li> a) it is unknown whether a type exists and 
197
	 * <li> b) it is known that no type exists
198
	 *  </ul>
199
	 * If a) is true there should be no TypeDesignation instance at all
200
	 * assigned to the "typified" taxon name.<BR>
201
	 * If b) is true there should be a TypeDesignation instance with the
202
	 * flag isNotDesignated set. The typeName attribute, in case of a
203
	 * {@link NameTypeDesignation name type designation}, or the typeSpecimen attribute,
204
	 * in case of a {@link SpecimenTypeDesignation specimen type designation}, should then be "null".
205
	 */
206
	public boolean isNotDesignated() {
207
		return notDesignated;
208
	}
210
    /**
211
     * @see   #isNotDesignated()
212
     */
213
    public void setNotDesignated(boolean notDesignated) {
214
        this.notDesignated = notDesignated;
215
    }
216

  
217
    /**
218
     * @deprecated for bidirectional use only
219
     */
220
    @Deprecated
221
    protected void addTypifiedName(TaxonNameBase taxonName){
222
        this.typifiedNames.add(taxonName);
223
    }
224

  
225
    /**
226
     * @deprecated for bidirectional use only
227
     */
228
    @Deprecated
229
    protected void removeTypifiedName(TaxonNameBase taxonName){
230
        this.typifiedNames.remove(taxonName);
231
        if (taxonName.getTypeDesignations().contains(this)){
232
            taxonName.removeTypeDesignation(this);
233
        }
234
    }
235

  
236
    public abstract void removeType();
209 237

  
210
	/**
211
	 * @see   #isNotDesignated()
212
	 */
213
	public void setNotDesignated(boolean notDesignated) {
214
		this.notDesignated = notDesignated;
215
	}
216
	
217
	/**
218
	 * @deprecated for bidirectional use only
219
	 */
220
	@Deprecated
221
	protected void addTypifiedName(TaxonNameBase taxonName){
222
		this.typifiedNames.add(taxonName);
223
	}
224
	
225
	/**
226
	 * @deprecated for bidirectional use only
227
	 */
228
	@Deprecated
229
	protected void removeTypifiedName(TaxonNameBase taxonName){
230
		this.typifiedNames.remove(taxonName);
231
		if (taxonName.getTypeDesignations().contains(this)){
232
			taxonName.removeTypeDesignation(this);
233
		}
234
	}
235
	
236
	public abstract void removeType();
237
	
238 238
//*********************** CLONE ********************************************************/
239
	
240
	/** 
241
	 * Clones <i>this</i> type designation. This is a shortcut that enables to create
242
	 * a new instance that differs only slightly from <i>this</i> type designation by
243
	 * modifying only some of the attributes.<BR>
244
	 * CAUTION: the typifiedNames set is not cloned but empty after cloning as the typified 
245
	 * names is considered to be the not owning part of a bidirectional relationship.
246
	 * This may be changed in future.
247
	 * 
248
	 * @throws CloneNotSupportedException 
249
	 * 
250
	 * @see eu.etaxonomy.cdm.model.common.ReferencedEntityBase#clone()
251
	 * @see java.lang.Object#clone()
252
	 */
253
	@Override
254
	public Object clone() throws CloneNotSupportedException {
255
		TypeDesignationBase result = (TypeDesignationBase)super.clone();
256
		
257
		result.typifiedNames = new HashSet<TaxonNameBase>();
239

  
240
    /**
241
     * Clones <i>this</i> type designation. This is a shortcut that enables to create
242
     * a new instance that differs only slightly from <i>this</i> type designation by
243
     * modifying only some of the attributes.<BR>
244
     * CAUTION: the typifiedNames set is not cloned but empty after cloning as the typified
245
     * names is considered to be the not owning part of a bidirectional relationship.
246
     * This may be changed in future.
247
     *
248
     * @throws CloneNotSupportedException
249
     *
250
     * @see eu.etaxonomy.cdm.model.common.ReferencedEntityBase#clone()
251
     * @see java.lang.Object#clone()
252
     */
253
    @Override
254
    public Object clone() throws CloneNotSupportedException {
255
        TypeDesignationBase result = (TypeDesignationBase)super.clone();
256

  
257
        result.typifiedNames = new HashSet<TaxonNameBase>();
258 258
//		for (TaxonNameBase taxonNameBase : getTypifiedNames()){
259 259
//			result.typifiedNames.add(taxonNameBase);
260 260
//		}
261 261

  
262
		
263
		//no changes to: notDesignated, typeStatus, homotypicalGroup
264
		return result;
265
	}	
262

  
263
        //no changes to: notDesignated, typeStatus, homotypicalGroup
264
        return result;
265
    }
266 266
}
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/description/IDescriptionDao.java
91 91
	 * @param propertyPaths Properties to initialize in the returned entities, following the syntax described in {@link BeanInitializer#initialize(Object, List)}
92 92
	 * @return a List of TaxonDescription instances
93 93
	 */
94
	List<TaxonDescription> getTaxonDescriptions(Taxon taxon, Set<Scope> scopes, Set<NamedArea> geographicalScope, Set<MarkerType> markerTypes, Integer pageSize, Integer pageNumber, List<String> propertyPaths);
94
	List<TaxonDescription> listTaxonDescriptions(Taxon taxon, Set<Scope> scopes, Set<NamedArea> geographicalScope, Set<MarkerType> markerTypes, Integer pageSize, Integer pageNumber, List<String> propertyPaths);
95 95
	
96 96
	/**
97 97
	 * Returns a count of TaxonDescription instances, optionally filtered by parameters passed to this method
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/description/DescriptionDaoImpl.java
1 1
/**
2 2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
3
* European Distributed Institute of Taxonomy
4 4
* http://www.e-taxonomy.eu
5
* 
5
*
6 6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7 7
* See LICENSE.TXT at the top of this package for the full license terms.
8 8
*/
......
46 46
import eu.etaxonomy.cdm.persistence.query.MatchMode;
47 47
import eu.etaxonomy.cdm.persistence.query.OrderHint;
48 48

  
49
@Repository 
49
@Repository
50 50
@Qualifier("descriptionDaoImpl")
51 51
public class DescriptionDaoImpl extends IdentifiableDaoBase<DescriptionBase> implements IDescriptionDao{
52 52

  
53
	@SuppressWarnings("unused")
54
	private static final Logger logger = Logger.getLogger(DescriptionDaoImpl.class);
55

  
56
	public DescriptionDaoImpl() {
57
		super(DescriptionBase.class); 
58
		indexedClasses = new Class[3];
59
		indexedClasses[0] = TaxonDescription.class;
60
		indexedClasses[1] = TaxonNameDescription.class;
61
		indexedClasses[2] = SpecimenDescription.class;
62
	}
63

  
64
	public int countDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status) {
65
		checkNotInPriorView("DescriptionDaoImpl.countDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status)");
66
		Query query = null;
67
		
68
		if(status == null) {
69
			query = getSession().createQuery("select count(distinct description) from TaxonDescription description left join description.descriptionElements element join element.area area where area in (:namedAreas)");
70
		} else {
71
			query = getSession().createQuery("select count(distinct description) from TaxonDescription description left join description.descriptionElements element join element.area area  join element.status status where area in (:namedAreas) and status = :status");
72
			query.setParameter("status", status);
73
		}
74
		query.setParameterList("namedAreas", namedAreas);
75
		
76
		return ((Long)query.uniqueResult()).intValue();
77
	}
78

  
79
	public int countDescriptionElements(DescriptionBase description, Set<Feature> features, Class<? extends DescriptionElementBase> clazz) {
80
		AuditEvent auditEvent = getAuditEventFromContext();
81
		if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
82
			Criteria criteria = null;
83
			if(clazz == null) {
84
		        criteria = getSession().createCriteria(DescriptionElementBase.class);
85
			} else {
86
			    criteria = getSession().createCriteria(clazz);	
87
			}	
88
			
89
		    if(description != null) {
90
		        criteria.add(Restrictions.eq("inDescription", description));
91
		    }
92
		
93
		    if(features != null && !features.isEmpty()) {
94
			    criteria.add(Restrictions.in("feature", features));
95
		    }
96
		
97
		    criteria.setProjection(Projections.rowCount());
98
		
99
		    return (Integer)criteria.uniqueResult();
100
		} else {
101
			if(features != null && !features.isEmpty()) {
102
				Integer count = 0;
103
			    for(Feature f : features) {
104
			        AuditQuery query = null;
105
			        if(clazz == null) {
106
			        	query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
107
			        } else {
108
			        	query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
109
			        }
110
			    
111
			        if(description != null) {
112
			    	    query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
113
			        }
114
			     
115
			        query.add(AuditEntity.relatedId("feature").eq(f.getId()));
116
			        query.addProjection(AuditEntity.id().count("id"));
117
			        count += ((Long)query.getSingleResult()).intValue();
118
			    }
119
			    
120
			    return count;
121
			} else {
122
				AuditQuery query = null;
123
		        if(clazz == null) {
124
		        	query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
125
		        } else {
126
		        	query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
127
		        }
128
			    
129
		        if(description != null) {
130
		    	    query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
131
		        }
132
		        query.addProjection(AuditEntity.id().count("id"));
133
		        return ((Long)query.getSingleResult()).intValue();
134
			}
135
		}
136
	}
137

  
138
	public int countDescriptions(Class<? extends DescriptionBase> clazz, Boolean hasImages, Boolean hasText, Set<Feature> features) {
139
		checkNotInPriorView("DescriptionDaoImpl.countDescriptions(Class<TYPE> type, Boolean hasImages, Boolean hasText, Set<Feature> features)");
140
		Criteria inner = null;
141
		
142
		if(clazz == null) {
143
			inner = getSession().createCriteria(type);
144
		} else {
145
			inner = getSession().createCriteria(clazz);
146
		}
147
		
148
		Criteria elementsCriteria = inner.createCriteria("descriptionElements");
149
		if(hasText != null) {
150
			if(hasText) {
151
				elementsCriteria.add(Restrictions.isNotEmpty("multilanguageText"));
152
			} else {
153
				elementsCriteria.add(Restrictions.isEmpty("multilanguageText"));
154
			}
155
		}
156
		
157
		if(hasImages != null) {
158
			if(hasImages) {
159
				elementsCriteria.add(Restrictions.isNotEmpty("media"));
160
			} else {
161
				elementsCriteria.add(Restrictions.isEmpty("media"));
162
			}
163
		}
164
		
165
		if(features != null && !features.isEmpty()) {
166
			elementsCriteria.add(Restrictions.in("feature", features));
167
		}
168
		
169
		inner.setProjection(Projections.countDistinct("id"));
170

  
171
		return (Integer) inner.uniqueResult();
172
	}
173

  
174
	public int countTaxonDescriptions(Taxon taxon, Set<Scope> scopes,Set<NamedArea> geographicalScopes, Set<MarkerType> markerTypes) {
175
		AuditEvent auditEvent = getAuditEventFromContext();
176
		if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
177
			Criteria criteria = getSession().createCriteria(TaxonDescription.class);
178

  
179
			if(taxon != null) {
180
				criteria.add(Restrictions.eq("taxon", taxon));
181
			}
182

  
183
			if(scopes != null && !scopes.isEmpty()) {
184
				Set<Integer> scopeIds = new HashSet<Integer>();
185
				for(Scope s : scopes) {
186
					scopeIds.add(s.getId());
187
				}
188
				criteria.createCriteria("scopes").add(Restrictions.in("id", scopeIds));
189
			}
190

  
191
			if(geographicalScopes != null && !geographicalScopes.isEmpty()) {
192
				Set<Integer> geoScopeIds = new HashSet<Integer>();
193
				for(NamedArea n : geographicalScopes) {
194
					geoScopeIds.add(n.getId());
195
				}
196
				criteria.createCriteria("geoScopes").add(Restrictions.in("id", geoScopeIds));
197
			}
198

  
199
			
200
			addMarkerTypesCriterion(markerTypes, criteria);
201

  
202
			
203
			criteria.setProjection(Projections.rowCount());
204

  
205
			return (Integer)criteria.uniqueResult();
206
		} else {
207
			if((scopes == null || scopes.isEmpty())&& (geographicalScopes == null || geographicalScopes.isEmpty()) && (markerTypes == null || markerTypes.isEmpty())) {
208
				AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonDescription.class,auditEvent.getRevisionNumber());
209
				if(taxon != null) {
210
				    query.add(AuditEntity.relatedId("taxon").eq(taxon.getId()));
211
				}
212
				
213
				query.addProjection(AuditEntity.id().count("id"));
214
				
215
				return ((Long)query.getSingleResult()).intValue();
216
			} else {
217
				throw new OperationNotSupportedInPriorViewException("countTaxonDescriptions(Taxon taxon, Set<Scope> scopes,Set<NamedArea> geographicalScopes)");
218
			}
219
		}
220
	}
221

  
222
	/**
223
	 * @param markerTypes
224
	 * @param criteria
225
	 *
226
	 */
227
	//TODO move to AnnotatableEntityDao(?)
228
	private void addMarkerTypesCriterion(Set<MarkerType> markerTypes,
229
			Criteria criteria) {
230
		if(markerTypes != null && !markerTypes.isEmpty()) {
231
			Set<Integer> markerTypeIds = new HashSet<Integer>();
232
			for(MarkerType markerType : markerTypes) {
233
				markerTypeIds.add(markerType.getId());
234
			}
235
			criteria.createCriteria("markers").add(Restrictions.eq("flag", true))
236
					.createAlias("markerType", "mt")
237
			 		.add(Restrictions.in("mt.id", markerTypeIds));
238
		}
239
	}
240

  
241
	public List<DescriptionElementBase> getDescriptionElements(DescriptionBase description, Set<Feature> features,Class<? extends DescriptionElementBase> clazz, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
242

  
243
		AuditEvent auditEvent = getAuditEventFromContext();
244
		if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
53
    @SuppressWarnings("unused")
54
    private static final Logger logger = Logger.getLogger(DescriptionDaoImpl.class);
55

  
56
    public DescriptionDaoImpl() {
57
        super(DescriptionBase.class);
58
        indexedClasses = new Class[3];
59
        indexedClasses[0] = TaxonDescription.class;
60
        indexedClasses[1] = TaxonNameDescription.class;
61
        indexedClasses[2] = SpecimenDescription.class;
62
    }
63

  
64
    public int countDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status) {
65
        checkNotInPriorView("DescriptionDaoImpl.countDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status)");
66
        Query query = null;
67

  
68
        if(status == null) {
69
            query = getSession().createQuery("select count(distinct description) from TaxonDescription description left join description.descriptionElements element join element.area area where area in (:namedAreas)");
70
        } else {
71
            query = getSession().createQuery("select count(distinct description) from TaxonDescription description left join description.descriptionElements element join element.area area  join element.status status where area in (:namedAreas) and status = :status");
72
            query.setParameter("status", status);
73
        }
74
        query.setParameterList("namedAreas", namedAreas);
75

  
76
        return ((Long)query.uniqueResult()).intValue();
77
    }
78

  
79
    public int countDescriptionElements(DescriptionBase description, Set<Feature> features, Class<? extends DescriptionElementBase> clazz) {
80
        AuditEvent auditEvent = getAuditEventFromContext();
81
        if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
82
            Criteria criteria = null;
83
            if(clazz == null) {
84
                criteria = getSession().createCriteria(DescriptionElementBase.class);
85
            } else {
86
                criteria = getSession().createCriteria(clazz);
87
            }
88

  
89
            if(description != null) {
90
                criteria.add(Restrictions.eq("inDescription", description));
91
            }
92

  
93
            if(features != null && !features.isEmpty()) {
94
                criteria.add(Restrictions.in("feature", features));
95
            }
96

  
97
            criteria.setProjection(Projections.rowCount());
98

  
99
            return (Integer)criteria.uniqueResult();
100
        } else {
101
            if(features != null && !features.isEmpty()) {
102
                Integer count = 0;
103
                for(Feature f : features) {
104
                    AuditQuery query = null;
105
                    if(clazz == null) {
106
                        query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
107
                    } else {
108
                        query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
109
                    }
110

  
111
                    if(description != null) {
112
                        query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
113
                    }
114

  
115
                    query.add(AuditEntity.relatedId("feature").eq(f.getId()));
116
                    query.addProjection(AuditEntity.id().count("id"));
117
                    count += ((Long)query.getSingleResult()).intValue();
118
                }
119

  
120
                return count;
121
            } else {
122
                AuditQuery query = null;
123
                if(clazz == null) {
124
                    query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
125
                } else {
126
                    query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
127
                }
128

  
129
                if(description != null) {
130
                    query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
131
                }
132
                query.addProjection(AuditEntity.id().count("id"));
133
                return ((Long)query.getSingleResult()).intValue();
134
            }
135
        }
136
    }
137

  
138
    public int countDescriptions(Class<? extends DescriptionBase> clazz, Boolean hasImages, Boolean hasText, Set<Feature> features) {
139
        checkNotInPriorView("DescriptionDaoImpl.countDescriptions(Class<TYPE> type, Boolean hasImages, Boolean hasText, Set<Feature> features)");
140
        Criteria inner = null;
141

  
142
        if(clazz == null) {
143
            inner = getSession().createCriteria(type);
144
        } else {
145
            inner = getSession().createCriteria(clazz);
146
        }
147

  
148
        Criteria elementsCriteria = inner.createCriteria("descriptionElements");
149
        if(hasText != null) {
150
            if(hasText) {
151
                elementsCriteria.add(Restrictions.isNotEmpty("multilanguageText"));
152
            } else {
153
                elementsCriteria.add(Restrictions.isEmpty("multilanguageText"));
154
            }
155
        }
156

  
157
        if(hasImages != null) {
158
            if(hasImages) {
159
                elementsCriteria.add(Restrictions.isNotEmpty("media"));
160
            } else {
161
                elementsCriteria.add(Restrictions.isEmpty("media"));
162
            }
163
        }
164

  
165
        if(features != null && !features.isEmpty()) {
166
            elementsCriteria.add(Restrictions.in("feature", features));
167
        }
168

  
169
        inner.setProjection(Projections.countDistinct("id"));
170

  
171
        return (Integer) inner.uniqueResult();
172
    }
173

  
174
    public int countTaxonDescriptions(Taxon taxon, Set<Scope> scopes,Set<NamedArea> geographicalScopes, Set<MarkerType> markerTypes) {
175
        AuditEvent auditEvent = getAuditEventFromContext();
176
        if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
177
            Criteria criteria = getSession().createCriteria(TaxonDescription.class);
178

  
179
            if(taxon != null) {
180
                criteria.add(Restrictions.eq("taxon", taxon));
181
            }
182

  
183
            if(scopes != null && !scopes.isEmpty()) {
184
                Set<Integer> scopeIds = new HashSet<Integer>();
185
                for(Scope s : scopes) {
186
                    scopeIds.add(s.getId());
187
                }
188
                criteria.createCriteria("scopes").add(Restrictions.in("id", scopeIds));
189
            }
190

  
191
            if(geographicalScopes != null && !geographicalScopes.isEmpty()) {
192
                Set<Integer> geoScopeIds = new HashSet<Integer>();
193
                for(NamedArea n : geographicalScopes) {
194
                    geoScopeIds.add(n.getId());
195
                }
196
                criteria.createCriteria("geoScopes").add(Restrictions.in("id", geoScopeIds));
197
            }
198

  
199

  
200
            addMarkerTypesCriterion(markerTypes, criteria);
201

  
202

  
203
            criteria.setProjection(Projections.rowCount());
204

  
205
            return (Integer)criteria.uniqueResult();
206
        } else {
207
            if((scopes == null || scopes.isEmpty())&& (geographicalScopes == null || geographicalScopes.isEmpty()) && (markerTypes == null || markerTypes.isEmpty())) {
208
                AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonDescription.class,auditEvent.getRevisionNumber());
209
                if(taxon != null) {
210
                    query.add(AuditEntity.relatedId("taxon").eq(taxon.getId()));
211
                }
212

  
213
                query.addProjection(AuditEntity.id().count("id"));
214

  
215
                return ((Long)query.getSingleResult()).intValue();
216
            } else {
217
                throw new OperationNotSupportedInPriorViewException("countTaxonDescriptions(Taxon taxon, Set<Scope> scopes,Set<NamedArea> geographicalScopes)");
218
            }
219
        }
220
    }
221

  
222
    /**
223
     * @param markerTypes
224
     * @param criteria
225
     *
226
     */
227
    //TODO move to AnnotatableEntityDao(?)
228
    private void addMarkerTypesCriterion(Set<MarkerType> markerTypes,
229
            Criteria criteria) {
230

  
231
        if(markerTypes != null && !markerTypes.isEmpty()) {
232
            Set<Integer> markerTypeIds = new HashSet<Integer>();
233
            for(MarkerType markerType : markerTypes) {
234
                markerTypeIds.add(markerType.getId());
235
            }
236
            criteria.createCriteria("markers").add(Restrictions.eq("flag", true))
237
                    .createAlias("markerType", "mt")
238
                     .add(Restrictions.in("mt.id", markerTypeIds));
239
        }
240
    }
241

  
242
    public List<DescriptionElementBase> getDescriptionElements(DescriptionBase description, Set<Feature> features,Class<? extends DescriptionElementBase> clazz, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
243

  
244
        AuditEvent auditEvent = getAuditEventFromContext();
245
        if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
245 246
            Criteria criteria = null;
246 247
            if(clazz == null) {
247
            	criteria = getSession().createCriteria(DescriptionElementBase.class);
248
                criteria = getSession().createCriteria(DescriptionElementBase.class);
248 249
            } else {
249
            	criteria = getSession().createCriteria(clazz);
250
                criteria = getSession().createCriteria(clazz);
250 251
            }
251
		
252

  
252 253
            if(description != null) {
253
		        criteria.add(Restrictions.eq("inDescription", description));
254
		    }
255
		
256
		    if(features != null && !features.isEmpty()) {
257
			    criteria.add(Restrictions.in("feature", features));
258
		    }
259
		
260
		    if(pageSize != null) {
261
			    criteria.setMaxResults(pageSize);
262
		        if(pageNumber != null) {
263
		    	    criteria.setFirstResult(pageNumber * pageSize);
264
		        }
265
		    }
266
		    
267
		    List<DescriptionElementBase> results = (List<DescriptionElementBase>)criteria.list();
268
		    
269
		    defaultBeanInitializer.initializeAll(results, propertyPaths);
270
		
271
	    	return results; 
272
		} else {
273
			List<DescriptionElementBase> result = new ArrayList<DescriptionElementBase>();
274
			if(features != null && !features.isEmpty()) {
275
				
276
			    for(Feature f : features) {
277
			    	AuditQuery query = null;
278
			    	if(clazz == null) {
279
			            query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
280
			    	} else {
281
			    		query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
282
			    	}
283
			    	
284
			        if(description != null) {
285
			    	    query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
286
			        }
287
			     
288
			        query.add(AuditEntity.relatedId("feature").eq(f.getId()));
289
			        result.addAll((List<DescriptionElementBase>)query.getResultList());
290
			    }
291
			} else {
292
				AuditQuery query = null;
293
		    	if(clazz == null) {
294
		            query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
295
		    	} else {
296
		    		query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
297
		    	}
298
			    
299
		        if(description != null) {
300
		    	    query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
301
		        }
302
		        
303
		        result = query.getResultList();
304
			}
305
			
306
			defaultBeanInitializer.initializeAll(result, propertyPaths);
307
			
308
			return result;
309
		}
310
	}
311

  
312
	public List<TaxonDescription> getTaxonDescriptions(Taxon taxon,	Set<Scope> scopes, Set<NamedArea> geographicalScopes, Set<MarkerType> markerTypes, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
313
		AuditEvent auditEvent = getAuditEventFromContext();
314
		if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
315
			Criteria criteria = getSession().createCriteria(TaxonDescription.class);
316

  
317
			if(taxon != null) {
318
				criteria.add(Restrictions.eq("taxon", taxon));
319
			}
320

  
321
			if(scopes != null && !scopes.isEmpty()) {
322
				Set<Integer> scopeIds = new HashSet<Integer>();
323
				for(Scope s : scopes) {
324
					scopeIds.add(s.getId());
325
				}
326
				criteria.createCriteria("scopes").add(Restrictions.in("id", scopeIds));
327
			}
328

  
329
			if(geographicalScopes != null && !geographicalScopes.isEmpty()) {
330
				Set<Integer> geoScopeIds = new HashSet<Integer>();
331
				for(NamedArea n : geographicalScopes) {
332
					geoScopeIds.add(n.getId());
333
				}
334
				criteria.createCriteria("geoScopes").add(Restrictions.in("id", geoScopeIds));
335
			}
336
			
337
			addMarkerTypesCriterion(markerTypes, criteria);
338

  
339
			if(pageSize != null) {
340
				criteria.setMaxResults(pageSize);
341
				if(pageNumber != null) {
342
					criteria.setFirstResult(pageNumber * pageSize);
343
				}
344
			}
345

  
346
			List<TaxonDescription> results = (List<TaxonDescription>)criteria.list();
347

  
348
			defaultBeanInitializer.initializeAll(results, propertyPaths);
349

  
350
			return results;
351
		} else {
352
			if((scopes == null || scopes.isEmpty())&& (geographicalScopes == null || geographicalScopes.isEmpty())&& (markerTypes == null || markerTypes.isEmpty())) {
353
				AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonDescription.class,auditEvent.getRevisionNumber());
354
				if(taxon != null) {
355
				    query.add(AuditEntity.relatedId("taxon").eq(taxon.getId()));
356
				}
357

  
358
				if(pageSize != null) {
359
			        query.setMaxResults(pageSize);
360
			        if(pageNumber != null) {
361
			            query.setFirstResult(pageNumber * pageSize);
362
			        } else {
363
			    	    query.setFirstResult(0);
364
			        }
365
			    }
366
				
367
				List<TaxonDescription> results = (List<TaxonDescription>)query.getResultList();
368
				defaultBeanInitializer.initializeAll(results, propertyPaths);
369
				return results;
370
			} else {
371
				throw new OperationNotSupportedInPriorViewException("countTaxonDescriptions(Taxon taxon, Set<Scope> scopes,Set<NamedArea> geographicalScopes)");
372
			}
373
		}
374
	}
375
	
254
                criteria.add(Restrictions.eq("inDescription", description));
255
            }
256

  
257
            if(features != null && !features.isEmpty()) {
258
                criteria.add(Restrictions.in("feature", features));
259
            }
260

  
261
            if(pageSize != null) {
262
                criteria.setMaxResults(pageSize);
263
                if(pageNumber != null) {
264
                    criteria.setFirstResult(pageNumber * pageSize);
265
                }
266
            }
267

  
268
            List<DescriptionElementBase> results = (List<DescriptionElementBase>)criteria.list();
269

  
270
            defaultBeanInitializer.initializeAll(results, propertyPaths);
271

  
272
            return results;
273
        } else {
274
            List<DescriptionElementBase> result = new ArrayList<DescriptionElementBase>();
275
            if(features != null && !features.isEmpty()) {
276

  
277
                for(Feature f : features) {
278
                    AuditQuery query = null;
279
                    if(clazz == null) {
280
                        query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
281
                    } else {
282
                        query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
283
                    }
284

  
285
                    if(description != null) {
286
                        query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
287
                    }
288

  
289
                    query.add(AuditEntity.relatedId("feature").eq(f.getId()));
290
                    result.addAll((List<DescriptionElementBase>)query.getResultList());
291
                }
292
            } else {
293
                AuditQuery query = null;
294
                if(clazz == null) {
295
                    query = getAuditReader().createQuery().forEntitiesAtRevision(DescriptionElementBase.class,auditEvent.getRevisionNumber());
296
                } else {
297
                    query = getAuditReader().createQuery().forEntitiesAtRevision(clazz,auditEvent.getRevisionNumber());
298
                }
299

  
300
                if(description != null) {
301
                    query.add(AuditEntity.relatedId("inDescription").eq(description.getId()));
302
                }
303

  
304
                result = query.getResultList();
305
            }
306

  
307
            defaultBeanInitializer.initializeAll(result, propertyPaths);
308

  
309
            return result;
310
        }
311
    }
312

  
313
    public List<TaxonDescription> listTaxonDescriptions(Taxon taxon, Set<Scope> scopes, Set<NamedArea> geographicalScopes, Set<MarkerType> markerTypes, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
314
        AuditEvent auditEvent = getAuditEventFromContext();
315
        if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
316
            Criteria criteria = getSession().createCriteria(TaxonDescription.class);
317

  
318
            if(taxon != null) {
319
                criteria.add(Restrictions.eq("taxon", taxon));
320
            }
321

  
322
            if(scopes != null && !scopes.isEmpty()) {
323
                Set<Integer> scopeIds = new HashSet<Integer>();
324
                for(Scope s : scopes) {
325
                    scopeIds.add(s.getId());
326
                }
327
                criteria.createCriteria("scopes").add(Restrictions.in("id", scopeIds));
328
            }
329

  
330
            if(geographicalScopes != null && !geographicalScopes.isEmpty()) {
331
                Set<Integer> geoScopeIds = new HashSet<Integer>();
332
                for(NamedArea n : geographicalScopes) {
333
                    geoScopeIds.add(n.getId());
334
                }
335
                criteria.createCriteria("geoScopes").add(Restrictions.in("id", geoScopeIds));
336
            }
337

  
338
            addMarkerTypesCriterion(markerTypes, criteria);
339

  
340
            if(pageSize != null) {
341
                criteria.setMaxResults(pageSize);
342
                if(pageNumber != null) {
343
                    criteria.setFirstResult(pageNumber * pageSize);
344
                }
345
            }
346

  
347
            List<TaxonDescription> results = (List<TaxonDescription>)criteria.list();
348

  
349
            defaultBeanInitializer.initializeAll(results, propertyPaths);
350

  
351
            return results;
352
        } else {
353
            if((scopes == null || scopes.isEmpty())&& (geographicalScopes == null || geographicalScopes.isEmpty())&& (markerTypes == null || markerTypes.isEmpty())) {
354
                AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonDescription.class,auditEvent.getRevisionNumber());
355
                if(taxon != null) {
356
                    query.add(AuditEntity.relatedId("taxon").eq(taxon.getId()));
357
                }
358

  
359
                if(pageSize != null) {
360
                    query.setMaxResults(pageSize);
361
                    if(pageNumber != null) {
362
                        query.setFirstResult(pageNumber * pageSize);
363
                    } else {
364
                        query.setFirstResult(0);
365
                    }
366
                }
367

  
368
                List<TaxonDescription> results = (List<TaxonDescription>)query.getResultList();
369
                defaultBeanInitializer.initializeAll(results, propertyPaths);
370
                return results;
371
            } else {
372
                throw new OperationNotSupportedInPriorViewException("countTaxonDescriptions(Taxon taxon, Set<Scope> scopes,Set<NamedArea> geographicalScopes)");
373
            }
374
        }
375
    }
376

  
376 377
    public List<TaxonNameDescription> getTaxonNameDescriptions(TaxonNameBase name, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
377 378
        AuditEvent auditEvent = getAuditEventFromContext();
378
	    if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
379
	        Criteria criteria = getSession().createCriteria(TaxonNameDescription.class);
380
	  
381
	      if(name != null) {
382
		      criteria.add(Restrictions.eq("taxonName", name));
383
	      }
384
	  
385
	      if(pageSize != null) {
386
			  criteria.setMaxResults(pageSize);
387
		      if(pageNumber != null) {
388
		    	  criteria.setFirstResult(pageNumber * pageSize);
389
		      }
390
	      }
391
	  
392
	      List<TaxonNameDescription> results = (List<TaxonNameDescription>)criteria.list();
393
	      
394
	      defaultBeanInitializer.initializeAll(results, propertyPaths);
395
	      
396
	      return results; 
397
	    } else {
398
	    	AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonNameDescription.class,auditEvent.getRevisionNumber());
399
	    	
400
	    	if(name != null) {
401
			    query.add(AuditEntity.relatedId("taxonName").eq(name.getId()));
402
		    }
403
	    	
404
	    	if(pageSize != null) {
405
				  query.setMaxResults(pageSize);
406
			      if(pageNumber != null) {
407
			    	  query.setFirstResult(pageNumber * pageSize);
408
			      }
409
		    }
410
	    	
411
	    	List<TaxonNameDescription> results = (List<TaxonNameDescription>)query.getResultList();
412
	    	
413
	    	defaultBeanInitializer.initializeAll(results, propertyPaths);
414
	    	
415
	    	return results;
416
	    }
417
	  
379
        if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
380
            Criteria criteria = getSession().createCriteria(TaxonNameDescription.class);
381

  
382
          if(name != null) {
383
              criteria.add(Restrictions.eq("taxonName", name));
384
          }
385

  
386
          if(pageSize != null) {
387
              criteria.setMaxResults(pageSize);
388
              if(pageNumber != null) {
389
                  criteria.setFirstResult(pageNumber * pageSize);
390
              }
391
          }
392

  
393
          List<TaxonNameDescription> results = (List<TaxonNameDescription>)criteria.list();
394

  
395
          defaultBeanInitializer.initializeAll(results, propertyPaths);
396

  
397
          return results;
398
        } else {
399
            AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonNameDescription.class,auditEvent.getRevisionNumber());
400

  
401
            if(name != null) {
402
                query.add(AuditEntity.relatedId("taxonName").eq(name.getId()));
403
            }
404

  
405
            if(pageSize != null) {
406
                  query.setMaxResults(pageSize);
407
                  if(pageNumber != null) {
408
                      query.setFirstResult(pageNumber * pageSize);
409
                  }
410
            }
411

  
412
            List<TaxonNameDescription> results = (List<TaxonNameDescription>)query.getResultList();
413

  
414
            defaultBeanInitializer.initializeAll(results, propertyPaths);
415

  
416
            return results;
417
        }
418

  
418 419
    }
419
	
420
	public int countTaxonNameDescriptions(TaxonNameBase name) {
421
		AuditEvent auditEvent = getAuditEventFromContext();
422
	    if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
423
		    Criteria criteria = getSession().createCriteria(TaxonNameDescription.class);
424
		  
425
		    if(name != null) {
426
			    criteria.add(Restrictions.eq("taxonName", name));
427
		    }
428
		  
429
		    criteria.setProjection(Projections.rowCount());
430
		  
431
		    return (Integer)criteria.uniqueResult();
432
	    } else {
420

  
421
    public int countTaxonNameDescriptions(TaxonNameBase name) {
422
        AuditEvent auditEvent = getAuditEventFromContext();
423
        if(auditEvent.equals(AuditEvent.CURRENT_VIEW)) {
424
            Criteria criteria = getSession().createCriteria(TaxonNameDescription.class);
425

  
426
            if(name != null) {
427
                criteria.add(Restrictions.eq("taxonName", name));
428
            }
429

  
430
            criteria.setProjection(Projections.rowCount());
431

  
432
            return (Integer)criteria.uniqueResult();
433
        } else {
433 434
            AuditQuery query = getAuditReader().createQuery().forEntitiesAtRevision(TaxonNameDescription.class,auditEvent.getRevisionNumber());
434
	    	
435
	    	if(name != null) {
436
			    query.add(AuditEntity.relatedId("taxonName").eq(name.getId()));
437
		    }
438
	    	
439
	    	query.addProjection(AuditEntity.id().count("id"));
440
	    	return ((Long)query.getSingleResult()).intValue();
441
	    }
442
	}
443

  
444
	/**
445
	 * Should use a DetachedCriteria & subquery, but HHH-158 prevents this, for now.
446
	 * 
447
	 * e.g. DetachedCriteria inner = DestachedCriteria.forClass(type);
448
	 * 
449
	 * outer.add(Subqueries.propertyIn("id", inner));
450
	 */
451
	public List<DescriptionBase> listDescriptions(Class<? extends DescriptionBase> clazz, Boolean hasImages, Boolean hasText,	Set<Feature> features, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
452
		checkNotInPriorView("DescriptionDaoImpl.listDescriptions(Class<TYPE> type, Boolean hasImages, Boolean hasText,	Set<Feature> features, Integer pageSize, Integer pageNumber)");
453
		Criteria inner = null;
454
		
455
		if(clazz == null) {
456
			inner = getSession().createCriteria(type);
457
		} else {
458
			inner = getSession().createCriteria(clazz);
459
		}
460
		
461
		Criteria elementsCriteria = inner.createCriteria("descriptionElements");
462
		if(hasText != null) {
463
			if(hasText) {
464
				elementsCriteria.add(Restrictions.isNotEmpty("multilanguageText"));
465
			} else {
466
				elementsCriteria.add(Restrictions.isEmpty("multilanguageText"));
467
			}
468
		}
469
		
470
		if(hasImages != null) {
471
			if(hasImages) {
472
				elementsCriteria.add(Restrictions.isNotEmpty("media"));
473
			} else {
474
				elementsCriteria.add(Restrictions.isEmpty("media"));
475
			}
476
		}
477
		
478
		if(features != null && !features.isEmpty()) {
479
			elementsCriteria.add(Restrictions.in("feature", features));
480
		}
481
		
482
		inner.setProjection(Projections.distinct(Projections.id()));
483
		
484
		List<Object> intermediateResult = (List<Object>)inner.list();
485
		
486
		if(intermediateResult.isEmpty()) {
487
			return new ArrayList<DescriptionBase>();
488
		}
489
		
490
		Integer[] resultIds = new Integer[intermediateResult.size()];
491
		for(int i = 0; i < resultIds.length; i++) {	
492
				resultIds[i] = (Integer)intermediateResult.get(i);
493
		}
494
		
495
		Criteria outer = null;
496
		
497
		if(clazz == null) {
498
			outer = getSession().createCriteria(type);
499
		} else {
500
			outer = getSession().createCriteria(clazz);
501
		}
502
		
503
		outer.add(Restrictions.in("id", resultIds));
504
		addOrder(outer, orderHints);
505
		
506
		if(pageSize != null) {
507
			outer.setMaxResults(pageSize);
508
		    if(pageNumber != null) {
509
		    	outer.setFirstResult(pageNumber * pageSize);
510
		    }
511
		}
512
		
513
		List<DescriptionBase> results = (List<DescriptionBase>)outer.list();
514
		defaultBeanInitializer.initializeAll(results, propertyPaths);
515
		return results;
516
	}
517

  
518
	public List<TaxonDescription> searchDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
519
		checkNotInPriorView("DescriptionDaoImpl.searchDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
520
        
435

  
436
            if(name != null) {
437
                query.add(AuditEntity.relatedId("taxonName").eq(name.getId()));
438
            }
439

  
440
            query.addProjection(AuditEntity.id().count("id"));
441
            return ((Long)query.getSingleResult()).intValue();
442
        }
443
    }
444

  
445
    /**
446
     * Should use a DetachedCriteria & subquery, but HHH-158 prevents this, for now.
447
     *
448
     * e.g. DetachedCriteria inner = DestachedCriteria.forClass(type);
449
     *
450
     * outer.add(Subqueries.propertyIn("id", inner));
451
     */
452
    public List<DescriptionBase> listDescriptions(Class<? extends DescriptionBase> clazz, Boolean hasImages, Boolean hasText,	Set<Feature> features, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
453
        checkNotInPriorView("DescriptionDaoImpl.listDescriptions(Class<TYPE> type, Boolean hasImages, Boolean hasText,	Set<Feature> features, Integer pageSize, Integer pageNumber)");
454
        Criteria inner = null;
455

  
456
        if(clazz == null) {
457
            inner = getSession().createCriteria(type);
458
        } else {
459
            inner = getSession().createCriteria(clazz);
460
        }
461

  
462
        Criteria elementsCriteria = inner.createCriteria("descriptionElements");
463
        if(hasText != null) {
464
            if(hasText) {
465
                elementsCriteria.add(Restrictions.isNotEmpty("multilanguageText"));
466
            } else {
467
                elementsCriteria.add(Restrictions.isEmpty("multilanguageText"));
468
            }
469
        }
470

  
471
        if(hasImages != null) {
472
            if(hasImages) {
473
                elementsCriteria.add(Restrictions.isNotEmpty("media"));
474
            } else {
475
                elementsCriteria.add(Restrictions.isEmpty("media"));
476
            }
477
        }
478

  
479
        if(features != null && !features.isEmpty()) {
480
            elementsCriteria.add(Restrictions.in("feature", features));
481
        }
482

  
483
        inner.setProjection(Projections.distinct(Projections.id()));
484

  
485
        List<Object> intermediateResult = (List<Object>)inner.list();
486

  
487
        if(intermediateResult.isEmpty()) {
488
            return new ArrayList<DescriptionBase>();
489
        }
490

  
491
        Integer[] resultIds = new Integer[intermediateResult.size()];
492
        for(int i = 0; i < resultIds.length; i++) {
493
                resultIds[i] = (Integer)intermediateResult.get(i);
494
        }
495

  
496
        Criteria outer = null;
497

  
498
        if(clazz == null) {
499
            outer = getSession().createCriteria(type);
500
        } else {
501
            outer = getSession().createCriteria(clazz);
502
        }
503

  
504
        outer.add(Restrictions.in("id", resultIds));
505
        addOrder(outer, orderHints);
506

  
507
        if(pageSize != null) {
508
            outer.setMaxResults(pageSize);
509
            if(pageNumber != null) {
510
                outer.setFirstResult(pageNumber * pageSize);
511
            }
512
        }
513

  
514
        List<DescriptionBase> results = (List<DescriptionBase>)outer.list();
515
        defaultBeanInitializer.initializeAll(results, propertyPaths);
516
        return results;
517
    }
518

  
519
    public List<TaxonDescription> searchDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
520
        checkNotInPriorView("DescriptionDaoImpl.searchDescriptionByDistribution(Set<NamedArea> namedAreas, PresenceAbsenceTermBase status, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
521

  
521 522
        Criteria criteria = getSession().createCriteria(TaxonDescription.class);
522 523
        Criteria elements = criteria.createCriteria("descriptionElements", "descriptionElement", Criteria.LEFT_JOIN);
523
		elements.add(Restrictions.in("area", namedAreas.toArray()));
524
		
525
		if(status != null) {
526
			elements.add(Restrictions.eq("status", status));
527
		}
528
		
529
		ProjectionList projectionList = Projections.projectionList().add(Projections.id());
530
		
531
		if(orderHints != null && !orderHints.isEmpty()) {
532
		    for(OrderHint orderHint : orderHints) {
533
			    projectionList = projectionList.add(Projections.property(orderHint.getPropertyName()));
534
		    }
535
		}
536
		
537
		criteria.setProjection(Projections.distinct(projectionList));	
538
		
539
		if(pageSize != null) {
540
			criteria.setMaxResults(pageSize);
541
		    if(pageNumber != null) {
542
		    	criteria.setFirstResult(pageNumber * pageSize);
543
		    }
544
		}
545
		
546
		addOrder(criteria,orderHints);
547
		
548
		List<Object> intermediateResult = (List<Object>)criteria.list();
549
		
550
		if(intermediateResult.isEmpty())
551
			return new ArrayList<TaxonDescription>();
552
		
553
		Integer[] resultIds = new Integer[intermediateResult.size()];
554
		for(int i = 0; i < resultIds.length; i++) {
555
			if(orderHints == null || orderHints.isEmpty()) {
556
				resultIds[i] = (Integer)intermediateResult.get(i);
557
			} else {
558
			  resultIds[i] = (Integer)((Object[])intermediateResult.get(i))[0];
559
			}
560
		}
561
		
562
		criteria = getSession().createCriteria(TaxonDescription.class);
563
		criteria.add(Restrictions.in("id", resultIds));
564
		addOrder(criteria,orderHints);
565
		
566
		List<TaxonDescription> results = (List<TaxonDescription>)criteria.list();
567
		defaultBeanInitializer.initializeAll(results, propertyPaths);
568
		return results;
569
	}
570
	
571
	public List<CommonTaxonName> searchDescriptionByCommonName(String queryString, MatchMode matchMode, Integer pageSize, Integer pageNumber) {
572
		
573
		Criteria crit = getSession().createCriteria(CommonTaxonName.class); 
574
		if (matchMode == MatchMode.EXACT) { 
575
			crit.add(Restrictions.eq("name", matchMode.queryStringFrom(queryString))); 
576
		} else { 
577
			crit.add(Restrictions.ilike("name", matchMode.queryStringFrom(queryString))); 
578
		} 
579

  
580
		if(pageSize != null) {
581
			crit.setMaxResults(pageSize); 
582
			if(pageNumber != null) {
583
				crit.setFirstResult(pageNumber * pageSize);
584
			}
585
		}
586
		List<CommonTaxonName> results = (List<CommonTaxonName>)crit.list();
587
		return results;
588
	}
589

  
590
	public Integer countDescriptionByCommonName(String queryString, MatchMode matchMode) {
591
		//TODO inprove performance
592
		List<CommonTaxonName> results =  searchDescriptionByCommonName(queryString, matchMode, null, null);
593
		return results.size();
594
	}
595
	
596
	@Override
597
	public DescriptionBase find(LSID lsid) {
598
		DescriptionBase descriptionBase = super.find(lsid);
599
		if(descriptionBase != null) {
600
			List<String> propertyPaths = new ArrayList<String>();
601
			propertyPaths.add("createdBy");
602
			propertyPaths.add("updatedBy");
603
			propertyPaths.add("taxon");
604
			propertyPaths.add("taxonName");
605
			propertyPaths.add("descriptionElements");
606
			propertyPaths.add("descriptionElements.createdBy");
607
			propertyPaths.add("descriptionElements.updatedBy");
608
			propertyPaths.add("descriptionElements.feature");
609
			propertyPaths.add("descriptionElements.multilanguageText");
610
			propertyPaths.add("descriptionElements.multilanguageText.language");
611
			propertyPaths.add("descriptionElements.area");
612
			propertyPaths.add("descriptionElements.status");
613
			propertyPaths.add("descriptionElements.modifyingText");
614
			propertyPaths.add("descriptionElementsmodifyingText.language");
615
			propertyPaths.add("descriptionElements.modifiers");
616
			
617
			defaultBeanInitializer.initialize(descriptionBase, propertyPaths);
618
		}
619
		return descriptionBase;
620
	}
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff