📄 savemapcompositiontask.java
字号:
package com.esri.solutions.jitk.web.tasks.mapcomp;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.esri.adf.web.faces.event.TaskEvent;
import com.esri.solutions.jitk.common.personalization.ADFPersonalizationContext;
import com.esri.solutions.jitk.common.personalization.IPersonalizable;
import com.esri.solutions.jitk.common.personalization.IPersonalizationContext;
import com.esri.solutions.jitk.common.resources.TextResources;
import com.esri.solutions.jitk.personalization.PersonalizationException;
import com.esri.solutions.jitk.personalization.data.IMapComposition;
import com.esri.solutions.jitk.web.tasks.RenderControlledTask;
/**
* Task to Save a Map Composition. If the Map Composition is new, this Task
* will allow the user to enter a Name and a Description for the Map Composition. If the
* Map Composition is not new, then the user will have the option of saving the current
* map (user will not be able to set the name or description) or save to a new Map Composition
* (user will be able to set the name and description).
*/
public class SaveMapCompositionTask extends RenderControlledTask implements IPersonalizable, Serializable {
private static final long serialVersionUID = 8079440565837607282L;
private static final String ERROR_NULL_MAP_COMP_SAVER =
"ISaveMapComposition object is required and cannot be null.";
private static final String ERROR_NULL_PERS_CTX =
"Personalization Context object is required and cannot be null.";
/**
* Implementation of {@link ISaveMapCompositionFormData} to expose the
* Task Parameter values through the interface in order to save a Map
* Composition.
*/
private class FormData implements ISaveMapCompositionFormData, Serializable {
private static final long serialVersionUID = 2847917872596238715L;
/**
* Description entered by User.
*/
private String m_description;
/**
* Name entered by User.
*/
private String m_name;
/**
* Save output destination entered by User.
*/
private String m_saveto;
/**
* Flag indicating if the Map Composition is new.
*/
private boolean m_isNew = true;
/**
* Returns the Description entered by the User.
*
* @return Description value or <code>null</code> if
* description has not been set.
*/
public String getDescription() {
return m_description;
}
/**
* Returns the ID of the Map Composition. If the Map
* Composition is not new, then the ID of the Current
* Map Composition is returned, otherwise <code>null</code>
* is returned.
*
* @return Map Composition ID.
*/
public String getId() {
if (!isNew()) {
return getCurrentMapComposition().getId().toString();
}
return null;
}
/**
* Returns the Name of the Map Composition entered by the
* user.
*
* @return Name of Map Composition or <code>null</code> if
* name has not been set.
*/
public String getName() {
return m_name;
}
/**
* Returns a flag indicating if the Map Composition is new.
*
* @return IsNew flag.
*/
public boolean isNew() {
return m_isNew;
}
/**
* Sets the description of the Map Composition. Leading and
* trailing whitespace will be removed.
*
* @param description Description of Map Composition.
*/
public void setDescription(String description) {
m_description = description;
if (m_description != null) {
m_description = m_description.trim();
}
}
/**
* Sets the name of the Map Composition. Leading and
* trailing whitespace will be removed.
*
* @param name Name of Map Composition.
*/
public void setName(String name) {
m_name = name;
if (m_name != null) {
m_name = m_name.trim();
}
}
/**
* Sets the IsNew flag to indicate if the Map Composition
* is new.
*
* @param isNew
*/
public void setNew(boolean isNew) {
m_isNew = isNew;
}
public String getSaveto() {
if(this.m_saveto == null || this.m_saveto.length() == 0) {
this.m_saveto = getSavetoRepository();
}
return this.m_saveto;
}
public void setSaveto(String s) {
this.m_saveto = s;
}
public String getSavetoRepository() {
return "repository";
}
public String getSavetoWMCfile() {
return "wmcfile";
}
}
/**
* Logger to use to log messages from this class.
*/
private static final Logger LOG = LogManager.getLogger(SaveMapCompositionTask.class);
/**
* Reference to instance of {@link ISaveMapComposition} for saving
* the Map Composition.
*/
private transient ISaveMapComposition m_saver;
/**
* Reference to Personalization Context for access to personalization data
* and functions.
*/
private transient IPersonalizationContext m_persCtx;
/**
* Reference to a Map Composition Event producer.
*/
private transient IMapCompositionEventProducer m_eventProducer;
/**
* Contains the data entered by the User into the form.
*/
private final FormData m_formData;
/**
* Indicates if a new Map Composition should be created on a name change between
* the User entry and the current Map Composition.
*/
private boolean m_createNewOnNameChange;
/**
* Constructs a new <code>SaveMapCompositionTask</code>.
*/
public SaveMapCompositionTask () {
this.setTaskInfo(new SaveMapCompositionTaskInfo());
m_formData = new FormData();
m_createNewOnNameChange = true;
}
/* (non-Javadoc)
* @see com.esri.solutions.jitk.web.tasks.mapcomp.ISaveMapCompositionFormData#getName()
*/
public String getName () {
return m_formData.getName();
}
/**
* Sets the name of the Map Composition that was inputed by the user.
*
* @param name Name of the Map Composition inputed by the user.
*/
public void setName (String name) {
m_formData.setName(name);
}
/* (non-Javadoc)
* @see com.esri.solutions.jitk.web.tasks.mapcomp.ISaveMapCompositionFormData#getDescription()
*/
public String getDescription () {
return m_formData.getDescription();
}
/**
* Sets the description of the Map Composition that was inputed by the
* user.
*
* @param desc Description of the Map Composition.
*/
public void setDescription (String desc) {
m_formData.setDescription(desc);
}
public String getSaveto() {
return m_formData.getSaveto();
}
public void setSaveto(String s) {
m_formData.setSaveto(s);
}
public String getSavetoRepository() {
return m_formData.getSavetoRepository();
}
public String getSavetoWMCfile() {
return m_formData.getSavetoWMCfile();
}
/**
* Returns the name of the WebContext. This is used to build up
* a URL in order to retrieve the current map image.
*
* @return WebContext name.
*/
public String getWebContextName () {
return this._context.getName();
}
/**
* Saves the Map Composition. If the Save Operation is SAVE_NEW then a
* new Map Composition is created, saved, and stored within the
* Personalization Context. If the Save Operation is SAVE_CURRENT,
* then the Map Composition is just saved.
*
* @param event Event information.
* @throws IllegalStateException Thrown if the Personalization Context and
* Map Composition Saver objects have
* not been injected via their respective setter methods.
*/
public void save (TaskEvent event) {
String msg = "";
if (m_persCtx == null) {
throw new IllegalStateException(ERROR_NULL_PERS_CTX);
}
if (m_saver == null) {
throw new IllegalStateException(ERROR_NULL_MAP_COMP_SAVER);
}
String name = getName();
if (name == null || name.trim().length() == 0) {
this.renderResourceMessage("savemapcomp.ui.name.blank.warning", messageType.WARNING);
return;
}
if(!getSaveto().equalsIgnoreCase(getSavetoRepository())) {
requestTaskRender();
return;
}
List<String> messages = new ArrayList<String>();
try {
IMapComposition currentMc = getCurrentMapComposition();
///////////////////////////////////////////////////////////
// If the Current Map Composition is not new, then we need
// to compare the Name (user entered vs. current map comp
// name) to see if they are different. If different, then
// a new Map Comp should be created.
///////////////////////////////////////////////////////////
if (!currentMc.isNew()) {
if (!currentMc.getName().equalsIgnoreCase(m_formData.getName())) {
m_formData.setNew(m_createNewOnNameChange);
} else {
m_formData.setNew(false);
}
} else {
m_formData.setNew(true);
}
////////////////////////////////////////////////////////////////////////////////////////
// Use the saver to save the Map Composition. The Map Composition returned should
// be set within the Personalization Context.
////////////////////////////////////////////////////////////////////////////////////////
IMapComposition savedMc = m_saver.save(m_formData);
m_persCtx.setAttribute(ADFPersonalizationContext.CURRENT_MAP_COMPOSITION_KEY, savedMc);
if (m_eventProducer != null) {
m_eventProducer.push(new MapCompositionEvent(this, MapCompositionEvent.MapCompositionEventType.SAVED, savedMc));
}
messages.addAll(m_saver.getMessages());
msg = MessageFormat.format(TextResources.getResourceString("savemapcomp.ui.msg.saveSuccess"), savedMc.getName());
if (LOG.isInfoEnabled()) {
LOG.info(msg);
}
} catch (PersonalizationException e) {
messages.addAll(m_saver.getMessages());
LOG.warn("", e);
} catch (Exception e) {
messages.addAll(m_saver.getMessages());
LOG.warn("", e);
} finally {
if(messages.isEmpty()) {
this.renderMessage(msg, messageType.SUCCESS);
} else {
renderErrors(messages);
}
requestTaskRender();
}
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.personalization.IPersonalizable#setPersonalizationContext(com.esri.solutions.jitk.common.personalization.IPersonalizationContext)
*/
public void setPersonalizationContext(IPersonalizationContext ctx) {
if (ctx == null) {
throw new NullPointerException ();
}
m_persCtx = ctx;
}
/**
* Sets the {@link ISaveMapComposition} object. This object will be used
* by this task to Save the Map composition.
*
*
* @param saver Instance of {@link ISaveMapComposition} to save the Map
* Composition, cannot be <code>null</code>.
*
* @throws NullPointerException Thrown if the <code>saver</code>
* argument is <code>null</code>.
*/
public void setSaveMapComposition (ISaveMapComposition saver) {
if (saver == null) {
throw new NullPointerException();
}
m_saver = saver;
}
/**
* Sets the Map Composition Event Producer object. If this object is specified then CREATED and
* SAVED events will be pushed to the Map Composition Event Observers within the producer object.
*
* @param producer Produces Map Composition Events to a set of observers, can be <code>null</code>.
*/
public void setMapCompositionEventProducer (IMapCompositionEventProducer producer) {
m_eventProducer = producer;
}
/**
* Set to <code>true</code> if a new Map Composition should be created when there is a Name property
* change between what the User entered in the form and what is in the current Map Composition.
*
* <p>
* Set to <code>false</code> if the current Map Composition should be updated with the Name value
* entered by the User.
* </p>
*
* @param flag
*/
public void setCreateNewOnNameChange (boolean flag) {
m_createNewOnNameChange = flag;
}
/**
* Indicates if a new Map Composition should be created when the name values
* differ between the user's entry and the name of the Map Composition.
*
* @return <code>true</code> if new Map Composition should be created when
* names are different, and <code>false</code> if not.
*/
public boolean getCreateNewOnNameChange () {
return m_createNewOnNameChange;
}
/**
* Helper method to retrieve the current Map Composition from the
* Personalization Context.
*
* @return Current Map Composition
*/
private IMapComposition getCurrentMapComposition() {
IMapComposition mc = (IMapComposition) m_persCtx.getAttribute(
ADFPersonalizationContext.CURRENT_MAP_COMPOSITION_KEY);
return mc;
}
/**
* Helper method to push the list of error messages to the Error Producer.
*
* @param messages List of error messages.
*/
private void renderErrors (List<String> messages) {
for (String msg : messages) {
renderMessage(msg);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -