unmarshal XML (*.orc) to ComponentPresets.
[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
35     /**
36      * Default constructor.
37      */
38     protected BaseComponentDTO() {
39     }
40
41     /**
42      * Constructor.
43      *
44      * @param preset  the preset to use to pull data values out of
45      *
46      * @throws net.sf.openrocket.util.BugException thrown if the expected body tube keys are not in the preset
47      */
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));
55         }
56     }
57
58     public String getManufacturer() {
59         return manufacturer;
60     }
61
62     public void setManufacturer(final String theManufacturer) {
63         manufacturer = theManufacturer;
64     }
65
66     public String getPartNo() {
67         return partNo;
68     }
69
70     public void setPartNo(final String thePartNo) {
71         partNo = thePartNo;
72     }
73
74     public String getDescription() {
75         return description;
76     }
77
78     public void setDescription(final String theDescription) {
79         description = theDescription;
80     }
81
82     public AnnotatedMaterialDTO getMaterial() {
83         return material;
84     }
85
86     public void setMaterial(final AnnotatedMaterialDTO theMaterial) {
87         material = theMaterial;
88     }
89
90     public double getMass() {
91         return mass;
92     }
93
94     public void setMass(final double theMass) {
95         mass = theMass;
96     }
97
98     public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException;
99
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);
106     }
107
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();
113             }
114         }
115         //Otherwise fallback and look at factory default materials.
116         return Databases.findMaterial(Material.Type.valueOf(material.type), material.material);
117     }
118
119     static class AnnotatedMaterialDTO {
120         @XmlAttribute(name = "Type")
121         private String type;
122         @XmlValue
123         private String material;
124
125         AnnotatedMaterialDTO() {}
126
127         AnnotatedMaterialDTO(Material theMaterial) {
128             type = theMaterial.getType().name();
129             material = theMaterial.getName();
130         }
131     }
132 }