⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookmarkimpl.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 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.BookmarkInfoRecord;
import com.esri.solutions.jitk.personalization.dao.BookmarkRecord;
import com.esri.solutions.jitk.personalization.dao.IBookmarkDAO;
import com.esri.solutions.jitk.personalization.dao.IPersonalizationDAOProfile;
import com.esri.solutions.jitk.personalization.dao.PersonalizationDAOException;
import com.esri.solutions.jitk.personalization.data.beans.v1.BookmarkType;
import com.esri.solutions.jitk.personalization.data.beans.v1.ObjectFactory;

public class BookmarkImpl implements IBookmark, Serializable {
	
	private static final String ERROR_NULL_PROFILE_OBJECT = "Object not injected with IPersonalizationDAOProfile object.";
	private static final String ERROR_NULL_BOOKMARK_DAO_OBJECT = "IPersonalizationDAOProfile returned a null IBookmarkDAO object.";
	private static final String ERROR_NULL_INFO_OBJECT_WHEN_NEW = "Bookmark is not new, should be injected with BookmarkInfoRecord object.";
	private static final String ERROR_NULL_INFO_ON_REMOVE = "Bookmark Info Record is required for the remove operation.";
	private static final String ERROR_NULL_DATA_BEAN_ON_SAVE = "Bookmark Data Bean is required for the save operation.";

	private static final String INFO_CREATE_BOOKMARK_DATA_BEAN = "Bookmark is new, creating a new Bookmark Data Bean";
	private static final String INFO_LOADING_BOOKMARK_RECORD = "Loading Bookmark Record [ID: {0}]";
	private static final String INFO_REMOVING_BOOKMARK = "Removing Bookmark [ID: {0}, Name: {1}]";
	private static final String INFO_INSERTING_BOOKMARK = "Bookmark [ID: {0}, Name: {1}] is new, inserting Bookmark Record";
	private static final String INFO_UPDATING_BOOKMARK = "Bookmark [ID: {0}, Name: {1}] is not new, updating Bookmark Record.";

	private static final String DEBUG_BOOKMARK_DATA_AFTER_MARSHALLING = "Bookmark debug after marshalling [ID: {0}, Name: {1}, Data: {2}]";
	private static final String DEBUG_BOOKMARK_DATA_BEFORE_UNMARSHALLING = "Bookmark debug before unmarshalling [ID: {0}, Name: {1}, Data: {2}]";

	/**
	 * Serialization Version ID
	 */
	private static final long serialVersionUID = -2203921908087031244L;

	/**
	 * Logger to use to log messages from this class.
	 */
	private static final Logger LOG = LogManager.getLogger(BookmarkImpl.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 Bookmark.
	 */
	private BookmarkInfoRecord m_info;

	/**
	 * Full information on a bookmark.
	 */
	private BookmarkType m_bean;

	/**
	 * Reference to Map Composition.  A Bookmark 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 Bookmark is new.
	 */
	private boolean m_isNew;

	/**
	 * Current User.
	 */
	private IUser m_currentUser;

	/*
	 * (non-Javadoc)
	 * @see com.esri.solutions.jitk.personalization.data.IBookmark#getBookmark()
	 */
	public BookmarkType getBookmark() throws PersonalizationException {
		if (m_daoProfile == null) {
			LOG.error(ERROR_NULL_PROFILE_OBJECT);
			throw new IllegalStateException (ERROR_NULL_PROFILE_OBJECT);
		}

		if (m_bean == null) {
			IBookmarkDAO dao = null;
			byte[] data = null;

			try {
				dao = m_daoProfile.getBookmarkDAO();

				if (dao == null) {
					LOG.error(ERROR_NULL_BOOKMARK_DAO_OBJECT);
					throw new IllegalArgumentException (ERROR_NULL_BOOKMARK_DAO_OBJECT);
				}

				if (!this.m_isNew) {
					if (LOG.isInfoEnabled()) {
						LOG.info(MessageFormat.format(INFO_LOADING_BOOKMARK_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);
					}
					BookmarkRecord record = dao.selectOne(m_info.getId());
					data = record.getData();
					unmarshallBean(data);
				} else {
					if (LOG.isInfoEnabled()) {
						LOG.info(INFO_CREATE_BOOKMARK_DATA_BEAN);
					}
					m_bean = new ObjectFactory().createBookmark();
					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 (BookmarkType) java.lang.reflect.Proxy.newProxyInstance(BookmarkType.class.getClassLoader(),
				new Class[] {BookmarkType.class},
				new ProtectIDModification(m_bean));
	}


	/*
	 * (non-Javadoc)
	 * @see com.esri.solutions.jitk.personalization.data.IBookmark#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.IBookmark#getMapComposition()
	 */
	public IMapComposition getMapComposition() {
		return m_mc;
	}

	/*
	 * (non-Javadoc)
	 * @see com.esri.solutions.jitk.personalization.data.IBookmark#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.IBookmark#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.IBookmark#isNew()
	 */
	public boolean isNew () {
		return m_isNew;
	}

	/*
	 * (non-Javadoc)
	 * @see com.esri.solutions.jitk.personalization.data.IBookmark#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);
		}

		IBookmarkDAO dao = null;

		try {
			dao = m_daoProfile.getBookmarkDAO();

			if (dao == null) {
				LOG.error(ERROR_NULL_BOOKMARK_DAO_OBJECT);
				throw new IllegalArgumentException (ERROR_NULL_BOOKMARK_DAO_OBJECT);
			}

			if (!m_isNew) {
				if (LOG.isInfoEnabled()) {
					LOG.info(MessageFormat.format(INFO_REMOVING_BOOKMARK, getId(), getName()));
				}

				dao.delete(m_info.getId());
				m_isNew = true;
			}

		} catch (PersonalizationDAOException e) {
			throw new PersonalizationException (PersonalizationException.Code.DELETE_DATA_ERROR, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.esri.solutions.jitk.personalization.data.IBookmark#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);
		}

		if (m_mc != null && m_mc.isNew()) {
			m_mc.getBookmarkSaveQueue().add(this);
			return;
		}
		
		try {
			IBookmarkDAO dao = null;
			BookmarkRecord record = createRecord();

			dao = m_daoProfile.getBookmarkDAO();

			if (dao == null) {
				LOG.error(ERROR_NULL_BOOKMARK_DAO_OBJECT);
				throw new IllegalArgumentException (ERROR_NULL_BOOKMARK_DAO_OBJECT);
			}

			if (m_isNew) {
				if (LOG.isInfoEnabled()) {
					LOG.info(MessageFormat.format(INFO_INSERTING_BOOKMARK, getId(), getName()));
				}
				dao.insert(record);
			} else {
				if (LOG.isInfoEnabled()) {
					LOG.info(MessageFormat.format(INFO_UPDATING_BOOKMARK, getId(), getName()));
				}
				dao.update(record);
			}

			m_isNew = false;
			m_info = record;
		} catch (JAXBException e) {
			throw new PersonalizationException (
					PersonalizationException.Code.SERIALIZATION_ERROR,
					e);
		} catch (PersonalizationDAOException e) {
			throw new PersonalizationException (
					PersonalizationException.Code.STORE_DATA_ERROR,
					e);
		}
	}

	/**
	 * Sets the reference to the Map Composition that is associated with this
	 * Bookmark.  This method will accept a <code>null</code> argument value.
	 *
	 * @param mc	Reference to Map Composition associated with this
	 * 				bookmark, can be <code>null</code>.
	 */
	public void setMapComposition (MapCompositionImpl mc) {
		m_mc = mc;
	}

	/**
	 * Sets the reference to the Bookmark Information.  This is typically
	 * produced by a Data Access Object.  It contains metadata on a Bookmark
	 * such as ID, Name, and Description.
	 *
	 * @param info Bookmark Metadata Information, cannot be <code>null</code>.
	 *
	 * @throws NullPointerException		Thrown if the <code>info</code> argument
	 * 									is <code>null</code>.
	 */
	public void setBookmarkInfoRecord (BookmarkInfoRecord info) {
		if (info == null) {
			throw new NullPointerException ();
		}
		m_info = info;
	}

	/**
	 * Sets the reference to the profile of DAO objects that will be used
	 * to retrieve and store personalization data.
	 *
	 * @param profile		Contains references to all DAO objects, cannot
	 * 						be <code>null</code>.
	 *
	 * @throws NullPointerException		Thrown if the <code>profile</code>
	 * 									argument is <code>null</code>
	 */
	public void setPersonalizationDAOProfile (IPersonalizationDAOProfile profile) {
		if (profile == null) {
			throw new NullPointerException();
		}

		m_daoProfile = profile;
	}

	/**
	 * Sets the flag to indicate if this is a new Bookmark.
	 *
	 * @param isNew	Flag indicating if the Bookmark is new.
	 */
	public void setNew (boolean isNew) {
		m_isNew = isNew;
	}

	/**
	 * Sets the User that created this Bookmark.
	 *
	 * @param user	User that created Bookmark.
	 */
	public void setCurrentUser (IUser user) {
		m_currentUser = user;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof BookmarkImpl)) {
			return false;
		}

		BookmarkImpl bookmark = (BookmarkImpl)obj;

		return bookmark.toString().equals(toString());
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return toString().hashCode();
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("ID:[");
		sb.append(getId());
		sb.append("],Name:[");
		sb.append(getName());
		sb.append("],Description:[");
		sb.append(getDescription());
		sb.append("]");

		return sb.toString();
	}


	private byte[] marshallBean() throws JAXBException {
		JAXBContext ctx = null;
		ctx = JAXBContext.newInstance(JAXB_BEAN_PACKAGE_NAME);
		Marshaller marshaller = ctx.createMarshaller();

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		marshaller.marshal(m_bean, out);

		byte[] data = out.toByteArray();
		if (LOG.isDebugEnabled()) {
			LOG.debug(MessageFormat.format(DEBUG_BOOKMARK_DATA_AFTER_MARSHALLING, getId(), getName(), new String(data)));
		}
		return data;
	}

	private void unmarshallBean(byte[] data) throws JAXBException {

		if (LOG.isDebugEnabled()) {
			LOG.debug(MessageFormat.format(DEBUG_BOOKMARK_DATA_BEFORE_UNMARSHALLING, getId(), getName(), new String(data)));
		}
		JAXBContext ctx = null;
		ctx = JAXBContext.newInstance(JAXB_BEAN_PACKAGE_NAME);

		Unmarshaller unmarshaller = ctx.createUnmarshaller();

		m_bean = (BookmarkType) unmarshaller.unmarshal(new ByteArrayInputStream(data));
	}

	private BookmarkRecord createRecord() throws JAXBException {
		BookmarkRecord record = new BookmarkRecord();
		record.setCreator(m_currentUser.getId().getUsername());
		record.setId(m_bean.getId());
		record.setName(m_bean.getName());
		record.setDescription(m_bean.getDescription());
		record.setTimeModified(new java.sql.Timestamp(System.currentTimeMillis()));
		byte[] data = marshallBean();
		record.setData(data);
		if (m_mc != null && m_mc.getId() != null) {
			record.setMapCompositionId(m_mc.getId().toString());
		}
		
		return record;
	}

	/**
	 * Protects the {@link BookmarkType} bean from having its ID
	 * property modified.  Don't want callers to be able to change the
	 * ID.  The ID will be managed by this class.
	 */
	private class ProtectIDModification implements InvocationHandler {

		/**
		 * Reference to the {@link BookmarkType} bean
		 */
		private BookmarkType m_bean;

		/**
		 * Constructs a new <code>ProtectIDModification</code> object
		 * with a reference to a {@link BookmarkType} bean.
		 *
		 * @param bean Reference to {@link BookmarkType} bean.
		 */
		public ProtectIDModification (BookmarkType bean) {
			m_bean = bean;
		}

		/*
		 * (non-Javadoc)
		 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
		 */
		public Object invoke(Object proxy, Method method, Object[] args)
				throws Throwable {
			if (!method.getName().equals("setId")) {
				return method.invoke(m_bean, args);
			} else {
				LOG.info ("Attempting to setId on BookmarkType, not allowed.");
			}
			return Void.TYPE;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.esri.solutions.jitk.personalization.data.IBookmark#getCreator()
	 */
	public IUserId getCreator() {
		
		if (m_info != null) {
			UserIdImpl creator = new UserIdImpl();
			creator.setUsername(m_info.getCreator());
			return creator;
		} else {
			return m_currentUser.getId();
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -