Example usage for java.util.concurrent.atomic AtomicInteger intValue

List of usage examples for java.util.concurrent.atomic AtomicInteger intValue

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicInteger intValue.

Prototype

public int intValue() 

Source Link

Document

Returns the current value of this AtomicInteger as an int , with memory effects as specified by VarHandle#getVolatile .

Usage

From source file:net.solarnetwork.node.dao.jdbc.AbstractBatchableJdbcDao.java

private BatchResult batchProcessInternal(final BatchCallback<T> callback, final BatchOptions options) {
    final String querySql = getBatchJdbcStatement(options);
    final AtomicInteger rowCount = new AtomicInteger(0);
    getJdbcTemplate().execute(new ConnectionCallback<Object>() {

        @Override/*  ww  w.j a  v  a 2 s  .c  om*/
        public net.solarnetwork.node.dao.BatchableDao.BatchResult doInConnection(Connection con)
                throws SQLException, DataAccessException {
            PreparedStatement queryStmt = null;
            ResultSet queryResult = null;
            try {
                queryStmt = con.prepareStatement(querySql,
                        (options.isUpdatable() ? ResultSet.TYPE_SCROLL_SENSITIVE : ResultSet.TYPE_FORWARD_ONLY),
                        (options.isUpdatable() ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY),
                        ResultSet.CLOSE_CURSORS_AT_COMMIT);
                queryResult = queryStmt.executeQuery();
                while (queryResult.next()) {
                    T entity = getBatchRowEntity(options, queryResult, rowCount.incrementAndGet());
                    BatchCallbackResult rowResult = callback.handle(entity);
                    switch (rowResult) {
                    case CONTINUE:
                        break;
                    case STOP:
                        return null;
                    case DELETE:
                        queryResult.deleteRow();
                        break;
                    case UPDATE:
                    case UPDATE_STOP:
                        updateBatchRowEntity(options, queryResult, rowCount.intValue(), entity);
                        queryResult.updateRow();
                        if (rowResult == BatchCallbackResult.UPDATE_STOP) {
                            return null;
                        }
                        break;
                    }
                }
            } finally {
                if (queryResult != null) {
                    queryResult.close();
                }
                if (queryStmt != null) {
                    queryStmt.close();
                }
            }

            return null;
        }
    });
    return new BasicBatchResult(rowCount.intValue());
}

From source file:com.norconex.committer.AbstractFileQueueCommitterTest.java

@Test
public void testMultipleCommitThread() throws Exception {

    final AtomicInteger counter = new AtomicInteger();

    final AbstractFileQueueCommitter committer = new AbstractFileQueueCommitter() {

        @Override//from   w  w w.  j a  v  a2  s  . c  o m
        protected void commitAddition(IAddOperation operation) throws IOException {
            counter.incrementAndGet();
            operation.delete();
        }

        @Override
        protected void commitDeletion(IDeleteOperation operation) throws IOException {
            counter.incrementAndGet();
            operation.delete();
        }

        @Override
        protected void commitComplete() {
        }
    };

    File queue = temp.newFolder();
    committer.setQueueDir(queue.getPath());
    // Use a bigger number to make sure the files are not 
    // committed while they are added.
    committer.setQueueSize(1000);

    // Queue 50 files for additions
    for (int i = 0; i < 50; i++) {
        Properties metadata = new Properties();
        committer.add(Integer.toString(i), IOUtils.toInputStream("hello world!"), metadata);
    }
    // Queue 50 files for deletions
    for (int i = 50; i < 100; i++) {
        Properties metadata = new Properties();
        committer.remove(Integer.toString(i), metadata);
    }

    ExecutorService pool = Executors.newFixedThreadPool(10);
    for (int i = 0; i < 10; i++) {
        pool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    committer.commit();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    pool.shutdown();
    pool.awaitTermination(10, TimeUnit.SECONDS);

    // Each file should have been processed exactly once
    assertEquals(100, counter.intValue());

    // All files should have been processed
    Collection<File> files = FileUtils.listFiles(queue, null, true);
    assertTrue(files.isEmpty());
}

From source file:com.iskyshop.manage.buyer.action.OrderBuyerAction.java

/**
 * //from w w  w  . j  a  v  a 2 s . c o m
 * @param request
 * @param response
 * @param id --?id
 * @param oid-?id
 * @param order_id--?order_id
 * @param view
 * @param goods_gsp_ids
 * @return
 */

@SecurityMapping(title = "", value = "/buyer/order_return_apply.htm*", rtype = "buyer", rname = "", rcode = "user_center", rgroup = "")
@RequestMapping("/buyer/order_return_apply.htm")
public ModelAndView order_return_apply(HttpServletRequest request, HttpServletResponse response, String id,
        String oid, String order_id, String view, String goods_gsp_ids, String original_id) {
    ModelAndView mv = new JModelAndView("user/default/usercenter/order_return_apply.html",
            configService.getSysConfig(), this.userConfigService.getUserConfig(), 0, request, response);
    OrderForm obj = this.orderFormService.getObjById(CommUtil.null2Long(oid));
    if (obj.getUser_id().equals(SecurityUserHolder.getCurrentUser().getId().toString())) {
        List<Map> maps = this.orderFormTools.queryGoodsInfo(obj.getGoods_info());
        Goods goods = this.goodsService.getObjById(CommUtil.null2Long(id));
        for (Map m : maps) {
            if (CommUtil.null2String(m.get("goods_id")).equals(id)) {
                mv.addObject("return_count", m.get("return_goods_count"));
                mv.addObject("oid", oid);
                mv.addObject("goods", goods);
                if (CommUtil.null2String(m.get("goods_return_status")).equals("5")) {
                    mv.addObject("view", true);
                    List<Map> return_maps = this.orderFormTools.queryGoodsInfo(obj.getReturn_goods_info());
                    for (Map map : return_maps) {
                        if (CommUtil.null2String(map.get("return_goods_id")).equals(id)) {
                            mv.addObject("return_content", map.get("return_goods_content"));
                        }
                    }
                }
            }
        }
        HttpSession session = request.getSession();
        AtomicInteger img_num = (AtomicInteger) session.getAttribute("img_num");
        String img_html = "";
        if (img_num != null) {
            for (int i = 1; i < img_num.intValue() + 1; i++) {
                img_html += "<li><a target='_blank' id='refund_href" + i + "' href='" + CommUtil.getURL(request)
                        + "/" + session.getAttribute("refund_img_" + i) + "'> <img id='refund_img_" + i
                        + "' width='55' height='55' src='" + CommUtil.getURL(request) + "/"
                        + session.getAttribute("refund_img_" + i) + "' /></a> <b onclick='delete_return_img("
                        + i + ")'>X</b></li>";
            }
        }
        mv.addObject("img_html", img_html);
    }
    mv.addObject("goods_gsp_ids", goods_gsp_ids);
    mv.addObject("original_id", original_id);

    return mv;
}

From source file:com.iskyshop.manage.buyer.action.OrderBuyerAction.java

/**
 * //from  ww w .  j  a  v a2  s. com
 * @param request
 * @param response
 * @param id --$!oid
 * @param currentPage
 * @param return_goods_content
 * @param goods_id--?order_id
 * @param return_goods_count
 * @param goods_gsp_ids
 * @return
 * @throws Exception
 */

@Transactional
@SecurityMapping(title = "?", value = "/buyer/order_return_apply_save.htm*", rtype = "buyer", rname = "", rcode = "user_center", rgroup = "")
@RequestMapping("/buyer/order_return_apply_save.htm")
public String order_return_apply_save(HttpServletRequest request, HttpServletResponse response, String id,
        String currentPage, String return_goods_content, String goods_id, String original_id,
        String return_goods_count, String goods_gsp_ids) throws Exception {
    OrderForm obj = this.orderFormService.getObjById(CommUtil.null2Long(id));
    List<Goods> goods_list = this.orderFormTools.queryOfGoods(obj.getId().toString());
    Goods goods = null;
    for (Goods g : goods_list) {
        if (g.getId().toString().equals(goods_id)) {
            goods = g;
        }
    }
    if (obj != null && obj.getUser_id().equals(SecurityUserHolder.getCurrentUser().getId().toString())
            && goods != null) {
        List<Map> list = new ArrayList<Map>();
        Map json = new HashMap();
        json.put("return_goods_id", goods.getId());
        json.put("return_goods_content", CommUtil.filterHTML(return_goods_content));
        json.put("return_goods_count", return_goods_count);
        json.put("return_goods_price", goods.getStore_price());
        json.put("return_goods_commission_rate", goods.getGc().getCommission_rate());
        json.put("return_order_id", id);
        list.add(json);
        obj.setReturn_goods_info(Json.toJson(list, JsonFormat.compact()));
        List<Map> maps = this.orderFormTools.queryGoodsInfo(obj.getGoods_info());
        List<Map> new_maps = new ArrayList<Map>();
        Map gls = new HashMap();
        for (Map m : maps) {
            String tmp_goods_gsp_ids = m.get("goods_gsp_ids").toString();
            if (m.get("goods_id").toString().equals(goods_id) && goods_gsp_ids.equals(tmp_goods_gsp_ids)) {
                m.put("goods_return_status", 5);
                gls.putAll(m);
            }
            new_maps.add(m);
        }

        obj.setGoods_info(Json.toJson(new_maps));
        this.orderFormService.update(obj);
        User user = this.userService.getObjById(SecurityUserHolder.getCurrentUser().getId());
        ReturnGoodsLog rlog = new ReturnGoodsLog();
        rlog.setReturn_service_id("re" + user.getId() + CommUtil.formatTime("yyyyMMddHHmmss", new Date()));
        rlog.setUser_name(user.getUserName());
        rlog.setUser_id(user.getId());
        rlog.setReturn_content(CommUtil.filterHTML(return_goods_content));
        rlog.setGoods_all_price(gls.get("goods_all_price").toString());
        rlog.setGoods_count(gls.get("goods_count").toString());
        rlog.setGoods_id(CommUtil.null2Long(gls.get("goods_id").toString()));
        rlog.setGoods_mainphoto_path(gls.get("goods_mainphoto_path").toString());
        rlog.setGoods_commission_rate(
                BigDecimal.valueOf(CommUtil.null2Double(gls.get("goods_commission_rate"))));
        rlog.setGoods_name(gls.get("goods_name").toString());
        rlog.setGoods_price(gls.get("goods_price").toString());
        rlog.setGoods_return_status("5");
        rlog.setAddTime(new Date());
        rlog.setReturn_order_id(CommUtil.null2Long(id));
        rlog.setGoods_type(goods.getGoods_type());
        if (goods.getGoods_store() != null) {
            rlog.setStore_id(goods.getGoods_store().getId());
        }
        String refund_img = "";
        HttpSession session = request.getSession();
        AtomicInteger num_img = (AtomicInteger) session.getAttribute("img_num");
        if (num_img != null) {
            for (int i = num_img.intValue(); i > 0; i--) {
                refund_img += session.getAttribute("refund_img_" + i) + ",";
            }
        }
        rlog.setRefund_img(refund_img);
        this.returnGoodsLogService.save(rlog);
        if (this.configService.getSysConfig().isEmailEnable()) {
            if (obj.getOrder_form() == 0) {
                User seller = this.userService.getObjById(
                        this.storeService.getObjById(CommUtil.null2Long(obj.getStore_id())).getUser().getId());
                Map map = new HashMap();
                map.put("buyer_id", user.getId().toString());
                map.put("seller_id", seller.getId().toString());
                String map_json = Json.toJson(map);
                this.sendMsgAndEmTools.sendEmail(CommUtil.getURL(request),
                        TytsmsStringUtils.generatorFilesFolderServerPath(request),
                        "email_toseller_order_return_apply_notify", seller.getEmail(), map_json);
            }
        }
        if (this.configService.getSysConfig().isSmsEnbale()) {
            if (obj.getOrder_form() == 0) {
                User seller = this.userService.getObjById(
                        this.storeService.getObjById(CommUtil.null2Long(obj.getStore_id())).getUser().getId());
                Map map = new HashMap();
                map.put("buyer_id", user.getId().toString());
                map.put("seller_id", seller.getId().toString());
                String map_json = Json.toJson(map);
                String path = TytsmsStringUtils.generatorImagesFolderServerPath(request);
                String url = CommUtil.getURL(request);
                this.sendMsgAndEmTools.sendMsg(url, path, "sms_toseller_order_return_apply_notify",
                        seller.getMobile(), map_json);
            }
        }
    }
    return "redirect:order_return_list.htm?currentPage=" + currentPage;
}

From source file:com.iskyshop.manage.buyer.action.OrderBuyerAction.java

@Transactional
@SecurityMapping(title = "", value = "/buyer/ajax_img_refund.htm*", rtype = "buyer", rname = "", rcode = "user_center", rgroup = "")
@RequestMapping("/buyer/ajax_img_refund.htm")
public void ajax_img_upload(HttpServletRequest request, HttpServletResponse response)
        throws FileUploadException {

    HttpSession session = request.getSession();
    int count = 1;
    AtomicInteger img_num = (AtomicInteger) session.getAttribute("img_num");
    response.setContentType("text/plain;charset=UTF-8");
    PrintWriter writer;//from  www  . ja v  a 2s.  co  m
    try {
        if (img_num == null) {
            count = 1;
        } else {
            count += img_num.intValue();
        }
        if (count > 5) {
            String res = "{\"num\":" + 6 + "}";
            writer = response.getWriter();
            writer.print(res);
        } else {
            String uploadFilePath = ConfigContants.UPLOAD_IMAGE_MIDDLE_NAME + "/" + "refund";
            String saveFilePathName = TytsmsStringUtils.generatorImagesFolderServerPath(request)
                    + uploadFilePath;
            File file = new File(saveFilePathName);
            if (!file.exists()) {
                file.mkdirs();
            }
            Map map = new HashMap();
            map = CommUtil.saveFileToServer(configService, request, "refund_img", saveFilePathName, "", null);
            if (map.size() > 0) {
                String reg = ".+(" + configService.getSysConfig().getImageSuffix() + ")$";
                String imgp = (String) map.get("fileName");
                Pattern pattern = Pattern.compile(reg);
                Matcher matcher = pattern.matcher(imgp.toLowerCase());
                if (matcher.find()) {
                    //CommUtil.del_acc(request, old_photo);
                    if (map.get("fileName") != "") {
                        String refund_img = uploadFilePath + "/" + map.get("fileName");
                        session.setAttribute("refund_img_" + count, refund_img);
                        session.setAttribute("img_num", new AtomicInteger(count));
                        String files = CommUtil.getURL(request) + "/" + refund_img;
                        String img_html = "";
                        for (int i = 1; i < count + 1; i++) {
                            img_html += "<li><a target='_blank' id='refund_href" + i + "' href='"
                                    + CommUtil.getURL(request) + "/" + session.getAttribute("refund_img_" + i)
                                    + "'> <img id='refund_img_" + i + "' width='55' height='55' src='"
                                    + CommUtil.getURL(request) + "/" + session.getAttribute("refund_img_" + i)
                                    + "' /></a> <b onclick='delete_return_img(" + i + ")'>X</b></li>";
                        }
                        String res = "{\"num\":" + count + ",\"img_html\":\"" + img_html + "\"}";
                        writer = response.getWriter();
                        writer.print(res);
                    } else {
                        String error = "error";
                        String res = "{\"error\":\"" + error + "\"}";
                        writer = response.getWriter();
                        writer.print(res);
                    }
                }
            } else {
                String error = "error";
                String res = "{\"error\":\"" + error + "\"}";
                writer = response.getWriter();
                writer.print(res);
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.iskyshop.manage.buyer.action.OrderBuyerAction.java

@SecurityMapping(title = "", value = "/buyer/refund_image_del.htm*", rtype = "buyer", rname = "", rcode = "user_center", rgroup = "")
@RequestMapping("/buyer/refund_image_del.htm")
public void refund_image_del(HttpServletRequest request, HttpServletResponse response, String image_id) {
    response.setContentType("text/plain");
    response.setHeader("Cache-Control", "no-cache");
    response.setCharacterEncoding("UTF-8");
    PrintWriter writer;// w w w .  j a  va2  s .  c o m
    try {
        HttpSession session = request.getSession();
        AtomicInteger img_num = (AtomicInteger) session.getAttribute("img_num");
        if (img_num != null) {
            String refund_img = (String) session.getAttribute("refund_img_" + image_id);
            CommUtil.deleteFile(CommUtil.getURL(request) + "/" + refund_img);
            session.removeAttribute(refund_img);
            session.removeAttribute(image_id);
            int id = Integer.valueOf(image_id);
            int j = 0;
            for (int i = 1; i < img_num.intValue(); i++) {
                j++;
                if (i == id) {
                    i--;
                    id--;
                    continue;
                }
                session.setAttribute("refund_img_" + i, session.getAttribute("refund_img_" + j));
            }
            session.setAttribute("img_num", new AtomicInteger(img_num.intValue() - 1));
            AtomicInteger count = (AtomicInteger) session.getAttribute("img_num");
            String img_html = "";
            for (int i = 1; i < count.intValue() + 1; i++) {
                img_html += "<li><a target='_blank' id='refund_href" + i + "' href='" + CommUtil.getURL(request)
                        + "/" + session.getAttribute("refund_img_" + i) + "'> <img id='refund_img_" + i
                        + "' width='55' height='55' src='" + CommUtil.getURL(request) + "/"
                        + session.getAttribute("refund_img_" + i) + "' /></a> <b onclick='delete_return_img("
                        + i + ")'>X</b></li>";
            }
            Map map = new HashMap();
            map.put("num", count);
            map.put("result", true);
            map.put("img_html", img_html);
            writer = response.getWriter();
            writer.print(Json.toJson(map));
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.dataconservancy.packaging.tool.impl.AnnotationDrivenPackageStateSerializerTest.java

@Test
public void testMarshalEntireState() throws Exception {
    ByteArrayOutputStream sink = new ByteArrayOutputStream();
    underTest.setArchive(false);//from  w w  w.  j av a 2s  . c  o m
    underTest.serialize(state, sink);

    AtomicInteger verifiedStreamCount = new AtomicInteger(0);
    mockedMarshallerMap.entrySet().forEach(entry -> {
        StreamId streamId = entry.getKey();
        StreamMarshaller streamMarshaller = entry.getValue();

        try {
            switch (streamId) {

            case APPLICATION_VERSION:
                verify(streamMarshaller.getMarshaller()).marshal(eq(applicationVersion),
                        isNotNull(Result.class));
                verifiedStreamCount.incrementAndGet();
                break;

            case PACKAGE_NAME:
                verify(streamMarshaller.getMarshaller()).marshal(eq(packageName), isNotNull(Result.class));
                verifiedStreamCount.incrementAndGet();
                break;

            case PACKAGE_METADATA:
                verify(streamMarshaller.getMarshaller()).marshal(eq(packageMetadata), isNotNull(Result.class));
                verifiedStreamCount.incrementAndGet();
                break;

            case DOMAIN_PROFILE_LIST:
                verify(streamMarshaller.getMarshaller()).marshal(eq(domainProfileUris),
                        isNotNull(Result.class));
                verifiedStreamCount.incrementAndGet();
                break;

            case DOMAIN_OBJECTS:
                verify(streamMarshaller.getMarshaller()).marshal(eq(domainObjectsRDF), isNotNull(Result.class));
                verifiedStreamCount.incrementAndGet();
                break;

            case USER_SPECIFIED_PROPERTIES:
                verify(streamMarshaller.getMarshaller()).marshal(eq(userProperties), isNotNull(Result.class));
                verifiedStreamCount.incrementAndGet();
                break;

            case PACKAGE_TREE:
                verify(streamMarshaller.getMarshaller()).marshal(eq(packageTreeRDF), isNotNull(Result.class));
                verifiedStreamCount.incrementAndGet();
                break;
            }
        } catch (IOException e) {
            fail("Encountered IOE: " + e.getMessage());
        }
    });

    assertEquals(mockedMarshallerMap.size(), verifiedStreamCount.intValue());
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

private int processQueryResults(Map<String, Integer> uidStats, String[] fields, String[] qaFields,
        RecordWriter rw, QueryResponse qr, DownloadDetailsDTO dd, boolean checkLimit,
        AtomicInteger resultsCount) {
    int count = 0;
    for (SolrDocument sd : qr.getResults()) {
        if (sd.getFieldValue("data_resource_uid") != null
                && (!checkLimit || (checkLimit && resultsCount.intValue() < MAX_DOWNLOAD_SIZE))) {

            //resultsCount++;
            count++;// ww  w .j ava 2  s . c o  m
            synchronized (resultsCount) {
                resultsCount.incrementAndGet();
            }

            //add the record
            String[] values = new String[fields.length + qaFields.length];

            //get all the "single" values from the index
            for (int j = 0; j < fields.length; j++) {
                Object value = sd.getFirstValue(fields[j]);
                if (value instanceof Date)
                    values[j] = value == null ? ""
                            : org.apache.commons.lang.time.DateFormatUtils.format((Date) value, "yyyy-MM-dd");
                else
                    values[j] = value == null ? "" : value.toString();
            }

            //now handle the assertions
            java.util.Collection<Object> assertions = sd.getFieldValues("assertions");

            //Handle the case where there a no assertions against a record
            if (assertions == null) {
                assertions = Collections.EMPTY_LIST;
            }

            for (int k = 0; k < qaFields.length; k++) {
                values[fields.length + k] = Boolean.toString(assertions.contains(qaFields[k]));
            }

            rw.write(values);

            //increment the counters....
            incrementCount(uidStats, sd.getFieldValue("institution_uid"));
            incrementCount(uidStats, sd.getFieldValue("collection_uid"));
            incrementCount(uidStats, sd.getFieldValue("data_provider_uid"));
            incrementCount(uidStats, sd.getFieldValue("data_resource_uid"));
        }
    }
    dd.updateCounts(count);
    return count;
}

From source file:au.org.ala.biocache.dao.SearchDAOImpl.java

/**
 * Writes the index fields to the supplied output stream in CSV format.
 *
 * DM: refactored to split the query by month to improve performance.
 * Further enhancements possible:/*  w  ww .  j  a v a2s. co m*/
 * 1) Multi threaded
 * 2) More filtering, by year or decade..
 *
 * @param downloadParams
 * @param out
 * @param includeSensitive
 * @throws Exception
 */
public Map<String, Integer> writeResultsFromIndexToStream(final DownloadRequestParams downloadParams,
        OutputStream out, boolean includeSensitive, final DownloadDetailsDTO dd, boolean checkLimit)
        throws Exception {
    long start = System.currentTimeMillis();
    final Map<String, Integer> uidStats = new HashMap<String, Integer>();
    if (server == null) {
        initServer();
    }
    try {
        SolrQuery solrQuery = new SolrQuery();
        formatSearchQuery(downloadParams);

        String dFields = downloadParams.getFields();

        if (includeSensitive) {
            //include raw latitude and longitudes
            dFields = dFields
                    .replaceFirst("decimalLatitude.p",
                            "sensitive_latitude,sensitive_longitude,decimalLatitude.p")
                    .replaceFirst(",locality,", ",locality,sensitive_locality,");
        }

        StringBuilder sb = new StringBuilder(dFields);
        if (!downloadParams.getExtra().isEmpty()) {
            sb.append(",").append(downloadParams.getExtra());
        }

        String[] requestedFields = sb.toString().split(",");
        List<String>[] indexedFields = downloadFields.getIndexFields(requestedFields);
        logger.debug("Fields included in download: " + indexedFields[0]);
        logger.debug("Fields excluded from download: " + indexedFields[1]);
        logger.debug("The headers in downloads: " + indexedFields[2]);

        //set the fields to the ones that are available in the index
        final String[] fields = indexedFields[0].toArray(new String[] {});
        solrQuery.setFields(fields);
        StringBuilder qasb = new StringBuilder();
        if (!"none".equals(downloadParams.getQa())) {
            solrQuery.addField("assertions");
            if (!"all".equals(downloadParams.getQa())) {
                //add all the qa fields
                qasb.append(downloadParams.getQa());
            }
        }
        solrQuery.addField("institution_uid").addField("collection_uid").addField("data_resource_uid")
                .addField("data_provider_uid");

        //add context information
        updateQueryContext(downloadParams);
        solrQuery.setQuery(buildSpatialQueryString(downloadParams));
        solrQuery.setFacetMinCount(1);
        solrQuery.setFacetLimit(-1);

        //get the assertion facets to add them to the download fields
        boolean getAssertionsFromFacets = "all".equals(downloadParams.getQa());
        SolrQuery monthAssertionsQuery = getAssertionsFromFacets
                ? solrQuery.getCopy().addFacetField("month", "assertions")
                : solrQuery.getCopy().addFacetField("month");
        if (getAssertionsFromFacets) {
            //set the order for the facet to be based on the index - this will force the assertions to be returned in the same order each time
            //based on alphabetical sort.  The number of QA's may change between searches so we can't guarantee that the order won't change
            monthAssertionsQuery.add("f.assertions.facet.sort", "index");
        }
        QueryResponse facetQuery = runSolrQuery(monthAssertionsQuery, downloadParams.getFq(), 0, 0, "score",
                "asc");

        //set the totalrecords for the download details
        dd.setTotalRecords(facetQuery.getResults().getNumFound());
        if (checkLimit && dd.getTotalRecords() < MAX_DOWNLOAD_SIZE) {
            checkLimit = false;
        }

        //get the month facets to add them to the download fields get the assertion facets.
        List<Count> splitByFacet = null;

        for (FacetField facet : facetQuery.getFacetFields()) {
            if (facet.getName().equals("assertions") && facet.getValueCount() > 0) {
                for (FacetField.Count facetEntry : facet.getValues()) {
                    if (qasb.length() > 0)
                        qasb.append(",");
                    qasb.append(facetEntry.getName());
                }
            }
            if (facet.getName().equals("month") && facet.getValueCount() > 0) {
                splitByFacet = facet.getValues();
            }
        }

        String qas = qasb.toString();
        final String[] qaFields = qas.equals("") ? new String[] {} : qas.split(",");
        String[] qaTitles = downloadFields.getHeader(qaFields, false);

        String[] header = org.apache.commons.lang3.ArrayUtils.addAll(indexedFields[2].toArray(new String[] {}),
                qaTitles);

        //construct correct RecordWriter based on the supplied fileType
        final au.org.ala.biocache.RecordWriter rw = downloadParams.getFileType().equals("csv")
                ? new CSVRecordWriter(out, header, downloadParams.getSep(), downloadParams.getEsc())
                : new ShapeFileRecordWriter(downloadParams.getFile(), out,
                        (String[]) ArrayUtils.addAll(fields, qaFields));

        if (rw instanceof ShapeFileRecordWriter) {
            dd.setHeaderMap(((ShapeFileRecordWriter) rw).getHeaderMappings());
        }

        //order the query by _docid_ for faster paging
        solrQuery.addSortField("_docid_", ORDER.asc);

        //for each month create a separate query that pages through 500 records per page
        List<SolrQuery> queries = new ArrayList<SolrQuery>();
        if (splitByFacet != null) {
            for (Count facet : splitByFacet) {
                if (facet.getCount() > 0) {
                    SolrQuery splitByFacetQuery = solrQuery.getCopy()
                            .addFilterQuery(facet.getFacetField().getName() + ":" + facet.getName());
                    splitByFacetQuery.setFacet(false);
                    queries.add(splitByFacetQuery);
                }
            }
            SolrQuery remainderQuery = solrQuery.getCopy()
                    .addFilterQuery("-" + splitByFacet.get(0).getFacetField().getName() + ":[* TO *]");
            queries.add(0, remainderQuery);
        } else {
            queries.add(0, solrQuery);
        }

        //multi-thread the requests...
        ExecutorService pool = Executors.newFixedThreadPool(6);
        Set<Future<Integer>> futures = new HashSet<Future<Integer>>();
        final AtomicInteger resultsCount = new AtomicInteger(0);
        final boolean threadCheckLimit = checkLimit;

        //execute each query, writing the results to stream
        for (final SolrQuery splitByFacetQuery : queries) {
            //define a thread
            Callable<Integer> solrCallable = new Callable<Integer>() {

                int startIndex = 0;

                @Override
                public Integer call() throws Exception {
                    QueryResponse qr = runSolrQuery(splitByFacetQuery, downloadParams.getFq(),
                            downloadBatchSize, startIndex, "_docid_", "asc");
                    int recordsForThread = 0;
                    logger.debug(splitByFacetQuery.getQuery() + " - results: " + qr.getResults().size());

                    while (qr != null && !qr.getResults().isEmpty()) {
                        logger.debug("Start index: " + startIndex + ", " + splitByFacetQuery.getQuery());
                        int count = 0;
                        synchronized (rw) {
                            count = processQueryResults(uidStats, fields, qaFields, rw, qr, dd,
                                    threadCheckLimit, resultsCount);
                            recordsForThread += count;
                        }
                        startIndex += downloadBatchSize;
                        //we have already set the Filter query the first time the query was constructed rerun with he same params but different startIndex
                        if (!threadCheckLimit || resultsCount.intValue() < MAX_DOWNLOAD_SIZE) {
                            if (!threadCheckLimit) {
                                //throttle the download by sleeping
                                try {
                                    Thread.currentThread().sleep(throttle);
                                } catch (InterruptedException e) {
                                    //don't care if the sleep was interrupted
                                }
                            }
                            qr = runSolrQuery(splitByFacetQuery, null, downloadBatchSize, startIndex, "_docid_",
                                    "asc");
                        } else {
                            qr = null;
                        }
                    }
                    return recordsForThread;
                }
            };
            futures.add(pool.submit(solrCallable));
        }

        //check the futures until all have finished
        int totalDownload = 0;
        Set<Future<Integer>> completeFutures = new HashSet<Future<Integer>>();
        boolean allComplete = false;
        while (!allComplete) {
            for (Future future : futures) {
                if (!completeFutures.contains(future)) {
                    if (future.isDone()) {
                        totalDownload += (Integer) future.get();
                        completeFutures.add(future);
                    }
                }
            }
            allComplete = completeFutures.size() == futures.size();
            if (!allComplete) {
                Thread.sleep(1000);
            }
        }
        pool.shutdown();
        rw.finalise();
        out.flush();

        long finish = System.currentTimeMillis();
        long timeTakenInSecs = (finish - start) / 1000;
        if (timeTakenInSecs == 0)
            timeTakenInSecs = 1;
        logger.info("Download of " + resultsCount + " records in " + timeTakenInSecs + " seconds. Record/sec: "
                + resultsCount.intValue() / timeTakenInSecs);

    } catch (SolrServerException ex) {
        logger.error("Problem communicating with SOLR server while processing download. " + ex.getMessage(),
                ex);
    }
    return uidStats;
}