📄 adfpersonalizationcontext.java
字号:
package com.esri.solutions.jitk.common.personalization;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.WebContextInitialize;
import com.esri.solutions.jitk.personalization.PersonalizationException;
import com.esri.solutions.jitk.personalization.data.IMapComposition;
import com.esri.solutions.jitk.personalization.data.IPersonalizationData;
import com.esri.solutions.jitk.personalization.data.IPersonalizationDataFactory;
import com.esri.solutions.jitk.personalization.data.IUserFactory;
import com.esri.solutions.jitk.personalization.data.IUserId;
import com.esri.solutions.jitk.personalization.data.beans.v1.PreferenceSettingsType;
public class ADFPersonalizationContext implements IPersonalizationContext, WebContextInitialize {
public static final String CURRENT_MAP_COMPOSITION_KEY = "com.esri.solutions.jitk.MapComposition";
private static final Logger LOG = LogManager.getLogger(ADFPersonalizationContext.class);
private IPersonalizationDataFactory m_dataFactory;
private IUserId m_userId;
private Map <String, Object> m_attributes;
private WebContext m_context;
private IPersonalizationData m_data;
private IUserFactory m_userFactory;
private DefaultPreferenceSettingsLoader m_defaultPrefLoader;
public ADFPersonalizationContext () {
m_attributes = new HashMap<String, Object>();
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.personalization.IPersonalizationContext#getData()
*/
public IPersonalizationData getData() throws PersonalizationException {
if (m_data == null) {
m_data = m_dataFactory.create(m_userId);
}
return m_data;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.personalization.IPersonalizationContext#getAttribute(java.lang.String)
*/
public Object getAttribute(String key) {
if (key == null) {
throw new NullPointerException();
}
return m_attributes.get(key);
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.personalization.IPersonalizationContext#getAttributes()
*/
public Map<String, Object> getAttributes() {
return m_attributes;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.personalization.IPersonalizationContext#setAttribute(java.lang.String, java.lang.Object)
*/
public void setAttribute(String key, Object value) {
if (key == null) {
throw new NullPointerException();
}
m_attributes.put(key, value);
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.personalization.IPersonalizationContext#setAttributes(java.util.Map)
*/
public void setAttributes(Map<? extends String, ? extends Object> attrs) {
if (attrs == null) {
throw new NullPointerException();
}
m_attributes.putAll(attrs);
}
/**
* Sets the Current User within this {@link IPersonalizationContext}. The
* Current User represents the current logged in User within the application.
* The Personalization Data will be retrieved for this user.
*
* @param user Current logged-in user, cannot be <code>null</code>.
*
* @throws NullPointerException Thrown if the <code>user</code>
* argument is <code>null</code>.
*/
public void setCurrentUserId (IUserId userId) {
if (userId == null) {
throw new NullPointerException();
}
m_userId = userId;
}
/**
* Sets the reference to the {@link IPersonalizationDataFactory} object. This
* object is needed to create {@link IPersonalizationData} objects for this
* object.
*
* @param factory Reference to {@link IPersonalizationDataFactory} object, cannot
* be <code>null</code>.
* @throws NullPointerException Thrown if the <code>factory</code> argument
* is <code>null</code>.
*/
public void setPersonalizationDataFactory (IPersonalizationDataFactory factory) {
if (factory == null) {
throw new NullPointerException();
}
m_dataFactory = factory;
}
/**
* Sets the reference to the {@link IUserFactory} object. A reference to this object
* is needed in order to initialize the Default Preference Settings within the User
* Factory.
*
* @param factory Reference to {@link IUserFactory} object, cannot be <code>null</code>
* @throws NullPointerException Thrown if the <code>factory</code> argument is
* <code>null</code>.
*/
public void setUserFactory (IUserFactory factory) {
if (factory == null) {
throw new NullPointerException ();
}
m_userFactory = factory;
}
/**
* Sets the reference to the {@link DefaultPreferenceSettingsLoader} object. A reference to
* this object is needed in order to initialize the Default Preference Settings within the
* User Factory. This object is needed to unmarshall the Default Preference Settings from an
* XML file within the scope of the web application into the User Factory object.
*
* @param loader Reference to {@link DefaultPreferenceSettingsLoader} object,
* cannot be <code>null</code>.
* @throws NullPointerException Thrown if the <code>loader</code> argument is
* <code>null</code>.
*/
public void setDefaultPreferenceSettingsLoader (DefaultPreferenceSettingsLoader loader) {
if (loader == null) {
throw new NullPointerException ();
}
m_defaultPrefLoader = loader;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.personalization.IPersonalizationContext#getWebContext()
*/
public WebContext getWebContext() {
return m_context;
}
/*
* (non-Javadoc)
* @see com.esri.adf.web.data.WebContextInitialize#destroy()
*/
public void destroy() {
destroyWebContextInitializeObjects();
}
/*
* (non-Javadoc)
* @see com.esri.adf.web.data.WebContextInitialize#init(com.esri.adf.web.data.WebContext)
*/
public void init(WebContext ctx) {
m_context = ctx;
PreferenceSettingsType defaultPrefs = m_defaultPrefLoader.getDefaultPreferenceSettings();
if (defaultPrefs != null) {
m_userFactory.setDefaultPreferenceSettings(defaultPrefs);
}
try {
if (getAttribute(CURRENT_MAP_COMPOSITION_KEY) == null) {
IPersonalizationData data = getData();
IMapComposition mc = data.createMapComposition();
setAttribute(CURRENT_MAP_COMPOSITION_KEY, mc);
}
} catch (PersonalizationException e) {
LOG.warn("", e);
}
initWebContextInitializeObjects();
initPersonalizables();
}
private void initPersonalizables() {
Map<String,Object> attrs = null;
attrs = getAttributes();
if (attrs == null) {
return;
}
Collection<Object> values = attrs.values();
for (Object obj : values) {
if (obj instanceof IPersonalizable) {
IPersonalizable p = (IPersonalizable) obj;
p.setPersonalizationContext(this);
}
}
}
private void initWebContextInitializeObjects () {
Map<String,Object> attrs = null;
attrs = getAttributes();
if (attrs == null) {
return;
}
Collection<Object> values = attrs.values();
for (Object obj : values) {
if (obj instanceof WebContextInitialize) {
WebContextInitialize wci = (WebContextInitialize) obj;
wci.init(m_context);
}
}
}
private void destroyWebContextInitializeObjects () {
Map<String,Object> attrs = null;
attrs = getAttributes();
if (attrs == null) {
return;
}
Collection<Object> values = attrs.values();
for (Object obj : values) {
if (obj instanceof WebContextInitialize) {
WebContextInitialize wci = (WebContextInitialize) obj;
wci.destroy();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -