⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 placesearchtask.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
字号:
package com.esri.solutions.jitk.web.tasks.search.place;

import com.esri.adf.web.data.WebApplication;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.results.WebResults;
import com.esri.adf.web.faces.event.TaskEvent;

import com.esri.solutions.jitk.common.gazetteer.GazetteerManagerFactoryBean;
import com.esri.solutions.jitk.common.gazetteer.GazetteerSearchResult;
import com.esri.solutions.jitk.common.gazetteer.IGazetteerManager;
import com.esri.solutions.jitk.common.gazetteer.IGazetteerService;
import com.esri.solutions.jitk.common.gazetteer.IGazetteerService.SearchMethod;
import com.esri.solutions.jitk.common.resources.TextResources;
import com.esri.solutions.jitk.web.tasks.RenderControlledTask;

import org.apache.log4j.Logger;

import java.text.MessageFormat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.TreeMap;


/**
 * This class represents a Java ADF Task for searching Gazetteer
 * services.  Extends {@link RenderControlledTask} for controlling
 * task rendering.
 */
public class PlaceSearchTask extends RenderControlledTask {
    /**
     * Serialization version ID.
     */
    private static final long serialVersionUID = -1928635711924611180L;
    
    /**
     * ID of 'Contains' radio button
     */
    private static final Integer SEARCH_METHOD_CONTAINS = 0;
    
    /**
     * ID of 'Begins with' radio button
     */
    private static final Integer SEARCH_METHOD_BEGINS_WITH = 1;
   

    /**
     * {@link Logger} used to log messages for this class.
     */
    private static final Logger _logger = Logger.getLogger(PlaceSearchTask.class);

    /**
     * Search text field.
     */
    private String m_searchTerm = null;

    /**
     * {@link Map} of configured Gazetteer services.
     */
    private Map<String, String> m_availableServices = null;

    /**
     * Selected Gazetteer service.
     */
    private String m_selectService = null;

    /**
     * Reference to the Java ADF {@link TaskInfo} object.
     */
    private PlaceSearchTaskInfo m_taskInfo = null;

    /**
     * Factory class for creating {@link IGazetteerService}.
     */
    private GazetteerManagerFactoryBean m_factoryBean = null;

    /**
     * {@link Map} of Integers that are used to populate a maximum
     * result drop down box.
     */
    private Map<Integer, String> m_maximumList = null;

    /**
     * Maximum number of results returned from a Gazetteer search.
     */
    private String m_maxRecordCount = null;

    /**
     * Set a text string for the footer note
     */
    private String footerNote = null;
    
    /**
     * Radio buttons for Search
     */
    private Map<Integer, String> m_searchMethods = null;
    
    /**
     * Current radio button selected for search method
     */
    private Integer m_searchMethod = 0;
    
    private SearchMethod searchMethod = SearchMethod.CONTAINS;
 
    /**
     * Default no-args constructor that initializes the ADF task
     * fields.
     */

    public PlaceSearchTask() {
        super();
        m_availableServices = new HashMap<String, String>();
        m_maximumList = new TreeMap<Integer, String>();
        
        // initialize the radio button choices for searching
        m_searchMethods = new LinkedHashMap<Integer, String>();
        m_searchMethods.put(SEARCH_METHOD_CONTAINS, 
         TextResources.getResourceString(ResourceProps.BUTTON_CONTAINS));
        m_searchMethods.put(SEARCH_METHOD_BEGINS_WITH, 
         TextResources.getResourceString(ResourceProps.BUTTON_BEGINS_WITH));

        m_taskInfo = new PlaceSearchTaskInfo();
        super.setTaskInfo(m_taskInfo);

        generateMaxRecordCountList();

        m_maxRecordCount = TextResources.getResourceString(ResourceProps.VAR_MAX_RECORD_COUNT);
    }
    
    /**
     * Gets the radio buttons used for searching 'Contains' 'Begins with'
     * @return
     */
    public Map<Integer, String> getSearchMethods() {
        return m_searchMethods;
    }
    
    /**
     * Get the currently selected Search method
     * @return {@link int} Search method.
     */
    public Integer getSearchMethod() {
        return m_searchMethod;
    }

    /**
     * Set the Search method
     * @param method
     */
    public void setSearchMethod(Integer method) {
    	m_searchMethod = method;
    	
    	if (method == 0)
    		searchMethod = SearchMethod.CONTAINS;
    	else if (method ==1)
    		searchMethod = SearchMethod.BEGINS_WITH;
    }


    /**
     * Gets a reference to the {@link GazetteerTaskInfo} object for this
     * task.
     *
     * @return {@link GazetteerTaskInfo} object.
     */
    public PlaceSearchTaskInfo getTaskInfo() {
        return m_taskInfo;
    }

    /**
     * Gets the Gazetteer search term used to search the Gazetteer
     * service.
     *
     * @return {@link String} place name.
     */
    public String getSearchTerm() {
        return m_searchTerm;
    }

    /**
     * Sets the Gazetteer search term/place name.
     *
     * @param term Place name search text.
     */
    public void setSearchTerm(String term) {
        m_searchTerm = term;
    }

    /**
     * Gets a {@link Map} of all configured Gazetteer services.
     *
     * @return {@link Map} of configured Gazetteer services.
     */
    public Map<String, String> getAvailableServices() {
        m_availableServices.clear();

        for (IGazetteerService gs : m_factoryBean.getFactory()
                                                 .getGazetteerManager()
                                                 .getServices()) {
            m_availableServices.put(gs.getName(), gs.getName());
        }

        return m_availableServices;
    }

    /**
     * Gets the selected Gazetteer service that will be searched against.
     *
     * @return {@link String} ID of the selected Gazetteer service.
     */
    public String getSelectService() {
        return m_selectService;
    }

    /**
     * Sets the selected Gazetteer service that will be searched against.
     *
     * @param service {@link String} ID of the Gazetteer service.
     */
    public void setSelectService(String service) {
        m_selectService = service;
    }

    /**
     * Sets a reference to the {@link GazetteerManagerFactoryBean} that is used to
     * get a reference to the {@link IGazetteerManagerFactory}.
     *
     * @param bean {@link GazetteerManagerFactoryBean} reference.
     */
    public void setGazetteerManagerFactoryBean(GazetteerManagerFactoryBean bean) {
        m_factoryBean = bean;
    }

    /**
     * Gets the Maximum number of results that will be returned with
     * a Gazetteer search.
     *
     * @return {@link String} representation of the max number of results.
     */
    public String getMaxRecordCount() {
        return m_maxRecordCount;
    }

    /**
     * Sets the Maximum number of results that will be returned with
     * a Gazetteer search.
     *
     * @param num {@link String} representation of the max number of results.
     */
    public void setMaxRecordCount(String num) {
        m_maxRecordCount = num;
    }

    /**
     * Gets the {@link Map} of max result options for populating a
     * drop-down box.
     *
     * @return {@link Map} containing the options for max results.
     */
    public Map<Integer, String> getMaxRecordCountList() {
        return m_maximumList;
    }

    /**
     * Java ADF event handler called when the user clicks the 'search' button
     * from the task.  Searches the selected Gazetteer service with the given
     * search text.  This method will delegate to {@link #performGazetteerSearch(TaskEvent)}.
     *
     * @param event Java ADF {@link TaskEvent}.
     */
    public void search(TaskEvent event) {
        try {
            requestTaskRender();
            performGazetteerSearch(event);
        } catch (Exception ex) {
            _logger.error("Could not perform gazetteer search", ex);
            renderExceptionMessage(ex);
        }
    }

    /**
     * Performs a search on the selected {@link IGazetteerService} and adds
     * the results to the {@link WebResults} object.
     *
     * @param event Java ADF {@link TaskEvent}.
     */
    private void performGazetteerSearch(TaskEvent event) {
        IGazetteerManager manager = null;
        ArrayList<GazetteerSearchResult> results = null;
        WebContext context = null;
        Map<String, String> actions = null;
        WebResults wResults = null;
        IGazetteerService service = null;
        int numResults;

        // inform task renderer to render task
        m_taskInfo.getTaskDescriptor().setShouldRender(Boolean.TRUE);

        context = event.getWebContext();

        manager = m_factoryBean.getFactory().getGazetteerManager();

        service = manager.getService(getSelectService());

        try {
            numResults = Integer.parseInt(m_maximumList.get(Integer.parseInt(
                            getMaxRecordCount())));
        } catch (NumberFormatException ex) {
            _logger.warn(
                "Error parsing max number of records - defaulting to view all");

            numResults = 500;
        }

        results = service.search(getSearchTerm(), numResults, searchMethod);

        if ((results == null) || (results.size() == 0)) {
            _logger.info("No hits found on service '" + getSelectService() +
                "' for term '" + getSearchTerm() + "'");

            String pattern = TextResources.getResourceString(
                    "jitk.task.placeSearch.message.noResults");
            String msg = MessageFormat.format(pattern, getSearchTerm());

            renderMessage(msg);

            return;
        }

        // clear graphics for new search
        context.getWebQuery().clearGraphics();

        for (GazetteerSearchResult gsr : results) {
            gsr.setWebContext(context);

            gsr.highlight();
        }

        actions = new LinkedHashMap<String, String>();
        actions.put(WebApplication.getResourceString(
                "SearchAttributesTask.SearchResultContextMenuZoom"), "zoomTo");
        actions.put(WebApplication.getResourceString(
                "SearchAttributesTask.SearchResultContextMenuHighLight"),
            "highlight");
        actions.put(WebApplication.getResourceString(
                "SearchAttributesTask.SearchResultContextMenuClearHighLight"),
            "clearGraphic");

        wResults = context.getWebResults();

        wResults.addResultsWithActionMap("Gazetteer: " + getSelectService(),
            results, "getName", "getDetails", actions);
    }

    /**
     * Utility method to generate the {@link Map} of max results from
     * a property file containing the key with the values.
     */
    private void generateMaxRecordCountList() {
        String value = null;
        String[] values = null;

        try {
            value = TextResources.getResourceString(ResourceProps.VAR_RESULT_LIST);

            values = value.split(",");

            for (int i = 0; i < values.length; i++) {
                m_maximumList.put(i, values[i]);
            }
        } catch (MissingResourceException ex) {
            _logger.warn("Missing resource value for key: [" + ex.getKey() +
                "].  Unable to generate maximum result drop down list.", ex);
        }
    }

    /**
     * Get footer note
     */
    public String getFooterNote() {
        return this.footerNote;
    }

    /**
     * Set footer note
     */
    public void setFooterNote(String footerNote) {
        this.footerNote = footerNote;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -