Example usage for org.apache.commons.lang ArrayUtils isEmpty

List of usage examples for org.apache.commons.lang ArrayUtils isEmpty

Introduction

In this page you can find the example usage for org.apache.commons.lang ArrayUtils isEmpty.

Prototype

public static boolean isEmpty(boolean[] array) 

Source Link

Document

Checks if an array of primitive booleans is empty or null.

Usage

From source file:nl.strohalm.cyclos.utils.lucene.Filters.java

/**
 * Normalizes the filters, returning null on empty array and removing null values
 *//*from w w w .  j  a v a 2  s. c o m*/
private static Filter[] normalize(final Filter[] filters) {
    if (ArrayUtils.isEmpty(filters)) {
        return null;
    }
    final List<Filter> list = new ArrayList<Filter>(filters.length);
    for (final Filter filter : filters) {
        if (filter != null) {
            list.add(filter);
        }
    }
    return list.isEmpty() ? null : list.toArray(new Filter[list.size()]);
}

From source file:nl.strohalm.cyclos.utils.lucene.LuceneQueryHandler.java

private <E extends Entity & Indexable> List<E> iterator(final Class<E> entityType, final Query query,
        final Filter filter, final Sort sort, final PageParameters pageParameters,
        final Relationship... fetch) {
    IndexSearcher searcher = null;//from   ww  w.  java  2s.co m
    // Prepare the parameters
    IndexReader reader;
    try {
        reader = indexHandler.openReader(entityType);
    } catch (final DaoException e) {
        // Probably index files don't exist
        return new IteratorListImpl<E>(Collections.<E>emptyList().iterator());
    }
    final int firstResult = pageParameters == null ? 0 : pageParameters.getFirstResult();
    int maxResults = (pageParameters == null || pageParameters.getMaxResults() == 0) ? Integer.MAX_VALUE
            : pageParameters.getMaxResults() + firstResult;

    try {
        // Run the search
        searcher = new IndexSearcher(reader);
        TopDocs topDocs;
        if (sort == null || ArrayUtils.isEmpty(sort.getSort())) {
            topDocs = searcher.search(query, filter, maxResults);
        } else {
            topDocs = searcher.search(query, filter, maxResults, sort);
        }
        // Open the iterator
        Iterator<E> iterator = new DocsIterator<E>(this, entityType, reader, topDocs, firstResult, fetch);
        DataIteratorHelper.registerOpen(iterator, false);

        // Wrap the iterator
        return new IteratorListImpl<E>(iterator);

    } catch (final Exception e) {
        throw new DaoException(e);
    } finally {
        try {
            searcher.close();
        } catch (final Exception e) {
            // Silently ignore
        }
    }
}

From source file:nl.strohalm.cyclos.utils.lucene.LuceneQueryHandler.java

private <E extends Entity & Indexable> List<E> listOrPage(final Class<E> entityType, final Query query,
        final Filter filter, final Sort sort, final ResultType resultType, final PageParameters pageParameters,
        final Relationship... fetch) {
    IndexSearcher searcher = null;// w ww  .j  av  a  2 s.c om
    // Prepare the parameters
    IndexReader reader;
    try {
        reader = indexHandler.openReader(entityType);
    } catch (final DaoException e) {
        // Probably index files don't exist
        return Collections.emptyList();
    }
    final int firstResult = pageParameters == null ? 0 : pageParameters.getFirstResult();
    int maxResults = pageParameters == null ? Integer.MAX_VALUE : pageParameters.getMaxResults() + firstResult;
    try {
        searcher = new IndexSearcher(reader);
        if (maxResults == 0 && resultType == ResultType.PAGE) {
            // We just want the total hit count.
            TotalHitCountCollector collector = new TotalHitCountCollector();
            searcher.search(query, filter, collector);
            int totalHits = collector.getTotalHits();
            return new PageImpl<E>(pageParameters, totalHits, Collections.<E>emptyList());
        } else {
            if (maxResults == 0) {
                maxResults = Integer.MAX_VALUE;
            }
            // Run the search
            TopDocs topDocs;
            if (sort == null || ArrayUtils.isEmpty(sort.getSort())) {
                topDocs = searcher.search(query, filter, maxResults);
            } else {
                topDocs = searcher.search(query, filter, maxResults, sort);
            }

            // Build the list
            ScoreDoc[] scoreDocs = topDocs.scoreDocs;
            List<E> list = new ArrayList<E>(Math.min(firstResult, scoreDocs.length));
            for (int i = firstResult; i < scoreDocs.length; i++) {
                ScoreDoc scoreDoc = scoreDocs[i];
                E entity = toEntity(reader, scoreDoc.doc, entityType, fetch);
                if (entity != null) {
                    list.add(entity);
                }
            }

            // When result type is page, get the additional data
            if (resultType == ResultType.PAGE) {
                list = new PageImpl<E>(pageParameters, topDocs.totalHits, list);
            }
            return list;
        }
    } catch (final EntityNotFoundException e) {
        throw new ValidationException("general.error.indexedRecordNotFound");
    } catch (ApplicationException e) {
        throw e;
    } catch (final Exception e) {
        throw new DaoException(e);
    } finally {
        // Close resources
        try {
            searcher.close();
        } catch (final Exception e) {
            // Silently ignore
        }
        try {
            reader.close();
        } catch (final Exception e) {
            // Silently ignore
        }
    }
}

From source file:org.ambraproject.action.BaseTest.java

protected void checkAnnotationProperties(AnnotationView result, Annotation expected) {
    if (expected.getType() == AnnotationType.MINOR_CORRECTION) {
        assertEquals(result.getTitle(), "Minor Correction: " + expected.getTitle(),
                "Annotation view had incorrect title");
    } else if (expected.getType() == AnnotationType.FORMAL_CORRECTION) {
        assertEquals(result.getTitle(), "Formal Correction: " + expected.getTitle(),
                "Annotation view had incorrect title");
    } else if (expected.getType() == AnnotationType.RETRACTION) {
        assertEquals(result.getTitle(), "Retraction: " + expected.getTitle(),
                "Annotation view had incorrect title");
    }/*from ww  w .j a  v a 2 s  . c  o  m*/
    assertEquals(result.getBody(), "<p>" + expected.getBody() + "</p>", "Annotation view had incorrect body");
    assertEquals(result.getCompetingInterestStatement(),
            expected.getCompetingInterestBody() == null ? "" : expected.getCompetingInterestBody(),
            "Annotation view had incorrect ci statement");
    assertEquals(result.getAnnotationUri(), expected.getAnnotationUri(),
            "Annotation view had incorrect annotation uri");
    assertEquals(result.getCreatorID(), expected.getCreator().getID(),
            "Annotation view had incorrect creator id");
    assertEquals(result.getCreatorDisplayName(), expected.getCreator().getDisplayName(),
            "Annotation view had incorrect creator name");

    if (Arrays.asList(AnnotationType.FORMAL_CORRECTION, AnnotationType.MINOR_CORRECTION,
            AnnotationType.RETRACTION).contains(expected.getType())) {
        assertTrue(result.isCorrection(), "Result should have been created as a correction");
    } else {
        assertFalse(result.isCorrection(), "Result should not have been created as a correction");
    }

    if (expected.getAnnotationCitation() == null) {
        assertNull(result.getCitation(), "returned non-null citation when null was expected");
    } else {
        assertNotNull(result.getCitation(), "returned null citation when non-null was expected");
        assertEquals(result.getCitation().getTitle(), expected.getAnnotationCitation().getTitle(),
                "Returned citation with incorrect title");
        assertEquals(result.getCitation().geteLocationId(), expected.getAnnotationCitation().getELocationId(),
                "Returned citation with incorrect eLocationId");
        assertEquals(result.getCitation().getJournal(), expected.getAnnotationCitation().getJournal(),
                "Returned citation with incorrect journal");
        assertEquals(result.getCitation().getYear(), expected.getAnnotationCitation().getYear(),
                "Returned citation with incorrect year");

        assertEquals(result.getCitation().getVolume(), expected.getAnnotationCitation().getVolume(),
                "Returned citation with incorrect volume");
        assertEquals(result.getCitation().getIssue(), expected.getAnnotationCitation().getIssue(),
                "Returned citation with incorrect issue");
        assertEquals(result.getCitation().getSummary(), expected.getAnnotationCitation().getSummary(),
                "Returned citation with incorrect summary");
        assertEquals(result.getCitation().getNote(), expected.getAnnotationCitation().getNote(),
                "Returned citation with incorrect note");

        if (expected.getAnnotationCitation().getCollaborativeAuthors() != null) {
            assertEqualsNoOrder(result.getCitation().getCollabAuthors(),
                    expected.getAnnotationCitation().getCollaborativeAuthors().toArray(),
                    "Returned citation with incorrect collab authors");
        } else {
            assertTrue(ArrayUtils.isEmpty(result.getCitation().getCollabAuthors()),
                    "Returned non-empty collab authors when empty was expected");
        }
        if (expected.getAnnotationCitation().getAuthors() != null) {
            assertNotNull(result.getCitation().getAuthors(),
                    "Returned null authors when authors were expected");
            assertEquals(result.getCitation().getAuthors().length,
                    expected.getAnnotationCitation().getAuthors().size(),
                    "Returned incorrect number of authors");
            for (int i = 0; i < result.getCitation().getAuthors().length; i++) {
                AuthorView actualAuthor = result.getCitation().getAuthors()[i];
                CorrectedAuthor expectedAuthor = expected.getAnnotationCitation().getAuthors().get(i);
                assertEquals(actualAuthor.getGivenNames(), expectedAuthor.getGivenNames(),
                        "Author " + (i + 1) + " had incorrect given names");
                assertEquals(actualAuthor.getSurnames(), expectedAuthor.getSurName(),
                        "Author " + (i + 1) + " had incorrect surnames");
                assertEquals(actualAuthor.getSuffix(), expectedAuthor.getSuffix(),
                        "Author " + (i + 1) + " had incorrect suffix");
            }
        }

    }
}

From source file:org.ambraproject.admin.action.ManageArticleListAction.java

/**
 * remove article list/*w  ww .  j ava 2s  . co m*/
 */
private void removeArticleList() {
    try {
        if (!ArrayUtils.isEmpty(listToDelete)) {
            String[] deletedList = adminService.deleteArticleList(getCurrentJournal(), listToDelete);
            addActionMessage(
                    "Successfully removed the following article list: " + Arrays.toString(deletedList));
        }
    } catch (Exception e) {
        log.error("Error deleting article list: " + Arrays.toString(listToDelete), e);
        addActionError("Article List remove failed due to the following error: " + e.getMessage());
    }
    repopulate();
}

From source file:org.ambraproject.admin.action.ManageVirtualJournalsAction.java

private void removeVolumes() {
    try {//from  w  w  w  .j  ava 2s  . co m
        if (!ArrayUtils.isEmpty(volsToDelete)) {
            String[] deletedVolumes = adminService.deleteVolumes(getCurrentJournal(), volsToDelete);
            addActionMessage("Successfully removed the following volumes: " + Arrays.toString(deletedVolumes));
        }
    } catch (Exception e) {
        log.error("Error deleting volumes: " + Arrays.toString(volsToDelete), e);
        addActionError("Volume remove failed due to the following error: " + e.getMessage());
    }
    repopulate();
}

From source file:org.ambraproject.admin.flags.action.ProcessFlagsAction.java

@Override
public String execute() throws Exception {
    try {/*from ww  w  .ja  v a  2  s  . c o m*/
        if (!ArrayUtils.isEmpty(commentsToUnflag)) {
            flagService.deleteFlags(commentsToUnflag);
            addActionMessage("Successfully deleted " + commentsToUnflag.length + "  flags");
        }
        if (!ArrayUtils.isEmpty(commentsToDelete)) {
            flagService.deleteFlagAndComment(commentsToDelete);
            addActionMessage("Successfully deleted " + commentsToDelete.length + "  comments");
        }
        if (!ArrayUtils.isEmpty(convertToFormalCorrection)) {
            flagService.convertToType(AnnotationType.FORMAL_CORRECTION, convertToFormalCorrection);
            addActionMessage("Successfully converted " + convertToFormalCorrection.length
                    + " annotations to formal correction");
        }
        if (!ArrayUtils.isEmpty(convertToMinorCorrection)) {
            flagService.convertToType(AnnotationType.MINOR_CORRECTION, convertToMinorCorrection);
            addActionMessage("Successfully converted " + convertToMinorCorrection.length
                    + " annotations to minor correction");
        }
        if (!ArrayUtils.isEmpty(convertToRetraction)) {
            flagService.convertToType(AnnotationType.RETRACTION, convertToRetraction);
            addActionMessage(
                    "Successfully converted " + convertToRetraction.length + " annotations to retraction");
        }
    } catch (Exception e) {
        log.error("error processing flags", e);
        addActionError("Error processing flags: " + e.getMessage());
        return ERROR;
    }
    return SUCCESS;
}

From source file:org.ambraproject.annotation.service.AnnotationServiceTest.java

@Test(dataProvider = "articleAnnotations")
public void testListAnnotationsNoReplies(final Article article, final Set<AnnotationType> annotationTypes,
        final AnnotationService.AnnotationOrder order, final AnnotationView[] expectedViews,
        final Map<Long, List<Annotation>> notUsed) {
    if (order != AnnotationService.AnnotationOrder.MOST_RECENT_REPLY) {
        AnnotationView[] resultViews = annotationService.listAnnotationsNoReplies(article.getID(),
                annotationTypes, order);
        //Not just calling assertEquals here, so we can give a more informative message (the .toSting() on the arrays is huge)
        assertNotNull(resultViews, "returned null array of results");
        assertEquals(resultViews.length, expectedViews.length, "returned incorrect number of results");
        for (int i = 0; i < resultViews.length; i++) {
            assertEquals(resultViews[i].getCitation(), expectedViews[i].getCitation(),
                    "Result " + (i + 1) + " had incorrect citation with order " + order);
            assertEquals(resultViews[i], expectedViews[i],
                    "Result " + (i + 1) + " was incorrect with order " + order);
            assertTrue(ArrayUtils.isEmpty(resultViews[i].getReplies()),
                    "returned annotation with replies loaded up");
        }//from  w w  w  .  j  ava2  s  . co  m
    }
}

From source file:org.ambraproject.annotation.service.AnnotationServiceTest.java

private void recursivelyCheckReplies(AnnotationView annotationView, Map<Long, List<Annotation>> fullReplyMap) {
    List<Annotation> expectedReplies = fullReplyMap.get(annotationView.getID());
    if (expectedReplies != null) {
        assertEquals(annotationView.getReplies().length, expectedReplies.size(),
                "Returned incorrect number of replies");
        for (AnnotationView actual : annotationView.getReplies()) {
            boolean foundMatch = false;

            for (Annotation expected : expectedReplies) {
                if (actual.getID().equals(expected.getID())) {
                    foundMatch = true;//from   w  ww  .  ja v a2 s  .c om
                    assertEquals(actual.getTitle(), expected.getTitle(), "Annotation view had incorrect title");
                    assertEquals(actual.getBody(), "<p>" + expected.getBody() + "</p>",
                            "Annotation view had incorrect body");
                    assertEquals(actual.getCompetingInterestStatement(),
                            expected.getCompetingInterestBody() == null ? ""
                                    : expected.getCompetingInterestBody(),
                            "Annotation view had incorrect ci statement");
                    assertEquals(actual.getAnnotationUri(), expected.getAnnotationUri(),
                            "Annotation view had incorrect annotation uri");
                }
            }
            assertTrue(foundMatch, "Returned unexpected reply: " + actual);
            //recursively check the replies
            recursivelyCheckReplies(actual, fullReplyMap);
        }
    } else {
        assertTrue(ArrayUtils.isEmpty(annotationView.getReplies()), "Returned replies when none were expected: "
                + Arrays.deepToString(annotationView.getReplies()));
    }
}

From source file:org.ambraproject.annotation.service.AnnotationServiceTest.java

@Test(dataProvider = "storedAnnotation")
public void testGetBasicAnnotationViewById(Annotation annotation, Map<Long, List<Annotation>> fullReplyMap) {
    AnnotationView result = annotationService.getBasicAnnotationView(annotation.getID());
    assertNotNull(result, "Returned null annotation view");
    String expectedDoi = dummyDataStore.get(Article.class, annotation.getArticleID()).getDoi();
    String expectedTitle = dummyDataStore.get(Article.class, annotation.getArticleID()).getTitle();

    assertEquals(result.getArticleDoi(), expectedDoi, "AnnotationView had incorrect article doi");
    assertEquals(result.getArticleTitle(), expectedTitle, "AnnotationView had incorrect article title");

    checkAnnotationProperties(result, annotation);
    assertTrue(ArrayUtils.isEmpty(result.getReplies()), "Returned annotation with replies");
}