<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page import="org.crosswire.utils.Sessions" %>
<%@ page import="org.crosswire.webtools.RightsAndRoles" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.ShelfInstance" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Transcription" %>
<%@ page import="org.crosswire.xml.XMLBlock" %>
<%@ page import="org.crosswire.webtools.annotation.*" %>
<%@ page import="org.crosswire.webtools.*" %>
<%@ page import="javax.validation.constraints.NotNull" %>
<%!
@Description(value = "Create a new or update an existing shelf instance record.", name = "shelfinstance/put")
public static class MyParameters extends Parameters<MyParameters> {

	@Description(value = "shelf instance id, supply to edit an existing shelf instance; otherwise, do not include when adding a new shelf instance", example = "146789")
	public Integer shelfID;

	@Description(value = "institution id of the holding institute", example = "49")
	public Integer instID;

	@Description(value = "shelf number, assigned by the holding institute", example = "s.n.27")
	public String shelfNum;

	@Description(value = "if this shelf instance was given a Gregory Aland number previously, which is now deprecated", example = "l 3323")
	public String formerGA;

	@Description(value = "content overview", example = "eapr")
	public String contentOverview;

	@Description(value = "leaves description", example = "12 fragments")
	public String leavesDesc;

	@Description(value = "leaves count", example = "12")
	public Integer leavesCount;

	@Description(value = "origin year description", example = "III")
	public String origYearDesc;

	@Override
	protected void afterLoad() {
		shelfNum        = Transcription.assureUnicode(shelfNum);
		formerGA        = Transcription.assureUnicode(formerGA);
		contentOverview = Transcription.assureUnicode(contentOverview);
		leavesDesc      = Transcription.assureUnicode(leavesDesc);
		origYearDesc    = Transcription.assureUnicode(origYearDesc);
	}
	@Override
	protected void customValidation() {
		if (getUser() == null) {
			addError(-5, "Must be logged in.");
			return;
		}
		if (shelfID == null && instID == null) {
			addError(-4, "Must supply {instID} when creating a new shelf instance.");
			return;
		}
	}
}
%>
<%
MyParameters params = new MyParameters().loadFromRequest(request, response, false);
if (params.getErrors().size() == 0) {
do {
	String role = "VMR Administrator";
	boolean permission = params.getUser().hasRole(role);
	if (!permission) {
		params.addError(-6, "No permission to edit shelf instance records. Must have the role: " + role + ".");
		break;
	}
	ShelfInstance orig = null;
	ShelfInstance inst = null;
	if (params.shelfID == null) {
		inst = new ShelfInstance();
	}
	else {
		orig = ShelfInstance.getShelfInstance(params.shelfID);
		if (orig == null) {
			params.addError(-8, "Couldn't find Shelf Instance ID: " + params.shelfID + ".");
			break;
		}
		inst = (ShelfInstance)orig.clone();
	}

	if (params.instID != null) inst.setInstitutionID(params.instID);
	if (params.shelfNum != null) inst.setShelfNumber(params.shelfNum);
	if (params.formerGA != null) inst.setFormerGANumber(params.formerGA);
	if (params.contentOverview != null) inst.setContentOverview(params.contentOverview);
	if (params.leavesDesc != null) inst.setLeavesDescription(params.leavesDesc);
	if (params.leavesCount != null) inst.setLeavesCount(params.leavesCount);
	if (params.origYearDesc != null) inst.setOriginYearDescription(params.origYearDesc);

	if (orig != null) {
		inst.save(orig);
	}
	else {
		inst = inst.saveNew();
	}
	Serializer.output(response, out, params, XMLBlock.createXMLBlock("<shelfInstance shelfID=\""+inst.getShelfInstanceID()+"\"/>"));
	return;
} while (false);
}
else params.format = "html";
Serializer.reportErrors(request, response, out, params, true);
%>