package org.fcrepo.api;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.ok;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.IOException;
import java.util.List;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.core.Response;

import org.fcrepo.AbstractResource;
import org.fcrepo.services.ObjectService;
import org.fcrepo.utils.FedoraJcrTypes;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;


@Component
@Path("/rest/{path: .*}/fcr:children")
public class FedoraChildren extends AbstractResource {

	private static final Logger logger = getLogger(FedoraChildren.class);

	/**
	 * Returns a list of the first-generation
	 * descendants of an object, filtered by
	 * an optional mixin parameter
	 *
	 * @param pathList
	 * @param mixin
	 * @return 200
	 * @throws javax.jcr.RepositoryException
	 * @throws java.io.IOException
	 */
	@GET
	@Timed
	@Produces({TEXT_XML, APPLICATION_JSON, TEXT_HTML})
	public Response getObjects(
									  @PathParam("path") final List<PathSegment> pathList,
									  @QueryParam("mixin") @DefaultValue("") final String limitByMixinValue
	) throws RepositoryException, IOException {


		final Session session = getAuthenticatedSession();

        final String mixin;
		try {
			final String path = toPath(pathList);
			logger.info("getting children of {}", path);

            switch (limitByMixinValue) {
                case "":
                    mixin = null;
                    break;
                case FedoraJcrTypes.FEDORA_OBJECT:
                    mixin = "nt:folder";
                    break;
                case FedoraJcrTypes.FEDORA_DATASTREAM:
                    mixin = "nt:file";
                    break;
                default:
                    mixin = limitByMixinValue;
                    break;
            }

			return ok(nodeService.getObjectNames(session, path, mixin).toString()).build();
		} finally {
			session.logout();
		}
	}


	public ObjectService getObjectService() {
		return objectService;
	}


	public void setObjectService(ObjectService objectService) {
		this.objectService = objectService;
	}

}

@@ -748,24 +748,67 @@
 raJcrTypes;%0A
+import org.modeshape.jcr.api.JcrConstants;%0A
 import org.s
@@ -1817,24 +1817,73 @@
 %7D%22, path);%0A%0A
+            if (limitByMixinValue != null) %7B%0A    
             
@@ -1923,24 +1923,28 @@
             
+    
 case %22%22:%0A   
@@ -1952,32 +1952,36 @@
                 
+  
+  
 mixin = null;%0A  
@@ -1990,32 +1990,36 @@
                 
+    
 break;%0A         
@@ -2017,32 +2017,36 @@
                 
+ 
+   
 case FedoraJcrTy
@@ -2064,16 +2064,20 @@
 OBJECT:%0A
+    
         
@@ -2100,21 +2100,36 @@
 n = 
-%22nt:folder%22;%0A
+JcrConstants.NT_FOLDER;%0A    
     
@@ -2143,32 +2143,36 @@
          break;%0A
+    
                 
@@ -2210,16 +2210,20 @@
 STREAM:%0A
+    
         
@@ -2246,19 +2246,33 @@
 n = 
-%22nt:file%22;%0A
+JcrConstants.NT_FILE;%0A   
     
@@ -2279,39 +2279,44 @@
                 
+ 
 break;%0A
+    
                 
@@ -2324,16 +2324,20 @@
 efault:%0A
+    
         
@@ -2387,39 +2387,112 @@
                 
+    
 break;%0A
+                %7D%0A            %7D else %7B%0A                mixin = null;%0A
             %7D%0A%0A%09

fix up children