Search in sources :

Example 1 with AreaReportItemDTO

use of au.org.ala.spatial.dto.AreaReportItemDTO in project spatial-portal by AtlasOfLivingAustralia.

the class AreaReportController method setUpModelMap.

/**
 * Set up the map for the model that is used to construct the area report table.
 *
 * @return an ordered map of hte maps that need to collect the sampled values
 */
private Map<String, AreaReportItemDTO> setUpModelMap(boolean isWorldSelected) {
    Map<String, AreaReportItemDTO> values = new LinkedHashMap<String, AreaReportItemDTO>();
    // String worldSuffix = isWorldSelected ? "*" : "";
    // area
    values.put(StringConstants.AREA, new AreaReportItemDTO("Area (sq km)"));
    // species
    values.put(StringConstants.SPECIES, new AreaReportItemDTO("Number of species"));
    // spatially valid species
    values.put(StringConstants.SPATIAL_SPECIES, new AreaReportItemDTO("Number of species - spatially valid only"));
    if (includeEndemic) {
        // endemic
        values.put(StringConstants.ENDEMIC_SPECIES, new AreaReportItemDTO("Number of endemic species"));
        values.put(StringConstants.SPATIAL_ENDEMIC_SPECIES, new AreaReportItemDTO("Number of endemic species - spatially valid only"));
    }
    // occurrences
    values.put(StringConstants.OCCURRENCES, new AreaReportItemDTO("Occurrences"));
    // spatially valid occurrences
    values.put(StringConstants.SPATIAL_OCCURRENCES, new AreaReportItemDTO("Occurrences - spatially valid only"));
    // expert distribution
    values.put(StringConstants.EXPERT_DISTRIBUTIONS, new AreaReportItemDTO("Expert distributions"));
    // checklist areas
    values.put(StringConstants.CHECKLIST_AREA, new AreaReportItemDTO(StringConstants.CHECKLIST_AREAS));
    // checklist species
    values.put(StringConstants.CHECKLIST_SPECIES, new AreaReportItemDTO("Checklist species"));
    // journalmap documents
    values.put(StringConstants.JOURNAL_MAP, new AreaReportItemDTO("Journalmap documents"));
    // gazetteer points
    values.put(StringConstants.GAZETTEER, new AreaReportItemDTO("Gazetteer points"));
    // points of interest
    if (CommonData.getDisplayPointsOfInterest()) {
        values.put("poi", new AreaReportItemDTO(StringConstants.POINTS_OF_INTEREST));
    }
    // configured facets
    for (int i = 0; i < CommonData.getAreaReportFacets().length; i++) {
        values.put(StringConstants.CONFIGURED_FACETS + i, new AreaReportItemDTO(""));
    }
    return values;
}
Also used : AreaReportItemDTO(au.org.ala.spatial.dto.AreaReportItemDTO)

Example 2 with AreaReportItemDTO

use of au.org.ala.spatial.dto.AreaReportItemDTO in project spatial-portal by AtlasOfLivingAustralia.

the class AreaReportController method facetCounts.

Map<String, Object> facetCounts(AreaReportItemDTO pmodel, String facet) {
    org.zkoss.util.Locales.setThreadLocal(null);
    Map<String, AreaReportItemDTO> countsData = new LinkedHashMap<String, AreaReportItemDTO>();
    LOGGER.debug("Starting to get facet counts for : " + facet);
    AreaReportItemDTO dto = pmodel;
    int colonIdx = facet.indexOf(':');
    String query = colonIdx > 0 ? facet : facet + ":*";
    Query sq = QueryUtil.queryFromSelectedArea(null, selectedArea, query, false, new boolean[] { true, true, false });
    sq = sq.newFacet(new Facet("occurrence_status_s", "absent", false), false);
    int count = sq.getSpeciesCount();
    if (count == -1)
        count = 0;
    String label = Labels.getLabel("facet." + facet, facet);
    // title
    dto.setTitle(label);
    // count
    dto.setCount(String.format("%,d", count));
    // check to see if it is a species list
    if (facet.equals(CommonData.speciesListThreatened)) {
        dto.setTitle("Threatened Species");
    } else if (facet.equals(CommonData.speciesListInvasive)) {
        dto.setTitle("Invasive Species");
    } else if (facet.startsWith("species_list") && colonIdx > 0) {
        // extract everything to the right of the colon and construct the url
        String dataResourceUid = facet.substring(colonIdx + 1);
        String title = SpeciesListUtil.getSpeciesListMap().get(dataResourceUid);
        if (title != null) {
            dto.setTitle(title);
        }
        dto.addUrlDetails("Full List", CommonData.getSpeciesListServer() + "/speciesListItem/list/" + dataResourceUid);
    } else if (facet.startsWith("species_group")) {
        dto.setTitle(facet.substring(colonIdx + 1));
    }
    // url
    if (count > 0) {
        dto.addUrlDetails(VIEW_RECORDS, CommonData.getBiocacheWebServer() + "/occurrences/search?q=" + sq.getQ() + (StringUtils.isNotEmpty(sq.getQc()) ? sq.getQc() : ""));
        dto.setExtraParams(query);
        dto.setExtraInfo(new ExtraInfoEnum[] { ExtraInfoEnum.LIST, ExtraInfoEnum.MAP_ALL });
        dto.setListType(ListType.SPECIES);
    }
    countsData.put(facet, dto);
    LOGGER.debug("Facet Counts ::: " + countsData);
    // this data needs to be added to the model on the correct thread...
    return (Map) countsData;
}
Also used : AreaReportItemDTO(au.org.ala.spatial.dto.AreaReportItemDTO) Facet(au.org.ala.legend.Facet)

Example 3 with AreaReportItemDTO

use of au.org.ala.spatial.dto.AreaReportItemDTO in project spatial-portal by AtlasOfLivingAustralia.

the class AreaReportController method onClick$btnDownload.

public void onClick$btnDownload(Event event) {
    String spid = pid;
    if (spid == null || StringConstants.NONE.equals(spid)) {
        spid = String.valueOf(System.currentTimeMillis());
    }
    SimpleDateFormat date = new SimpleDateFormat(StringConstants.DATE);
    String sdate = date.format(new Date());
    StringBuilder sb = new StringBuilder();
    // area name
    sb.append("Area: " + areaDisplayName);
    for (Map.Entry<String, AreaReportItemDTO> i : reportModelMap.entrySet()) {
        sb.append("\n\"").append(i.getValue().getTitle()).append("\",\"").append(i.getValue().getCount()).append("\"");
    }
    Filedownload.save(sb.toString(), StringConstants.TEXT_PLAIN, "Area_report_" + sdate + "_" + spid + ".csv");
}
Also used : AreaReportItemDTO(au.org.ala.spatial.dto.AreaReportItemDTO) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with AreaReportItemDTO

use of au.org.ala.spatial.dto.AreaReportItemDTO in project spatial-portal by AtlasOfLivingAustralia.

the class AreaReportController method checkFutures.

public void checkFutures() {
    try {
        LOGGER.debug("Check futures.....");
        int cancelled = 0;
        for (Map.Entry<String, Future<Map<String, String>>> futureEntry : futures.entrySet()) {
            String eventToFire = "render" + futureEntry.getKey();
            if (futureEntry.getValue().isDone() && !firedEvents.contains(eventToFire)) {
                firedEvents.add(eventToFire);
                // inform the list model to update
                areaReportListModel.setModelChanged();
            }
            // kill anything taking longer than 5 mins
            if (!futureEntry.getValue().isDone() && (System.currentTimeMillis() - futuresStart) > 300000) {
                futureEntry.getValue().cancel(true);
                // now set everything not completed to "Request timed out"
                for (AreaReportItemDTO model : (List<AreaReportItemDTO>) areaReportListModel.getInnerList()) {
                    if (model.isLoading()) {
                        model.setCount("Request timed out");
                    }
                }
                cancelled++;
                areaReportListModel.setModelChanged();
            }
        }
        LOGGER.debug("Fired events: " + firedEvents.size() + ", Futures: " + futures.size() + ", Cancelled: " + cancelled);
        boolean allComplete = firedEvents.size() + cancelled == futures.size();
        if (!allComplete) {
            Thread.sleep(1000);
            Events.echoEvent(StringConstants.CHECK_FUTURES, this, null);
        } else {
            LOGGER.debug("All futures completed.");
            this.pool.shutdown();
            futures = null;
        }
    } catch (InterruptedException e) {
        LOGGER.error("", e);
    }
}
Also used : Future(java.util.concurrent.Future) AreaReportItemDTO(au.org.ala.spatial.dto.AreaReportItemDTO)

Example 5 with AreaReportItemDTO

use of au.org.ala.spatial.dto.AreaReportItemDTO in project spatial-portal by AtlasOfLivingAustralia.

the class AreaReportController method startQueries.

void startQueries() {
    final boolean worldAreaSelected = CommonData.WORLD_WKT.equals(selectedArea.getFacets() == null ? selectedArea.getWkt() : null);
    reportModelMap = setUpModelMap(worldAreaSelected);
    areaReportListModel = new ChangableSimpleListModel(new ArrayList(reportModelMap.values()));
    facetsValues.setModel(areaReportListModel);
    // Set the renderer that is responsible for the pretties and associating actions to the buttons
    facetsValues.setRowRenderer(new RowRenderer() {

        @Override
        public void render(Row row, Object data, int itemIdx) throws Exception {
            // data should be a map of facet result information
            if (data instanceof AreaReportItemDTO) {
                final AreaReportItemDTO dto = (AreaReportItemDTO) data;
                row.appendChild(new Label(dto.getTitle()));
                row.appendChild(new Label(dto.getCount()));
                // check for the buttons to display
                Div listDiv = new Div();
                Div mapDiv = new Div();
                Div sampleDiv = new Div();
                row.appendChild(listDiv);
                row.appendChild(mapDiv);
                row.appendChild(sampleDiv);
                listDiv.setZclass("areaReportListCol");
                mapDiv.setZclass("areaReportMapCol");
                sampleDiv.setZclass("areaReportSampleCol");
                Button b;
                if (dto.getExtraInfo() != null) {
                    final boolean[] gk = dto.isGeospatialKosher() ? new boolean[] { true, false, false } : new boolean[] { true, true, false };
                    final boolean kosher = dto.isGeospatialKosher();
                    for (ExtraInfoEnum type : dto.getExtraInfo()) {
                        switch(type) {
                            case LIST:
                                b = new Button("List");
                                b.setZclass(StringConstants.BTN_BTN_MINI);
                                b.addEventListener(StringConstants.ONCLICK, new EventListener() {

                                    @Override
                                    public void onEvent(Event event) throws Exception {
                                        if (dto.getListType() == ListType.SPECIES) {
                                            new SpeciesListEvent(areaName, gk, dto.isEndemic(), dto.getExtraParams()).onEvent(event);
                                        } else if (dto.getListType() == ListType.DISTRIBUTION) {
                                            listDistributions(dto);
                                        } else if (dto.getListType() == ListType.AREA_CHECKLIST) {
                                            listAreaChecklists(dto);
                                        } else if (dto.getListType() == ListType.SPECIES_CHECKLIST) {
                                            listSpeciesChecklists(dto);
                                        } else if (dto.getListType() == ListType.JOURNAL_MAP) {
                                            listJournalmap();
                                        }
                                    }
                                });
                                listDiv.appendChild(b);
                                break;
                            case SAMPLE:
                                b = new Button("Sample");
                                b.setZclass(StringConstants.BTN_BTN_MINI);
                                final SamplingEvent sle = new SamplingEvent(null, areaName, null, gk);
                                b.addEventListener(StringConstants.ONCLICK, new EventListener() {

                                    @Override
                                    public void onEvent(Event event) throws Exception {
                                        sle.onEvent(new ForwardEvent("", getMapComposer(), null));
                                    }
                                });
                                sampleDiv.appendChild(b);
                                break;
                            case MAP_ALL:
                                // set up the map button
                                b = new Button("Map all");
                                b.setZclass(StringConstants.BTN_BTN_MINI);
                                b.addEventListener(StringConstants.ONCLICK, new EventListener() {

                                    @Override
                                    public void onEvent(Event event) throws Exception {
                                        if (dto.getTitle().contains("Gazetteer")) {
                                            mapGazetteer();
                                        } else if (StringConstants.POINTS_OF_INTEREST.equals(dto.getTitle())) {
                                            onMapPointsOfInterest(event);
                                        } else if (kosher) {
                                            onMapSpeciesKosher(new Event("", null, dto.getExtraParams()));
                                        } else {
                                            onMapSpecies(new Event("", null, dto.getExtraParams()));
                                        }
                                    }
                                });
                                mapDiv.appendChild(b);
                                break;
                            default:
                                LOGGER.error("invalid type for AreaReportController: " + type);
                        }
                    }
                }
                if (dto.getUrlDetails() != null) {
                    Vlayout vlayout = new Vlayout();
                    for (Map.Entry<String, String> entry : dto.getUrlDetails().entrySet()) {
                        String urlTitle = entry.getKey();
                        org.zkoss.zul.A viewRecords = new org.zkoss.zul.A(urlTitle);
                        String url = entry.getValue();
                        if (url.startsWith("http")) {
                            viewRecords.setHref(url);
                            viewRecords.setTarget(StringConstants.BLANK);
                        } else {
                            final String helpUrl = CommonData.getSettings().get("help_url") + "/spatial-portal-help/" + url;
                            viewRecords.addEventListener(StringConstants.ONCLICK, new EventListener() {

                                @Override
                                public void onEvent(Event event) throws Exception {
                                    Events.echoEvent(StringConstants.OPEN_URL, getMapComposer(), helpUrl);
                                }
                            });
                        }
                        vlayout.appendChild(viewRecords);
                    }
                    row.appendChild(vlayout);
                } else {
                    row.appendChild(new Label(""));
                }
            }
        }
    });
    divWorldNote.setVisible(worldAreaSelected);
    Callable occurrenceCount = new Callable<Map<String, Object>>() {

        @Override
        public Map<String, Object> call() {
            return occurrenceCount(worldAreaSelected, reportModelMap.get(StringConstants.OCCURRENCES));
        }
    };
    Callable occurrenceCountKosher = new Callable<Map<String, Object>>() {

        @Override
        public Map<String, Object> call() {
            return occurrenceCountKosher(worldAreaSelected, reportModelMap.get(StringConstants.SPATIAL_OCCURRENCES));
        }
    };
    Callable speciesCount = new Callable<Map<String, Integer>>() {

        @Override
        public Map<String, Integer> call() {
            return speciesCount(worldAreaSelected, reportModelMap.get(StringConstants.SPECIES));
        }
    };
    Callable speciesCountKosher = new Callable<Map<String, Integer>>() {

        @Override
        public Map<String, Integer> call() {
            return speciesCountKosher(worldAreaSelected, reportModelMap.get(StringConstants.SPATIAL_SPECIES));
        }
    };
    Callable endemismCount = new Callable<Map<String, Integer>>() {

        @Override
        public Map<String, Integer> call() {
            return endemismCount(worldAreaSelected, reportModelMap.get(StringConstants.ENDEMIC_SPECIES));
        }
    };
    Callable endemismCountKosher = new Callable<Map<String, Integer>>() {

        @Override
        public Map<String, Integer> call() {
            return endemismCountKosher(worldAreaSelected, reportModelMap.get(StringConstants.SPATIAL_ENDEMIC_SPECIES));
        }
    };
    Callable speciesDistributions = new Callable<Map<String, Integer>>() {

        @Override
        public Map<String, Integer> call() {
            return intersectWithSpeciesDistributions(reportModelMap.get(StringConstants.EXPERT_DISTRIBUTIONS));
        }
    };
    Callable calculatedArea = new Callable<Map<String, String>>() {

        @Override
        public Map<String, String> call() {
            return calculateArea(reportModelMap.get(StringConstants.AREA));
        }
    };
    Callable speciesChecklists = new Callable<Map<String, String>>() {

        @Override
        public Map<String, String> call() {
            return intersectWithSpeciesChecklists(reportModelMap.get(StringConstants.CHECKLIST_AREA), reportModelMap.get(StringConstants.CHECKLIST_SPECIES));
        }
    };
    Callable gazPointsC = new Callable<Map<String, String>>() {

        @Override
        public Map<String, String> call() {
            return countGazPoints(reportModelMap.get(StringConstants.GAZETTEER));
        }
    };
    Callable journalmap = new Callable<Map<String, String>>() {

        @Override
        public Map<String, String> call() {
            return journalmap(reportModelMap.get(StringConstants.JOURNAL_MAP));
        }
    };
    Callable pointsOfInterestC = new Callable<Map<String, String>>() {

        @Override
        public Map<String, String> call() {
            return countPointsOfInterest(reportModelMap.get("poi"));
        }
    };
    Callable[] areaFacets = new Callable[CommonData.getAreaReportFacets().length];
    for (int i = 0; i < CommonData.getAreaReportFacets().length; i++) {
        final String facet = CommonData.getAreaReportFacets()[i];
        final String s = String.valueOf(i);
        areaFacets[i] = new Callable<Map<String, Object>>() {

            @Override
            public Map<String, Object> call() {
                return facetCounts(reportModelMap.get(StringConstants.CONFIGURED_FACETS + s), facet);
            }
        };
    }
    try {
        this.pool = Executors.newFixedThreadPool(50);
        this.futures = new HashMap<String, Future<Map<String, String>>>();
        this.firedEvents = new ArrayList<String>();
        // add all futures
        futures.put("CalculatedArea", pool.submit(calculatedArea));
        futures.put("OccurrenceCount", pool.submit(occurrenceCount));
        futures.put("OccurrenceCountKosher", pool.submit(occurrenceCountKosher));
        for (int i = 0; i < areaFacets.length; i++) {
            futures.put("AreaFacetCounts" + i, pool.submit(areaFacets[i]));
        }
        futures.put("SpeciesCount", pool.submit(speciesCount));
        futures.put("SpeciesCountKosher", pool.submit(speciesCountKosher));
        futures.put("GazPoints", pool.submit(gazPointsC));
        futures.put("SpeciesChecklists", pool.submit(speciesChecklists));
        futures.put("SpeciesDistributions", pool.submit(speciesDistributions));
        futures.put("Journalmap", pool.submit(journalmap));
        if (CommonData.getDisplayPointsOfInterest()) {
            futures.put("PointsOfInterest", pool.submit(pointsOfInterestC));
        }
        if (includeEndemic) {
            futures.put("EndemicCount", pool.submit(endemismCount));
            futures.put("EndemicCountKosher", pool.submit(endemismCountKosher));
        }
        futuresStart = System.currentTimeMillis();
    } catch (Exception e) {
        LOGGER.error("error setting counts for futures", e);
    }
}
Also used : AreaReportItemDTO(au.org.ala.spatial.dto.AreaReportItemDTO) Callable(java.util.concurrent.Callable) org.zkoss.zul(org.zkoss.zul) ExtraInfoEnum(au.org.ala.spatial.dto.AreaReportItemDTO.ExtraInfoEnum) EventListener(org.zkoss.zk.ui.event.EventListener) SamplingEvent(au.org.ala.spatial.composer.quicklinks.SamplingEvent) ForwardEvent(org.zkoss.zk.ui.event.ForwardEvent) Event(org.zkoss.zk.ui.event.Event) ListDataEvent(javax.swing.event.ListDataEvent) ForwardEvent(org.zkoss.zk.ui.event.ForwardEvent) SpeciesListEvent(au.org.ala.spatial.composer.quicklinks.SpeciesListEvent) SamplingEvent(au.org.ala.spatial.composer.quicklinks.SamplingEvent) SpeciesListEvent(au.org.ala.spatial.composer.quicklinks.SpeciesListEvent) Future(java.util.concurrent.Future) JSONObject(org.json.simple.JSONObject)

Aggregations

AreaReportItemDTO (au.org.ala.spatial.dto.AreaReportItemDTO)5 Future (java.util.concurrent.Future)2 Facet (au.org.ala.legend.Facet)1 SamplingEvent (au.org.ala.spatial.composer.quicklinks.SamplingEvent)1 SpeciesListEvent (au.org.ala.spatial.composer.quicklinks.SpeciesListEvent)1 ExtraInfoEnum (au.org.ala.spatial.dto.AreaReportItemDTO.ExtraInfoEnum)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Callable (java.util.concurrent.Callable)1 ListDataEvent (javax.swing.event.ListDataEvent)1 JSONObject (org.json.simple.JSONObject)1 Event (org.zkoss.zk.ui.event.Event)1 EventListener (org.zkoss.zk.ui.event.EventListener)1 ForwardEvent (org.zkoss.zk.ui.event.ForwardEvent)1 org.zkoss.zul (org.zkoss.zul)1