package mosaic;

import java.io.IOException;
import java.sql.Types;

import app.Request;
import app.Site;
import db.DBConnection;
import db.JDBCColumn;
import db.JDBCTable;
import db.Options;
import db.OrderBy;
import db.SectionDef;
import db.SectionDef.Type;
import db.Select;
import db.View;
import db.ViewDef;
import db.access.RecordOwnerAccessPolicy;
import db.column.Column;
import social.NewsProvider;

public class Reviews extends NewsProvider
{
	private Options	m_categories;

	// --------------------------------------------------------------------------

	public
	Reviews()
	{
		super("reviews", "posted a review");
	}

	// --------------------------------------------------------------------------

	@Override
	public String
	getDescription() {
		return "Share reviews of anything, including movies, books, plumbers, dentists, etc.";
	}

	//--------------------------------------------------------------------------

	@Override
	public void
	init(DBConnection db)
	{
		JDBCTable table_def = new JDBCTable()
			.add(new JDBCColumn("title", Types.VARCHAR, 60))
			.add(new JDBCColumn("categories_id", Types.INTEGER))
			.add(new JDBCColumn("review", Types.VARCHAR))
			.add(new JDBCColumn("_owner_", "people"));
		addColumns(table_def);
		db.getTable("reviews", true).matchColumns(table_def, db);
		db.createTable("reviews_categories", "text VARCHAR");
		m_categories = new Options(new Select("*").from("reviews_categories").orderBy("text"), true, m_site).setAllowEditing(true);
		m_site.addObjects(m_categories);
	}

	//--------------------------------------------------------------------------

	@Override
	public ViewDef
	_newViewDef(String name, Site site)
	{
		if (name.equals("reviews"))
			return addHooks(new ViewDef(name)
				.setAccessPolicy(new RecordOwnerAccessPolicy().add().delete().edit().view())
				.setDefaultOrderBy("title")
				.setFormWidth("520px")
				.setSectionDef(new SectionDef("categories_id", Type.TABS, new OrderBy("categories_id")))
				.setRecordName("Review")
				.setColumnNamesForm(new String[] { "title", "categories_id", "review" })
				.setColumnNamesTable(new String[] { "title" })
				.setColumn(((Options)site.getObjects("reviews_categories")).newColumn("categories_id").setDisplayName("category"))
				.setColumn(new Column("coho").setDefaultValue("true").setIsHidden(true))
				.setColumn(new Column("family").setDefaultValue("false").setIsHidden(true)));
		if (name.equals("reviews_categories"))
			return m_categories.newViewDef(name, site)
				.setDefaultOrderBy("text")
				.setDialogModes(View.Mode.ADD_FORM, View.Mode.EDIT_FORM, View.Mode.READ_ONLY_FORM)
				.setRecordName("Category")
				.setColumn(new Column("text").setDisplayName("category").setIsRequired(true));
		return null;
	}

	//--------------------------------------------------------------------------

	@Override
	public void
	writeItem(int item_id, Request request) throws IOException
	{
		String[] row = request.db.readRow(new Select("title,review").from("reviews").whereIdEquals(item_id));
		request.writer.h5(row[0]);
		request.writer.tag("p", row[1]);
	}
}
