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

📄 userdata.java

📁 esri的ArcGIS Server超级学习模板程序(for java)
💻 JAVA
字号:
/**
 * UserData.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.PreferenceSettingsType;
import com.esri.solutions.jitk.services.common.ServicesException;

/**
 * Serializable Java Bean representation of a User.
 */
public class UserData 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 user.
	 */
	private String _id;
	/**
	 * {@code boolean} indicating if the user's information is locked, thus
	 * preventing modifications. {@code true} indicates locked.
	 */
	private boolean _locked;

	/**
	 * {@link Object} used by the {@link #equals(Object)} method to improve the
	 * performance of the calculations.
	 */
	private Object _equalsCalc = null;

	/**
	 * {@code boolean} used by the {@link #hashCode()} method to improve the
	 * performance of the hash algorithm.
	 */
	private boolean _hashCodeCalc = false;
	/**
	 * {@link PreferenceSettingsType} representation of the User's Preference Settings.
	 */
	private PreferenceSettingsType _preferenceSettings = null;
	private static final String JAXB_BEAN_PACKAGE_NAME = "com.esri.solutions.jitk.personalization.data.beans.v1";
	private static final String ERROR_DATA_DOES_NOT_REPRESENT_PREFERENCES = "The byte array provided does not unmarshall to a Preference Settings object.";

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

	/**
	 * Gets the id of the User, which corresponds to the user's login.
	 * 
	 * @return id {@code String} containing the user's ID.
	 */
	public String getId() {
		return _id;
	}

	/**
	 * Sets the id of the User, which corresponds to the user's login.
	 * 
	 * @param id {@code String} containing the user's ID.
	 */
	public void setId(String id) {
		_id = id;
	}

	/**
	 * Gets the value indicating if the user's information is locked, thus
	 * preventing modifications.
	 * 
	 * @param locked {@code boolean} {@code true} if the user's information is locked.  {@code false} otherwise.
	 */
	public boolean getLocked() {
		return _locked;
	}

	/**
	 * Sets the value indicating if the user's information is locked, thus
	 * preventing modifications.
	 * 
	 * @param locked {@code boolean} {@code true} if the user's information is to be locked.  {@code false} otherwise.
	 */
	public void setLocked(boolean locked) {
		_locked = locked;
	}

	/**
	 * Retrieves the user's Preference Settings, in the form of a {@code byte[]}.
	 * @return Xml-encoded data of the user preferences as a byte array.
	 */
	public byte[] getData() {
		return _data;
	}

	/**
	 * Sets the user's Preference Settings, in the form of a {@code byte[]}.
	 * <p>
	 * The {@code byte[]} parameter must unmarshall into a valid {@link PreferenceSettingsType}.  If the parameter is {@code null},
	 * then the property in which {@code data} is stored, as well as the internal {@link PreferenceSettingsType} representation, will both
	 * be set to {@code null}, thus causing {@code null} to be returned from {@link #dataAsPreferenceSettings()}.
	 * </p>
	 * <p>
	 * If {@code data} is not {@code null}, this method will unmarshall the byte array into a {@link PreferenceSettingsType} that 
	 * can be retrieved from {@link #dataAsPreferenceSettings()}.
	 * </p>
	 * @param data {@code byte[]} representation of a {@link PreferenceSettingsType}.  The parameter can be {@code null} 
	 *        or of zero length, but if it is not, it must unmarshall to a {@link PreferenceSettingsType}.
	 * @throws ServicesException  If {@code data} does not unmarshall to a valid {@link PreferenceSettingsType}.
	 */
	public void setData(byte[] data) throws ServicesException {

		PreferenceSettingsType p = null;
		if (data != null && data.length>0 ) {
			try {
				p = unmarshall(data);
				/*
				 * t could be null if the unmarshall was successful but the data
				 * could not be converted to a MapCompositionType
				 */
				if (p == null) {
					throw new ServicesException(
							ERROR_DATA_DOES_NOT_REPRESENT_PREFERENCES);
				}
				_preferenceSettings = p;
			} catch (JAXBException e) {
				throw new ServicesException(
						ERROR_DATA_DOES_NOT_REPRESENT_PREFERENCES, e);
			}
		}
		/*
		 * Set both the map composition and the data array, that way if one is
		 * nulled out, the other will be as well.
		 */
		_preferenceSettings = p;
		_data = data;
	}

	/**
	 * Retrieves the value set by {@link #setData(byte[])} in the form of a 
	 * {@link PreferenceSettingsType}. 
	 * @return {@link PreferenceSettingsType} representation of the user's Preference Settings.
	 */
	public PreferenceSettingsType dataAsPreferenceSettings() {
		return _preferenceSettings;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public synchronized boolean equals(Object obj) {
		if (!(obj instanceof UserData))
			return false;
		UserData other = (UserData) obj;
		if (obj == null)
			return false;
		if (this == obj)
			return true;
		if (_equalsCalc != null) {
			return (_equalsCalc == obj);
		}
		_equalsCalc = obj;
		boolean _equals;
		_equals = true
				&& ((getId() == null && other.getId() == null) || (_id != null && _id
						.equals(other.getId())))
				&& ((getData() == null && other.getData() == null) || (_data != null && _data
								.equals(other.getData())))
				&& (_locked && other.getLocked());
		_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 (getId() != null) {
			_hashCode += getId().hashCode();
		}
		if (getData() != null) {
			_hashCode += getData().hashCode();
		}
		Boolean b = new Boolean(getLocked());
		_hashCode += b.hashCode();
		_hashCodeCalc = false;
		return _hashCode;
	}

	/**
	 * Unmarshalls a {@code byte[]} into a {@link PreferenceSettingsType}
	 * 
	 * @param data
	 * @throws JAXBException
	 */
	private PreferenceSettingsType 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));
		PreferenceSettingsType t = null;
		if (o instanceof PreferenceSettingsType) {
			t = (PreferenceSettingsType) o;
		}
		return t;
	}

}

⌨️ 快捷键说明

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