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

📄 querydata.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
字号:
package com.esri.solutions.jitk.services.personalization.data;

import java.io.ByteArrayInputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.esri.solutions.jitk.personalization.data.beans.v1.QueryType;
import com.esri.solutions.jitk.services.common.ServicesException;

/**
 * Serializable Java Bean representation of a Query.
 */
public class QueryData extends PersonalizationData implements java.io.Serializable {
	private static final long serialVersionUID = 1L;
	/**
	 * {@code byte[]} representation of the actual item
	 */
	private byte[] _data;
	/**
	 * {@link String} containing the ID of the query against which the query was
	 * created.
	 */
	private String _mapCompositionId;
	/**
	 * 
	 */
	private int _queryType;
	/**
	 * {@link Object} used by the {@link #equals(Object)} method to improve the
	 * performance of the calculations.
	 */
	private Object _equalsCalc = null;

	private QueryType _query = null;
	/**
	 * {@code boolean} used by the {@link #hashCode()} method to improve the
	 * performance of the hash algorithm.
	 */
	private boolean _hashCodeCalc = false;
	private static final String ERROR_DATA_DOES_NOT_REPRESENT_A_QUERY = "The byte array provided does not unmarshall to a Query Type.";

	/**
	 * Default constructor
	 */
	public QueryData() {
	}

	/**
	 * Retrieves the ID of the Map Composition with which this query is associated.
	 * 
	 * @return {@link String} containing the map composition ID.
	 */
	public String getMapCompositionId() {
		return _mapCompositionId;
	}

	/**
	 * Sets the ID of the Map Composition with which this query is associated.
	 * 
	 * @param _mapCompositionId {@link String} containing the map composition ID.
	 */
	public void setMapCompositionId(String mapCompositionId) {
		_mapCompositionId = mapCompositionId;
	}

	/**
	 * Gets the query type indicator.
	 * 
	 * @return {@code int} containing the query type
	 */
	public int getQueryType() {
		return _queryType;
	}

	/**
	 * Sets the query type indicator.
	 * 
	 * @param queryType
	 */
	public void setQueryType(int queryType) {
		_queryType = queryType;
	}

	/**
	 * Gets the Query information in the form of a XML String in {@code byte[]} format.
	 * @return The xml data for a query as a byte array.
	 */
	public byte[] getData() {
		return _data;
	}

	/**
	 * Sets the definition of the query, using the form of a byte array.
	 * <p>
	 * {@code data} must unmarshall into a valid {@link QueryType}.  If it does, then 
	 * this method will both store {@code data} internally as well as the 
	 * {@link QueryType} it represents.  The {@link QueryType will then be retrievable
	 * through {@link #dataAsQuery()}
	 * </p>
	 * <p>
	 * {@code data} can be {@code null} or of zero-length.  If so, the 
	 * internal {@code byte[]} and {@link QueryType} will be nullified.
	 * @param data
	 *            The xml data for a Query Record as a byte array.
	 * @throws ServicesException if {@code data} does not unmarshall to a {@link QueryType}
	 */
	public void setData(byte[] data) throws ServicesException {
		QueryType q = null;
		if (data != null && data.length>0 ) {
			try {
				q = unmarshall(data);
				/*
				 * t will be null if the unmarshall was successful but the data
				 * could not be converted to a MapCompositionType
				 */
				if (q == null) {
					throw new ServicesException(
							ERROR_DATA_DOES_NOT_REPRESENT_A_QUERY);
				}
				_query = q;
			} catch (JAXBException e) {
				throw new ServicesException(
						ERROR_DATA_DOES_NOT_REPRESENT_A_QUERY, e);
			}
		}
		/*
		 * Set both the query and the byte array of data, that way if one is
		 * nulled out, the other will be as well.
		 */
		_query = q;
		_data = data;
	}

	/**
	 * Retrieves the {@link QueryType} that represents an unmarshalled version of the {@code byte[]} 
	 * set through a call to {@link #setData(byte[])}.
	 * @return {@link QueryType}
	 */
	public QueryType dataAsQuery() {
		return _query;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public synchronized boolean equals(Object obj) {
		if (!(obj instanceof QueryData))
			return false;
		QueryData other = (QueryData) obj;
		if (obj == null)
			return false;
		if (this == obj)
			return true;
		if (_equalsCalc != null) {
			return (_equalsCalc == obj);
		}
		_equalsCalc = obj;
		boolean _equals;
		_equals = true
				&& ((_creator == null && other.getCreator() == null) || (_creator != null && _creator
						.equals(other.getCreator())))
				&& ((_mapCompositionId == null && other.getMapCompositionId() == null) || (_mapCompositionId != null && _mapCompositionId
						.equals(other.getMapCompositionId())))
				&& (_queryType == other.getQueryType());
		_equalsCalc = null;
		return _equals;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	public synchronized int hashCode() {
		if (_hashCodeCalc) {
			return 0;
		}
		_hashCodeCalc = true;
		int _hashCode = 1;
		if (getCreator() != null) {
			_hashCode += getCreator().hashCode();
		}
		if (getCreator() != null) {
			_hashCode += getCreator().hashCode();
		}
		Integer i = new Integer(getQueryType());
		_hashCode += i.hashCode();
		_hashCodeCalc = false;
		return _hashCode;
	}

	/**
	 * Unmarshalls a {@code byte[]} into a {@link QueryType}
	 * 
	 * @param data {@code byte[]} representation of a {@link QueryType}
	 * @throws JAXBException if an exception occurs while unmarshalling
	 */
	private QueryType unmarshall(byte[] data) throws JAXBException {

		JAXBContext ctx = null;
		ctx = JAXBContext.newInstance(JAXB_BEAN_PACKAGE_NAME);

		Unmarshaller unmarshaller = ctx.createUnmarshaller();

		Object o = unmarshaller.unmarshal(new ByteArrayInputStream(data));
		QueryType t = null;
		if (o instanceof QueryType) {
			t = (QueryType) o;
		}
		return t;
	}

}

⌨️ 快捷键说明

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