Category=Database Maintenance - CAUTION
Name=08. Generate- Pages for Empty Documents
Description=Generates pages for documents with no page information and assigns those pages to a shelf instance if possible

Heading=Generate Pages Into PAGE from LEAVES * 2

OutputLevel=1
ImportSQL=SELECT		\
	\
CAST(SUBSTR(USERDATA,1,5) AS SIGNED) AS DOCUMENTID,		\
SHELFID,		\
(LEAVES * 2) - (select count(*) from PAGE P where P.DOCUMENTID=CAST(SUBSTR(USERDATA,1,5) AS SIGNED) and not exists (select 1 from SHELFINSTANCEPAGES SIP WHERE SIP.DOCUMENTID=P.DOCUMENTID and SIP.PAGEID=P.PAGEID))		\
	\
FROM SHELFINSTANCE T1 WHERE NOT EXISTS (SELECT * FROM SHELFINSTANCEPAGES T2 WHERE T2.SHELFID = T1.SHELFID)|	\
[1..{2}]INSERT INTO PAGE (	\
		\
DOCUMENTID,	\
PAGEID,		\
FOLIONUMBER,	\
SORTORDER)	\
		\
SELECT		\
		\
{1} DOCUMENTID,	\
COALESCE(MAX(PAGEID), 0) + 10 PAGEID,	\
'' FOLIONUMBER,				\
cast(lpad(COALESCE(MAX(PAGEID), 0) + 10, 5, '0') as char)		\
		\
FROM PAGE WHERE DOCUMENTID={1}

IgnoreException=Error Code: 1146

