Implement Folders

In milton a resource is a folder if it can have children. You tell milton that an object has children with the ChildrenOf annotation. In our case we want products to contain files about the product. So we'll mimic another domain object ProductFile. A file needs to have a name (as we saw) and some bytes. So add this class to your controller:

    public class ProductFile {
        private String name;
        private byte[] bytes;

        public ProductFile(String name, byte[] bytes) {
            this.name = name;
            this.bytes = bytes;
        }

        public String getName() {
            return name;
        }                
    }

And we'll add a list of product files or our product class:

    public class Product {
        private String name;
        private List<ProductFile> productFiles = new ArrayList<ProductFile>();

        public Product(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }             

        public List<ProductFile> getProductFiles() {
            return productFiles;
        }                
    }

And we add a controller method so milton knows how to get the files for a product:

    @ChildrenOf
    public List<ProductFile> getProductFiles(Product product) {
        return product.getProductFiles();
    }

 

The final controller class should look like this:


package com.helloworld;

import io.milton.annotations.ChildrenOf;
import io.milton.annotations.ResourceController;
import io.milton.annotations.Root;
import java.util.ArrayList;
import java.util.List;

@ResourceController
public class HelloWorldController  {

    private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(HelloWorldController.class);

    private List<Product> products = new ArrayList<Product>();

    public HelloWorldController() {
        products.add(new Product("hello"));
        products.add(new Product("world"));
    }
            
    @Root
    public HelloWorldController getRoot() {
        return this;
    }    
    
    @ChildrenOf
    public List<Product> getProducts(HelloWorldController root) {
        return products;
    }
    
    @ChildrenOf
    public List<ProductFile> getProductFiles(Product product) {
        return product.getProductFiles();
    }
    
    
    public class Product {
        private String name;
        private List<ProductFile> productFiles = new ArrayList<ProductFile>();

        public Product(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }             

        public List<ProductFile> getProductFiles() {
            return productFiles;
        }                
    }
    
    public class ProductFile {
        private String name;
        private byte[] bytes;

        public ProductFile(String name, byte[] bytes) {
            this.name = name;
            this.bytes = bytes;
        }

        public String getName() {
            return name;
        }                
    }
}

 

So lets run that and see what we get...