📄 cswpersistenceservice.java
字号:
package com.esri.solutions.jitk.common.csw;
import com.esri.solutions.jitk.common.personalization.IPersonalizable;
import com.esri.solutions.jitk.common.personalization.IPersonalizationContext;
import com.esri.solutions.jitk.personalization.PersonalizationException;
import com.esri.solutions.jitk.personalization.data.IQuery;
import com.esri.solutions.jitk.personalization.data.beans.v1.AttributeCollectionType;
import com.esri.solutions.jitk.personalization.data.beans.v1.AttributeType;
import com.esri.solutions.jitk.personalization.data.beans.v1.CatalogQueryType;
import com.esri.solutions.jitk.personalization.data.beans.v1.CatalogServiceType;
import com.esri.solutions.jitk.personalization.data.beans.v1.EnvelopeN;
import com.esri.solutions.jitk.personalization.data.beans.v1.ObjectFactory;
import com.esri.solutions.jitk.personalization.data.beans.v1.QueryType;
import com.esri.solutions.jitk.personalization.data.beans.v1.SpatialFilter;
import org.apache.log4j.Logger;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
/**
* Service class that is used to store and retrieve CS/W queries from
* a data store. This class will delegate calls to the
* {@link IPersonalizationContext}.
*/
public class CswPersistenceService implements IPersonalizable {
/**
* {@link Logger} used to log message for this class.
*/
private static final Logger LOG = Logger.getLogger(CswPersistenceService.class);
/**
* Name of Java Package that contains JAXB-generated beans
*/
private static final String JAXB_BEAN_PACKAGE_NAME = "com.esri.solutions.jitk.personalization.data.beans.v1";
/**
* Debug statement.
*/
private static final String DEBUG_QUERY_DATA_AFTER_MARSHALLING = "Query debug after marshalling [ID: {0}, Name: {1}, Data: {2}]";
/**
* Debug statement.
*/
private static final String DEBUG_QUERY_DATA_BEFORE_UNMARSHALLING = "Query debug before unmarshalling [Data: {0}]";
/**
* Reference to the {@link IPersonalizationContext} that is used for access
* to personalization data.
*/
private IPersonalizationContext _personalizationContext = null;
/**
* Default no-args constructor.
*/
public CswPersistenceService() {
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.personalization.IPersonalizable#setPersonalizationContext(com.esri.solutions.jitk.common.personalization.IPersonalizationContext)
*/
public void setPersonalizationContext(IPersonalizationContext ctx) {
if (ctx == null) {
throw new NullPointerException("context cannot be null");
}
_personalizationContext = ctx;
}
/**
* Deletes the stored CS/W query with the given ID.
*
* <p>
* First, a call is made to the {@link IPersonalizationContext} to
* get the {@link IQuery} object. The query is then deleted via
* {@link IQuery#remove()}.
* </p>
*
* @param id {@link UUID} of the query.
* @throws PersonalizationException Thrown if there was an exception with the
* {@link IPersonalizationContext}.
* @throws NullPointerException Thrown if the <code>id</code> argument is <code>null</code>.
* @throws IllegalStateException Thrown if the {@link IPersonalizationContext} reference
* has not been set.
*/
public void delete(UUID id) throws PersonalizationException {
IQuery query = null;
if (id == null) {
throw new NullPointerException("id cannot be null");
}
if (_personalizationContext == null) {
throw new IllegalStateException("Personalization context not set");
}
LOG.debug("Attempting to delete query with ID: " + id.toString());
query = _personalizationContext.getData().getQuery(id);
if (query == null) {
LOG.info("Query not found - unable to delete");
LOG.debug("Unable to delete query with ID: " + id.toString());
return;
}
query.remove();
LOG.debug("Successfully deleted query with ID: " +
query.getId().toString());
LOG.info("Successfully deleted query with ID:" +
query.getId().toString());
}
/**
* Persists the {@link CswPersistenceObject} to it's XML representation.
* The {@link CswPersistenceObject} is just a go-between object, the actual
* XML representation will be that of the {@link IQuery} implementation.
*
* @param obj The {@link CswPersistenceObject} representation of the CS/W query.
* @return {@link String} XML representation.
* @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>.
*/
public String saveToXml(CswPersistenceObject obj)
throws PersonalizationException {
String strQuery = null;
IQuery iquery = null;
if (obj == null) {
throw new NullPointerException("persistence object cannot be null");
}
iquery = getQuery(obj);
if (iquery == null) {
LOG.warn("Invalid Query - unable to export to XML");
throw new PersonalizationException(PersonalizationException.Code.STORE_DATA_ERROR);
}
strQuery = marshallQuery(iquery);
return strQuery;
}
/**
* Stores the CS/W query via {@link IQuery#save()}.
*
* @param obj The {@link CswPersistenceObject} representation of the query to be saved.
* @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>.
* @see com.esri.solutions.jitk.personalization.data.IQuery
*/
public void save(CswPersistenceObject obj) throws PersonalizationException {
IQuery iquery = null;
if (obj == null) {
throw new NullPointerException("persistence object cannot be null");
}
iquery = getQuery(obj);
if (iquery == null) {
LOG.warn("Invalid Query - unable to save");
throw new PersonalizationException(PersonalizationException.Code.STORE_DATA_ERROR);
}
iquery.save();
}
/**
* Creates a go-between {@link CswPersistenceObject} object that represents
* the stored state of the CS/W query XML.
*
* @param xml {@link String} XML representation of the CS/W query.
* @return {@link CswPersistenceObject} go-between object.
* @throws PersonalizationException Thrown if there was an exception with the
* {@link IPersonalizationContext}.
* @throws NullPointerException Thrown if the <code>xml</code> argument is
* <code>null</code>.
*/
public CswPersistenceObject retrieveFromXml(String xml)
throws PersonalizationException {
CswPersistenceObject obj = null;
QueryType queryType = null;
IQuery query = null;
if (xml == null) {
throw new NullPointerException();
}
queryType = unmarshallQuery(xml);
if (queryType == null) {
throw new PersonalizationException(PersonalizationException.Code.RETRIEVE_DATA_ERROR);
}
query = _personalizationContext.getData().createQuery(queryType);
obj = createPersistenceObject(query);
return obj;
}
/**
* Retrieves the CS/W query with the given {@link UUID} via the
* {@link IPersonalizationContext} and then returns it in the form of a
* {@link CswPersistenceObject}.
*
* @param id {@link UUID} of the requested CS/W query.
* @return {@link CswPersistenceObject} go-between object representing the retrieved
* query.
* @throws PersonalizationException Thrown if there was an exception with the
* {@link IPersonalizationContext}.
* @throws NullPointerException Thrown if the <code>id</code> argument is <code>null</code>.
* @throws IllegalStateException Thrown if the {@link IPersonalizationContext} has
* not been set.
*/
public CswPersistenceObject retrieve(UUID id)
throws PersonalizationException {
CswPersistenceObject obj = null;
IQuery query = null;
if (id == null) {
throw new NullPointerException("id cannot be null");
}
if (_personalizationContext == null) {
throw new IllegalStateException("Personalization context not set");
}
query = _personalizationContext.getData().getQuery(id);
obj = createPersistenceObject(query);
return obj;
}
/**
* Retrieves a {@link List} of all stored CS/W queries in the form
* of {@link CswPersistenceObject}s.
*
* @return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -