<%@ 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.ProjectManagement" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.DocumentGroup" %>
<%@ page import="org.crosswire.community.projects.ntmss.data.Transcription" %>
<%@ page import="org.crosswire.xml.XMLBlock" %>
<%@ page import="org.crosswire.webtools.RightsAndRoles.UserGroup" %>
<%@ page import="org.crosswire.webtools.*" %>
<%@ page import="org.crosswire.webtools.annotation.*" %>

<%!
@Description(value = "Create a new or update an existing document group.", name = "documentgroup/put")
public static class MyParameters extends Parameters<MyParameters> {

	protected ProjectManagement.Project project = null;
	protected long userID = -1;

	@Description(value = "Document Group ID, supply to edit an existing document group; otherwise, do not include when adding a new document group", defaultValue = "null - create new document group", example = "1")
	public Integer documentGroupID = null;

	@Description(value = "Name of the document group", example = "My Liturgical Mss")
	public String name;

	@Description(value = "Description of the document group", example = "Mss which are interesting for my Liturgical research")
	public String description;

	@Description(value = "Project ID for which to retrieve document groups", example = "1")
	public Integer projectID;

	@Description(value = "Project Name for which to retrieve document groups", example = "ECM Matthew")
	public String projectName;

	@Description(value = "internal")
	public String userGroupName;

	@Override
	protected void afterLoad() {
		if (projectName == null) projectName = userGroupName;
		projectName = Transcription.assureUnicode(projectName);
		name        = Transcription.assureUnicode(name);
		description = Transcription.assureUnicode(description);
	}

	@Override
	protected void customValidation() {

		if (getUser() == null) {
			addError(-5, "Must be logged in to edit document groups.");
			return;
		}

		if (documentGroupID == null) {
			if (name == null) {
				addError(-6, "Must specify {name} when creating a new document group.");
			}
		}

		if (projectID != null || projectName != null) {
			project = projectID != null ? ProjectManagement.getProject(projectID) : ProjectManagement.getProject(projectName);
			if (project == null) {
				addError(-7, "Project not found.");
				return;
			}
		}
	}
}

%>
<%

MyParameters params = new MyParameters().loadFromRequest(request, response, false);

if (params.getErrors().size() == 0) {

	// TODO: really?  we allow saving as owned by a different user than the current user?
	// Remove this if possible
	String userName = request.getParameter("userName");
	if (userName == null) userName = params.getUser().getUserName();


	do {
	String role = "VMR Administrator";
	boolean admin = params.getUser().hasRole(role);

	DocumentGroup orig = null;
	DocumentGroup inst = null;
	if (params.documentGroupID == null) {
		inst = new DocumentGroup();
	}
	else {
		orig = DocumentGroup.getDocumentGroup(params.documentGroupID);
		if (orig == null) {
			params.addError(-8, "Couldn't find DocumentGroup ID: " + params.documentGroupID);
			break;
		}
		if (!admin && !userName.equals(orig.getUserID())) {
			params.addError(-9, "No permission to edit document groups for other users. Must be have role: " + role + ".");
			break;
		}
		inst = (DocumentGroup)orig.clone();
	}

	if (params.name != null) inst.setName(params.name);
	if (params.description != null) inst.setDescription(params.description);
	inst.setUserID(userName);
	if (params.project != null) inst.setGroupID(params.project.getProjectName());

	if (orig != null) {
		inst.save(orig);
	}
	else {
		inst = inst.saveNew();
	}

	Serializer.output(response, out, params, XMLBlock.createXMLBlock("<documentGroup documentGroupID=\""+inst.getDocumentGroupID()+"\"/>"));
	return;
	} while (false);
}
else params.format = "html";
Serializer.reportErrors(request, response, out, params, true);
%>