Skip to content

Commit

Permalink
more domain changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Eichar committed Sep 10, 2013
1 parent 96b5dc6 commit fa45657
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 41 deletions.
37 changes: 37 additions & 0 deletions domain/src/main/java/org/fao/geonet/domain/Localized.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.fao.geonet.domain;

import org.fao.geonet.repository.LocalizedEntityRepositoryImpl;
import org.jdom.Element;
import org.springframework.beans.BeanWrapperImpl;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.persistence.*;
import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -91,4 +94,38 @@ public void setLabelTranslations(List<Element> translations) {
getLabelTranslations().put(langId, value);
}
}

public Element asXml() {
Element record = new Element(LocalizedEntityRepositoryImpl.RECORD_EL_NAME);
BeanWrapperImpl wrapper = new BeanWrapperImpl(this);

for (PropertyDescriptor desc : wrapper.getPropertyDescriptors()) {
try {
if (desc.getReadMethod().getDeclaringClass() == getClass() && desc.getReadMethod().getAnnotation(Transient.class)
== null) {
final String descName = desc.getName();
if (descName.equalsIgnoreCase("labelTranslations")) {
Element labelEl = new Element(LocalizedEntityRepositoryImpl.LABEL_EL_NAME);

Map<String, String> labels = (Map<String, String>) desc.getReadMethod().invoke(this);
for (Map.Entry<String, String> entry : labels.entrySet()) {
labelEl.addContent(new Element(entry.getKey().toLowerCase()).setText(entry.getValue()));
}

record.addContent(labelEl);
} else {
final String value;
value = desc.getReadMethod().invoke(this).toString();
record.addContent(
new Element(descName.toLowerCase()).setText(value)
);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

return record;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.fao.geonet.repository;

import org.fao.geonet.domain.Group;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -11,7 +12,7 @@
* @author Jesse
*/
public interface GroupRepository extends GeonetRepository<Group, Integer>, GroupRepositoryCustom,
LocalizedEntityRepository<Group, Integer> {
LocalizedEntityRepository<Group, Integer>, JpaSpecificationExecutor<Group> {
/**
* Look up a group by its name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.fao.geonet.domain.Localized;
import org.jdom.Element;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import javax.annotation.Nonnull;
import java.io.Serializable;
Expand All @@ -23,8 +25,38 @@ public interface LocalizedEntityRepository<T extends Localized, ID extends Seria
* ...
* &lt;/entityName&gt;
* </pre>
* @return
* @return all entities in XML.
*/
@Nonnull
Element findAllAsXml();

/**
* Load all entities that satisfy the criteria provided and convert each to XML of the form:
* <pre>
* &lt;entityName&gt;
* &lt;property&gt;propertyValue&lt;/property&gt;
* ...
* &lt;/entityName&gt;
* </pre>
*
* @param specification A specification of the criteria that must be satisfied for entity to be selected.
* @return all entities in XML.
*/
@Nonnull
Element findAllAsXml(Specification<T> specification);

/**
* Load all entities that satisfy the criteria provided and convert each to XML of the form:
* <pre>
* &lt;entityName&gt;
* &lt;property&gt;propertyValue&lt;/property&gt;
* ...
* &lt;/entityName&gt;
* </pre>
*
* @param specification A specification of the criteria that must be satisfied for entity to be selected.
* @return all entities in XML.
*/
@Nonnull
Element findAllAsXml(Specification<T> specification, Sort sort);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import org.fao.geonet.domain.Localized;
import org.jdom.Element;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import javax.annotation.Nonnull;
import javax.persistence.EntityManager;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.*;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -38,16 +38,39 @@ public LocalizedEntityRepositoryImpl(Class<T> entityType) {
this._entityType = entityType;
}

/**
* Get the entity manager.
*/
protected abstract EntityManager getEntityManager();

@Nonnull
@Override
public Element findAllAsXml() {
final CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
final CriteriaQuery<T> query = cb.createQuery(_entityType);
return findAllAsXml(null, null);
}

query.from(_entityType);
@Nonnull
@Override
public Element findAllAsXml(final Specification<T> specification) {
return findAllAsXml(specification, null);
}

@Nonnull
@Override
public Element findAllAsXml(final Specification<T> specification, final Sort sort) {
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<T> query = cb.createQuery(_entityType);
Root<T> root = query.from(_entityType);

if (specification != null) {
final Predicate predicate = specification.toPredicate(root, query, cb);
query.where(predicate);
}

if (sort != null) {
List<Order> orders = SortUtils.sortToJpaOrders(cb, sort, root);
query.orderBy(orders);
}

return toXml(getEntityManager().createQuery(query).getResultList());
}
Expand All @@ -56,36 +79,7 @@ private Element toXml(List<T> resultList) {
Element root = new Element(_entityType.getSimpleName().toLowerCase());

for (T t : resultList) {
Element record = new Element(RECORD_EL_NAME);
root.addContent(record);
BeanWrapperImpl wrapper = new BeanWrapperImpl(t);

for (PropertyDescriptor desc : wrapper.getPropertyDescriptors()) {
try {
if (desc.getReadMethod().getDeclaringClass() == _entityType && desc.getReadMethod().getAnnotation(Transient.class)
== null) {
final String descName = desc.getName();
if (descName.equalsIgnoreCase("labelTranslations")) {
Element labelEl = new Element(LABEL_EL_NAME);

Map<String, String> labels = (Map<String, String>) desc.getReadMethod().invoke(t);
for (Map.Entry<String, String> entry : labels.entrySet()) {
labelEl.addContent(new Element(entry.getKey().toLowerCase()).setText(entry.getValue()));
}

record.addContent(labelEl);
} else {
final String value;
value = desc.getReadMethod().invoke(t).toString();
record.addContent(
new Element(descName.toLowerCase()).setText(value)
);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
root.addContent(t.asXml());
}
return root;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.fao.geonet.repository.specification;

import org.fao.geonet.domain.*;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.*;

public final class GroupSpecs {

private GroupSpecs() {
// don't permit instantiation
}

public static Specification<Group> isNotReserved() {
return new Specification<Group>() {
@Override
public Predicate toPredicate(Root<Group> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
int maxId = Integer.MIN_VALUE;
for (ReservedGroup reservedGroup : ReservedGroup.values()) {
if (maxId < reservedGroup.getId()) {
maxId = reservedGroup.getId();
}
}

return cb.greaterThan(root.get(Group_.id), maxId);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public void testFindByEmail() throws Exception {
assertSameContents(savedGroup, _repo.findByEmail(savedGroup.getEmail()));
assertNull(_repo.findByEmail("some wrong email"));
}


@Test
public void testFindReservedGroup() throws Exception {
Group savedGroup = _repo.save(ReservedGroup.all.getGroupEntityTemplate());

Expand Down Expand Up @@ -148,7 +149,7 @@ private void setId(ReservedGroup group, int normalId) throws Exception {
private Group newGroup() {
return newGroup(_nextId);
}
static Group newGroup(AtomicInteger nextId) {
public static Group newGroup(AtomicInteger nextId) {
int id = nextId.incrementAndGet();
return new Group()
.setDescription("Desc "+id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package org.fao.geonet.repository;

import org.fao.geonet.domain.IsoLanguage;
import org.fao.geonet.domain.IsoLanguage_;
import org.jdom.Element;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -41,7 +48,6 @@ public void testFindAllAsXml() throws Exception {
assertEquals(IsoLanguage.class.getSimpleName().toLowerCase(), xml.getName());
assertEquals(2, xml.getChildren().size());


for (Element element : (List<Element>) xml.getChildren()) {
assertEquals(LocalizedEntityRepositoryImpl.RECORD_EL_NAME, element.getName());
assertNotNull(element.getChild(LocalizedEntityRepositoryImpl.LABEL_EL_NAME));
Expand All @@ -58,4 +64,69 @@ public void testFindAllAsXml() throws Exception {
assertEquals(entity.getLabel("fra"), element.getChild(LocalizedEntityRepositoryImpl.LABEL_EL_NAME).getChildText("fra"));
}
}

@Test
public void testFindAllAsXmlSpec() throws Exception {
IsoLanguage language = IsoLanguageRepositoryTest.createIsoLanguage(_inc);
language.getLabelTranslations().put("eng", "eng1");
language.getLabelTranslations().put("fra", "fra1");
language = _repository.save(language);

final int firstId = language.getId();

IsoLanguage language2 = IsoLanguageRepositoryTest.createIsoLanguage(_inc);
language2.getLabelTranslations().put("eng", "eng2");
language2.getLabelTranslations().put("fra", "fra2");
language2 = _repository.save(language2);

Element xml = _repository.findAllAsXml(new Specification<IsoLanguage>() {
@Override
public Predicate toPredicate(Root<IsoLanguage> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

return cb.gt(root.get(IsoLanguage_.id), firstId);
}
});

assertEquals(IsoLanguage.class.getSimpleName().toLowerCase(), xml.getName());
assertEquals(1, xml.getChildren().size());

Element element = (Element) xml.getChildren().get(0);
assertEquals(LocalizedEntityRepositoryImpl.RECORD_EL_NAME, element.getName());
assertNotNull(element.getChild(LocalizedEntityRepositoryImpl.LABEL_EL_NAME));

IsoLanguage entity = language;
if (element.getChildText("id").equalsIgnoreCase("" + language2.getId())) {
entity = language2;
}

assertEquals(entity.getId(), Integer.valueOf(element.getChildText("id")).intValue());
assertEquals(entity.getCode(), element.getChildText("code"));
assertEquals(entity.getShortCode(), element.getChildText("shortcode"));
assertEquals(entity.getLabel("eng"), element.getChild(LocalizedEntityRepositoryImpl.LABEL_EL_NAME).getChildText("eng"));
assertEquals(entity.getLabel("fra"), element.getChild(LocalizedEntityRepositoryImpl.LABEL_EL_NAME).getChildText("fra"));
}


@Test
public void testFindAllAsXmlSortBy() throws Exception {
IsoLanguage language = IsoLanguageRepositoryTest.createIsoLanguage(_inc);
language.getLabelTranslations().put("eng", "eng1");
language.getLabelTranslations().put("fra", "fra1");
language = _repository.save(language);

IsoLanguage language2 = IsoLanguageRepositoryTest.createIsoLanguage(_inc);
language2.getLabelTranslations().put("eng", "eng2");
language2.getLabelTranslations().put("fra", "fra2");
language2 = _repository.save(language2);

Element xml = _repository.findAllAsXml(null, new Sort(Sort.Direction.DESC, IsoLanguage_.id.getName()));

assertEquals(String.valueOf(language2.getId()), ((Element)xml.getChildren().get(0)).getChildText("id"));
assertEquals(String.valueOf(language.getId()), ((Element)xml.getChildren().get(1)).getChildText("id"));

xml = _repository.findAllAsXml(null, new Sort(Sort.Direction.ASC, IsoLanguage_.id.getName()));

assertEquals(String.valueOf(language.getId()), ((Element)xml.getChildren().get(0)).getChildText("id"));
assertEquals(String.valueOf(language2.getId()), ((Element)xml.getChildren().get(1)).getChildText("id"));
}
}
Loading

0 comments on commit fa45657

Please sign in to comment.