Added filled property. Made Filled and Mass optional by using objects instead of primitives. Changed the serialization process so only properties set in the ComponentPreset are serialized. Change the deserialization process so if properties are not specified in the xml file, they are not assigned.

This commit is contained in:
Kevin Ruland 2012-04-26 18:18:00 +00:00
parent 727e388482
commit 85fe891980

View File

@ -21,112 +21,139 @@ import java.util.List;
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public abstract class BaseComponentDTO { public abstract class BaseComponentDTO {
@XmlElement(name = "Manufacturer") @XmlElement(name = "Manufacturer")
private String manufacturer; private String manufacturer;
@XmlElement(name = "PartNumber") @XmlElement(name = "PartNumber")
private String partNo; private String partNo;
@XmlElement(name = "Description") @XmlElement(name = "Description")
private String description; private String description;
@XmlElement(name = "Material") @XmlElement(name = "Material")
private AnnotatedMaterialDTO material; private AnnotatedMaterialDTO material;
@XmlElement(name = "Mass") @XmlElement(name = "Mass")
private double mass; private Double mass;
@XmlElement(name="Filled")
private Boolean filled;
/** /**
* Default constructor. * Default constructor.
*/ */
protected BaseComponentDTO() { protected BaseComponentDTO() {
} }
/** /**
* Constructor. * Constructor.
* *
* @param preset the preset to use to pull data values out of * @param preset the preset to use to pull data values out of
* *
* @throws net.sf.openrocket.util.BugException thrown if the expected body tube keys are not in the preset * @throws net.sf.openrocket.util.BugException thrown if the expected body tube keys are not in the preset
*/ */
protected BaseComponentDTO(final ComponentPreset preset) { protected BaseComponentDTO(final ComponentPreset preset) {
setManufacturer(preset.getManufacturer().getSimpleName()); setManufacturer(preset.getManufacturer().getSimpleName());
setPartNo(preset.getPartNo()); setPartNo(preset.getPartNo());
setDescription(preset.get(ComponentPreset.DESCRIPTION)); if ( preset.has(ComponentPreset.DESCRIPTION )) {
setMaterial(new AnnotatedMaterialDTO(preset.get(ComponentPreset.MATERIAL))); setDescription(preset.get(ComponentPreset.DESCRIPTION));
if (preset.has(ComponentPreset.MASS)) { }
setMass(preset.get(ComponentPreset.MASS)); if ( preset.has(ComponentPreset.MATERIAL)) {
} setMaterial(new AnnotatedMaterialDTO(preset.get(ComponentPreset.MATERIAL)));
} }
if (preset.has(ComponentPreset.MASS)) {
setMass(preset.get(ComponentPreset.MASS));
}
if ( preset.has(ComponentPreset.FILLED) ) {
setFilled( preset.get(ComponentPreset.FILLED));
}
}
public String getManufacturer() { public String getManufacturer() {
return manufacturer; return manufacturer;
} }
public void setManufacturer(final String theManufacturer) { public void setManufacturer(final String theManufacturer) {
manufacturer = theManufacturer; manufacturer = theManufacturer;
} }
public String getPartNo() { public String getPartNo() {
return partNo; return partNo;
} }
public void setPartNo(final String thePartNo) { public void setPartNo(final String thePartNo) {
partNo = thePartNo; partNo = thePartNo;
} }
public String getDescription() { public String getDescription() {
return description; return description;
} }
public void setDescription(final String theDescription) { public void setDescription(final String theDescription) {
description = theDescription; description = theDescription;
} }
public AnnotatedMaterialDTO getMaterial() { public AnnotatedMaterialDTO getMaterial() {
return material; return material;
} }
public void setMaterial(final AnnotatedMaterialDTO theMaterial) { public void setMaterial(final AnnotatedMaterialDTO theMaterial) {
material = theMaterial; material = theMaterial;
} }
public double getMass() { public double getMass() {
return mass; return mass;
} }
public void setMass(final double theMass) { public void setMass(final double theMass) {
mass = theMass; mass = theMass;
} }
public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException; public Boolean getFilled() {
return filled;
}
void addProps(TypedPropertyMap props, List<MaterialDTO> materialList) { public void setFilled(Boolean filled) {
props.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer(manufacturer)); this.filled = filled;
props.put(ComponentPreset.PARTNO, partNo); }
props.put(ComponentPreset.DESCRIPTION, description);
props.put(ComponentPreset.MATERIAL, find(materialList, material));
props.put(ComponentPreset.MASS, mass);
}
private Material find(List<MaterialDTO> materialList, AnnotatedMaterialDTO dto) { public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException;
for (int i = 0; i < materialList.size(); i++) {
MaterialDTO materialDTO = materialList.get(i);
if (materialDTO.getType().name().equals(dto.type) && materialDTO.getName().equals(dto.material)) {
return materialDTO.asMaterial();
}
}
//Otherwise fallback and look at factory default materials.
return Databases.findMaterial(Material.Type.valueOf(material.type), material.material);
}
static class AnnotatedMaterialDTO { void addProps(TypedPropertyMap props, List<MaterialDTO> materialList) {
@XmlAttribute(name = "Type") props.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer(manufacturer));
private String type; props.put(ComponentPreset.PARTNO, partNo);
@XmlValue if ( description != null ) {
private String material; props.put(ComponentPreset.DESCRIPTION, description);
}
Material m = find(materialList, material);
if ( m != null ) {
props.put(ComponentPreset.MATERIAL, find(materialList, material));
}
if ( mass != null ) {
props.put(ComponentPreset.MASS, mass);
}
if ( filled != null ) {
props.put(ComponentPreset.FILLED, filled);
}
}
AnnotatedMaterialDTO() {} private Material find(List<MaterialDTO> materialList, AnnotatedMaterialDTO dto) {
for (int i = 0; i < materialList.size(); i++) {
MaterialDTO materialDTO = materialList.get(i);
if (materialDTO.getType().name().equals(dto.type) && materialDTO.getName().equals(dto.material)) {
return materialDTO.asMaterial();
}
}
//Otherwise fallback and look at factory default materials.
return Databases.findMaterial(Material.Type.valueOf(material.type), material.material);
}
AnnotatedMaterialDTO(Material theMaterial) { static class AnnotatedMaterialDTO {
type = theMaterial.getType().name(); @XmlAttribute(name = "Type")
material = theMaterial.getName(); private String type;
} @XmlValue
} private String material;
AnnotatedMaterialDTO() {}
AnnotatedMaterialDTO(Material theMaterial) {
type = theMaterial.getType().name();
material = theMaterial.getName();
}
}
} }