📄 mapcompositionimpl.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.ArrayList;
import java.util.Date;
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;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.esri.solutions.jitk.personalization.PersonalizationException;
import com.esri.solutions.jitk.personalization.dao.BookmarkInfoRecord;
import com.esri.solutions.jitk.personalization.dao.Criteria;
import com.esri.solutions.jitk.personalization.dao.IBookmarkDAO;
import com.esri.solutions.jitk.personalization.dao.IMapCompositionDAO;
import com.esri.solutions.jitk.personalization.dao.IPersonalizationDAOProfile;
import com.esri.solutions.jitk.personalization.dao.IQueryDAO;
import com.esri.solutions.jitk.personalization.dao.MapCompositionInfoRecord;
import com.esri.solutions.jitk.personalization.dao.MapCompositionRecord;
import com.esri.solutions.jitk.personalization.dao.PersonalizationDAOException;
import com.esri.solutions.jitk.personalization.dao.QueryInfoRecord;
import com.esri.solutions.jitk.personalization.data.beans.v1.MapCompositionType;
import com.esri.solutions.jitk.personalization.data.beans.v1.ObjectFactory;
/**
* Implementation of {@link IMapComposition}. This implementation
* will keep an info record that contains the Name and Description
* of the Map Composition. When the full detail is required, this
* implementation will use a {@link IMapCompositionDAO} object to
* retrieve the data. This implementation will also use the
* {@link IMapCompositionDAO} object to save and remove the
* Map Composition.
*/
public class MapCompositionImpl implements IMapComposition, Serializable {
private static final String ERROR_NULL_INFO_OBJECT_WHEN_NEW = "Map Composition is not new, should be injected with MapCompositionInfoRecord object.";
private static final String ERROR_NULL_PROFILE_OBJECT = "Object not injected with IPersonalizationDAOProfile object.";
private static final String ERROR_NULL_DATA_FACTORY_OBJECT = "Object not injected with IDataFactory object.";
private static final String ERROR_NULL_MAP_COMP_DAO_OBJECT = "IPersonalizationDAOProfile returned a null IMapCompositionDAO object.";
private static final String ERROR_NULL_INFO_ON_REMOVE = "Map Composition Info Record is required for the remove operation.";
private static final String ERROR_NULL_DATA_BEAN_ON_SAVE = "Map Composition Data Bean is required for the save operation.";
private static final String ERROR_NULL_BOOKMARK_DAO_OBJECT = "IPersonalizationDAOProfile returned a null IBookmarkDAO object.";
private static final String ERROR_NULL_QUERY_DAO_OBJECT = "IPersonalizationDAOProfile returned a null IQueryDAO object.";
private static final String WARN_NEW_MAP_COMP_ON_REMOVE = "Map Composition is new and has not been saved so it cannot be removed.";
private static final String INFO_CREATE_MAP_COMP_DATA_BEAN = "Map Composition is new, creating a new Map Composition Data Bean";
private static final String INFO_LOADING_MAP_COMP_RECORD = "Loading Map Composition Record [ID: {0}]";
private static final String INFO_UPDATING_MAP_COMP = "Map Composition [ID: {0}, Name: {1}] is not new, updating Map Compsition Record.";
private static final String INFO_INSERTING_MAP_COMP = "Map Composition [ID: {0}, Name: {1}] is new, inserting Map Composition Record";
private static final String INFO_REMOVING_MAP_COMP = "Removing Map Composition [ID: {0}, Name: {1}]";
private static final String DEBUG_MAP_COMPOSITION_DATA_AFTER_MARSHALLING = "Map Composition debug after marshalling [ID: {0}, Name: {1}, Data: {2}]";
private static final String DEBUG_MAP_COMPOSITION_DATA_BEFORE_UNMARSHALLING = "Map Composition debug before unmarshalling [ID: {0}, Name: {1}, Data: {2}]";
/**
* Logger to use to log messages from this class.
*/
private static final Logger LOG = LogManager.getLogger(MapCompositionImpl.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";
/**
* Serialization Version ID
*/
private static final long serialVersionUID = -122782061795509862L;
/**
* Contains basic information on a Map Composition.
*/
private MapCompositionInfoRecord m_info;
/**
* Full information on a Map Composition.
*/
private MapCompositionType m_bean;
/**
* Indicates if the Map Composition is new.
*/
private boolean m_isNew = true;
/**
* Reference to a profile of DAO implementations to use
* to retrieve and store data.
*/
private IPersonalizationDAOProfile m_daoProfile;
/**
* Reference to factory that creates {@link IMapComposition},
* {@link IQuery}, and {@link IBookmark} objects.
*/
private IDataFactory m_dataFactory;
/**
* List of bookmarks to save when the Map Composition is saved.
* Only applies if the Map Composition is new, and bookmarks
* have been created.
*/
private final List<IBookmark> m_bookmarkSaveQueue;
/**
* List of queries to save when the Map Composition is saved.
* Only applies if the Map Composition is new, and queries
* have been created.
*/
private final List<IQuery> m_querySaveQueue;
private IUser m_currentUser;
public MapCompositionImpl () {
m_bookmarkSaveQueue = new ArrayList<IBookmark>();
m_querySaveQueue = new ArrayList<IQuery>();
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IMapComposition#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.IMapComposition#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.IMapComposition#getMapComposition()
*/
public MapCompositionType getMapComposition() throws PersonalizationException {
if (m_daoProfile == null) {
LOG.error(ERROR_NULL_PROFILE_OBJECT);
throw new IllegalStateException (ERROR_NULL_PROFILE_OBJECT);
}
if (m_bean == null) {
IMapCompositionDAO dao = null;
byte[] data = null;
try {
dao = m_daoProfile.getMapCompositionDAO();
if (dao == null) {
LOG.error(ERROR_NULL_MAP_COMP_DAO_OBJECT);
throw new IllegalArgumentException (ERROR_NULL_MAP_COMP_DAO_OBJECT);
}
if (!this.m_isNew) {
if (LOG.isInfoEnabled()) {
LOG.info(MessageFormat.format(INFO_LOADING_MAP_COMP_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);
}
MapCompositionRecord record = dao.selectOne(m_info.getId());
data = record.getData();
unmarshallBean(data);
} else {
if (LOG.isInfoEnabled()) {
LOG.info(INFO_CREATE_MAP_COMP_DATA_BEAN);
}
m_bean = new ObjectFactory().createMapComposition();
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 (MapCompositionType) java.lang.reflect.Proxy.newProxyInstance(MapCompositionType.class.getClassLoader(),
new Class[] {MapCompositionType.class},
new ProtectIDModification(m_bean));
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IMapComposition#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.IMapComposition#getCreatorUser()
*/
public IUserId getCreatorUser () {
UserIdImpl creator = new UserIdImpl();
if (m_info != null) {
creator.setUsername(m_info.getCreator());
} else {
creator.setUsername(m_currentUser.getId().getUsername());
}
return creator;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IMapComposition#remove()
*/
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);
}
IMapCompositionDAO dao = null;
try {
dao = m_daoProfile.getMapCompositionDAO();
if (dao == null) {
LOG.error(ERROR_NULL_MAP_COMP_DAO_OBJECT);
throw new IllegalArgumentException (ERROR_NULL_MAP_COMP_DAO_OBJECT);
}
if (!m_isNew) {
LOG.info(MessageFormat.format(INFO_REMOVING_MAP_COMP, getId(), getName()));
dao.delete(m_info.getId());
m_isNew = true;
} else {
LOG.warn(WARN_NEW_MAP_COMP_ON_REMOVE);
}
} catch (PersonalizationDAOException e) {
throw new PersonalizationException (PersonalizationException.Code.DELETE_DATA_ERROR,
e);
}
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IMapComposition#save()
*/
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);
}
IMapCompositionDAO dao = null;
try {
MapCompositionRecord record = createRecord();
dao = m_daoProfile.getMapCompositionDAO();
if (dao == null) {
LOG.error(ERROR_NULL_MAP_COMP_DAO_OBJECT);
throw new IllegalArgumentException (ERROR_NULL_MAP_COMP_DAO_OBJECT);
}
if (m_isNew) {
if (LOG.isInfoEnabled()) {
LOG.info(MessageFormat.format(INFO_INSERTING_MAP_COMP, getId(), getName()));
}
dao.insert(record);
}
else {
if (LOG.isInfoEnabled()) {
LOG.info(MessageFormat.format(INFO_UPDATING_MAP_COMP, getId(), getName()));
}
dao.update(record);
}
m_isNew = false;
m_info = record;
for (IBookmark bookmark : m_bookmarkSaveQueue) {
bookmark.save();
m_bookmarkSaveQueue.remove(bookmark);
}
for (IQuery query : m_querySaveQueue) {
query.save();
m_querySaveQueue.remove(query);
}
} catch (JAXBException e) {
throw new PersonalizationException (
PersonalizationException.Code.SERIALIZATION_ERROR,
e);
} catch (PersonalizationDAOException e) {
throw new PersonalizationException (
PersonalizationException.Code.STORE_DATA_ERROR,
e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -