2 package net.sf.openrocket.preset.xml;
6 import javax.xml.bind.annotation.XmlAccessType;
7 import javax.xml.bind.annotation.XmlAccessorType;
8 import javax.xml.bind.annotation.XmlAttribute;
9 import javax.xml.bind.annotation.XmlElement;
10 import javax.xml.bind.annotation.XmlValue;
12 import net.sf.openrocket.database.Databases;
13 import net.sf.openrocket.material.Material;
14 import net.sf.openrocket.motor.Manufacturer;
15 import net.sf.openrocket.preset.ComponentPreset;
16 import net.sf.openrocket.preset.InvalidComponentPresetException;
17 import net.sf.openrocket.preset.TypedPropertyMap;
18 import net.sf.openrocket.unit.UnitGroup;
21 * Base class for the external representation of all component presets.
23 @XmlAccessorType(XmlAccessType.FIELD)
24 public abstract class BaseComponentDTO {
26 @XmlElement(name = "Manufacturer")
27 private String manufacturer;
28 @XmlElement(name = "PartNumber")
29 private String partNo;
30 @XmlElement(name = "Description")
31 private String description;
32 @XmlElement(name = "Material")
33 private AnnotatedMaterialDTO material;
34 @XmlElement(name = "Mass")
35 private AnnotatedMassDTO mass;
36 @XmlElement(name="Filled")
37 private Boolean filled;
40 * Default constructor.
42 protected BaseComponentDTO() {
48 * @param preset the preset to use to pull data values out of
50 * @throws net.sf.openrocket.util.BugException thrown if the expected body tube keys are not in the preset
52 protected BaseComponentDTO(final ComponentPreset preset) {
53 setManufacturer(preset.getManufacturer().getSimpleName());
54 setPartNo(preset.getPartNo());
55 if ( preset.has(ComponentPreset.DESCRIPTION )) {
56 setDescription(preset.get(ComponentPreset.DESCRIPTION));
58 if ( preset.has(ComponentPreset.MATERIAL)) {
59 setMaterial(new AnnotatedMaterialDTO(preset.get(ComponentPreset.MATERIAL)));
61 if (preset.has(ComponentPreset.MASS)) {
62 setMass(preset.get(ComponentPreset.MASS));
64 if ( preset.has(ComponentPreset.FILLED) ) {
65 setFilled( preset.get(ComponentPreset.FILLED));
69 public String getManufacturer() {
73 public void setManufacturer(final String theManufacturer) {
74 manufacturer = theManufacturer;
77 public String getPartNo() {
81 public void setPartNo(final String thePartNo) {
85 public String getDescription() {
89 public void setDescription(final String theDescription) {
90 description = theDescription;
93 public AnnotatedMaterialDTO getMaterial() {
97 public void setMaterial(final AnnotatedMaterialDTO theMaterial) {
98 material = theMaterial;
101 public double getMass() {
102 return mass.getValue();
105 public void setMass(final AnnotatedMassDTO theMass) {
109 public void setMass(final double theMass) {
110 mass = new AnnotatedMassDTO(theMass);
113 public Boolean getFilled() {
117 public void setFilled(Boolean filled) {
118 this.filled = filled;
121 public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException;
123 void addProps(TypedPropertyMap props, List<MaterialDTO> materialList) {
124 props.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer(manufacturer));
125 props.put(ComponentPreset.PARTNO, partNo);
126 if ( description != null ) {
127 props.put(ComponentPreset.DESCRIPTION, description);
129 Material m = find(materialList, material);
131 props.put(ComponentPreset.MATERIAL, find(materialList, material));
133 if ( mass != null ) {
134 props.put(ComponentPreset.MASS, getMass());
136 if ( filled != null ) {
137 props.put(ComponentPreset.FILLED, getFilled());
141 private Material find(List<MaterialDTO> materialList, AnnotatedMaterialDTO dto) {
142 for (int i = 0; i < materialList.size(); i++) {
143 MaterialDTO materialDTO = materialList.get(i);
144 if (materialDTO.getType().name().equals(dto.type) && materialDTO.getName().equals(dto.material)) {
145 return materialDTO.asMaterial();
148 //Otherwise fallback and look at factory default materials.
149 return Databases.findMaterial(Material.Type.valueOf(material.type), material.material);
152 static class AnnotatedMaterialDTO {
153 @XmlAttribute(name = "Type")
156 private String material;
158 AnnotatedMaterialDTO() {}
160 AnnotatedMaterialDTO(Material theMaterial) {
161 type = theMaterial.getType().name();
162 material = theMaterial.getName();
166 static class AnnotatedLengthDTO {
167 @XmlAttribute(name="Unit", required=false)
168 private String unitName = "m";
170 private double length;
172 AnnotatedLengthDTO() {}
174 AnnotatedLengthDTO( double length ) {
175 this.length = length;
178 public double getValue() {
179 return UnitGroup.UNITS_LENGTH.getUnit(unitName).fromUnit(length);
183 static class AnnotatedMassDTO {
184 @XmlAttribute(name="Unit", required=false)
185 private String unitName = "kg";
189 AnnotatedMassDTO() {}
191 AnnotatedMassDTO( double mass ) {
195 public double getValue() {
196 return UnitGroup.UNITS_MASS.getUnit(unitName).fromUnit(mass);