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")
34 @XmlElement(name="Filled")
35 private Boolean filled;
38 * Default constructor.
40 protected BaseComponentDTO() {
46 * @param preset the preset to use to pull data values out of
48 * @throws net.sf.openrocket.util.BugException thrown if the expected body tube keys are not in the preset
50 protected BaseComponentDTO(final ComponentPreset preset) {
51 setManufacturer(preset.getManufacturer().getSimpleName());
52 setPartNo(preset.getPartNo());
53 if ( preset.has(ComponentPreset.DESCRIPTION )) {
54 setDescription(preset.get(ComponentPreset.DESCRIPTION));
56 if ( preset.has(ComponentPreset.MATERIAL)) {
57 setMaterial(new AnnotatedMaterialDTO(preset.get(ComponentPreset.MATERIAL)));
59 if (preset.has(ComponentPreset.MASS)) {
60 setMass(preset.get(ComponentPreset.MASS));
62 if ( preset.has(ComponentPreset.FILLED) ) {
63 setFilled( preset.get(ComponentPreset.FILLED));
67 public String getManufacturer() {
71 public void setManufacturer(final String theManufacturer) {
72 manufacturer = theManufacturer;
75 public String getPartNo() {
79 public void setPartNo(final String thePartNo) {
83 public String getDescription() {
87 public void setDescription(final String theDescription) {
88 description = theDescription;
91 public AnnotatedMaterialDTO getMaterial() {
95 public void setMaterial(final AnnotatedMaterialDTO theMaterial) {
96 material = theMaterial;
99 public double getMass() {
103 public void setMass(final double theMass) {
107 public Boolean getFilled() {
111 public void setFilled(Boolean filled) {
112 this.filled = filled;
115 public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException;
117 void addProps(TypedPropertyMap props, List<MaterialDTO> materialList) {
118 props.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer(manufacturer));
119 props.put(ComponentPreset.PARTNO, partNo);
120 if ( description != null ) {
121 props.put(ComponentPreset.DESCRIPTION, description);
123 Material m = find(materialList, material);
125 props.put(ComponentPreset.MATERIAL, find(materialList, material));
127 if ( mass != null ) {
128 props.put(ComponentPreset.MASS, mass);
130 if ( filled != null ) {
131 props.put(ComponentPreset.FILLED, filled);
135 private Material find(List<MaterialDTO> materialList, AnnotatedMaterialDTO dto) {
136 for (int i = 0; i < materialList.size(); i++) {
137 MaterialDTO materialDTO = materialList.get(i);
138 if (materialDTO.getType().name().equals(dto.type) && materialDTO.getName().equals(dto.material)) {
139 return materialDTO.asMaterial();
142 //Otherwise fallback and look at factory default materials.
143 return Databases.findMaterial(Material.Type.valueOf(material.type), material.material);
146 static class AnnotatedMaterialDTO {
147 @XmlAttribute(name = "Type")
150 private String material;
152 AnnotatedMaterialDTO() {}
154 AnnotatedMaterialDTO(Material theMaterial) {
155 type = theMaterial.getType().name();
156 material = theMaterial.getName();