📄 defaultsavemapcomposition.java
字号:
package com.esri.solutions.jitk.web.tasks.mapcomp;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.xml.bind.JAXBException;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.esri.adf.web.data.WebContext;
import com.esri.adf.web.data.geometry.WebExtent;
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.personalization.data.beans.v1.AttributeCollectionType;
import com.esri.solutions.jitk.personalization.data.beans.v1.AttributeType;
import com.esri.solutions.jitk.personalization.data.beans.v1.EnvelopeN;
import com.esri.solutions.jitk.personalization.data.beans.v1.MapCompositionType;
import com.esri.solutions.jitk.personalization.data.beans.v1.ObjectFactory;
import com.esri.solutions.jitk.personalization.data.beans.v1.PreviewType;
import com.esri.solutions.jitk.personalization.data.beans.v1.ServiceListType;
import com.esri.solutions.jitk.personalization.data.beans.v1.SpatialReferenceType;
/**
* Default implementation of {@link ISaveMapComposition} in order to provide
* a default implementation of saving Map Compositions. This implementation will use
* an {@link IADFToBeanConverter} object that will convert ADF objects to their Map Composition
* Bean counterparts. This implementation will only save the following data within a Map
* Composition:
*
* <ul>
* <li>Services
* <li>Spatial Reference
* <li>Initial Extent
* </ul>
*/
public class DefaultSaveMapComposition implements ISaveMapComposition, IPersonalizable {
private static final String ERROR_NULL_ADF_TO_BEAN_CONVERTER = "ADF To Bean Converter object is required and cannot be null.";
private static final String ERROR_NULL_PERS_CTX = "Personalization Context object is required, and cannot be null.";
private static final String ERROR_NULL_PREVIEW_CREATOR = "Preview Bean Creator is required, and cannot be null.";
private static final Logger LOG = LogManager.getLogger(DefaultSaveMapComposition.class);
/**
* Reference to the {@link IPersonalizationContext}
*/
private IPersonalizationContext m_persCtx;
/**
* Reference to a converter object that converts ADF objects to
* Map Composition Bean object.
*/
private IADFToBeanConverter m_converter;
/**
* List of messages that are set during operations.
*/
private final List<String> m_messages;
/**
* Reference to bean to creates a Preview Bean for within a Map Composition.
*/
private IPreviewBeanCreator m_previewCreator;
/**
* Constructs a new <code>DefaultSaveMapComposition</code> object.
*/
public DefaultSaveMapComposition () {
m_messages = new ArrayList<String>();
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.web.tasks.mapcomp.ISaveMapComposition#save(com.esri.solutions.jitk.web.tasks.mapcomp.ISaveMapCompositionFormData)
*/
public IMapComposition save(ISaveMapCompositionFormData formData) throws PersonalizationException {
IMapComposition mc = null;
if (formData == null) {
throw new NullPointerException();
}
if (m_persCtx == null) {
LOG.error(ERROR_NULL_PERS_CTX);
throw new IllegalStateException (ERROR_NULL_PERS_CTX);
}
if (m_converter == null) {
LOG.error(ERROR_NULL_ADF_TO_BEAN_CONVERTER);
throw new IllegalStateException (ERROR_NULL_ADF_TO_BEAN_CONVERTER);
}
m_messages.clear();
if (!formData.isNew()) {
/////////////////////////////////////////////////////////////////////////
// If the form data indicates that the Map Composition is not new, then
// use the ID from formData to retrieve the Map Composition from
// Personalization. This Map Composition will be used to store the data
/////////////////////////////////////////////////////////////////////////
UUID id = UUID.fromString(formData.getId());
mc = m_persCtx.getData().getMapComposition(id);
} else {
/////////////////////////////////////////////////////////////////////////
// Form Data indicates that the Map Composition should be new, so
// create a new Map Composition within Personalization. This new
// Map Composition will be used to store the data.
/////////////////////////////////////////////////////////////////////////
mc = m_persCtx.getData().createMapComposition();
}
MapCompositionType bean = mc.getMapComposition();
bean.setName(formData.getName());
bean.setDescription(formData.getDescription());
update(mc);
mc.save();
return mc;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.web.tasks.mapcomp.ISaveMapComposition#update(com.esri.solutions.jitk.personalization.data.IMapComposition)
*/
@SuppressWarnings("unchecked")
public void update (IMapComposition mc) throws PersonalizationException {
if (mc == null) {
throw new NullPointerException ();
}
if (m_persCtx == null) {
LOG.error(ERROR_NULL_PERS_CTX);
throw new IllegalStateException (ERROR_NULL_PERS_CTX);
}
if (m_converter == null) {
LOG.error(ERROR_NULL_ADF_TO_BEAN_CONVERTER);
throw new IllegalStateException (ERROR_NULL_ADF_TO_BEAN_CONVERTER);
}
if (m_previewCreator == null) {
LOG.error(ERROR_NULL_PREVIEW_CREATOR);
throw new IllegalStateException (ERROR_NULL_PREVIEW_CREATOR);
}
MapCompositionType bean = null;
ServiceListType services = null;
SpatialReferenceType sr = null;
EnvelopeN initial = null;
WebContext ctx = null;
ctx = m_persCtx.getWebContext();
m_messages.clear();
bean = mc.getMapComposition();
bean.setOrderedLayers(null);
bean.setToc(null);
services = m_converter.createServiceListType(ctx);
if (services != null) {
bean.setServices(services);
} else {
m_messages.add(TextResources.getResourceString("savemapcomp.ui.msg.error.saveServices"));
}
sr = m_converter.createSpatialReference(ctx.getSpatialReference());
if (sr != null) {
bean.setSpatialReference(sr);
} else {
m_messages.add(TextResources.getResourceString("savemapcomp.ui.msg.error.saveSpatialReference"));
}
if (ctx.getWebMap() != null) {
initial = m_converter.createEnvelope(ctx.getWebMap().getCurrentExtent());
if (initial != null) {
bean.setInitialEnvelope(initial);
} else {
m_messages.add(TextResources.getResourceString("savemapcomp.ui.msg.error.saveEnvelope"));
}
StringBuilder fullExtentStr = new StringBuilder();
WebExtent fullExtent = ctx.getWebMap().getFullExtent();
if (fullExtent.getSpatialReference() == null) {
fullExtent.setSpatialReference(ctx.getSpatialReference());
}
fullExtentStr.append(fullExtent.getMinX());
fullExtentStr.append(",");
fullExtentStr.append(fullExtent.getMinY());
fullExtentStr.append(",");
fullExtentStr.append(fullExtent.getMaxX());
fullExtentStr.append(",");
fullExtentStr.append(fullExtent.getMaxY());
fullExtentStr.append(",");
fullExtentStr.append(fullExtent.getSpatialReference().getDefinitionString());
try {
AttributeCollectionType attrs = new ObjectFactory().createAttributes();
AttributeType fullExtentAttr = new ObjectFactory().createAttributeType();
fullExtentAttr.setName("FullExtent");
fullExtentAttr.setValue(fullExtentStr.toString());
attrs.getAttribute().add(fullExtentAttr);
bean.setAttributes(attrs);
} catch (JAXBException e) {
LOG.warn("JAXBException occurred", e);
}
} else {
m_messages.add(TextResources.getResourceString("savemapcomp.ui.msg.error.saveEnvelope"));
}
if (ctx.getWebMap() != null) {
PreviewType preview = m_previewCreator.createPreview();
if (preview != null) {
bean.setPreview(preview);
}
}
}
/*
* (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 instance of an {@link IADFToBeanConverter} object to use
* internally.
*
* @param converter Converts ADF objects into Map Composition Beans
*/
public void setConverter (IADFToBeanConverter converter) {
if (converter == null) {
throw new NullPointerException();
}
m_converter = converter;
}
/**
* Sets the Preview Bean Creator object to use to create a Preview
* bean. A Preview Bean Creator object is required and cannot
* be <code>null</code>.
*
* @param creator Reference to Preview Bean Creator, cannot be
* <code>null</code>.
* @throws NullPointerException Thrown if the <code>creator</code>
* argument is <code>null</code>.
*/
public void setPreviewBeanCreator (IPreviewBeanCreator creator) {
if (creator == null) {
throw new NullPointerException ();
}
m_previewCreator = creator;
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.web.tasks.mapcomp.ISaveMapComposition#clearMessages()
*/
public void clearMessages() {
m_messages.clear();
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.web.tasks.mapcomp.ISaveMapComposition#getMessages()
*/
public List<String> getMessages() {
return new ArrayList<String>(m_messages);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -