📄 cswmetadatadao.java
字号:
package com.esri.solutions.jitk.common.metadata.csw;
import com.esri.solutions.jitk.common.metadata.IMetadataDAO;
import com.esri.solutions.jitk.common.metadata.IMetadataDocument;
import com.esri.solutions.jitk.common.metadata.IMetadataID;
import com.esri.solutions.jitk.common.metadata.InvalidMetadataOperationException;
import com.esri.solutions.jitk.common.metadata.MetadataID;
import com.esri.solutions.jitk.datasources.ogc.csw.CswCatalog;
import com.esri.solutions.jitk.datasources.ogc.csw.CswProfile;
import com.esri.solutions.jitk.datasources.ogc.csw.CswRecord;
import com.esri.solutions.jitk.datasources.ogc.csw.CswSearchCriteria;
import com.esri.solutions.jitk.datasources.ogc.csw.CswSearchRequest;
import com.esri.solutions.jitk.datasources.ogc.csw.CswSearchResponse;
import com.esri.solutions.jitk.datasources.ogc.csw.Envelope;
import com.esri.solutions.jitk.datasources.ogc.csw.InvalidOperationException;
import com.esri.solutions.jitk.datasources.ogc.csw.NullReferenceException;
import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;
/**
* Concrete implementation of the {@link IMetadataDAO} interface. This implementation
* will access the metadata service via the OGC CS/W specification.
*/
public class CSWMetadataDAO implements IMetadataDAO {
/**
* Constant wildcard character used in the CS/W get records request.
*/
public static final String WILDCARD_CHARACTER = "%";
/**
* Key for extracting/storing the search term from the search criteria
* {@link Map}.
*/
public static final String SEARCH_TERM_KEY = "searchTerm";
/**
* Key for extracting/storing the number of max records returned from the
* search criteria {@link Map}.
*/
public static final String NUMBER_OF_RECORDS_KEY = "numRecords";
/**
* Key for extracting/storing the live data and/or maps value from the
* search criteria {@link Map}.
*/
public static final String LIVE_DATA_ONLY_KEY = "liveOnly";
/**
* Key for extracting/storing the min longitude value from the search
* criteria {@link Map}.
*/
public static final String EXTENT_MINX_KEY = "extent.minx";
/**
* Key for extracting/storing the min latitude value from the search
* criteria {@link Map}.
*/
public static final String EXTENT_MINY_KEY = "extent.miny";
/**
* Key for extracting/storing the max longitude value from the search
* criteria {@link Map}.
*/
public static final String EXTENT_MAXX_KEY = "extent.maxx";
/**
* Key for extracting/storing the max latitude value from the search
* criteria {@link Map}.
*/
public static final String EXTENT_MAXY_KEY = "extent.maxy";
/**
* Key for extracting/storing the type of geographic extent search
* from the search criteria {@link Map}.
*
* <p>
* Possible values are {@link CswSearchCriteria#GEOMETRY_SEARCH_ANYWHERE},
* {@link CswSearchCriteria#GEOMETRY_SEARCH_FULLY_WITHIN}, and
* {@link CswSearchCriteria#GEOMETRY_SEARCH_OVERLAPS}.
* </p>
*/
public static final String EXTENT_TYPE_KEY = "extent.type";
/**
* Key for extracting/storing the topic categories value from the search
* criteria {@link Map}.
*/
public static final String TOPIC_CATEGORIES_KEY = "topicCategories";
/**
* Constant value indicating a view all records request.
*/
public static final int VIEW_ALL_RECORDS = 999999;
/**
* {@link Logger} used to log messages for this class.
*/
private static final Logger _logger = Logger.getLogger(CSWMetadataDAO.class);
/**
* ID of the DAO.
*/
private String _id = null;
/**
* {@link Map} of configured properties.
*/
private Map<String, Object> _properties = null;
/**
* Reference to the {@link CSWMetadataCatalog}.
*/
private CSWMetadataCatalog _catalog = null;
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.metadata.IMetadataDAO#findDocuments(java.util.Map)
*/
public List<IMetadataDocument> findDocuments(Map<String, Object> criteria) {
CswCatalog cswCatalog = null;
CswSearchRequest searchRequest = null;
CswSearchResponse searchResponse = null;
String searchTerm = WILDCARD_CHARACTER;
int numRecords = VIEW_ALL_RECORDS;
boolean liveDataAndMapsOnly = Boolean.FALSE;
Iterator<CswRecord> iter = null;
List<IMetadataDocument> results = new ArrayList<IMetadataDocument>();
int geometrySearchType = CswSearchCriteria.GEOMETRY_SEARCH_ANYWHERE;
double minx;
double miny;
double maxx;
double maxy;
Envelope envelope = null;
if (criteria == null) {
throw new NullPointerException("search criteria cannot be null");
}
if (criteria.containsKey(SEARCH_TERM_KEY)) {
searchTerm = (String) criteria.get(SEARCH_TERM_KEY);
} else {
_logger.debug("Search term not found - searching using " +
WILDCARD_CHARACTER);
}
if (criteria.containsKey(NUMBER_OF_RECORDS_KEY)) {
numRecords = (Integer) criteria.get(NUMBER_OF_RECORDS_KEY);
} else {
_logger.debug("Max records count not found - returning all records");
}
if (criteria.containsKey(LIVE_DATA_ONLY_KEY)) {
liveDataAndMapsOnly = (Boolean) criteria.get(LIVE_DATA_ONLY_KEY);
} else {
_logger.debug(
"Live data and maps only indicator not found - defaulting to false");
}
if (criteria.containsKey(EXTENT_TYPE_KEY)) {
geometrySearchType = (Integer) criteria.get(EXTENT_TYPE_KEY);
} else {
_logger.debug(
"Geography search type not found - defaulting to full extent");
}
cswCatalog = createCswCatalog();
searchRequest = new CswSearchRequest(cswCatalog, searchTerm);
searchRequest.getCriteria().setMaxRecords(numRecords);
searchRequest.getCriteria().setLiveDataAndMapsOnly(liveDataAndMapsOnly);
if (geometrySearchType != CswSearchCriteria.GEOMETRY_SEARCH_ANYWHERE) {
if (criteria.containsKey(EXTENT_MINX_KEY) &&
criteria.containsKey(EXTENT_MINY_KEY) &&
criteria.containsKey(EXTENT_MAXX_KEY) &&
criteria.containsKey(EXTENT_MAXY_KEY)) {
minx = (Double) criteria.get(EXTENT_MINX_KEY);
miny = (Double) criteria.get(EXTENT_MINY_KEY);
maxx = (Double) criteria.get(EXTENT_MAXX_KEY);
maxy = (Double) criteria.get(EXTENT_MAXY_KEY);
envelope = new Envelope(minx, miny, maxx, maxy);
searchRequest.getCriteria()
.setGeometrySearchType(geometrySearchType);
searchRequest.getCriteria().setEnvelope(envelope);
} else {
_logger.debug(
"Missing extent values - defaulting to full extent search");
}
}
if (criteria.containsKey(TOPIC_CATEGORIES_KEY)) {
String[] topicCodes = ((String) criteria.get(TOPIC_CATEGORIES_KEY)).split(
",");
for (String code : topicCodes) {
searchRequest.getCriteria().addTopicCategory(code);
}
}
try {
searchRequest.search();
searchResponse = searchRequest.getCswSearchResponse();
iter = searchResponse.getRecords().iterator();
CswRecord record = null;
CSWMetadataDocument document = null;
MetadataID id = null;
while (iter.hasNext()) {
record = iter.next();
document = new CSWMetadataDocument();
id = new MetadataID();
id.setCatalog(_catalog);
id.setId(record.getId());
id.setDocument(document);
document.setRecord(record);
document.setMetadataProfile(_catalog.getMetadataProfile());
document.setMetadataID(id);
results.add(document);
}
} catch (IOException ex) {
_logger.warn("IOException occurred while searching catalog: [" +
_catalog.getName() + "]", ex);
} catch (TransformerException ex) {
_logger.warn(
"TransformerException occurred while searching catalog: [" +
_catalog.getName() + "]", ex);
} catch (XPathExpressionException ex) {
_logger.warn(
"XPathExpressionException occurred while searching catalog: [" +
_catalog.getName() + "]", ex);
} catch (SAXException ex) {
_logger.warn("SAXException occurred while searching catalog: [" +
_catalog.getName() + "]", ex);
} catch (NullReferenceException ex) {
_logger.warn(
"NullReferenceException occurred while searching catalog: [" +
_catalog.getName() + "]", ex);
} catch (ParserConfigurationException ex) {
_logger.warn(
"ParserConfigurationException occurred while searching catalog: [" +
_catalog.getName() + "]", ex);
}
return results;
}
/*
* (non-Javadoc)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -