<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package com.restbucks.ordering.representations;

import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import com.restbucks.ordering.activities.InvalidOrderException;
import com.restbucks.ordering.domain.Item;
import com.restbucks.ordering.domain.Location;
import com.restbucks.ordering.domain.Order;
import com.restbucks.ordering.domain.OrderStatus;

// This annotation is needed, to define the root element's name and namespace
// not otherwise specified here
@XmlRootElement(name = "order", namespace = Representation.RESTBUCKS_NAMESPACE)
// Make sure the schema type is in the same namespace
@XmlType(name = "orderRepresentation", namespace = Representation.RESTBUCKS_NAMESPACE)
// Better in general for us: default of .PUBLIC_MEMBER requires a public getter/setter *pair*
// to ensure inclusion of that property in the XML without an @XmlElement.
// Here cost has a getter but no setter, for example
@XmlAccessorType(XmlAccessType.FIELD)
public class OrderRepresentation {
	@XmlElement(name = "id", namespace = Representation.RESTBUCKS_NAMESPACE)	
	private String id;
    public String getId() {
		return id;
	}
    // we want to override the default here with required = true, but we could let the namespace
    // be defaulted to parent's NS
	@XmlElement(name = "item", required = true, namespace = Representation.RESTBUCKS_NAMESPACE)
    private List&lt;Item&gt; items;
    @XmlElement(name = "location", required = true, namespace = Representation.RESTBUCKS_NAMESPACE)
    private Location location;
  
    @XmlElement(name = "cost", namespace = Representation.RESTBUCKS_NAMESPACE)
    private BigDecimal cost;
   
	@XmlElement(name = "status", namespace = Representation.RESTBUCKS_NAMESPACE)
    private OrderStatus status;

    /**
     * For JAXB :-(
     */
    OrderRepresentation() {}

// for use with OrderResource's commented-out createOrder version
//    public static OrderRepresentation fromXmlString(String xmlRepresentation) {
//        try {
//            JAXBContext context = JAXBContext.newInstance(OrderRepresentation.class);
//            Unmarshaller unmarshaller = context.createUnmarshaller();
//            return (OrderRepresentation) unmarshaller.unmarshal(new ByteArrayInputStream(xmlRepresentation.getBytes()));
//        } catch (Exception e) {
//            throw new InvalidOrderException(e);
//        }
//    }

    public OrderRepresentation(Order order, String id) {
   
        try {
            this.location = order.getLocation();
            this.items = order.getItems();
            this.cost = order.calculateCost();
            this.status = order.getStatus();
            this.id = id;
        } catch (Exception ex) {
            throw new InvalidOrderException(ex);
        }
    }
    
    // for debugging and testing: look at object as XML
    public String toString() {
        try {
            JAXBContext context = JAXBContext.newInstance(OrderRepresentation.class);
            Marshaller marshaller = context.createMarshaller();

            StringWriter stringWriter = new StringWriter();
            marshaller.marshal(this, stringWriter);

            return stringWriter.toString();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    // For use only inside server: create an Order from this OrderRepresentation
    // The Order object belongs to the server implementation
    // Luckily, a POJO built from the XML will not have this
    // method.
    public Order getOrder() {
        if (location == null || items == null) {
            throw new InvalidOrderException();
        }
        for (Item i : items) {
            if (i == null) {
                throw new InvalidOrderException();
            }
        }

        return new Order(location, status, items);
    }
    
    public OrderStatus getStatus() {
        return status;
    }

    public BigDecimal getCost() {
        return cost;
    }
}
</pre></body></html>