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

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

Introduction

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

Prototype

public static boolean[] subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive) 

Source Link

Document

Produces a new boolean array containing the elements between the start and end indices.

Usage

From source file:org.dragonet.utilities.io.ArraySplitter.java

public static byte[][] splitArray(byte[] array, int singleSlice) {
    if (array.length <= singleSlice) {
        byte[][] singleRet = new byte[1][];
        singleRet[0] = array;/*w w w.  j  av a  2  s.  c o  m*/
        return singleRet;
    }
    byte[][] ret = new byte[(array.length / singleSlice + (array.length % singleSlice == 0 ? 0 : 1))][];
    int pos = 0;
    int slice = 0;
    while (slice < ret.length) {
        if (pos + singleSlice < array.length) {
            ret[slice] = ArrayUtils.subarray(array, pos, singleSlice);
            pos += singleSlice;
            slice++;
        } else {
            ret[slice] = ArrayUtils.subarray(array, pos, array.length);
            pos += array.length - pos;
            slice++;
        }
    }
    return ret;
}

From source file:org.dspace.app.cris.discovery.CrisSearchService.java

private void indexProperty(SolrInputDocument doc, String uuid, String field, String svalue, String authority,
        List<String> toIgnoreFields, Map<String, List<DiscoverySearchFilter>> searchFilters,
        List<String> toProjectionFields, Map<String, DiscoverySortFieldConfiguration> sortFields,
        List<String> sortFieldsAdded, Set<String> hitHighlightingFields, Set<String> moreLikeThisFields) {
    if (toIgnoreFields.contains(field)) {
        return;//  ww w.j a  v  a  2  s .c om
    }

    if ((searchFilters.get(field) != null)) {
        List<DiscoverySearchFilter> searchFilterConfigs = searchFilters.get(field);

        for (DiscoverySearchFilter searchFilter : searchFilterConfigs) {
            String separator = new DSpace().getConfigurationService()
                    .getProperty("discovery.solr.facets.split.char");
            if (separator == null) {
                separator = FILTER_SEPARATOR;
            }
            doc.addField(searchFilter.getIndexFieldName(), svalue);
            doc.addField(searchFilter.getIndexFieldName() + "_keyword", svalue);
            if (authority != null) {
                doc.addField(searchFilter.getIndexFieldName() + "_keyword",
                        svalue + AUTHORITY_SEPARATOR + authority);
                doc.addField(searchFilter.getIndexFieldName() + "_authority", authority);
                doc.addField(searchFilter.getIndexFieldName() + "_acid",
                        svalue.toLowerCase() + separator + svalue + AUTHORITY_SEPARATOR + authority);
            }

            // Add a dynamic fields for auto complete in search
            doc.addField(searchFilter.getIndexFieldName() + "_ac", svalue.toLowerCase() + separator + svalue);

            if (searchFilter.getFilterType().equals(DiscoverySearchFilterFacet.FILTER_TYPE_FACET)) {
                if (searchFilter.getType().equals(DiscoveryConfigurationParameters.TYPE_TEXT)) {
                    // Add a special filter
                    // We use a separator to split up the lowercase
                    // and regular case, this is needed to get our
                    // filters in regular case
                    // Solr has issues with facet prefix and cases
                    if (authority != null) {
                        String facetValue = svalue;
                        doc.addField(searchFilter.getIndexFieldName() + "_filter", facetValue.toLowerCase()
                                + separator + facetValue + AUTHORITY_SEPARATOR + authority);
                    } else {
                        doc.addField(searchFilter.getIndexFieldName() + "_filter",
                                svalue.toLowerCase() + separator + svalue);
                    }
                } else if (searchFilter.getType().equals(DiscoveryConfigurationParameters.TYPE_DATE)) {
                    Date date = toDate(svalue);
                    if (date != null) {
                        String indexField = searchFilter.getIndexFieldName() + ".year";
                        doc.addField(searchFilter.getIndexFieldName() + "_keyword",
                                DateFormatUtils.formatUTC(date, "yyyy"));
                        doc.addField(indexField, DateFormatUtils.formatUTC(date, "yyyy"));
                        // Also save a sort value of this year, this
                        // is required for determining the upper &
                        // lower bound year of our facet
                        if (doc.getField(indexField + "_sort") == null) {
                            // We can only add one year so take the
                            // first one
                            doc.addField(indexField + "_sort", DateFormatUtils.formatUTC(date, "yyyy"));
                        }
                    }
                } else if (searchFilter.getType().equals(DiscoveryConfigurationParameters.TYPE_HIERARCHICAL)) {
                    HierarchicalSidebarFacetConfiguration hierarchicalSidebarFacetConfiguration = (HierarchicalSidebarFacetConfiguration) searchFilter;
                    String[] subValues = svalue.split(hierarchicalSidebarFacetConfiguration.getSplitter());
                    if (hierarchicalSidebarFacetConfiguration.isSkipFirstNodeLevel() && 1 < subValues.length) {
                        // Remove the first element of our array
                        subValues = (String[]) ArrayUtils.subarray(subValues, 1, subValues.length);
                    }
                    for (int i = 0; i < subValues.length; i++) {
                        StringBuilder valueBuilder = new StringBuilder();
                        for (int j = 0; j <= i; j++) {
                            valueBuilder.append(subValues[j]);
                            if (j < i) {
                                valueBuilder.append(hierarchicalSidebarFacetConfiguration.getSplitter());
                            }
                        }

                        String indexValue = valueBuilder.toString().trim();
                        doc.addField(searchFilter.getIndexFieldName() + "_tax_" + i + "_filter",
                                indexValue.toLowerCase() + separator + indexValue);
                        // We add the field x times that it has
                        // occurred
                        for (int j = i; j < subValues.length; j++) {
                            doc.addField(searchFilter.getIndexFieldName() + "_filter",
                                    indexValue.toLowerCase() + separator + indexValue);
                            doc.addField(searchFilter.getIndexFieldName() + "_keyword", indexValue);
                        }
                    }
                }
            }
        }
    }

    if ((sortFields.get(field) != null && !sortFieldsAdded.contains(field))) {
        // Only add sort value once
        String type = "";
        if (sortFields.get(field) != null) {
            type = sortFields.get(field).getType();
        }

        if (type.equals(DiscoveryConfigurationParameters.TYPE_DATE)) {
            Date date = toDate(svalue);
            if (date != null) {
                doc.addField(field + "_dt", date);
            } else {
                log.warn("Error while indexing sort date field, cris: " + uuid + " metadata field: " + field
                        + " date value: " + svalue);
            }
        } else {
            doc.addField(field + "_sort", svalue);
        }
        sortFieldsAdded.add(field);
    }

    if (hitHighlightingFields.contains(field) || hitHighlightingFields.contains("*")) {
        doc.addField(field + "_hl", svalue);
    }

    if (moreLikeThisFields.contains(field)) {
        doc.addField(field + "_mlt", svalue);
    }

    doc.addField(field, svalue);
    if (authority != null) {
        doc.addField(field + "_authority", authority);
    }
    if (toProjectionFields.contains(field)) {
        doc.addField(field + "_stored", svalue + STORE_SEPARATOR + authority);
    }
}

From source file:org.dspace.discovery.SolrServiceImpl.java

/**
 * Build a Lucene document for a DSpace Item and write the index
 *
 * @param context Users Context//  w  w w  . j  a v  a2  s . c o m
 * @param item    The DSpace Item to be indexed
 * @throws SQLException
 * @throws IOException
 */
protected void buildDocument(Context context, Item item) throws SQLException, IOException {
    String handle = item.getHandle();

    if (handle == null) {
        handle = HandleManager.findHandle(context, item);
    }

    // get the location string (for searching by collection & community)
    List<String> locations = getItemLocations(item);

    SolrInputDocument doc = buildDocument(Constants.ITEM, item.getID(), handle, locations);

    log.debug("Building Item: " + handle);

    doc.addField("withdrawn", item.isWithdrawn());
    doc.addField("discoverable", item.isDiscoverable());

    //Keep a list of our sort values which we added, sort values can only be added once
    List<String> sortFieldsAdded = new ArrayList<String>();
    Set<String> hitHighlightingFields = new HashSet<String>();
    try {
        List<DiscoveryConfiguration> discoveryConfigurations = SearchUtils.getAllDiscoveryConfigurations(item);

        //A map used to save each sidebarFacet config by the metadata fields
        Map<String, List<DiscoverySearchFilter>> searchFilters = new HashMap<String, List<DiscoverySearchFilter>>();
        Map<String, DiscoverySortFieldConfiguration> sortFields = new HashMap<String, DiscoverySortFieldConfiguration>();
        Map<String, DiscoveryRecentSubmissionsConfiguration> recentSubmissionsConfigurationMap = new HashMap<String, DiscoveryRecentSubmissionsConfiguration>();
        Set<String> moreLikeThisFields = new HashSet<String>();
        for (DiscoveryConfiguration discoveryConfiguration : discoveryConfigurations) {
            for (int i = 0; i < discoveryConfiguration.getSearchFilters().size(); i++) {
                DiscoverySearchFilter discoverySearchFilter = discoveryConfiguration.getSearchFilters().get(i);
                for (int j = 0; j < discoverySearchFilter.getMetadataFields().size(); j++) {
                    String metadataField = discoverySearchFilter.getMetadataFields().get(j);
                    List<DiscoverySearchFilter> resultingList;
                    if (searchFilters.get(metadataField) != null) {
                        resultingList = searchFilters.get(metadataField);
                    } else {
                        //New metadata field, create a new list for it
                        resultingList = new ArrayList<DiscoverySearchFilter>();
                    }
                    resultingList.add(discoverySearchFilter);

                    searchFilters.put(metadataField, resultingList);
                }
            }

            DiscoverySortConfiguration sortConfiguration = discoveryConfiguration.getSearchSortConfiguration();
            if (sortConfiguration != null) {
                for (DiscoverySortFieldConfiguration discoverySortConfiguration : sortConfiguration
                        .getSortFields()) {
                    sortFields.put(discoverySortConfiguration.getMetadataField(), discoverySortConfiguration);
                }
            }

            DiscoveryRecentSubmissionsConfiguration recentSubmissionConfiguration = discoveryConfiguration
                    .getRecentSubmissionConfiguration();
            if (recentSubmissionConfiguration != null) {
                recentSubmissionsConfigurationMap.put(recentSubmissionConfiguration.getMetadataSortField(),
                        recentSubmissionConfiguration);
            }

            DiscoveryHitHighlightingConfiguration hitHighlightingConfiguration = discoveryConfiguration
                    .getHitHighlightingConfiguration();
            if (hitHighlightingConfiguration != null) {
                List<DiscoveryHitHighlightFieldConfiguration> fieldConfigurations = hitHighlightingConfiguration
                        .getMetadataFields();
                for (DiscoveryHitHighlightFieldConfiguration fieldConfiguration : fieldConfigurations) {
                    hitHighlightingFields.add(fieldConfiguration.getField());
                }
            }
            DiscoveryMoreLikeThisConfiguration moreLikeThisConfiguration = discoveryConfiguration
                    .getMoreLikeThisConfiguration();
            if (moreLikeThisConfiguration != null) {
                for (String metadataField : moreLikeThisConfiguration.getSimilarityMetadataFields()) {
                    moreLikeThisFields.add(metadataField);
                }
            }
        }

        List<String> toProjectionFields = new ArrayList<String>();
        String projectionFieldsString = new DSpace().getConfigurationService()
                .getProperty("discovery.index.projection");
        if (projectionFieldsString != null) {
            if (projectionFieldsString.indexOf(",") != -1) {
                for (int i = 0; i < projectionFieldsString.split(",").length; i++) {
                    toProjectionFields.add(projectionFieldsString.split(",")[i].trim());
                }
            } else {
                toProjectionFields.add(projectionFieldsString);
            }
        }

        DCValue[] mydc = item.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY);
        for (DCValue meta : mydc) {
            String field = meta.schema + "." + meta.element;
            String unqualifiedField = field;

            String value = meta.value;

            if (value == null) {
                continue;
            }

            if (meta.qualifier != null && !meta.qualifier.trim().equals("")) {
                field += "." + meta.qualifier;
            }

            List<String> toIgnoreMetadataFields = SearchUtils.getIgnoredMetadataFields(item.getType());
            //We are not indexing provenance, this is useless
            if (toIgnoreMetadataFields != null && (toIgnoreMetadataFields.contains(field)
                    || toIgnoreMetadataFields.contains(unqualifiedField + "." + Item.ANY))) {
                continue;
            }

            String authority = null;
            String preferedLabel = null;
            List<String> variants = null;
            boolean isAuthorityControlled = MetadataAuthorityManager.getManager()
                    .isAuthorityControlled(meta.schema, meta.element, meta.qualifier);

            int minConfidence = isAuthorityControlled
                    ? MetadataAuthorityManager.getManager().getMinConfidence(meta.schema, meta.element,
                            meta.qualifier)
                    : Choices.CF_ACCEPTED;

            if (isAuthorityControlled && meta.authority != null && meta.confidence >= minConfidence) {
                boolean ignoreAuthority = new DSpace().getConfigurationService().getPropertyAsType(
                        "discovery.index.authority.ignore." + field, new DSpace().getConfigurationService()
                                .getPropertyAsType("discovery.index.authority.ignore", new Boolean(false)),
                        true);
                if (!ignoreAuthority) {
                    authority = meta.authority;

                    boolean ignorePrefered = new DSpace().getConfigurationService()
                            .getPropertyAsType("discovery.index.authority.ignore-prefered." + field,
                                    new DSpace().getConfigurationService().getPropertyAsType(
                                            "discovery.index.authority.ignore-prefered", new Boolean(false)),
                                    true);
                    if (!ignorePrefered) {

                        preferedLabel = ChoiceAuthorityManager.getManager().getLabel(meta.schema, meta.element,
                                meta.qualifier, meta.authority, meta.language);
                    }

                    boolean ignoreVariants = new DSpace().getConfigurationService()
                            .getPropertyAsType("discovery.index.authority.ignore-variants." + field,
                                    new DSpace().getConfigurationService().getPropertyAsType(
                                            "discovery.index.authority.ignore-variants", new Boolean(false)),
                                    true);
                    if (!ignoreVariants) {
                        variants = ChoiceAuthorityManager.getManager().getVariants(meta.schema, meta.element,
                                meta.qualifier, meta.authority, meta.language);
                    }

                }
            }

            if ((searchFilters.get(field) != null
                    || searchFilters.get(unqualifiedField + "." + Item.ANY) != null)) {
                List<DiscoverySearchFilter> searchFilterConfigs = searchFilters.get(field);
                if (searchFilterConfigs == null) {
                    searchFilterConfigs = searchFilters.get(unqualifiedField + "." + Item.ANY);
                }

                for (DiscoverySearchFilter searchFilter : searchFilterConfigs) {
                    Date date = null;
                    String separator = new DSpace().getConfigurationService()
                            .getProperty("discovery.solr.facets.split.char");
                    if (separator == null) {
                        separator = FILTER_SEPARATOR;
                    }
                    if (searchFilter.getType().equals(DiscoveryConfigurationParameters.TYPE_DATE)) {
                        //For our search filters that are dates we format them properly
                        date = toDate(value);
                        if (date != null) {
                            //TODO: make this date format configurable !
                            value = DateFormatUtils.formatUTC(date, "yyyy-MM-dd");
                        }
                    }
                    doc.addField(searchFilter.getIndexFieldName(), value);
                    doc.addField(searchFilter.getIndexFieldName() + "_keyword", value);

                    if (authority != null && preferedLabel == null) {
                        doc.addField(searchFilter.getIndexFieldName() + "_keyword",
                                value + AUTHORITY_SEPARATOR + authority);
                        doc.addField(searchFilter.getIndexFieldName() + "_authority", authority);
                        doc.addField(searchFilter.getIndexFieldName() + "_acid",
                                value.toLowerCase() + separator + value + AUTHORITY_SEPARATOR + authority);
                    }

                    if (preferedLabel != null) {
                        doc.addField(searchFilter.getIndexFieldName(), preferedLabel);
                        doc.addField(searchFilter.getIndexFieldName() + "_keyword", preferedLabel);
                        doc.addField(searchFilter.getIndexFieldName() + "_keyword",
                                preferedLabel + AUTHORITY_SEPARATOR + authority);
                        doc.addField(searchFilter.getIndexFieldName() + "_authority", authority);
                        doc.addField(searchFilter.getIndexFieldName() + "_acid", preferedLabel.toLowerCase()
                                + separator + preferedLabel + AUTHORITY_SEPARATOR + authority);
                    }
                    if (variants != null) {
                        for (String var : variants) {
                            doc.addField(searchFilter.getIndexFieldName() + "_keyword", var);
                            doc.addField(searchFilter.getIndexFieldName() + "_acid",
                                    var.toLowerCase() + separator + var + AUTHORITY_SEPARATOR + authority);
                        }
                    }

                    //Add a dynamic fields for auto complete in search
                    doc.addField(searchFilter.getIndexFieldName() + "_ac",
                            value.toLowerCase() + separator + value);
                    if (preferedLabel != null) {
                        doc.addField(searchFilter.getIndexFieldName() + "_ac",
                                preferedLabel.toLowerCase() + separator + preferedLabel);
                    }
                    if (variants != null) {
                        for (String var : variants) {
                            doc.addField(searchFilter.getIndexFieldName() + "_ac",
                                    var.toLowerCase() + separator + var);
                        }
                    }

                    if (searchFilter.getFilterType().equals(DiscoverySearchFilterFacet.FILTER_TYPE_FACET)) {
                        if (searchFilter.getType().equals(DiscoveryConfigurationParameters.TYPE_TEXT)) {
                            //Add a special filter
                            //We use a separator to split up the lowercase and regular case, this is needed to get our filters in regular case
                            //Solr has issues with facet prefix and cases
                            if (authority != null) {
                                String facetValue = preferedLabel != null ? preferedLabel : value;
                                doc.addField(searchFilter.getIndexFieldName() + "_filter",
                                        facetValue.toLowerCase() + separator + facetValue + AUTHORITY_SEPARATOR
                                                + authority);
                            } else {
                                doc.addField(searchFilter.getIndexFieldName() + "_filter",
                                        value.toLowerCase() + separator + value);
                            }
                        } else if (searchFilter.getType().equals(DiscoveryConfigurationParameters.TYPE_DATE)) {
                            if (date != null) {
                                String indexField = searchFilter.getIndexFieldName() + ".year";
                                doc.addField(searchFilter.getIndexFieldName() + "_keyword",
                                        DateFormatUtils.formatUTC(date, "yyyy"));
                                doc.addField(indexField, DateFormatUtils.formatUTC(date, "yyyy"));
                                //Also save a sort value of this year, this is required for determining the upper & lower bound year of our facet
                                if (doc.getField(indexField + "_sort") == null) {
                                    //We can only add one year so take the first one
                                    doc.addField(indexField + "_sort", DateFormatUtils.formatUTC(date, "yyyy"));
                                }
                            }
                        } else if (searchFilter.getType()
                                .equals(DiscoveryConfigurationParameters.TYPE_HIERARCHICAL)) {
                            HierarchicalSidebarFacetConfiguration hierarchicalSidebarFacetConfiguration = (HierarchicalSidebarFacetConfiguration) searchFilter;
                            String[] subValues = value
                                    .split(hierarchicalSidebarFacetConfiguration.getSplitter());
                            if (hierarchicalSidebarFacetConfiguration.isSkipFirstNodeLevel()
                                    && 1 < subValues.length) {
                                //Remove the first element of our array
                                subValues = (String[]) ArrayUtils.subarray(subValues, 1, subValues.length);
                            }
                            for (int i = 0; i < subValues.length; i++) {
                                StringBuilder valueBuilder = new StringBuilder();
                                for (int j = 0; j <= i; j++) {
                                    valueBuilder.append(subValues[j]);
                                    if (j < i) {
                                        valueBuilder
                                                .append(hierarchicalSidebarFacetConfiguration.getSplitter());
                                    }
                                }

                                String indexValue = valueBuilder.toString().trim();
                                doc.addField(searchFilter.getIndexFieldName() + "_tax_" + i + "_filter",
                                        indexValue.toLowerCase() + separator + indexValue);
                                //We add the field x times that it has occurred
                                for (int j = i; j < subValues.length; j++) {
                                    doc.addField(searchFilter.getIndexFieldName() + "_filter",
                                            indexValue.toLowerCase() + separator + indexValue);
                                    doc.addField(searchFilter.getIndexFieldName() + "_keyword", indexValue);
                                }
                            }
                        }
                    }
                }
            }

            if ((sortFields.get(field) != null || recentSubmissionsConfigurationMap.get(field) != null)
                    && !sortFieldsAdded.contains(field)) {
                //Only add sort value once
                String type;
                if (sortFields.get(field) != null) {
                    type = sortFields.get(field).getType();
                } else {
                    type = recentSubmissionsConfigurationMap.get(field).getType();
                }

                if (type.equals(DiscoveryConfigurationParameters.TYPE_DATE)) {
                    Date date = toDate(value);
                    if (date != null) {
                        doc.addField(field + "_dt", date);
                    } else {
                        log.warn("Error while indexing sort date field, item: " + item.getHandle()
                                + " metadata field: " + field + " date value: " + date);
                    }
                } else {
                    doc.addField(field + "_sort", value);
                }
                sortFieldsAdded.add(field);
            }

            if (hitHighlightingFields.contains(field) || hitHighlightingFields.contains("*")
                    || hitHighlightingFields.contains(unqualifiedField + "." + Item.ANY)) {
                doc.addField(field + "_hl", value);
            }

            if (moreLikeThisFields.contains(field)
                    || moreLikeThisFields.contains(unqualifiedField + "." + Item.ANY)) {
                doc.addField(field + "_mlt", value);
            }

            doc.addField(field, value);
            if (toProjectionFields.contains(field)
                    || toProjectionFields.contains(unqualifiedField + "." + Item.ANY)) {
                StringBuffer variantsToStore = new StringBuffer();
                if (variants != null) {
                    for (String var : variants) {
                        variantsToStore.append(VARIANTS_STORE_SEPARATOR);
                        variantsToStore.append(var);
                    }
                }
                doc.addField(field + "_stored", value + STORE_SEPARATOR + preferedLabel + STORE_SEPARATOR
                        + (variantsToStore.length() > VARIANTS_STORE_SEPARATOR.length()
                                ? variantsToStore.substring(VARIANTS_STORE_SEPARATOR.length())
                                : "null")
                        + STORE_SEPARATOR + authority + STORE_SEPARATOR + meta.language);
            }

            if (meta.language != null && !meta.language.trim().equals("")) {
                String langField = field + "." + meta.language;
                doc.addField(langField, value);
            }
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    log.debug("  Added Metadata");

    try {

        DCValue[] values = item.getMetadata("dc.relation.ispartof");

        if (values != null && values.length > 0 && values[0] != null && values[0].value != null) {
            // group on parent
            String handlePrefix = ConfigurationManager.getProperty("handle.canonical.prefix");
            if (handlePrefix == null || handlePrefix.length() == 0) {
                handlePrefix = "http://hdl.handle.net/";
            }

            doc.addField("publication_grp", values[0].value.replaceFirst(handlePrefix, ""));

        } else {
            // group on self
            doc.addField("publication_grp", item.getHandle());
        }

    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    log.debug("  Added Grouping");

    Vector<InputStreamReader> readers = new Vector<InputStreamReader>();

    try {
        // now get full text of any bitstreams in the TEXT bundle
        // trundle through the bundles
        Bundle[] myBundles = item.getBundles();

        for (Bundle myBundle : myBundles) {
            if ((myBundle.getName() != null) && myBundle.getName().equals("TEXT")) {
                // a-ha! grab the text out of the bitstreams
                Bitstream[] myBitstreams = myBundle.getBitstreams();

                for (Bitstream myBitstream : myBitstreams) {
                    try {
                        InputStreamReader is = new InputStreamReader(myBitstream.retrieve()); // get input
                        readers.add(is);

                        // Add each InputStream to the Indexed Document
                        String value = IOUtils.toString(is);
                        doc.addField("fulltext", value);

                        if (hitHighlightingFields.contains("*") || hitHighlightingFields.contains("fulltext")) {
                            doc.addField("fulltext_hl", value);
                        }

                        log.debug("  Added BitStream: " + myBitstream.getStoreNumber() + "   "
                                + myBitstream.getSequenceID() + "   " + myBitstream.getName());

                    } catch (Exception e) {
                        // this will never happen, but compiler is now
                        // happy.
                        log.trace(e.getMessage(), e);
                    }
                }
            }
        }
    } catch (RuntimeException e) {
        log.error(e.getMessage(), e);
    } finally {
        Iterator<InputStreamReader> itr = readers.iterator();
        while (itr.hasNext()) {
            InputStreamReader reader = itr.next();
            if (reader != null) {
                reader.close();
            }
        }
        log.debug("closed " + readers.size() + " readers");
    }

    //Do any additional indexing, depends on the plugins
    List<SolrServiceIndexPlugin> solrServiceIndexPlugins = new DSpace().getServiceManager()
            .getServicesByType(SolrServiceIndexPlugin.class);
    for (SolrServiceIndexPlugin solrServiceIndexPlugin : solrServiceIndexPlugins) {
        solrServiceIndexPlugin.additionalIndex(context, item, doc);
    }

    // write the index and close the inputstreamreaders
    try {
        writeDocument(doc);
        log.info("Wrote Item: " + handle + " to Index");
    } catch (RuntimeException e) {
        log.error("Error while writing item to discovery index: " + handle + " message:" + e.getMessage(), e);
    }
}

From source file:org.dspace.statistics.content.StatisticsDataSearches.java

@Override
public Dataset createDataset(Context context)
        throws SQLException, SolrServerException, IOException, ParseException {
    // Check if we already have one.
    // If we do then give it back.
    if (getDataset() != null) {
        return getDataset();
    }//from  w w w .  ja va  2 s.  c o  m

    List<StatisticsFilter> filters = getFilters();
    List<String> defaultFilters = new ArrayList<String>();
    for (StatisticsFilter statisticsFilter : filters) {
        defaultFilters.add(statisticsFilter.toQuery());
    }

    String defaultFilterQuery = StringUtils.join(defaultFilters.iterator(), " AND ");

    String query = getQuery();

    Dataset dataset = new Dataset(0, 0);
    List<DatasetGenerator> datasetGenerators = getDatasetGenerators();
    if (0 < datasetGenerators.size()) {
        //At the moment we can only have one dataset generator
        DatasetGenerator datasetGenerator = datasetGenerators.get(0);
        if (datasetGenerator instanceof DatasetSearchGenerator) {
            DatasetSearchGenerator typeGenerator = (DatasetSearchGenerator) datasetGenerator;

            if (typeGenerator.getMode() == DatasetSearchGenerator.Mode.SEARCH_OVERVIEW) {
                StringBuilder fqBuffer = new StringBuilder(defaultFilterQuery);
                if (0 < fqBuffer.length()) {
                    fqBuffer.append(" AND ");
                }
                fqBuffer.append(getSearchFilterQuery());

                ObjectCount[] topCounts = SolrLogger.queryFacetField(query, fqBuffer.toString(),
                        typeGenerator.getType(), typeGenerator.getMax(),
                        (typeGenerator.isPercentage() || typeGenerator.isIncludeTotal()), null);
                long totalCount = -1;
                if (typeGenerator.isPercentage() && 0 < topCounts.length) {
                    //Retrieve the total required to calculate the percentage
                    totalCount = topCounts[topCounts.length - 1].getCount();
                    //Remove the total count from view !
                    topCounts = (ObjectCount[]) ArrayUtils.subarray(topCounts, 0, topCounts.length - 1);
                }

                int nrColumns = 2;
                if (typeGenerator.isPercentage()) {
                    nrColumns++;
                }
                if (typeGenerator.isRetrievePageViews()) {
                    nrColumns++;
                }

                dataset = new Dataset(topCounts.length, nrColumns);
                dataset.setColLabel(0, "search-terms");
                dataset.setColLabel(1, "searches");
                if (typeGenerator.isPercentage()) {
                    dataset.setColLabel(2, "percent-total");
                }
                if (typeGenerator.isRetrievePageViews()) {
                    dataset.setColLabel(3, "views-search");
                }
                for (int i = 0; i < topCounts.length; i++) {
                    ObjectCount queryCount = topCounts[i];

                    dataset.setRowLabel(i, String.valueOf(i + 1));
                    String displayedValue = queryCount.getValue();
                    if (new DSpace().getConfigurationService().getPropertyAsType(
                            "usage-statistics.search.statistics.unescape.queries", Boolean.TRUE)) {
                        displayedValue = displayedValue.replace("\\", "");
                    }
                    dataset.addValueToMatrix(i, 0, displayedValue);
                    dataset.addValueToMatrix(i, 1, queryCount.getCount());
                    if (typeGenerator.isPercentage()) {
                        //Calculate our percentage from the total !
                        dataset.addValueToMatrix(i, 2,
                                percentageFormat.format(((float) queryCount.getCount() / totalCount)));
                    }
                    if (typeGenerator.isRetrievePageViews()) {
                        String queryString = ClientUtils.escapeQueryChars(queryCount.getValue());
                        if (queryString.equals("")) {
                            queryString = "\"\"";
                        }

                        ObjectCount totalPageViews = getTotalPageViews("query:" + queryString,
                                defaultFilterQuery);
                        dataset.addValueToMatrix(i, 3, pageViewFormat
                                .format((float) totalPageViews.getCount() / queryCount.getCount()));
                    }
                }
            } else if (typeGenerator.getMode() == DatasetSearchGenerator.Mode.SEARCH_OVERVIEW_TOTAL) {
                //Retrieve the total counts !
                ObjectCount totalCount = SolrLogger.queryTotal(query, getSearchFilterQuery());

                //Retrieve the filtered count by using the default filter query
                StringBuilder fqBuffer = new StringBuilder(defaultFilterQuery);
                if (0 < fqBuffer.length()) {
                    fqBuffer.append(" AND ");
                }
                fqBuffer.append(getSearchFilterQuery());

                ObjectCount totalFiltered = SolrLogger.queryTotal(query, fqBuffer.toString());

                fqBuffer = new StringBuilder(defaultFilterQuery);
                if (0 < fqBuffer.length()) {
                    fqBuffer.append(" AND ");
                }
                fqBuffer.append("statistics_type:").append(SolrLogger.StatisticsType.SEARCH_RESULT.text());

                ObjectCount totalPageViews = getTotalPageViews(query, defaultFilterQuery);

                dataset = new Dataset(1, 3);
                dataset.setRowLabel(0, "");

                dataset.setColLabel(0, "searches");
                dataset.addValueToMatrix(0, 0, totalFiltered.getCount());
                dataset.setColLabel(1, "percent-total");
                //Ensure that we do NOT divide by 0
                float percentTotal;
                if (totalCount.getCount() == 0) {
                    percentTotal = 0;
                } else {
                    percentTotal = (float) totalFiltered.getCount() / totalCount.getCount();
                }

                dataset.addValueToMatrix(0, 1, percentageFormat.format(percentTotal));
                dataset.setColLabel(2, "views-search");
                //Ensure that we do NOT divide by 0
                float pageViews;
                if (totalFiltered.getCount() == 0) {
                    pageViews = 0;
                } else {
                    pageViews = (float) totalPageViews.getCount() / totalFiltered.getCount();
                }

                dataset.addValueToMatrix(0, 2, pageViewFormat.format(pageViews));
            }
        } else {
            throw new IllegalArgumentException(
                    "Data generator with class" + datasetGenerator.getClass().getName()
                            + " is not supported by the statistics search engine !");
        }
    }

    return dataset;
}

From source file:org.eclipse.jubula.app.autrun.AutRunApplication.java

/**
 * Creates and returns settings for starting an AUT based on the given
 * command line.//w  w  w.j a  v  a  2 s . co  m
 *  
 * @param cmdLine Provides the settings for the AUT configuration.
 * @return new settings for starting an AUT.
 */
private static Map<String, Object> createAutConfig(CommandLine cmdLine) {
    Map<String, Object> autConfig = new HashMap<String, Object>();
    if (cmdLine.hasOption(OPT_WORKING_DIR)) {
        autConfig.put(AutConfigConstants.WORKING_DIR, cmdLine.getOptionValue(OPT_WORKING_DIR));
    } else {
        autConfig.put(AutConfigConstants.WORKING_DIR, System.getProperty("user.dir")); //$NON-NLS-1$
    }

    if (cmdLine.hasOption(OPT_NAME_TECHNICAL_COMPONENTS)) {
        autConfig.put(AutConfigConstants.NAME_TECHNICAL_COMPONENTS,
                Boolean.valueOf(cmdLine.getOptionValue(OPT_NAME_TECHNICAL_COMPONENTS)));
    } else {
        autConfig.put(AutConfigConstants.NAME_TECHNICAL_COMPONENTS, DEFAULT_NAME_TECHNICAL_COMPONENTS);
    }
    autConfig.put(AutConfigConstants.EXECUTABLE, cmdLine.getOptionValue(OPT_EXECUTABLE));

    if (cmdLine.hasOption(OPT_KEYBOARD_LAYOUT)) {
        autConfig.put(AutConfigConstants.KEYBOARD_LAYOUT, cmdLine.getOptionValue(OPT_KEYBOARD_LAYOUT));
    }

    String[] autArguments = cmdLine.getOptionValues(OPT_EXECUTABLE);
    if (autArguments.length > 1) {
        autConfig.put(AutConfigConstants.AUT_RUN_AUT_ARGUMENTS,
                ArrayUtils.subarray(autArguments, 1, autArguments.length));
    }

    return autConfig;
}

From source file:org.eclipse.nebula.widgets.nattable.examples.examples._150_Column_and_row_grouping._010_Column_categories.java

public Control createExampleControl(Composite parent) {
    ConfigRegistry configRegistry = new ConfigRegistry();

    gridLayer = new GlazedListsGridLayer<RowDataFixture>(GlazedLists.eventList(RowDataListFixture.getList()),
            (String[]) ArrayUtils.subarray(RowDataListFixture.getPropertyNames(), 0, 20),
            RowDataListFixture.getPropertyToLabelMap(), configRegistry);

    NatTable natTable = new NatTable(parent, gridLayer, false);
    natTable.addConfiguration(new DefaultNatTableStyleConfiguration());
    natTable.addConfiguration(new HeaderMenuConfiguration(natTable) {
        @Override/*w  w  w . j  a va2 s  . co  m*/
        protected PopupMenuBuilder createColumnHeaderMenu(NatTable natTable) {
            return super.createColumnHeaderMenu(natTable).withCategoriesBasedColumnChooser("Choose columns");
        }
    });

    configureColumnCategoriesInChooser();

    natTable.configure();
    return natTable;
}

From source file:org.eclipse.nebula.widgets.nattable.selection.SelectionModel.java

@Override
public int[] getFullySelectedColumnPositions(int columnHeight) {
    final int[] selectedColumns = getSelectedColumnPositions();
    int[] columnsToHide = new int[selectedColumns.length];
    int index = 0;
    for (int columnPosition : selectedColumns) {
        if (isColumnPositionFullySelected(columnPosition, columnHeight)) {
            columnsToHide[index++] = columnPosition;
        }//from  w ww  . j  a v a2  s .c om
    }

    return index > 0 ? ArrayUtils.subarray(columnsToHide, 0, index) : new int[0];
}

From source file:org.eclipse.nebula.widgets.nattable.selection.SelectionModel.java

@Override
public int[] getFullySelectedRowPositions(int rowWidth) {
    final Set<Range> selectedRows = getSelectedRowPositions();
    int[] fullySelectedRows = new int[getSelectedRowCount()];
    int index = 0;

    for (Range rowRange : selectedRows) {
        for (int i = rowRange.start; i < rowRange.end; i++) {
            if (isRowPositionFullySelected(i, rowWidth)) {
                fullySelectedRows[index++] = i;
            }//  w  ww.  j a  va 2 s. com
        }
    }

    return index > 0 ? ArrayUtils.subarray(fullySelectedRows, 0, index) : new int[0];
}

From source file:org.eclipse.smarthome.binding.homematic.internal.communicator.message.BinRpcMessage.java

/**
 * Decodes a BIN-RPC message from the given InputStream.
 *///from   w  w w . ja  v a2  s.  c o m
public BinRpcMessage(InputStream is, boolean methodHeader, String encoding) throws IOException {
    this.encoding = encoding;
    byte sig[] = new byte[8];
    int length = is.read(sig, 0, 4);
    if (length != 4) {
        throw new EOFException("Only " + length + " bytes received reading signature");
    }
    validateBinXSignature(sig);
    length = is.read(sig, 4, 4);
    if (length != 4) {
        throw new EOFException("Only " + length + " bytes received reading message length");
    }
    int datasize = (new BigInteger(ArrayUtils.subarray(sig, 4, 8))).intValue();
    byte payload[] = new byte[datasize];
    int offset = 0;
    int currentLength;

    while (offset < datasize && (currentLength = is.read(payload, offset, datasize - offset)) != -1) {
        offset += currentLength;
    }
    if (offset != datasize) {
        throw new EOFException("Only " + offset + " bytes received while reading message payload, expected "
                + datasize + " bytes");
    }
    byte[] message = ArrayUtils.addAll(sig, payload);
    decodeMessage(message, methodHeader);
}

From source file:org.eclipse.smarthome.core.audio.internal.AudioConsoleCommandExtension.java

@Override
public void execute(String[] args, Console console) {
    if (args.length > 0) {
        String subCommand = args[0];
        switch (subCommand) {
        case SUBCMD_PLAY:
            if (args.length > 1) {
                play((String[]) ArrayUtils.subarray(args, 1, args.length), console);
            } else {
                console.println(//from   ww  w.  j  a v  a 2 s . co  m
                        "Specify file to play, and optionally the sink(s) to use (e.g. 'play javasound hello.mp3')");
            }
            return;
        case SUBCMD_STREAM:
            if (args.length > 1) {
                stream((String[]) ArrayUtils.subarray(args, 1, args.length), console);
            } else {
                console.println("Specify url to stream from, and optionally the sink(s) to use");
            }
            return;
        case SUBCMD_SOURCES:
            listSources(console);
            return;
        case SUBCMD_SINKS:
            listSinks(console);
            return;
        default:
            break;
        }
    } else {
        printUsage(console);
    }
}