view src/main/java/de/mpiwg/gazetteer/rest/GetSectionMetadata.java @ 10:5610250d021a default tip

SectionsIndex, we added a method to print the setting of the VM
author "jurzua <jurzua@mpiwg-berlin.mpg.de>"
date Thu, 19 Mar 2015 11:46:33 +0100
parents 7682c04c63a8
children
line wrap: on
line source

package de.mpiwg.gazetteer.rest;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import de.mpiwg.gazetteer.db.DBBook;
import de.mpiwg.gazetteer.db.DBSection;
import de.mpiwg.gazetteer.utils.DBService;

public class GetSectionMetadata  extends AbstractServletMethod {

	public static String name = "getSectionMetadata";
	
	public static void execute(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		Long sectionId = getQueryLongParam(request, "sectionId");
		
		if(sectionId != null){
			DBSection section = DBService.getSectionWithContent(sectionId);
			if(section != null){
				DBBook book = DBService.getBookFromDB(section.getBookId());
				
				response.setContentType("application/json");
				JSONObject json = new JSONObject();
				json.put("status", "ok");
				
				
				JSONObject sectionJson = new JSONObject();
				sectionJson.put("id", section.getId());
				sectionJson.put("name", section.getName());
				
				JSONObject bookJson = new JSONObject();
				bookJson.put("id", book.getId());
				bookJson.put("author", book.getAuthor());
				bookJson.put("edition", book.getEdition());
				bookJson.put("line", book.getLine());
				bookJson.put("period", book.getPeriod());
				bookJson.put("volume", book.getVolume());
				
				sectionJson.put("book", bookJson);
				
				
				json.put("section", sectionJson);
				
				
				PrintWriter out = response.getWriter();
				out.print(json.toString());
				out.flush();
				
			}else{
				response.setContentType("application/json");
				JSONObject json = new JSONObject();
				json.put("status", "error");
				json.put("message", "Section no found (" + sectionId + ")");
				PrintWriter out = response.getWriter();
				out.print(json.toString());
				out.flush();
			}
		}else{
			response.setContentType("application/json");
			JSONObject json = new JSONObject();
			json.put("status", "error");
			json.put("message", "Following parameters are mandatory: sectionId.");
			PrintWriter out = response.getWriter();
			out.print(json.toString());
			out.flush();
		}
	}
}