package com.github.aureliano.verbum_domini.web.mb;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.view.ViewScoped;
import javax.validation.ValidationException;

import org.apache.log4j.Logger;

import com.github.aureliano.verbum_domini.core.bean.BibleBean;
import com.github.aureliano.verbum_domini.core.impl.bean.BibleBeanImpl;
import com.github.aureliano.verbum_domini.helper.WebHelper;
import com.github.aureliano.verbum_domini.web.SessionKey;
import com.github.aureliano.verbum_domini.web.bc.BibleBC;

@ManagedBean(name = "bibleEditMB")
@ViewScoped
public class BibleEditMB {

	private static final Logger logger = Logger.getLogger(BibleEditMB.class);
	
	private BibleBean bible;
	
	public BibleEditMB() {
		super();
	}
	
	@PostConstruct
	public void preRender() {
		Integer id = this.bibleId();
		logger.info("Preparing to edit bible with id " + id);
		this.bible = (id != null) ? BibleBC.fetchBible(id) : new BibleBeanImpl();
	}
	
	public void save() {
		try {
			BibleBC.save(this.bible);
			WebHelper.setSessionAttribute(SessionKey.INFO_MESSAGE.name(), "Bible saved successfuly.");
			logger.info("Bible with id " + this.bible.getId() + " saved successfuly.");
			
			WebHelper.sendRedirect("/verbumdomini/app/bibles");
		} catch (ValidationException ex) {
			logger.warn("Bible bean validation has failed.");
		}
	}

	public BibleBean getBible() {
		return this.bible;
	}

	public void setBible(BibleBean bible) {
		this.bible = bible;
	}
	
	private Integer bibleId() {
		Object id = WebHelper.getRequestParameter("bible.id");
		if (id == null) {
			return null;
		}
		
		return Integer.parseInt(id.toString());
	}
}
@@ -830,21 +830,51 @@
 d = 
-this.
+WebHelper.getEntityIdFromRequest(%22
 bible
-Id(
+.id%22
 );%0A%09
@@ -1524,181 +1524,5 @@
 %0A%09%7D%0A
-%09%0A%09private Integer bibleId() %7B%0A%09%09Object id = WebHelper.getRequestParameter(%22bible.id%22);%0A%09%09if (id == null) %7B%0A%09%09%09return null;%0A%09%09%7D%0A%09%09%0A%09%09return Integer.parseInt(id.toString());%0A%09%7D%0A
 %7D

Use common method to get entity id.