CCM NG: Some repositories still returned null instead of Optional

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4550 8810af33-2d31-482b-a856-94f89814c4df
ccm-docs
jensp 2017-02-03 07:40:13 +00:00
parent a4a6cfddc8
commit 7b1d0fd625
12 changed files with 86 additions and 70 deletions

View File

@ -46,7 +46,7 @@ public class ContentCenterAppCreator implements ApplicationCreator<CcmApplicatio
+ "/content-center/"); + "/content-center/");
} }
return appRepository.retrieveApplicationForPath(primaryUrl); return appRepository.retrieveApplicationForPath(primaryUrl).get();
} }
} }

View File

@ -39,7 +39,7 @@ public class ContentSectionCreator
@Override @Override
public CcmApplication createInstance(final String primaryUrl, public CcmApplication createInstance(final String primaryUrl,
final ApplicationType type) { final ApplicationType type) {
return appRepo.retrieveApplicationForPath(primaryUrl); return appRepo.retrieveApplicationForPath(primaryUrl).get();
} }

View File

@ -48,7 +48,7 @@ public class AdminApplicationCreator
+ "which is mounted at /admin"); + "which is mounted at /admin");
} }
return appRepository.retrieveApplicationForPath(primaryUrl); return appRepository.retrieveApplicationForPath(primaryUrl).get();
} }

View File

@ -40,6 +40,8 @@ import org.libreccm.web.ApplicationRepository;
import org.libreccm.web.ApplicationType; import org.libreccm.web.ApplicationType;
import org.libreccm.web.CcmApplication; import org.libreccm.web.CcmApplication;
import java.util.Optional;
import static com.arsdigita.ui.admin.AdminUiConstants.*; import static com.arsdigita.ui.admin.AdminUiConstants.*;
/** /**
@ -213,15 +215,15 @@ public class ApplicationsAdministrationTab extends LayoutPanel {
final ApplicationRepository appRepo = cdiUtil.findBean( final ApplicationRepository appRepo = cdiUtil.findBean(
ApplicationRepository.class); ApplicationRepository.class);
final CcmApplication application = appRepo final Optional<CcmApplication> application = appRepo
.retrieveApplicationForPath(selectedKey); .retrieveApplicationForPath(selectedKey);
final LegacyApplicationInstancePane pane; final LegacyApplicationInstancePane pane;
if (application != null) { if (application.isPresent()) {
pane = instancePanes.get(application.getClass(). pane = instancePanes.get(application.getClass().
getName()); getName());
if (pane != null) { if (pane != null) {
pane.setApplication(application); pane.setApplication(application.get());
} }
} else { } else {
pane = null; pane = null;

View File

@ -47,7 +47,7 @@ public class LoginApplicationCreator
+ "which is mounted at /login"); + "which is mounted at /login");
} }
return appRepository.retrieveApplicationForPath(primaryUrl); return appRepository.retrieveApplicationForPath(primaryUrl).get();
} }
} }

View File

@ -45,7 +45,7 @@ public class AdminJsfApplicationCreator implements ApplicationCreator<CcmApplica
+ "which is mounted at /admin-jsf"); + "which is mounted at /admin-jsf");
} }
return appRepo.retrieveApplicationForPath(primaryUrl); return appRepo.retrieveApplicationForPath(primaryUrl).get();
} }

View File

@ -84,7 +84,7 @@ public class CategoryRepository extends AbstractEntityRepository<Long, Category>
} }
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public Category findByPath(final String path) { public Optional<Category> findByPath(final String path) {
if (path == null || path.isEmpty()) { if (path == null || path.isEmpty()) {
throw new IllegalArgumentException("Path can't be null or empty."); throw new IllegalArgumentException("Path can't be null or empty.");
} }
@ -102,18 +102,18 @@ public class CategoryRepository extends AbstractEntityRepository<Long, Category>
+ "Valid path format: domainKey:path"); + "Valid path format: domainKey:path");
} }
final Domain domain = domainRepo.findByDomainKey(tokens[0]); final Optional<Domain> domain = domainRepo.findByDomainKey(tokens[0]);
if (domain == null) { if (domain.isPresent()) {
return findByPath(domain.get(), tokens[1]);
} else {
throw new InvalidCategoryPathException(String.format( throw new InvalidCategoryPathException(String.format(
"No domain identified by the key '%s' found.", "No domain identified by the key '%s' found.",
tokens[0])); tokens[0]));
} }
return findByPath(domain, tokens[1]);
} }
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public Category findByPath(final Domain domain, final String path) { public Optional<Category> findByPath(final Domain domain, final String path) {
if (domain == null) { if (domain == null) {
throw new IllegalArgumentException("Domain can't be null."); throw new IllegalArgumentException("Domain can't be null.");
} }
@ -133,37 +133,40 @@ public class CategoryRepository extends AbstractEntityRepository<Long, Category>
} }
LOGGER.debug("Trying to find category with path \"{}\" in " LOGGER.debug("Trying to find category with path \"{}\" in "
+ "domain \"{}\".", + "domain \"{}\".",
normalizedPath, normalizedPath,
domain.getDomainKey()); domain.getDomainKey());
final String[] tokens = normalizedPath.split("/"); final String[] tokens = normalizedPath.split("/");
Category current = domain.getRoot(); Category current = domain.getRoot();
for (final String token : tokens) { for (final String token : tokens) {
if (current.getSubCategories() == null) { if (current.getSubCategories() == null) {
return null; return Optional.empty();
} }
final Optional<Category> result = current.getSubCategories() final Optional<Category> result = current.getSubCategories()
.stream() .stream()
.filter((c) -> { .filter(c -> filterCategoryByName(c, token))
LOGGER.debug("#findByPath(Domain, String): c = {}",
c.toString());
LOGGER.debug(
"#findByPath(Domain, String): c.getName = \"{}\"",
c.getName());
LOGGER.debug("#findByPath(Domain, String): token = \"{}\"",
token);
return c.getName().equals(token);
})
.findFirst(); .findFirst();
if (result.isPresent()) { if (result.isPresent()) {
current = result.get(); current = result.get();
} else { } else {
return null; return Optional.empty();
} }
} }
return current; return Optional.of(current);
}
private boolean filterCategoryByName(final Category category,
final String name) {
LOGGER.debug("#findByPath(Domain, String): c = {}",
category.toString());
LOGGER.debug(
"#findByPath(Domain, String): c.getName = \"{}\"",
category.getName());
LOGGER.debug("#findByPath(Domain, String): token = \"{}\"",
name);
return category.getName().equals(name);
} }
@AuthorizationRequired @AuthorizationRequired

View File

@ -24,6 +24,7 @@ import org.libreccm.security.RequiresPrivilege;
import java.net.URI; import java.net.URI;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
@ -76,7 +77,7 @@ public class DomainRepository extends AbstractEntityRepository<Long, Domain> {
* @return The {@code Domain} identified by {@code domainKey} or * @return The {@code Domain} identified by {@code domainKey} or
* {@code null} if there is no such {@code Domain}. * {@code null} if there is no such {@code Domain}.
*/ */
public Domain findByDomainKey(final String domainKey) { public Optional<Domain> findByDomainKey(final String domainKey) {
final TypedQuery<Domain> query = entityManager.createNamedQuery( final TypedQuery<Domain> query = entityManager.createNamedQuery(
"Domain.findByKey", Domain.class); "Domain.findByKey", Domain.class);
query.setParameter("key", domainKey); query.setParameter("key", domainKey);
@ -86,9 +87,9 @@ public class DomainRepository extends AbstractEntityRepository<Long, Domain> {
query.setHint("javax.persistence.fetchgraph", graph); query.setHint("javax.persistence.fetchgraph", graph);
try { try {
return query.getSingleResult(); return Optional.of(query.getSingleResult());
} catch (NoResultException ex) { } catch (NoResultException ex) {
return null; return Optional.empty();
} }
} }

View File

@ -24,6 +24,7 @@ import org.libreccm.security.AuthorizationRequired;
import org.libreccm.security.RequiresPrivilege; import org.libreccm.security.RequiresPrivilege;
import java.util.List; import java.util.List;
import java.util.Optional;
import javax.enterprise.context.RequestScoped; import javax.enterprise.context.RequestScoped;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
@ -57,15 +58,15 @@ public class ApplicationRepository
* is no application mounted at that {@code path}. * is no application mounted at that {@code path}.
*/ */
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public CcmApplication retrieveApplicationForPath(final String path) { public Optional<CcmApplication> retrieveApplicationForPath(final String path) {
final TypedQuery<CcmApplication> query = getEntityManager() final TypedQuery<CcmApplication> query = getEntityManager()
.createNamedQuery("CcmApplication.retrieveApplicationForPath", .createNamedQuery("CcmApplication.retrieveApplicationForPath",
CcmApplication.class); CcmApplication.class);
query.setParameter("path", path); query.setParameter("path", path);
try { try {
return query.getSingleResult(); return Optional.of(query.getSingleResult());
} catch (NoResultException ex) { } catch (NoResultException ex) {
return null; return Optional.empty();
} }
} }

View File

@ -659,7 +659,7 @@ public class CategoryManagerTest {
public void createMultipleCategories() { public void createMultipleCategories() {
shiro.getSystemUser().execute(() -> { shiro.getSystemUser().execute(() -> {
final Domain domain = domainRepo.findByDomainKey("test"); final Domain domain = domainRepo.findByDomainKey("test").get();
final Category root = domain.getRoot(); final Category root = domain.getRoot();
final Category com = new Category(); final Category com = new Category();

View File

@ -54,6 +54,8 @@ import static org.libreccm.testutils.DependenciesHelpers.*;
import org.jboss.arquillian.persistence.CleanupUsingScript; import org.jboss.arquillian.persistence.CleanupUsingScript;
import java.util.Optional;
/** /**
* *
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a> * @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
@ -160,23 +162,26 @@ public class CategoryRepositoryTest {
"datasets/org/libreccm/categorization/CategoryRepositoryTest/data.yml") "datasets/org/libreccm/categorization/CategoryRepositoryTest/data.yml")
@InSequence(1100) @InSequence(1100)
public void findByPathString() { public void findByPathString() {
final Category category1 = categoryRepo.findByPath("test:/foo/bar/"); final Optional<Category> category1 = categoryRepo.findByPath(
final Category category2 = categoryRepo.findByPath("test:/foo/bar"); "test:/foo/bar/");
final Category category3 = categoryRepo.findByPath("test:/foo/"); final Optional<Category> category2 = categoryRepo.findByPath(
"test:/foo/bar");
final Optional<Category> category3 = categoryRepo.findByPath(
"test:/foo/");
final Category notFound = categoryRepo final Optional<Category> notFound = categoryRepo
.findByPath("test:/does/not/exist"); .findByPath("test:/does/not/exist");
assertThat(category1, is(not(nullValue()))); assertThat(category1.isPresent(), is(true));
assertThat(category1.getName(), is(equalTo("bar"))); assertThat(category1.get().getName(), is(equalTo("bar")));
assertThat(category2, is(not(nullValue()))); assertThat(category2.isPresent(), is(true));
assertThat(category2.getName(), is(equalTo("bar"))); assertThat(category2.get().getName(), is(equalTo("bar")));
assertThat(category3, is(not(nullValue()))); assertThat(category3.isPresent(), is(true));
assertThat(category3.getName(), is(equalTo("foo"))); assertThat(category3.get().getName(), is(equalTo("foo")));
assertThat(notFound, is(nullValue())); assertThat(notFound.isPresent(), is(false));
} }
@Test @Test
@ -184,10 +189,10 @@ public class CategoryRepositoryTest {
"datasets/org/libreccm/categorization/CategoryRepositoryTest/data.yml") "datasets/org/libreccm/categorization/CategoryRepositoryTest/data.yml")
@InSequence(1150) @InSequence(1150)
public void findByPathStringNotExisting() { public void findByPathStringNotExisting() {
final Category doesNotExist = categoryRepo.findByPath( final Optional<Category> doesNotExist = categoryRepo.findByPath(
"test:/does/not/exist"); "test:/does/not/exist");
assertThat(doesNotExist, is(nullValue())); assertThat(doesNotExist.isPresent(), is(false));
} }
@Test(expected = InvalidCategoryPathException.class) @Test(expected = InvalidCategoryPathException.class)
@ -204,29 +209,33 @@ public class CategoryRepositoryTest {
"datasets/org/libreccm/categorization/CategoryRepositoryTest/data.yml") "datasets/org/libreccm/categorization/CategoryRepositoryTest/data.yml")
@InSequence(2100) @InSequence(2100)
public void findByPathDomainString() { public void findByPathDomainString() {
final Domain domain = domainRepo.findByDomainKey("test"); final Domain domain = domainRepo.findByDomainKey("test").get();
final Category category1 = categoryRepo.findByPath(domain, "/foo/bar/"); final Optional<Category> category1 = categoryRepo.findByPath(
final Category category2 = categoryRepo.findByPath(domain, "foo/bar/"); domain, "/foo/bar/");
final Category category3 = categoryRepo.findByPath(domain, "/foo/bar"); final Optional<Category> category2 = categoryRepo.findByPath(
final Category category4 = categoryRepo.findByPath(domain, "foo/bar"); domain, "foo/bar/");
final Optional<Category> category3 = categoryRepo.findByPath(
domain, "/foo/bar");
final Optional<Category> category4 = categoryRepo.findByPath(
domain, "foo/bar");
final Category notFound = categoryRepo.findByPath(domain, final Optional<Category> notFound = categoryRepo.findByPath(
"/does/not/exist"); domain, "/does/not/exist");
assertThat(category1, is(not(nullValue()))); assertThat(category1.isPresent(), is(true));
assertThat(category1.getName(), is(equalTo("bar"))); assertThat(category1.get().getName(), is(equalTo("bar")));
assertThat(category2, is(not(nullValue()))); assertThat(category2.isPresent(), is(true));
assertThat(category2.getName(), is(equalTo("bar"))); assertThat(category2.get().getName(), is(equalTo("bar")));
assertThat(category3, is(not(nullValue()))); assertThat(category3.isPresent(), is(true));
assertThat(category3.getName(), is(equalTo("bar"))); assertThat(category3.get().getName(), is(equalTo("bar")));
assertThat(category4, is(not(nullValue()))); assertThat(category4.isPresent(), is(true));
assertThat(category4.getName(), is(equalTo("bar"))); assertThat(category4.get().getName(), is(equalTo("bar")));
assertThat(notFound, is(nullValue())); assertThat(notFound.isPresent(), is(false));
} }
@Test @Test
@ -234,12 +243,12 @@ public class CategoryRepositoryTest {
"datasets/org/libreccm/categorization/CategoryRepositoryTest/data.yml") "datasets/org/libreccm/categorization/CategoryRepositoryTest/data.yml")
@InSequence(1150) @InSequence(1150)
public void findByPathDomainStringNotExisting() { public void findByPathDomainStringNotExisting() {
final Domain domain = domainRepo.findByDomainKey("test"); final Domain domain = domainRepo.findByDomainKey("test").get();
final Category doesNotExist = categoryRepo.findByPath(domain, final Optional<Category> doesNotExist = categoryRepo.findByPath(domain,
"/does/not/exist"); "/does/not/exist");
assertThat(doesNotExist, is(nullValue())); assertThat(doesNotExist.isPresent(), is(false));
} }
@Test @Test

View File

@ -47,7 +47,7 @@ public class ShortcutsApplicationCreator
+ "/shortcuts"); + "/shortcuts");
} }
return appRepository.retrieveApplicationForPath(primaryUrl); return appRepository.retrieveApplicationForPath(primaryUrl).get();
} }