2 package net.sf.openrocket.preset.xml;
4 import net.sf.openrocket.database.Databases;
5 import net.sf.openrocket.material.Material;
6 import net.sf.openrocket.motor.Manufacturer;
7 import net.sf.openrocket.preset.ComponentPreset;
8 import net.sf.openrocket.preset.InvalidComponentPresetException;
9 import net.sf.openrocket.preset.TypedPropertyMap;
11 import javax.xml.bind.annotation.XmlAccessType;
12 import javax.xml.bind.annotation.XmlAccessorType;
13 import javax.xml.bind.annotation.XmlAttribute;
14 import javax.xml.bind.annotation.XmlElement;
15 import javax.xml.bind.annotation.XmlValue;
16 import java.util.List;
19 * Base class for the external representation of all component presets.
21 @XmlAccessorType(XmlAccessType.FIELD)
22 public abstract class BaseComponentDTO {
24 @XmlElement(name = "Manufacturer")
25 private String manufacturer;
26 @XmlElement(name = "PartNumber")
27 private String partNo;
28 @XmlElement(name = "Description")
29 private String description;
30 @XmlElement(name = "Material")
31 private AnnotatedMaterialDTO material;
32 @XmlElement(name = "Mass")
36 * Default constructor.
38 protected BaseComponentDTO() {
44 * @param preset the preset to use to pull data values out of
46 * @throws net.sf.openrocket.util.BugException thrown if the expected body tube keys are not in the preset
48 protected BaseComponentDTO(final ComponentPreset preset) {
49 setManufacturer(preset.getManufacturer().getSimpleName());
50 setPartNo(preset.getPartNo());
51 setDescription(preset.get(ComponentPreset.DESCRIPTION));
52 setMaterial(new AnnotatedMaterialDTO(preset.get(ComponentPreset.MATERIAL)));
53 if (preset.has(ComponentPreset.MASS)) {
54 setMass(preset.get(ComponentPreset.MASS));
58 public String getManufacturer() {
62 public void setManufacturer(final String theManufacturer) {
63 manufacturer = theManufacturer;
66 public String getPartNo() {
70 public void setPartNo(final String thePartNo) {
74 public String getDescription() {
78 public void setDescription(final String theDescription) {
79 description = theDescription;
82 public AnnotatedMaterialDTO getMaterial() {
86 public void setMaterial(final AnnotatedMaterialDTO theMaterial) {
87 material = theMaterial;
90 public double getMass() {
94 public void setMass(final double theMass) {
98 public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException;
100 void addProps(TypedPropertyMap props, List<MaterialDTO> materialList) {
101 props.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer(manufacturer));
102 props.put(ComponentPreset.PARTNO, partNo);
103 props.put(ComponentPreset.DESCRIPTION, description);
104 props.put(ComponentPreset.MATERIAL, find(materialList, material));
105 props.put(ComponentPreset.MASS, mass);
108 private Material find(List<MaterialDTO> materialList, AnnotatedMaterialDTO dto) {
109 for (int i = 0; i < materialList.size(); i++) {
110 MaterialDTO materialDTO = materialList.get(i);
111 if (materialDTO.getType().name().equals(dto.type) && materialDTO.getName().equals(dto.material)) {
112 return materialDTO.asMaterial();
115 //Otherwise fallback and look at factory default materials.
116 return Databases.findMaterial(Material.Type.valueOf(material.type), material.material);
119 static class AnnotatedMaterialDTO {
120 @XmlAttribute(name = "Type")
123 private String material;
125 AnnotatedMaterialDTO() {}
127 AnnotatedMaterialDTO(Material theMaterial) {
128 type = theMaterial.getType().name();
129 material = theMaterial.getName();