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

Former-commit-id: 95b2d335fa
pull/2/head
jensp 2017-02-03 07:40:13 +00:00
parent fecde66302
commit b41b5e7c93
12 changed files with 86 additions and 70 deletions

View File

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

View File

@ -39,7 +39,7 @@ public class ContentSectionCreator
@Override
public CcmApplication createInstance(final String primaryUrl,
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");
}
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.CcmApplication;
import java.util.Optional;
import static com.arsdigita.ui.admin.AdminUiConstants.*;
/**
@ -213,15 +215,15 @@ public class ApplicationsAdministrationTab extends LayoutPanel {
final ApplicationRepository appRepo = cdiUtil.findBean(
ApplicationRepository.class);
final CcmApplication application = appRepo
final Optional<CcmApplication> application = appRepo
.retrieveApplicationForPath(selectedKey);
final LegacyApplicationInstancePane pane;
if (application != null) {
if (application.isPresent()) {
pane = instancePanes.get(application.getClass().
getName());
if (pane != null) {
pane.setApplication(application);
pane.setApplication(application.get());
}
} else {
pane = null;

View File

@ -47,7 +47,7 @@ public class LoginApplicationCreator
+ "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");
}
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)
public Category findByPath(final String path) {
public Optional<Category> findByPath(final String path) {
if (path == null || path.isEmpty()) {
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");
}
final Domain domain = domainRepo.findByDomainKey(tokens[0]);
if (domain == null) {
final Optional<Domain> domain = domainRepo.findByDomainKey(tokens[0]);
if (domain.isPresent()) {
return findByPath(domain.get(), tokens[1]);
} else {
throw new InvalidCategoryPathException(String.format(
"No domain identified by the key '%s' found.",
tokens[0]));
}
return findByPath(domain, tokens[1]);
}
@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) {
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 "
+ "domain \"{}\".",
+ "domain \"{}\".",
normalizedPath,
domain.getDomainKey());
final String[] tokens = normalizedPath.split("/");
Category current = domain.getRoot();
for (final String token : tokens) {
if (current.getSubCategories() == null) {
return null;
return Optional.empty();
}
final Optional<Category> result = current.getSubCategories()
.stream()
.filter((c) -> {
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);
})
.filter(c -> filterCategoryByName(c, token))
.findFirst();
if (result.isPresent()) {
current = result.get();
} 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

View File

@ -24,6 +24,7 @@ import org.libreccm.security.RequiresPrivilege;
import java.net.URI;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
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
* {@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(
"Domain.findByKey", Domain.class);
query.setParameter("key", domainKey);
@ -86,9 +87,9 @@ public class DomainRepository extends AbstractEntityRepository<Long, Domain> {
query.setHint("javax.persistence.fetchgraph", graph);
try {
return query.getSingleResult();
return Optional.of(query.getSingleResult());
} 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 java.util.List;
import java.util.Optional;
import javax.enterprise.context.RequestScoped;
import javax.persistence.NoResultException;
@ -57,15 +58,15 @@ public class ApplicationRepository
* is no application mounted at that {@code path}.
*/
@Transactional(Transactional.TxType.REQUIRED)
public CcmApplication retrieveApplicationForPath(final String path) {
public Optional<CcmApplication> retrieveApplicationForPath(final String path) {
final TypedQuery<CcmApplication> query = getEntityManager()
.createNamedQuery("CcmApplication.retrieveApplicationForPath",
CcmApplication.class);
query.setParameter("path", path);
try {
return query.getSingleResult();
return Optional.of(query.getSingleResult());
} catch (NoResultException ex) {
return null;
return Optional.empty();
}
}

View File

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

View File

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

View File

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