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

📄 user.java

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

import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;

import javax.xml.rpc.ServiceException;

import org.apache.commons.beanutils.BeanUtils;

import com.esri.solutions.jitk.personalization.dao.IUserDAO;
import com.esri.solutions.jitk.personalization.dao.PersonalizationDAOException;
import com.esri.solutions.jitk.personalization.dao.UserInfoRecord;
import com.esri.solutions.jitk.personalization.dao.UserRecord;
import com.esri.solutions.jitk.services.common.ServicesException;
import com.esri.solutions.jitk.services.personalization.data.UserData;

/**
 * Web service endpoint for the User service.
 * <p>
 * This class utilizes an {@link IUserDAO} to perform the CRUD-type operations
 * against the user datastore. The DAO is initialized via the {@link #onInit()}
 * method, which retrieves a bean named {@code userDAO} from the Spring
 * configuration.
 * </p>
 * <p>
 * If {@code userDAO} is not configured properly in the Spring configuration
 * file, this service will never initialize, and thus, will not be available.
 * </p>
 */
public class User extends PersonalizationService implements IUserService {
	/**
	 * The DAO object that is used to access the user datastore and perform the
	 * actual CRUD operations.
	 */
	private IUserDAO _dao = null;

	/**
	 * Default constructor
	 */
	public User() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.esri.solutions.jitk.services.personalization.PersonalizationService#onInit()
	 */
	@Override
	protected void onInit() throws ServiceException {
		super.onInit();

		Object o = this.getWebApplicationContext().getBean("userDAO");
		if (o instanceof IUserDAO && o != null) {
			IUserDAO dao = (IUserDAO) o;
			setDAO(dao);
		} else {
			throw new ServiceException(
					"userDAO is improperly configured and cannot be set on the Query service");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.esri.solutions.jitk.services.personalization.PersonalizationService#checkForId(java.lang.String)
	 */
	@Override
	protected boolean checkForId(String id) throws ServicesException {
		UserData d = selectOne(id);
		return (d != null) && (d.getId() != null);
	}

	/**
	 * Sets the {@link IUserDAO} instance that is used to access and manipulate
	 * the user datastore.
	 * 
	 * @param dao
	 *            {@link IUserDAO} instance, which cannot be null.
	 */
	public void setDAO(IUserDAO dao) {
		if (dao == null) {
			throw new NullPointerException("dao cannot be null");
		}
		_dao = dao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.esri.solutions.jitk.services.personalization.IUserService#getDAO()
	 */
	public IUserDAO getDAO() {
		return _dao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.esri.solutions.jitk.services.personalization.IUserService#delete(java.lang.String)
	 */
	public void delete(String id) throws ServicesException {
		if (_dao == null) {
			throw new IllegalStateException("DAO has not yet been set.");
		}

		if (id == null) {
			throw new NullPointerException("id cannot be null");
		}

		try {
			_dao.delete(id);
		} catch (PersonalizationDAOException e) {
			throw new ServicesException(e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.esri.solutions.jitk.services.personalization.IUserService#insert(com.esri.solutions.jitk.services.personalization.data.UserData)
	 */
	public void insert(UserData data) throws ServicesException {
		if (_dao == null) {
			throw new IllegalStateException("DAO has not yet been set.");
		}
		if (data == null) {
			throw new NullPointerException("data cannot be null");
		}
		try {
			UserRecord record = createRecord(data);
			if (okToInsert(record.getId())) {
				_dao.insert(record);
			} else {
				throw new ServicesException(MessageFormat.format(
						ERROR_ID_ALREADY_EXISTS, "User", record.getId()));
			}
		} catch (IllegalAccessException e) {
			throw new ServicesException(e);
		} catch (InvocationTargetException e) {
			throw new ServicesException(e);
		} catch (PersonalizationDAOException e) {
			throw new ServicesException(e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.esri.solutions.jitk.services.personalization.IUserService#selectOne(java.lang.String)
	 */
	public UserData selectOne(String id) throws ServicesException {
		if (_dao == null) {
			throw new IllegalStateException("DAO has not yet been set.");
		}
		if (id == null) {
			throw new NullPointerException("id cannot be null");
		}
		UserRecord original = null;
		UserData data = null;
		try {
			original = _dao.selectOne(id);
			data = createData(original);
		} catch (IllegalAccessException e) {
			throw new ServicesException(e);
		} catch (InvocationTargetException e) {
			throw new ServicesException(e);
		} catch (PersonalizationDAOException e) {
			throw new ServicesException(e);
		}
		return data;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.esri.solutions.jitk.services.personalization.IUserService#update(com.esri.solutions.jitk.services.personalization.data.UserData)
	 */
	public void update(UserData data) throws ServicesException {
		if (_dao == null) {
			throw new IllegalStateException("DAO has not yet been set.");
		}
		if (data == null) {
			throw new NullPointerException("data cannot be null");
		}
		try {
			UserRecord record = createRecord(data);
			_dao.update(record);
		} catch (IllegalAccessException e) {
			throw new ServicesException(e);
		} catch (InvocationTargetException e) {
			throw new ServicesException(e);
		} catch (PersonalizationDAOException e) {
			throw new ServicesException(e);
		}
	}

	/**
	 * Converts a {@link UserInfoRecord} into an {@link UserData}
	 * 
	 * @param record
	 *            The {@link UserInfoRecord} to convert. {@code data} can be
	 *            {@code null}, in which case an new, empty {@link UserData}
	 *            will be returned.
	 * @return {@link UserData} representation of the {@link UserInfoRecord}
	 * @throws IllegalAccessException
	 *             if an IllegalAccessException occurs when the bean properties
	 *             are being copied
	 * @throws InvocationTargetException
	 *             if an InvocationTargetException occurs when the bean
	 *             properties are being copied
	 */
	private UserData createData(UserInfoRecord record)
			throws IllegalAccessException, InvocationTargetException {
		UserData data = new UserData();
		if (record != null) {
			BeanUtils.copyProperties(data, record);
			data.setLocked(record.getIsLocked().equals("1") ? true : false);
		}
		return data;

	}

	/**
	 * Converts a {@link UserData} into an {@link UserInfoRecord}
	 * 
	 * @param data
	 *            The {@link UserData} to convert. {@code data} can be
	 *            {@code null}, in which case an new, empty
	 *            {@link UserInfoRecord} will be returned.
	 * @return {@link UserInfoRecord} representation of the {@link UserData}
	 * @throws IllegalAccessException
	 *             if an IllegalAccessException occurs when the bean properties
	 *             are being copied
	 * @throws InvocationTargetException
	 *             if an InvocationTargetException occurs when the bean
	 *             properties are being copied
	 */
	private UserRecord createRecord(UserData data)
			throws IllegalAccessException, InvocationTargetException,
			ServicesException {
		UserRecord record = new UserRecord();
		if (data != null) {
			BeanUtils.copyProperties(record, data);
			record.setIsLocked(data.getLocked() ? "1" : "0");
		}
		record.setId(getUsername());
		return record;

	}

}

⌨️ 快捷键说明

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