📄 queryimpl.java
字号:
package com.esri.solutions.jitk.personalization.data;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.UUID;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.esri.solutions.jitk.personalization.PersonalizationException;
import com.esri.solutions.jitk.personalization.dao.IPersonalizationDAOProfile;
import com.esri.solutions.jitk.personalization.dao.IQueryDAO;
import com.esri.solutions.jitk.personalization.dao.PersonalizationDAOException;
import com.esri.solutions.jitk.personalization.dao.QueryInfoRecord;
import com.esri.solutions.jitk.personalization.dao.QueryRecord;
import com.esri.solutions.jitk.personalization.data.beans.v1.BookmarkType;
import com.esri.solutions.jitk.personalization.data.beans.v1.CatalogQueryType;
import com.esri.solutions.jitk.personalization.data.beans.v1.FeatureQueryType;
import com.esri.solutions.jitk.personalization.data.beans.v1.ObjectFactory;
import com.esri.solutions.jitk.personalization.data.beans.v1.QueryType;
public class QueryImpl implements IQuery, Serializable {
private static final String ERROR_NULL_PROFILE_OBJECT = "Object not injected with IPersonalizationDAOProfile object.";
private static final String ERROR_NULL_QUERY_DAO_OBJECT = "IPersonalizationDAOProfile returned a null IQueryDAO object.";
private static final String ERROR_NULL_INFO_OBJECT_WHEN_NEW = "Query is not new, should be injected with QueryInfoRecord object.";
private static final String ERROR_NULL_INFO_ON_REMOVE = "Query Info Record is required for the remove operation.";
private static final String ERROR_NULL_DATA_BEAN_ON_SAVE = "Query Data Bean is required for the save operation.";
private static final String ERROR_NULL_QUERY_TYPE = "Type of Query is not known. Unable to create appropriate Query bean.";
private static final String INFO_CREATE_QUERY_DATA_BEAN = "Query is new, creating a new Query Data Bean";
private static final String INFO_LOADING_QUERY_RECORD = "Loading Query Record [ID: {0}]";
private static final String INFO_REMOVING_QUERY = "Removing Query [ID: {0}, Name: {1}]";
private static final String INFO_INSERTING_QUERY = "Query [ID: {0}, Name: {1}] is new, inserting Query Record";
private static final String INFO_UPDATING_QUERY = "Query [ID: {0}, Name: {1}] is not new, updating Query Record.";
private static final String DEBUG_QUERY_DATA_AFTER_MARSHALLING = "Query debug after marshalling [ID: {0}, Name: {1}, Data: {2}]";
private static final String DEBUG_QUERY_DATA_BEFORE_UNMARSHALLING = "Query debug before unmarshalling [ID: {0}, Name: {1}, Data: {2}]";
/**
* Serialization Version ID
*/
private static final long serialVersionUID = -156696504651772344L;
/**
* Logger to use to log messages from this class.
*/
private static final Logger LOG = LogManager.getLogger(QueryImpl.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";
/**
* Contains metadata information on a Query
*/
private QueryInfoRecord m_info;
/**
* Full information on a query.
*/
private QueryType m_bean;
/**
* Reference to Map Composition. A Query may or may not be associated
* with a Map Composition.
*/
private MapCompositionImpl m_mc;
/**
* Reference to a profile of DAO implementations to use to retrieve
* and store data.
*/
private IPersonalizationDAOProfile m_daoProfile;
/**
* Indicates if the Query is new.
*/
private boolean m_isNew;
private Type m_type;
private IUser m_currentUser;
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IQuery#getDescription()
*/
public String getDescription() {
if (m_bean != null) {
return m_bean.getDescription();
}
if (m_info != null) {
return m_info.getDescription();
}
return null;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IQuery#getId()
*/
public UUID getId() {
if (m_bean != null) {
return UUID.fromString(m_bean.getId());
}
if (m_info != null) {
return UUID.fromString(m_info.getId());
}
return null;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IQuery#getName()
*/
public String getName() {
if (m_bean != null) {
return m_bean.getName();
}
if (m_info != null) {
return m_info.getName();
}
return null;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IQuery#getMapComposition()
*/
public IMapComposition getMapComposition() {
return m_mc;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IQuery#getQuery()
*/
@SuppressWarnings("unchecked")
public QueryType getQuery() throws PersonalizationException {
if (m_daoProfile == null) {
LOG.error(ERROR_NULL_PROFILE_OBJECT);
throw new IllegalStateException(ERROR_NULL_PROFILE_OBJECT);
}
if (m_type == null) {
LOG.error(ERROR_NULL_QUERY_TYPE);
throw new IllegalStateException(ERROR_NULL_QUERY_TYPE);
}
if (m_bean == null) {
IQueryDAO dao = null;
byte[] data = null;
try {
dao = m_daoProfile.getQueryDAO();
if (dao == null) {
LOG.error(ERROR_NULL_QUERY_DAO_OBJECT);
throw new IllegalArgumentException(ERROR_NULL_QUERY_DAO_OBJECT);
}
if (!this.m_isNew) {
if (LOG.isInfoEnabled()) {
LOG.info(MessageFormat.format(
INFO_LOADING_QUERY_RECORD, m_info.getId()));
}
if (m_info == null) {
LOG.error(ERROR_NULL_INFO_OBJECT_WHEN_NEW);
throw new IllegalStateException(ERROR_NULL_INFO_OBJECT_WHEN_NEW);
}
QueryRecord record = dao.selectOne(m_info.getId());
data = record.getData();
unmarshallBean(data);
} else {
if (LOG.isInfoEnabled()) {
LOG.info(INFO_CREATE_QUERY_DATA_BEAN);
}
if (m_type == IQuery.Type.FEATURE) {
m_bean = new ObjectFactory().createFeatureQuery();
} else if (m_type == IQuery.Type.CATALOG) {
m_bean = new ObjectFactory().createCatalogQuery();
}
m_bean.setId(UUID.randomUUID().toString());
}
} catch (JAXBException e) {
throw new PersonalizationException(PersonalizationException.Code.SERIALIZATION_ERROR,
e);
} catch (PersonalizationDAOException e) {
throw new PersonalizationException(PersonalizationException.Code.RETRIEVE_DATA_ERROR,
e);
}
}
return (QueryType) java.lang.reflect.Proxy.newProxyInstance(QueryType.class.getClassLoader(),
m_bean.getClass().getInterfaces(), new ProtectIDModification(m_bean));
}
public void setQuery(QueryType queryType) {
if (queryType == null) {
throw new NullPointerException();
}
m_bean = queryType;
}
public boolean isNew() {
return this.m_isNew;
}
public void remove() throws PersonalizationException {
if (m_daoProfile == null) {
LOG.error(ERROR_NULL_PROFILE_OBJECT);
throw new IllegalStateException(ERROR_NULL_PROFILE_OBJECT);
}
if (m_info == null) {
LOG.error(ERROR_NULL_INFO_ON_REMOVE);
throw new IllegalStateException(ERROR_NULL_INFO_ON_REMOVE);
}
IQueryDAO dao = null;
try {
dao = m_daoProfile.getQueryDAO();
if (dao == null) {
LOG.error(ERROR_NULL_QUERY_DAO_OBJECT);
throw new IllegalArgumentException(ERROR_NULL_QUERY_DAO_OBJECT);
}
if (!m_isNew) {
if (LOG.isInfoEnabled()) {
LOG.info(MessageFormat.format(INFO_REMOVING_QUERY, getId(),
getName()));
}
dao.delete(m_info.getId());
m_isNew = true;
}
} catch (PersonalizationDAOException e) {
throw new PersonalizationException(PersonalizationException.Code.DELETE_DATA_ERROR,
e);
}
}
public void save() throws PersonalizationException {
if (m_daoProfile == null) {
LOG.error(ERROR_NULL_PROFILE_OBJECT);
throw new IllegalStateException(ERROR_NULL_PROFILE_OBJECT);
}
if (m_bean == null) {
LOG.error(ERROR_NULL_DATA_BEAN_ON_SAVE);
throw new IllegalStateException(ERROR_NULL_DATA_BEAN_ON_SAVE);
}
if ((m_mc != null) && m_mc.isNew()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -