Added filled property. Made Filled and Mass optional by using objects instead of...
[debian/openrocket] / core / src / net / sf / openrocket / preset / xml / BaseComponentDTO.java
1
2 package net.sf.openrocket.preset.xml;
3
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;
10
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;
17
18 /**
19  * Base class for the external representation of all component presets.
20  */
21 @XmlAccessorType(XmlAccessType.FIELD)
22 public abstract class BaseComponentDTO {
23
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")
33         private Double mass;
34         @XmlElement(name="Filled")
35         private Boolean filled;
36
37         /**
38          * Default constructor.
39          */
40         protected BaseComponentDTO() {
41         }
42
43         /**
44          * Constructor.
45          *
46          * @param preset  the preset to use to pull data values out of
47          *
48          * @throws net.sf.openrocket.util.BugException thrown if the expected body tube keys are not in the preset
49          */
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));
55                 }
56                 if ( preset.has(ComponentPreset.MATERIAL)) {
57                         setMaterial(new AnnotatedMaterialDTO(preset.get(ComponentPreset.MATERIAL)));
58                 }
59                 if (preset.has(ComponentPreset.MASS)) {
60                         setMass(preset.get(ComponentPreset.MASS));
61                 }
62                 if ( preset.has(ComponentPreset.FILLED) ) {
63                         setFilled( preset.get(ComponentPreset.FILLED));
64                 }
65         }
66
67         public String getManufacturer() {
68                 return manufacturer;
69         }
70
71         public void setManufacturer(final String theManufacturer) {
72                 manufacturer = theManufacturer;
73         }
74
75         public String getPartNo() {
76                 return partNo;
77         }
78
79         public void setPartNo(final String thePartNo) {
80                 partNo = thePartNo;
81         }
82
83         public String getDescription() {
84                 return description;
85         }
86
87         public void setDescription(final String theDescription) {
88                 description = theDescription;
89         }
90
91         public AnnotatedMaterialDTO getMaterial() {
92                 return material;
93         }
94
95         public void setMaterial(final AnnotatedMaterialDTO theMaterial) {
96                 material = theMaterial;
97         }
98
99         public double getMass() {
100                 return mass;
101         }
102
103         public void setMass(final double theMass) {
104                 mass = theMass;
105         }
106
107         public Boolean getFilled() {
108                 return filled;
109         }
110
111         public void setFilled(Boolean filled) {
112                 this.filled = filled;
113         }
114
115         public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException;
116
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);
122                 }
123                 Material m = find(materialList, material);
124                 if ( m != null ) {
125                         props.put(ComponentPreset.MATERIAL, find(materialList, material));
126                 }
127                 if ( mass != null ) {
128                         props.put(ComponentPreset.MASS, mass);
129                 }
130                 if ( filled != null ) {
131                         props.put(ComponentPreset.FILLED, filled);
132                 }
133         }
134
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();
140                         }
141                 }
142                 //Otherwise fallback and look at factory default materials.
143                 return Databases.findMaterial(Material.Type.valueOf(material.type), material.material);
144         }
145
146         static class AnnotatedMaterialDTO {
147                 @XmlAttribute(name = "Type")
148                 private String type;
149                 @XmlValue
150                 private String material;
151
152                 AnnotatedMaterialDTO() {}
153
154                 AnnotatedMaterialDTO(Material theMaterial) {
155                         type = theMaterial.getType().name();
156                         material = theMaterial.getName();
157                 }
158         }
159 }