📄 user.java
字号:
package com.esri.solutions.jitk.personalization.data;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Serializable;
import java.text.MessageFormat;
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.IPersonalizationDAOProfile;
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.personalization.data.beans.v1.ObjectFactory;
import com.esri.solutions.jitk.personalization.data.beans.v1.PreferenceSettingsType;
/**
* Default implementation of the {@link IUser} interface. This implementation
* simply provides a setter for the UserId property.
*/
public class User implements IUser, Serializable {
private static final String DEBUG_PREF_SETTINGS_BEFORE_UNMARSHALLING = "Preference Settings before Unmarshalling [{0}, {1}]";
private static final String DEBUG_PREF_SETTINGS_AFTER_MARSHALL = "Preference Settings After Marshalling [{0}, {1}]";
/**
* Serial Version UID
*/
private static final long serialVersionUID = 1160546624410653062L;
/**
* 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";
private static final Logger LOG = LogManager.getLogger(User.class);
@SuppressWarnings("unused")
private UserInfoRecord m_info;
private PreferenceSettingsType m_prefs;
private IPersonalizationDAOProfile m_daoProfile;
private boolean m_new;
private IUserId m_id;
private boolean m_locked = false;
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IUser#getId()
*/
public IUserId getId () {
return m_id;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IUser#isNew()
*/
public boolean isNew () {
return m_new;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IUser#isLocked()
*/
public boolean isLocked () {
return m_locked;
}
public void setUserInfoRecord (UserInfoRecord record) {
if (record == null) {
throw new NullPointerException();
}
m_info = record;
if (m_info.getIsLocked() == null) {
m_locked = false;
} else if (m_info.getIsLocked().equals("1")) {
m_locked = true;
} else {
m_locked = false;
}
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IUser#getPreferenceSettings()
*/
public PreferenceSettingsType getPreferenceSettings () throws PersonalizationException {
if (m_prefs == null) {
if (m_id == null) {
throw new IllegalStateException();
}
if (m_daoProfile == null) {
throw new IllegalStateException();
}
try {
IUserDAO dao = m_daoProfile.getUserDAO();
if (dao == null) {
throw new IllegalArgumentException ();
}
UserRecord record = dao.selectOne(m_id.getUsername());
byte[] data = record.getData();
if (data != null) {
if (LOG.isDebugEnabled()) {
LOG.debug(MessageFormat.format(DEBUG_PREF_SETTINGS_BEFORE_UNMARSHALLING, m_id.getUsername(), new String(data)));
}
JAXBContext ctx = JAXBContext.newInstance(JAXB_BEAN_PACKAGE_NAME);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
m_prefs = (PreferenceSettingsType) unmarshaller.unmarshal(new ByteArrayInputStream(data));
}
} catch (PersonalizationDAOException e) {
throw new PersonalizationException (
PersonalizationException.Code.RETRIEVE_DATA_ERROR,
e);
} catch (JAXBException e) {
throw new PersonalizationException (
PersonalizationException.Code.SERIALIZATION_ERROR,
e);
}
}
return m_prefs;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IUser#save()
*/
public void save () throws PersonalizationException {
if (m_daoProfile == null) {
throw new IllegalStateException();
}
if (m_id == null) {
throw new IllegalStateException ();
}
IUserDAO dao = null;
try {
dao = m_daoProfile.getUserDAO();
UserRecord record = new UserRecord();
record.setId(m_id.getUsername());
record.setTimeModified(new java.sql.Timestamp(System.currentTimeMillis()));
if (m_locked) {
record.setIsLocked("1");
} else {
record.setIsLocked("0");
}
if (m_prefs != null) {
JAXBContext ctx = JAXBContext.newInstance(JAXB_BEAN_PACKAGE_NAME);
Marshaller marshaller = ctx.createMarshaller();
ByteArrayOutputStream dataOut = new ByteArrayOutputStream();
marshaller.marshal(m_prefs, dataOut);
record.setData(dataOut.toByteArray());
if (LOG.isDebugEnabled()) {
LOG.debug(MessageFormat.format(
DEBUG_PREF_SETTINGS_AFTER_MARSHALL,
m_id.getUsername(),
new String(dataOut.toByteArray())));
}
}
if (m_new) {
dao.insert(record);
} else {
dao.update(record);
}
m_new = false;
m_info = record;
} catch (PersonalizationDAOException e) {
throw new PersonalizationException (
PersonalizationException.Code.STORE_DATA_ERROR,
e);
} catch (JAXBException e) {
throw new PersonalizationException (
PersonalizationException.Code.SERIALIZATION_ERROR,
e);
}
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IUser#lock()
*/
public void lock () throws PersonalizationException {
if (m_daoProfile == null) {
throw new IllegalStateException ();
}
if (m_id == null) {
throw new IllegalStateException();
}
m_locked = true;
save();
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IUser#unlock()
*/
public void unlock () throws PersonalizationException {
if (m_daoProfile == null) {
throw new IllegalStateException ();
}
if (m_id == null) {
throw new IllegalStateException();
}
m_locked = false;
save();
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.personalization.data.IUser#remove()
*/
public void remove () throws PersonalizationException {
if (m_daoProfile == null) {
throw new IllegalStateException ();
}
if (m_id == null) {
throw new IllegalStateException ();
}
IUserDAO dao = null;
try {
dao = m_daoProfile.getUserDAO();
if (!m_new) {
dao.delete(m_id.getUsername());
}
} catch (PersonalizationDAOException e) {
throw new PersonalizationException (
PersonalizationException.Code.DELETE_DATA_ERROR,
e);
}
}
public void setUserId (IUserId id) {
if (id == null) {
throw new NullPointerException();
}
m_id = id;
}
public void setNew (boolean b) {
m_new = b;
}
public void setPreferenceSettings (PreferenceSettingsType prefs) {
m_prefs = prefs;
}
public void setPersonalizationDAOProfile (IPersonalizationDAOProfile profile) {
if (profile == null) {
throw new NullPointerException();
}
m_daoProfile = profile;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals (Object o) {
if (o == null) {
return false;
}
if (m_id == null) {
return false;
}
if (o == this) {
return true;
}
if (!(o instanceof User)) {
return false;
}
User u = (User) o;
return m_id.equals(u.getId());
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
public int hashCode () {
if (m_id == null) {
return 0;
} else {
return m_id.hashCode();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -