}
addTriggerToHTMLPage();
}
private void triggerChanges() throws SQLException {
DotConnect dc = new DotConnect();
String trigger = "";
if(DbConnectionFactory.isPostgres()){
trigger = "CREATE OR REPLACE FUNCTION structure_host_folder_check() RETURNS trigger AS '\n" +
"DECLARE\n" +
"folderInode varchar(36);\n" +
"hostInode varchar(36);\n" +
"BEGIN\n" +
"IF ((tg_op = ''INSERT'' OR tg_op = ''UPDATE'') AND (NEW.host IS NOT NULL AND NEW.host <> '''' AND NEW.host <> ''SYSTEM_HOST''\n" +
"AND NEW.folder IS NOT NULL AND NEW.folder <> ''SYSTEM_FOLDER'' AND NEW.folder <> '''')) THEN\n" +
"select host_inode,folder.inode INTO hostInode,folderInode from folder,identifier where folder.identifier = identifier.id and folder.inode=NEW.folder;\n" +
"IF (FOUND AND NEW.host = hostInode) THEN\n" +
"RETURN NEW;\n" +
"ELSE\n" +
"RAISE EXCEPTION ''Cannot assign host/folder to structure, folder does not belong to given host'';\n" +
"RETURN NULL;\n" +
"END IF;\n" +
"ELSE\n" +
"IF((tg_op = ''INSERT'' OR tg_op = ''UPDATE'') AND (NEW.host IS NULL OR NEW.host = '''' OR NEW.host= ''SYSTEM_HOST''\n" +
"OR NEW.folder IS NULL OR NEW.folder = '''' OR NEW.folder = ''SYSTEM_FOLDER'')) THEN\n" +
"IF(NEW.host = ''SYSTEM_HOST'' OR NEW.host IS NULL OR NEW.host = '''') THEN\n" +
"NEW.host = ''SYSTEM_HOST'';\n" +
"NEW.folder = ''SYSTEM_FOLDER'';\n" +
"END IF;\n" +
"IF(NEW.folder = ''SYSTEM_FOLDER'' OR NEW.folder IS NULL OR NEW.folder = '''') THEN\n" +
"NEW.folder = ''SYSTEM_FOLDER'';\n" +
"END IF;\n" +
"RETURN NEW;\n" +
"END IF;\n" +
"END IF;\n" +
"RETURN NULL;\n" +
"END\n" +
"' LANGUAGE plpgsql;";
}else if(DbConnectionFactory.isOracle()){
trigger = "CREATE OR REPLACE TRIGGER structure_host_folder_trigger\n" +
"BEFORE INSERT OR UPDATE ON structure\n" +
"FOR EACH ROW\n" +
"DECLARE\n" +
"folderInode varchar2(36);\n" +
"hostInode varchar2(36);\n" +
"BEGIN\n" +
"IF (:NEW.host <> 'SYSTEM_HOST' AND :NEW.folder <> 'SYSTEM_FOLDER') THEN\n" +
"select host_inode, folder.inode INTO hostInode, folderInode from folder,identifier where folder.identifier = identifier.id and folder.inode = :NEW.folder;\n" +
"IF (:NEW.host <> hostInode) THEN\n" +
"RAISE_APPLICATION_ERROR(-20000, 'Cannot assign host/folder to structure, folder does not belong to given host');\n" +
"END IF;\n" +
"ELSE\n" +
"IF(:NEW.host IS NULL OR :NEW.host = '' OR :NEW.host = 'SYSTEM_HOST' OR :NEW.folder IS NULL OR :NEW.folder = '' OR :NEW.folder = 'SYSTEM_FOLDER') THEN\n" +
"IF(:NEW.host = 'SYSTEM_HOST' OR :NEW.host IS NULL OR :NEW.host = '') THEN\n" +
":NEW.host := 'SYSTEM_HOST';\n" +
":NEW.folder := 'SYSTEM_FOLDER';\n" +
"END IF;\n" +
"IF(:NEW.folder = 'SYSTEM_FOLDER' OR :NEW.folder IS NULL OR :NEW.folder = '') THEN\n" +
":NEW.folder := 'SYSTEM_FOLDER';\n" +
"END IF;\n" +
"END IF;\n" +
"END IF;\n" +
"END;\n" +
"/";
}else if(DbConnectionFactory.isMsSql()){
trigger = "ALTER TRIGGER structure_host_folder_trigger\n" +
"ON structure\n" +
"FOR INSERT, UPDATE AS\n" +
"DECLARE @newFolder varchar(100)\n" +
"DECLARE @newHost varchar(100)\n" +
"DECLARE @folderInode varchar(36)\n" +
"DECLARE @hostInode varchar(36)\n" +
"DECLARE cur_Inserted3 cursor LOCAL FAST_FORWARD for\n" +
"Select folder, host\n" +
"from inserted\n" +
"for Read Only\n" +
"open cur_Inserted3\n" +
"fetch next from cur_Inserted3 into @newFolder,@newHost\n" +
"while @@FETCH_STATUS <> -1\n" +
"BEGIN\n" +
"IF (@newHost <> 'SYSTEM_HOST' AND @newFolder <> 'SYSTEM_FOLDER')\n" +
"BEGIN\n" +
"SELECT @hostInode = identifier.host_inode, @folderInode = folder.inode from folder,identifier where folder.identifier = identifier.id and folder.inode = @newFolder\n" +
"IF (@folderInode IS NULL OR @folderInode = '' OR @newHost <> @hostInode)\n" +
"BEGIN\n" +
"RAISERROR (N'Cannot assign host/folder to structure, folder does not belong to given host', 10, 1)\n" +
"ROLLBACK WORK\n" +
"END\n" +
"END\n" +
"fetch next from cur_Inserted3 into @newFolder,@newHost\n" +
"END;";
}
List<String> triggers = SQLUtil.tokenize(trigger);
for(String t:triggers){
dc.executeStatement(t);
}
}