📄 datatransferobjectfactory.java
字号:
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
* This code is licensed under the GPL 2.0 license, availible at the root
* application directory.
*/
package org.vfny.geoserver.global.dto;
import com.vividsolutions.jts.geom.Envelope;
import org.geotools.feature.AttributeType;
import org.geotools.feature.FeatureType;
import org.vfny.geoserver.global.xml.NameSpaceElement;
import org.vfny.geoserver.global.xml.NameSpaceTranslator;
import org.vfny.geoserver.global.xml.NameSpaceTranslatorFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Generate Data Transfer Objects from "real" objects in the system.
*
* <p>
* This class is used to isolate the DTO from the details of generating them.
* This allows DTO objects to be safely used as a wire protocol with out
* unrequired dependencies on such things as AttributeType and FeatureType.
* </p>
*
* <p>
* This class may choose to opperate as a facade on the services of global.xml?
* </p>
*
* @author jgarnett, Refractions Research, Inc.
* @author $Author: dmzwiers $ (last modification)
* @version $Id: DataTransferObjectFactory.java 6893 2007-05-29 09:38:46Z groldan $
*/
public class DataTransferObjectFactory {
/**
* Construct DTO based on provided AttributeType.
*
* <p>
* GMLUtils is used to provide the mapping from
* attributeType.getName/attributeType.getType() to an XML type/fragement.
* </p>
*
* @param attributeType Real geotools2 AttributeType
*
* @return Data Transfer Object for provided attributeType
*/
public static AttributeTypeInfoDTO create(String schemaBase, AttributeType attributeType) {
AttributeTypeInfoDTO dto = new AttributeTypeInfoDTO();
dto.setName(attributeType.getName());
if (isManditory(schemaBase, attributeType.getName()) || (attributeType.getMinOccurs() > 0)) {
dto.setMinOccurs(1);
} else {
dto.setMinOccurs(0);
}
dto.setMaxOccurs(1);
dto.setNillable(attributeType.isNillable());
NameSpaceTranslator xs = NameSpaceTranslatorFactory.getInstance()
.getNameSpaceTranslator("xs");
NameSpaceTranslator gml = NameSpaceTranslatorFactory.getInstance()
.getNameSpaceTranslator("gml");
NameSpaceElement element;
element = xs.getElement(attributeType.getType(), attributeType.getName());
if (element == null) {
element = gml.getElement(attributeType.getType(), attributeType.getName());
}
if (element == null) {
element = xs.getElement("string");
}
// element = xs.getElement( attributeType.getName() );
// if(element == null) element = gml.getElement( attributeType.getName() );
// if(element == null) element = xs.getElement( "string" );
dto.setComplex(false);
dto.setType(element.getTypeRefName());
return dto;
}
/**
* Construct any of the well-known GML attributeTypes.
* <p>
* SchemaBase is used to ensure that attribute required by the XMLSchema
* have a minOccurs of 1.
* </p>
* <p>
* This method uses NameSpaceTranslatorFactorys for xs and gml in order to
* provide accurate type information describing the provided attribute
* </p>
* @param schemaBase used to determine manditory attributes
* @param attributeName Name of attribute being described
* @return DataTransferObject encapsulating attribute information.
*/
public static AttributeTypeInfoDTO create(String schemaBase, String attributeName) {
AttributeTypeInfoDTO dto = new AttributeTypeInfoDTO();
dto.setName(attributeName);
dto.setMinOccurs(isManditory(schemaBase, attributeName) ? 1 : 0);
dto.setMaxOccurs(1);
dto.setNillable(true); // nillable by default?
NameSpaceTranslator xs = NameSpaceTranslatorFactory.getInstance()
.getNameSpaceTranslator("xs");
NameSpaceTranslator gml = NameSpaceTranslatorFactory.getInstance()
.getNameSpaceTranslator("gml");
NameSpaceElement element;
element = xs.getElement(attributeName);
if (element == null) {
element = gml.getElement(attributeName);
}
if (element == null) {
element = xs.getElement("string");
}
dto.setComplex(false);
dto.setType(element.getTypeRefName());
return dto;
}
/**
* Construct DTO based on provided schema.
*
* <p>
* GMLUtils is used to provide the mapping to an XML type/fragement for
* each attribute
* </p>
*
* @param dataStoreId Used as a backpointer to locate dataStore
* @param schema Real geotools2 FeatureType
*
* @return Data Transfer Object for provided schema
*/
public static FeatureTypeInfoDTO create(String dataStoreId, FeatureType schema) {
FeatureTypeInfoDTO dto = new FeatureTypeInfoDTO();
dto.setAbstract(null);
dto.setDataStoreId(dataStoreId);
dto.setDefaultStyle("styles/normal.sld");
dto.setDefinitionQuery(null); // no extra restrictions
dto.setDirName(dataStoreId + "_" + schema.getTypeName());
dto.setKeywords(Collections.EMPTY_LIST);
dto.setLatLongBBox(new Envelope());
dto.setNativeBBox(new Envelope());
dto.setName(schema.getTypeName());
dto.setNumDecimals(8);
dto.setSchemaAttributes(generateAttributes(schema));
NameSpaceTranslator gml = NameSpaceTranslatorFactory.getInstance()
.getNameSpaceTranslator("gml");
String schemaBase = gml.getElement("AbstractFeatureType").getQualifiedTypeDefName();
dto.setSchemaBase(schemaBase);
dto.setSchemaName(dataStoreId.toUpperCase() + "_" + schema.getTypeName().toUpperCase()
+ "_TYPE");
dto.setSRS(schema.getDefaultGeometry().getGeometryFactory().getSRID());
dto.setTitle(schema.getNamespace() + " " + schema.getTypeName());
return dto;
}
/**
* List of attributes DTO information gernated from schema.
*
* @param schema
* @return
*/
public static List generateAttributes(FeatureType schema) {
AttributeType[] attributes = schema.getAttributeTypes();
List list = new ArrayList(attributes.length);
for (int i = 0; i < attributes.length; i++) {
list.add(create("AbstractFeatureType", attributes[i]));
}
return list;
}
/**
* List of attribtue DTO information generated from schemaBase.
* <p>
* Please note this is currently only used for display by TypesForm,
* TypeInfo simply makes use of getRequiredBaseAttributes to select
* AttributeTypes from the FeatureType schema.
* </p>
* <p>
* More specifically the values of isNillable, minOccurs and maxOccurs
* provided by the DataStore may not agree with the results of this
* function. TypeInfo opperatates on the assumption minOccurs=1, maxOccurs=1
* and AttributeType.isNillable() is correct.
* </p>
* @param schemaBase SchemaBase
* @return List of AttributeTypeInfoDTO representative of schemaBase required
* Attributes
*/
public static List generateRequiredAttributes(String schemaBase) {
String[] attributeNames = getRequiredBaseAttributes(schemaBase);
List list = new ArrayList(attributeNames.length);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -