📄 cswpersistenceservice.java
字号:
getExtendedAttributeValue(query, CswPersistenceObject.UTILITIES_KEY));
obj.setAttribute(CswPersistenceObject.QUERY_OVERLAP_EXTENT_KEY,
getExtendedAttributeValue(query,
CswPersistenceObject.QUERY_OVERLAP_EXTENT_KEY));
return obj;
}
/**
* Utility method to retrieve a {@link List} of {@link IQuery} objects.
* This is done by making a call to {@link IPersonalizationContext}.
*
* @return {@link List} of {@link IQuery} objects retrieved from the data store.
* @throws PersonalizationException Thrown if there was an exception with the
* {@link IPersonalizationContext}.
* @throws IllegalStateException Thrown if the {@link IPersonalizationContext} has not
* been set.
*/
private List<IQuery> getSavedQueries() throws PersonalizationException {
List<IQuery> queries = new ArrayList<IQuery>();
if (_personalizationContext == null) {
throw new IllegalStateException("personalization context not set");
}
for (IQuery q : _personalizationContext.getData().getQueries()) {
if (q.getType().equals(IQuery.Type.CATALOG)) {
queries.add(q);
}
}
return queries;
}
/**
* Utility method to extract an attribute value out of the given
* {@link IQuery} object using the given <code>key</code>.
*
* @param query The {@link IQuery} from which the attribute value will
* be extracted.
* @param key Key name of the attribute.
* @return {@link String} value of the attribute.
* @throws PersonalizationException Thrown if there was an exception with the
* {@link IPersonalizationContext}.
* @throws NullPointerException Thrown if the <code>query</code> or <code>key</code>
* arguments are <code>null</code>.
*/
@SuppressWarnings("unchecked")
private String getExtendedAttributeValue(IQuery query, String key)
throws PersonalizationException {
String value = null;
Iterator iter = null;
AttributeType attr = null;
if (query == null) {
throw new NullPointerException("query cannot be null");
}
if (key == null) {
throw new NullPointerException("key cannot be null");
}
iter = query.getQuery().getExtendedAttributes().getAttribute().iterator();
while (iter.hasNext()) {
attr = (AttributeType) iter.next();
if (attr.getName().equals(key)) {
value = attr.getValue();
break;
}
}
return value;
}
/**
* Returns a {@link IQuery} object for the given {@link CswPersistenceObject}.
*
* <p>
* First, all stored CS/W queries will be retrieved and then iterated over to see
* if the given {@link CswPersistenceObject} already exists, based on the query name.
* If it already exists, a reference to it will be returned. If it doesn't exists,
* a new {@link IQuery} with the appropriate attributes set will be returned.
* </p>
*
* @param obj Reference to the {@link CswPersistenceObject} representation of the CS/W query.
* @return {@link IQuery} object retrieved from the given {@link CswPersistenceObject}.
* @throws PersonalizationException Thrown if there was an exception with the
* {@link IPersonalizationContext}.
* @throws NullPointerException Thrown if the <code>obj</code> argument is <code>null</code>.
* @throws IllegalStateException Thrown if the {@link IPersonalizationContext} has not been set.
*/
@SuppressWarnings("unchecked")
private IQuery getQuery(CswPersistenceObject obj)
throws PersonalizationException {
IQuery iquery = null;
CatalogQueryType catalogQuery = null;
CatalogServiceType catalogService = null;
AttributeCollectionType attr = null;
ArrayList<AttributeType> persistenceAttr = null;
SpatialFilter spatialFilter = null;
ObjectFactory factory = null;
List<IQuery> queries = null;
if (obj == null) {
throw new NullPointerException("persistence object cannot be null");
}
if (_personalizationContext == null) {
throw new IllegalStateException("Personalization context not set");
}
queries = getSavedQueries();
for (IQuery q : queries) {
if (q.getName()
.equals(obj.getAttribute(
CswPersistenceObject.QUERY_NAME_KEY))) {
iquery = q;
LOG.debug("Updating query - ID: [" + q.getId() + "] Name: [" +
q.getName() + "]");
break;
}
}
// if the query was not found, create a new one
if (iquery == null) {
iquery = _personalizationContext.getData()
.createQuery(IQuery.Type.CATALOG);
}
catalogQuery = (CatalogQueryType) iquery.getQuery();
// set the query name and description
catalogQuery.setName(obj.getAttribute(
CswPersistenceObject.QUERY_NAME_KEY));
catalogQuery.setDescription(obj.getAttribute(
CswPersistenceObject.QUERY_DESCRIPTION_KEY));
catalogQuery.setMaxResults(Integer.parseInt(obj.getAttribute(
CswPersistenceObject.MAX_RESULTS_KEY)));
try {
// create the object factory
factory = new ObjectFactory();
// add the extended search attributes
attr = factory.createAttributeCollectionType();
persistenceAttr = generatePersistenceAttributes(obj);
attr.getAttribute().addAll(persistenceAttr);
catalogQuery.setExtendedAttributes(attr);
// set the catalog service
catalogService = factory.createCatalogServiceType();
catalogService.setId(obj.getAttribute(
CswPersistenceObject.CATALOG_ID_KEY));
catalogQuery.setCatalogService(catalogService);
// set the query filter
spatialFilter = generateSpatialFilter(obj);
catalogQuery.setSpatialFilter(spatialFilter);
} catch (JAXBException ex) {
LOG.warn("JAXBException occurred creating IQuery", ex);
iquery = null;
}
return iquery;
}
/**
* Utility method to convert the serialized XML representation of a CS/W
* query to a {@link QueryType} via JAXB.
*
* @param xml {@link String} serialized version of the query.
* @return {@link QueryType} object resulting in the JAXB unmarshall operation.
* @throws PersonalizationException Thrown if there was a serialization problem.
* @throws NullPointerException Thrown if the <code>xml</code> argument is
* <code>null</code>.
*/
private QueryType unmarshallQuery(String xml)
throws PersonalizationException {
JAXBContext ctx = null;
Unmarshaller unmarshaller = null;
QueryType queryType = null;
if (xml == null) {
throw new NullPointerException();
}
if (LOG.isDebugEnabled()) {
LOG.debug(MessageFormat.format(
DEBUG_QUERY_DATA_BEFORE_UNMARSHALLING, xml));
}
try {
ctx = JAXBContext.newInstance(JAXB_BEAN_PACKAGE_NAME);
unmarshaller = ctx.createUnmarshaller();
queryType = (QueryType) unmarshaller.unmarshal(new ByteArrayInputStream(
xml.getBytes()));
return queryType;
} catch (JAXBException ex) {
LOG.warn("JAXBException occurred unmarshalling query xml", ex);
LOG.debug("JAXBException occurred unmarshalling query xml: " + xml);
throw new PersonalizationException(PersonalizationException.Code.SERIALIZATION_ERROR);
}
}
/**
* Utility method to serialize the given {@link IQuery} object
* to it's XML representation via JAXB.
*
* @param query Reference to the {@link IQuery} to be serialized.
* @return {@link String} XML query representation.
* @throws PersonalizationException Thrown if there was a serialization problem.
* @throws NullPointerException Thrown if the <code>query</code> argument is <code>null</code>.
*/
private String marshallQuery(IQuery query) throws PersonalizationException {
JAXBContext ctx = null;
Marshaller marshaller = null;
ByteArrayOutputStream out = null;
byte[] data = null;
if (query == null) {
throw new NullPointerException("query cannot be null");
}
try {
ctx = JAXBContext.newInstance(JAXB_BEAN_PACKAGE_NAME);
marshaller = ctx.createMarshaller();
out = new ByteArrayOutputStream();
marshaller.marshal(query.getQuery(), out);
data = out.toByteArray();
if (LOG.isDebugEnabled()) {
LOG.debug(MessageFormat.format(
DEBUG_QUERY_DATA_AFTER_MARSHALLING, query.getId(),
query.getName(), new String(data)));
}
return new String(data);
} catch (JAXBException ex) {
LOG.warn("JAXBException occurred marshalling query to xml", ex);
throw new PersonalizationException(PersonalizationException.Code.SERIALIZATION_ERROR);
} catch (Exception ex) {
LOG.warn("Exception occurred marshalling query to xml", ex);
throw new PersonalizationException(PersonalizationException.Code.SERIALIZATION_ERROR);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -