+ public abstract ComponentPreset asComponentPreset(List<MaterialDTO> materials) throws InvalidComponentPresetException;
+
+ void addProps(TypedPropertyMap props, List<MaterialDTO> materialList) {
+ props.put(ComponentPreset.MANUFACTURER, Manufacturer.getManufacturer(manufacturer));
+ props.put(ComponentPreset.PARTNO, partNo);
+ if ( description != null ) {
+ props.put(ComponentPreset.DESCRIPTION, description);
+ }
+ Material m = find(materialList, material);
+ if ( m != null ) {
+ props.put(ComponentPreset.MATERIAL, find(materialList, material));
+ }
+ if ( mass != null ) {
+ props.put(ComponentPreset.MASS, getMass());
+ }
+ if ( filled != null ) {
+ props.put(ComponentPreset.FILLED, getFilled());
+ }
+ if (image != null) {
+ props.put(ComponentPreset.IMAGE, image);
+ }
+ }
+
+ protected Material find(List<MaterialDTO> materialList, AnnotatedMaterialDTO dto) {
+ if ( dto == null ) {
+ return null;
+ }
+ for (int i = 0; i < materialList.size(); i++) {
+ MaterialDTO materialDTO = materialList.get(i);
+ if (materialDTO.getType().name().equals(dto.type) && materialDTO.getName().equals(dto.material)) {
+ return materialDTO.asMaterial();
+ }
+ }
+
+ // Don't have one, first check OR's database
+ Material m = Databases.findMaterial(dto.getORMaterialType(), dto.material);
+ if ( m != null ) {
+ return m;
+ }
+
+ return Material.newUserMaterial(dto.getORMaterialType(), dto.material, 0.0);
+
+ }
+
+ static class AnnotatedMaterialDTO {
+ @XmlAttribute(name = "Type")
+ private String type;
+ @XmlValue
+ private String material;
+
+ AnnotatedMaterialDTO() {}
+
+ AnnotatedMaterialDTO(Material theMaterial) {
+ type = theMaterial.getType().name();
+ material = theMaterial.getName();
+ }
+
+ public Material.Type getORMaterialType() {
+ if ( "BULK".equals(type) ) {
+ return Material.Type.BULK;
+ } else if ( "SURFACE".equals(type) ) {
+ return Material.Type.SURFACE;
+ } else if ( "LINE".equals(type) ) {
+ return Material.Type.LINE;
+ }
+ throw new IllegalArgumentException( "Inavlid material type " + type +" specified for Component" );
+ }
+ }
+
+ static class AnnotatedLengthDTO {
+ @XmlAttribute(name="Unit", required=false)
+ private String unitName = "m";
+ @XmlValue
+ private double length;
+
+ AnnotatedLengthDTO() {}
+
+ AnnotatedLengthDTO( double length ) {
+ this.length = length;
+ }
+
+ public double getValue() {
+ return UnitGroup.UNITS_LENGTH.getUnit(unitName).fromUnit(length);
+ }
+ }
+
+ static class AnnotatedMassDTO {
+ @XmlAttribute(name="Unit", required=false)
+ private String unitName = "kg";
+ @XmlValue
+ private double mass;
+
+ AnnotatedMassDTO() {}
+
+ AnnotatedMassDTO( double mass ) {
+ this.mass = mass;
+ }
+
+ public double getValue() {
+ return UnitGroup.UNITS_MASS.getUnit(unitName).fromUnit(mass);
+ }
+ }
+
+ static class Base64Adapter extends XmlAdapter<String, byte[]> {
+ @Override
+ public byte[] unmarshal(String s) {
+ if (s == null) {
+ return null;
+ }
+ return DatatypeConverter.parseBase64Binary(s);
+ }
+
+ @Override
+ public String marshal(byte[] bytes) {
+ if (bytes == null) {
+ return null;
+ }
+ return DatatypeConverter.printBase64Binary(bytes);
+ }