--[[----------------------------------------------------------------------------

WFOldOzDocStoreWriter.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2016 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

--------------------------------------------------------------------------------

This file implements an interface to writing full representations of documents
to the old OzDocStore format. It is only intended for the OzDocStore migration to the v2 
DocStore format.
------------------------------------------------------------------------------]]

	-- RxLua
local Rx = require 'Rx'

	-- WF.core.blonde
local blonde = require 'blonde'

	-- WF.core.utils
local WFArguments = require 'WFArguments'
local WFCloseSupport = require 'WFCloseSupport'

	-- WF.sqlite.database
local WFSQLiteDatabase = require 'WFSQLiteDatabase'
local WFSQLiteDatabaseSchema = require 'WFSQLiteDatabaseSchema'


--------------------------------------------------------------------------------

local check = assert( WFArguments.check )


--============================================================================--

-- @mark database schema

local schema = WFSQLiteDatabaseSchema( '1.0' ):useWAL()

--------------------------------------------------------------------------------

schema:addTable
	[[CREATE TABLE localState(
						name UNIQUE NOT NULL,
						dataType,
						value )]]
	:onConflict 'replace'

schema:addIndex
	[[CREATE INDEX localState_name ON localState( name )]]

--------------------------------------------------------------------------------

schema:addTable
	[[CREATE TABLE documents(
						docId UNIQUE NOT NULL,
						docType,
						changeCounter,
						lockCount NOT NULL DEFAULT 0,
						embargoedServerValue,
						serverBaseValue,
						localOverlay )]]
	:onConflict 'replace'
		-- FWIW lockCount and embargoedServerValue have not been used since
		-- Revel 1.0 (according to a 2011-10-04 comment) but were left in
		-- because we don't have schema migration support.

schema:addIndex
	[[CREATE INDEX documents_changeCounter ON documents( changeCounter )]]


--============================================================================--

-- @mark WFOldOzDocStoreWriter class

local wfOldOzDocStoreWriterMethods = {

	_db = false,

	_path = false,
	
	_changeCounter = false,

	WFCloseSupport.addCloseSupportMethods,

}

--------------------------------------------------------------------------------

function wfOldOzDocStoreWriterMethods:__init( path )

	local db = WFSQLiteDatabase( path, schema )
	self._db = db

	WFCloseSupport.mustCloseAtThreadTermination( self )
	
	local changeCounterSeedRow = db:execOnce( "SELECT max( changeCounter ) FROM documents" )
	self._changeCounter = ( changeCounterSeedRow
		and changeCounterSeedRow[ "max( changeCounter )" ] or 0 ) + 1
	
end

--------------------------------------------------------------------------------

function wfOldOzDocStoreWriterMethods:close()

	-- Using close intead of setOwned so the client can close
	-- and then delete the database immediately afterwards.

	self._db:close()
	self._db = nil

end

--------------------------------------------------------------------------------

function wfOldOzDocStoreWriterMethods:writeDocument( doc )

	check( 'WFOldOzDocStoreWriter', '?table' )

	self._db:exec( [[INSERT OR REPLACE INTO documents( docId, docType, localOverlay, serverBaseValue )
					VALUES( ?, ?, ?, ?)]], doc.docId, doc.docType, 
					doc.localOverlay and blonde.encode( doc.localOverlay ),
					doc.serverBaseValue and blonde.encode( doc.serverBaseValue) )
					
end

--------------------------------------------------------------------------------

function wfOldOzDocStoreWriterMethods:__tostring()

	return string.format( "WFOldOzDocStoreWriter( %s )", self._db:path() )

end

--------------------------------------------------------------------------------

return Rx.Class 'WFOldOzDocStoreWriter'( wfOldOzDocStoreWriterMethods )
