alter table `FILE` add column `KEY_JOB` int(11);
alter table `FILE` add index (`KEY_JOB`);
alter table `QUEUE_JOB` add column `KEY_FILE` int(11);
alter table `QUEUE_JOB` add index (`KEY_FILE`);

INSERT INTO FILE_LOCAL_CONTENT
SELECT NULL, QUEUE_JOB.CONTENT,
CONCAT("<virtualpath><nodes><virtualpathnode><name>GeneratedDocuments</name><description>Generated Documents</description></virtualpathnode><virtualpathnode><name>",PARTY.ID_INTERNAL, "</name><description>",SUBSTRING(PARTY_NAME,LOCATE(':', PARTY_NAME, 5)+1),"</description></virtualpathnode><virtualpathnode><name>QUEUE_JOB</name><description>QUEUE_JOB</description></virtualpathnode></nodes></virtualpath>"), NULL, 1
FROM QUEUE_JOB, PARTY
WHERE QUEUE_JOB.KEY_PERSON = PARTY.ID_INTERNAL AND CONTENT IS NOT NULL AND PARTY.OJB_CONCRETE_CLASS = "net.sourceforge.fenixedu.domain.Person";


INSERT INTO FILE
SELECT NULL, "QueueJobFile","QueueJobFile","application/octet-stream",NULL,NULL,OCTET_LENGTH(QUEUE_JOB.CONTENT),NULL,1,NULL,NULL,"net.sourceforge.fenixedu.domain.QueueJobResultFile",NULL,JOB_END_TIME,NULL,NULL,NULL,NULL,NULL,1,0,NULL,"role('PERSON')",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,KEY_PERSON,NULL,KEY_PERSON,"QUEUE_JOB",NULL,NULL,NULL,FILE_LOCAL_CONTENT.ID_INTERNAL, QUEUE_JOB.ID_INTERNAL
FROM FILE_LOCAL_CONTENT, QUEUE_JOB
WHERE QUEUE_JOB.CONTENT IS NOT NULL AND FILE_LOCAL_CONTENT.CONTENT = QUEUE_JOB.CONTENT;

UPDATE FILE_LOCAL_CONTENT, FILE
SET FILE_LOCAL_CONTENT.KEY_FILE = FILE.ID_INTERNAL
WHERE FILE.KEY_LOCAL_CONTENT = FILE_LOCAL_CONTENT.ID_INTERNAL;

UPDATE QUEUE_JOB, FILE
SET QUEUE_JOB.KEY_FILE = FILE.ID_INTERNAL
WHERE FILE.KEY_JOB = QUEUE_JOB.ID_INTERNAL;

ALTER TABLE QUEUE_JOB DROP COLUMN CONTENT;
DELETE FROM OJB_HL_SEQ;




