Variety of changes to the orc file processing. Added unit annotations to all lengths...
[debian/openrocket] / core / src / net / sf / openrocket / preset / xml / BaseComponentDTO.java
1
2 package net.sf.openrocket.preset.xml;
3
4 import java.util.List;
5
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;
11
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;
19
20 /**
21  * Base class for the external representation of all component presets.
22  */
23 @XmlAccessorType(XmlAccessType.FIELD)
24 public abstract class BaseComponentDTO {
25
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;
38
39         /**
40          * Default constructor.
41          */
42         protected BaseComponentDTO() {
43         }
44
45         /**
46          * Constructor.
47          *
48          * @param preset  the preset to use to pull data values out of
49          *
50          * @throws net.sf.openrocket.util.BugException thrown if the expected body tube keys are not in the preset
51          */
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));
57                 }
58                 if ( preset.has(ComponentPreset.MATERIAL)) {
59                         setMaterial(new AnnotatedMaterialDTO(preset.get(ComponentPreset.MATERIAL)));
60                 }
61                 if (preset.has(ComponentPreset.MASS)) {
62                         setMass(preset.get(ComponentPreset.MASS));
63                 }
64                 if ( preset.has(ComponentPreset.FILLED) ) {
65                         setFilled( preset.get(ComponentPreset.FILLED));
66                 }
67         }
68
69         public String getManufacturer() {
70                 return manufacturer;
71         }
72
73         public void setManufacturer(final String theManufacturer) {
74                 manufacturer = theManufacturer;
75         }
76
77         public String getPartNo() {
78                 return partNo;
79         }
80
81         public void setPartNo(final String thePartNo) {
82                 partNo = thePartNo;
83         }
84
85         public String getDescription() {
86                 return description;
87         }
88
89         public void setDescription(final String theDescription) {
90                 description = theDescription;
91         }
92
93         public AnnotatedMaterialDTO getMaterial() {
94                 return material;
95         }
96
97         public void setMaterial(final AnnotatedMaterialDTO theMaterial) {
98                 material = theMaterial;
99         }
100
101         public double getMass() {
102                 return mass.getValue();
103         }
104
105         public void setMass(final AnnotatedMassDTO theMass) {
106                 mass = theMass;
107         }
108
109         public void setMass(final double theMass) {
110                 mass = new AnnotatedMassDTO(theMass);
111         }
112
113         public Boolean getFilled() {
114                 return filled;
115         }
116
117         public void setFilled(Boolean filled) {
118                 this.filled = filled;
119         }
120
121         public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException;
122
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);
128                 }
129                 Material m = find(materialList, material);
130                 if ( m != null ) {
131                         props.put(ComponentPreset.MATERIAL, find(materialList, material));
132                 }
133                 if ( mass != null ) {
134                         props.put(ComponentPreset.MASS, getMass());
135                 }
136                 if ( filled != null ) {
137                         props.put(ComponentPreset.FILLED, getFilled());
138                 }
139         }
140
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();
146                         }
147                 }
148                 //Otherwise fallback and look at factory default materials.
149                 return Databases.findMaterial(Material.Type.valueOf(material.type), material.material);
150         }
151
152         static class AnnotatedMaterialDTO {
153                 @XmlAttribute(name = "Type")
154                 private String type;
155                 @XmlValue
156                 private String material;
157
158                 AnnotatedMaterialDTO() {}
159
160                 AnnotatedMaterialDTO(Material theMaterial) {
161                         type = theMaterial.getType().name();
162                         material = theMaterial.getName();
163                 }
164         }
165         
166         static class AnnotatedLengthDTO {
167                 @XmlAttribute(name="Unit", required=false)
168                 private String unitName = "m";
169                 @XmlValue
170                 private double length;
171                 
172                 AnnotatedLengthDTO() {}
173                 
174                 AnnotatedLengthDTO( double length ) {
175                         this.length = length;
176                 }
177
178                 public double getValue() {
179                         return UnitGroup.UNITS_LENGTH.getUnit(unitName).fromUnit(length);
180                 }
181         }
182         
183         static class AnnotatedMassDTO {
184                 @XmlAttribute(name="Unit", required=false)
185                 private String unitName = "kg";
186                 @XmlValue
187                 private double mass;
188                 
189                 AnnotatedMassDTO() {}
190                 
191                 AnnotatedMassDTO( double mass ) {
192                         this.mass = mass;
193                 }
194                 
195                 public double getValue() {
196                         return UnitGroup.UNITS_MASS.getUnit(unitName).fromUnit(mass);
197                 }
198         }
199 }