view src/main/java/de/mpiwg/web/jsp/BooksPage.java @ 14:3387d855a194

new: toc status in books page
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Fri, 29 May 2015 11:00:09 +0200
parents 9c6e74761f60
children d81a5401b9af
line wrap: on
line source

package de.mpiwg.web.jsp;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.apache.log4j.Logger;

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

public class BooksPage extends AbstractJSPPage{

	private static Logger logger = Logger.getLogger(BooksPage.class);
	
	public static String bean = "booksBean";
	public static String page = "pages/books.jsp";
	private List<DBBook> completeBookList;
	

	private Map<String, DBSectionVersion> sectionVersionMap = null;
	private int bookNumber;

	private String tocBookId = new String();


	public void loadParameters(HttpServletRequest request, HttpServletResponse response){
		this.request = request;
		this.response = response;
		
		this.tocBookId = getParameter("tocBookId");
	}
	
	
	public String getTocBookId() {
		return tocBookId;
	}

	public void setTocBookId(String tocBookId) {
		this.tocBookId = tocBookId;
	}

	public int getBookNumber() {
		return bookNumber;
	}

	public void setBookNumber(int bookNumber) {
		this.bookNumber = bookNumber;
	}

	
	public List<DBBook> getCompleteBookList() {
		return completeBookList;
	}

	public void loadBooks(){
		if(this.completeBookList == null){
			this.forceLoadBooks();
		}
	}
	
	public void forceLoadBooks(){
		logger.debug("loadBooks");

		this.loadSectionsVersion();
		
		this.completeBookList = new ArrayList<DBBook>();
		if(getSessionBean().getUser() != null){
			logger.debug("loading book list");
			// === load book table from db ==
			
			for(DBBook book : DBService.getInstance().getBooks()){		
				// set editor and date (current version)
				book.setCurrentSectionVersion(sectionVersionMap.get(book.getId()));
				
				this.completeBookList.add(book);
			}	
			
			this.setBookNumber(this.completeBookList.size());
			
		}
		
	}
	
	private void loadSectionsVersion(){
		this.sectionVersionMap = new HashMap<String, DBSectionVersion>();
		
		try {
			for(DBSectionVersion sv : DBService.getInstance().getSectionVersionList()){
				this.sectionVersionMap.put(sv.getBooks_id(), sv);
			}	
		} catch (Exception e) {
			addMsg("There is an internal error: " + e.getLocalizedMessage());
			e.printStackTrace();
		}
		
		
	}

	public void setTocCorrection() {
		String tocBookId = this.getTocBookId();		
		logger.debug("tocFinishedBookId=" + tocBookId);
		try {
			DBService.getInstance().updateTocCorrection(tocBookId, 1);
		}catch (Exception e) {
			addMsg("There is an internal error: " + e.getLocalizedMessage());
			e.printStackTrace();
		} 
	
		//logger.debug("toc_correction:"+DBService.getInstance().getBook(tocBookId).getToc_correction());
		
	}


	public void unSetTocCorrection() {
		String tocBookId = this.getTocBookId();
		logger.debug("tocFinishedBookId=" + tocBookId);
		try {
			DBService.getInstance().updateTocCorrection(tocBookId, 0);
		}catch (Exception e) {
			addMsg("There is an internal error: " + e.getLocalizedMessage());
			e.printStackTrace();
		}
		
		
	}

	

}