import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;\r
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;\r
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;\r
+import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;\r
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;\r
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;\r
import eu.etaxonomy.cdm.model.taxon.Synonym;\r
\r
@Override\r
public long countMedia(SpecimenOrObservationBase occurence) {\r
- checkNotInPriorView("OccurrenceDaoHibernateImpl.countMedia(SpecimenOrObservationBase occurence)");\r
- Query query = getSession().createQuery("SELECT count(media) FROM SpecimenOrObservationBase occurence JOIN occurence.media media WHERE occurence = :occurence");\r
+ return this.getMediaIds(occurence).size();\r
+ }\r
+\r
+ @Override\r
+ public List<Media> getMedia(SpecimenOrObservationBase occurence,\r
+ Integer pageSize, Integer pageNumber, List<String> propertyPaths) {\r
+ checkNotInPriorView("OccurrenceDaoHibernateImpl.getMedia(SpecimenOrObservationBase occurence, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");\r
+ List<Integer> ids = this.getMediaIds(occurence);\r
+ Query query = getSession().createQuery(\r
+ " SELECT m "\r
+ + " FROM Media m "\r
+ + " WHERE m.id in (:mediaIds)");\r
+ query.setParameterList("mediaIds", ids);\r
+\r
+ addPageSizeAndNumber(query, pageSize, pageNumber);\r
+\r
+ @SuppressWarnings("unchecked")\r
+ List<Media> results = query.list();\r
+ defaultBeanInitializer.initializeAll(results, propertyPaths);\r
+ return results;\r
+ }\r
+\r
+ private List<Integer> getMediaIds(SpecimenOrObservationBase occurence) {\r
+ Query query = getSession().createQuery(\r
+ " SELECT DISTINCT m.id "\r
+ + " FROM SpecimenOrObservationBase occ JOIN occ.descriptions d "\r
+ + " JOIN d.descriptionElements el JOIN el.media m "\r
+ + " WHERE occ = :occurence AND d.imageGallery = true "\r
+ + " ORDER BY m.id ");\r
query.setParameter("occurence", occurence);\r
+ @SuppressWarnings("unchecked")\r
+ List<Integer> list = query.list();\r
+\r
+ if (occurence.isInstanceOf(MediaSpecimen.class)){\r
+ String q2Str = " SELECT DISTINCT m.id "\r
+ + " FROM MediaSpecimen spec "\r
+ + " JOIN spec.mediaSpecimen m "\r
+ + " WHERE spec = :occurence "\r
+ + " ORDER BY m.id ";\r
+ Query q2 = getSession().createQuery(q2Str);\r
+ q2.setParameter("occurence", occurence);\r
+ List<Integer> list2 = q2.list();\r
+ list.addAll(list2);\r
+ Set<Integer> dedupSet = new HashSet<>(list);\r
+ list = new ArrayList<>(dedupSet);\r
+ Collections.sort(list);\r
+ }\r
\r
- return (Long)query.uniqueResult();\r
+ return list;\r
}\r
\r
@Override\r
}\r
}\r
\r
- @Override\r
- public List<Media> getMedia(SpecimenOrObservationBase occurence,\r
- Integer pageSize, Integer pageNumber, List<String> propertyPaths) {\r
- checkNotInPriorView("OccurrenceDaoHibernateImpl.getMedia(SpecimenOrObservationBase occurence, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");\r
- Query query = getSession().createQuery(\r
- " SELECT media "\r
- + " FROM SpecimenOrObservationBase occurence "\r
- + " JOIN occurence.media media "\r
- + " WHERE occurence = :occurence");\r
- query.setParameter("occurence", occurence);\r
-\r
- addPageSizeAndNumber(query, pageSize, pageNumber);\r
-\r
- @SuppressWarnings("unchecked")\r
- List<Media> results = query.list();\r
- defaultBeanInitializer.initializeAll(results, propertyPaths);\r
- return results;\r
- }\r
-\r
@Override\r
public void rebuildIndex() {\r
FullTextSession fullTextSession = Search.getFullTextSession(getSession());\r
package eu.etaxonomy.cdm.persistence.dao.hibernate.occurrence;
import java.io.FileNotFoundException;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
import org.h2.util.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import org.unitils.spring.annotation.SpringBeanByType;
+import eu.etaxonomy.cdm.common.URI;
+import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.description.SpecimenDescription;
+import eu.etaxonomy.cdm.model.description.TextData;
+import eu.etaxonomy.cdm.model.media.Media;
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
+import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
import eu.etaxonomy.cdm.persistence.dao.occurrence.IOccurrenceDao;
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
logger.warn("Not yet implemented");
}
- @Test
- public void testCountMedia() {
- logger.warn("Not yet implemented");
- }
+ @Test
+ public void testCountMedia() {
+ MediaSpecimen unit = MediaSpecimen.NewInstance(SpecimenOrObservationType.Media);
+ SpecimenDescription desc = SpecimenDescription.NewInstance(unit);
+ desc.setImageGallery(true);
+ TextData textData = TextData.NewInstance(Feature.IMAGE());
+ desc.addElement(textData);
+ Media media1 = Media.NewInstance(URI.create("https://www.abc.de"), 5, "jpg", "jpg");
+ Media media2 = Media.NewInstance(URI.create("https://www.abc.de"), 5, "jpg", "jpg");
+ textData.addMedia(media1);
+ textData.addMedia(media2);
+
+ Media media3 = Media.NewInstance(URI.create("https://www.abc.de"), 5, "jpg", "jpg");
+ unit.setMediaSpecimen(media3);
+ dao.save(unit);
+
+ Assert.assertEquals(3, dao.countMedia(unit));
+ unit.setMediaSpecimen(media2);
+ Assert.assertEquals(2, dao.countMedia(unit));
+ }
+
+ @Test
+ public void testGetMedia() {
+ MediaSpecimen unit = MediaSpecimen.NewInstance(SpecimenOrObservationType.Media);
+ SpecimenDescription desc = SpecimenDescription.NewInstance(unit);
+ desc.setImageGallery(true);
+ TextData textData = TextData.NewInstance(Feature.IMAGE());
+ desc.addElement(textData);
+ Media media1 = Media.NewInstance(URI.create("https://www.abc.de"), 5, "jpg", "jpg");
+ Media media2 = Media.NewInstance(URI.create("https://www.defg.de"), 5, "jpg", "jpg");
+ textData.addMedia(media1);
+ textData.addMedia(media2);
+
+ Media media3 = Media.NewInstance(URI.create("https://www.hij.de"), 5, "jpg", "jpg");
+ unit.setMediaSpecimen(media3);
+ dao.save(unit);
+
+ List<Media> media = dao.getMedia(unit, null, null, null);
+ Assert.assertEquals(3, media.size());
+ //test that paging works (note: the sorting is not a requirement for the method, but with current implementation it works; if implementation is changed the test may need to be adapted
+ List<Integer> ids = media.stream().map(m->m.getId()).collect(Collectors.toList());
+ Collections.sort(ids);
+ Assert.assertEquals(ids.get(0), (Integer)media.get(0).getId());
+ Assert.assertEquals(ids.get(1), (Integer)media.get(1).getId());
+ Assert.assertEquals(ids.get(2), (Integer)media.get(2).getId());
+ media = dao.getMedia(unit, 2, 1, null);
+ Assert.assertEquals(1, media.size());
+ Assert.assertEquals(ids.get(2), (Integer)media.get(0).getId());
+
+ //test deduplication
+ unit.setMediaSpecimen(media2);
+ Assert.assertEquals(2, dao.countMedia(unit));
+ }
@Test
public void testGetDerivationEvents() {
logger.warn("Not yet implemented");
}
- @Test
- public void testGetMedia() {
- logger.warn("Not yet implemented");
- }
-
@Test
public void testCountClassOfQextendsSpecimenOrObservationBaseTaxonBase() {
logger.warn("Not yet implemented");
//assert no exception
}
- /* (non-Javadoc)
- * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
- */
@Override
- public void createTestDataSet() throws FileNotFoundException {
- // TODO Auto-generated method stub
-
- }
-
-}
+ public void createTestDataSet() throws FileNotFoundException {}
+}
\ No newline at end of file