if(invoice == null) {
throw new IllegalArgumentException("Parameter invoice can't be null");
}
final Shipment shipment = invoice.getShipment();
if(shipment == null) {
throw new IllegalStateException("The given invoice.getShipment() returned null. Here is your invoice object:\n" + invoice);
}
final Element result = new Element("shipment");
result.setText(shipment.getShipmentType().toString().toUpperCase());
// set email/copy-addresses
if(shipment.getShipmentType() == ShipmentType.email || shipment.getShipmentType() == ShipmentType.paper_and_email) {
// email addresses is required
final List<String> emailAddresses = shipment.getEmailAddresses();
if(emailAddresses == null || emailAddresses.size() == 0) {
throw new IllegalStateException("You must specify email addresses on shipment when shipment type is "
+ shipment.getShipmentType().toString());
}
// create <emailaddresses> child element and add it to the shipment element
final Element emailAddressesElement = new Element("emailaddresses");
result.addContent(emailAddressesElement);
// iterate all email addresses
for(final String currentEmailAddress : emailAddresses) {
// create email element
final Element emailAddressElement = new Element("email");
emailAddressElement.setText(currentEmailAddress);
// add element to parent element (<emailaddresses>)
emailAddressesElement.addContent(emailAddressElement);
}
// copy addresses is optional
final List<String> copyAddresses = shipment.getCopyAddresses();
if(copyAddresses != null && copyAddresses.size() > 0) {
// create <copyaddresses> child element and add it to the shipment element
final Element copyAddressesElement = new Element("copyaddresses");