package info.bukova.isspst.services.dbinfo;

import info.bukova.isspst.Constants;
import info.bukova.isspst.data.DbInfo;
import info.bukova.isspst.services.AbstractService;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;
import org.springframework.transaction.annotation.Transactional;

public class DbInfoServiceImpl extends AbstractService<DbInfo> implements DbInfoService
{
	private DbInfo getDbInfo()
	{
		DbInfo dbInfo = null;

		List<DbInfo> list = this.getAll();

		if (list.isEmpty())
		{
			dbInfo = new DbInfo();
			this.add(dbInfo);
			list = this.getAll();
		}

		for (DbInfo item : list)
		{
			dbInfo = item;
			break;
		}

		return dbInfo;
	}

	@Override
	@Transactional
	public void changeDatabase()
	{
		class Str2Str
		{
			private String key;
			private String value;

			public Str2Str(String key, String value)
			{
				this.key = key;
				this.value = value;
			}

			public String getKey()
			{
				return this.key;
			}

			public String getValue()
			{
				return this.value;
			}
		}

		long dbVersion = this.getDbInfo().getVersion();

		if (Constants.DB_VERSION > dbVersion)
		{
			if (dbVersion < 1)
			{
				List<Str2Str> tables = new ArrayList<Str2Str>();
				tables.add(new Str2Str("address", "DESCRIPTION"));
				tables.add(new Str2Str("building", "DESCRIPTION"));
				tables.add(new Str2Str("material", "DESCRIPTION"));
				tables.add(new Str2Str("munit", "DESCRIPTION"));
				tables.add(new Str2Str("orders", "DESCRIPTION"));
				tables.add(new Str2Str("order_item", "DESCRIPTION"));
				tables.add(new Str2Str("permission", "DESCRIPTION"));
				tables.add(new Str2Str("requirement", "DESCRIPTION"));
				tables.add(new Str2Str("requirementtype", "DESCRIPTION"));
				tables.add(new Str2Str("requirement_items", "DESCRIPTION"));
				tables.add(new Str2Str("role", "DESCRIPTION"));
				tables.add(new Str2Str("service", "DESCRIPTION"));
				tables.add(new Str2Str("triprequirement", "DESCRIPTION"));

				tables.add(new Str2Str("material", "MUNIT_DESCRIPTION"));
				tables.add(new Str2Str("orders", "INVOICE_DESCRIPTION"));
				tables.add(new Str2Str("orders", "DELIVERY_DESCRIPTION"));
				tables.add(new Str2Str("orders", "SUPPLIER_DESCRIPTION"));
				tables.add(new Str2Str("order_item", "MUNIT_DESCRIPTION"));
				tables.add(new Str2Str("requirement_items", "MUNIT_DESCRIPTION"));
				tables.add(new Str2Str("triprequirement", "VEHICLE_DESCRIPTION"));
				tables.add(new Str2Str("trip_bill_items", "BACK_VEHICLE_DESCRIPTION"));
				tables.add(new Str2Str("trip_bill_items", "VEHICLE_DESCRIPTION"));
				
				SQLQuery sq = null;
				String sql = "";

				for (Str2Str item : tables)
				{
					sql = "ALTER TABLE " + item.getKey() + " MODIFY " + item.getValue() + " VARCHAR(" + String.valueOf(Constants.LEN_DESCRIPTION) + ")";
					sq = this.dao.getSession().createSQLQuery(sql);
					sq.executeUpdate();
				}
			}

			this.updateDatabaseVersion();
		}
	}

	@Override
	@Transactional
	public void updateDatabaseVersion()
	{
		DbInfo dbInfo = this.getDbInfo();
		dbInfo.setVersion(Constants.DB_VERSION);
		this.update(dbInfo);
	}
}
