/**
 * Copyright © 2011-2012 EMBL - European Bioinformatics Institute
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.  
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.ac.ebi.phenotype.rest.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import uk.ac.ebi.phenotype.imaging.persistence.ImaImageRecord;
import uk.ac.ebi.phenotype.imaging.springrest.images.Images;
import uk.ac.ebi.phenotype.imaging.springrest.images.dao.ImagesDao;


@Controller
public class ImagesController {
	
	private ImagesDao imagesDao;

	@Autowired 
	public ImagesController(ImagesDao imagesDao) {
		this.imagesDao = imagesDao;
	}
	
	
	/*
	 * Zero based access with start and length of images returned from the query
	 */
	@RequestMapping(value = "/images*", method = { RequestMethod.GET,
			RequestMethod.HEAD })
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public Images getAllImages(@RequestParam(required=false,defaultValue="0",value="start") int start,@RequestParam(required=false, defaultValue="10",value="length") int length,
			@RequestParam(required=false,defaultValue="",value="search") String search, @RequestParam(required=false, defaultValue="",value="mpid") String mpId, @RequestParam(required=false, defaultValue="",value="gene_id") String geneId, HttpServletResponse resp){
			System.out.println(start+" and length="+length);
			
			if(!geneId.equals("")){
				//alert('gene_id found!');
				geneId=geneId.replace("MGI:", "MGI\\:");
				search="accession:"+geneId;
				
			}
			
			if(!mpId.equals("")){
				mpId=mpId.replace("MP:", "MP\\:");
				search="annotationTermId:"+mpId;
			}
			
			ImagesController.setHeaders(resp);
		return imagesDao.getAllImages(start, length, search);
	}
	
	
	
	@RequestMapping(value = "/images/{id}", method = { RequestMethod.GET,
			RequestMethod.HEAD })
	@ResponseStatus(HttpStatus.OK)
	@ResponseBody
	public ImaImageRecord getImageById(@PathVariable int id, HttpServletResponse resp){
//		System.out.println("request for images star ");
//		Images images=new Images();
//		System.out.println(images.getImages().get(0).getUrl());
		//return images;
		ImagesController.setHeaders(resp);
		ImaImageRecord rec= imagesDao.getImageWithId(id);
		if(rec==null){
			setErrorHeaders(resp);
		}
		return rec;
	}
	
	public static void setHeaders(HttpServletResponse resp) {
		resp.setHeader("Access-Control-Allow-Origin", "*");
	}
	
	public static void setErrorHeaders(HttpServletResponse resp){
		try {
			resp.sendError(HttpServletResponse.SC_NOT_FOUND);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

@@ -20,17 +20,17 @@
 2011-201
-2
+3
  EMBL - 

Change copyright in Apache 2 license to 2013