set @adminAuditModuleId := (SELECT id FROM ohrm_audittrail_module WHERE name = 'HR Administration');

INSERT INTO `ohrm_audittrail_section` (`name`, `description`, `affected_entity`, `module_id`) VALUES
  ('NewsArticle', 'News', 'News', @adminAuditModuleId),
  ('DocumentArticle', 'Document', 'Document', @adminAuditModuleId);
set @newsArticleId := (SELECT id FROM ohrm_audittrail_section WHERE name = 'NewsArticle');
set @documentArticleId := (SELECT id FROM ohrm_audittrail_section WHERE name = 'DocumentArticle');

INSERT INTO `ohrm_audittrail_section_actions` (`section_id`, `action`, `description`) VALUES
  (@documentArticleId, 'SAVE DOCUMENT', 'Save'),
  (@documentArticleId, 'PUBLISH DOCUMENT', 'Publish'),
  (@documentArticleId, 'ACKNOWLEDGE DOCUMENT', 'Acknowledge'),
  (@documentArticleId, 'DELETE DOCUMENT', 'Delete'),
  (@documentArticleId, 'ARCHIVE DOCUMENT', 'Archive'),
  (@newsArticleId, 'SAVE NEWS', 'Save'),
  (@newsArticleId, 'PUBLISH NEWS', 'Publish'),
  (@newsArticleId, 'DELETE NEWS', 'Delete'),
  (@newsArticleId, 'ARCHIVE NEWS', 'Archive'),
  (@newsArticleId, 'ACKNOWLEDGE NEWS', 'Acknowledge');


DROP TABLE IF EXISTS `ohrm_audittrail_admin_news_article_trail`;

CREATE TABLE `ohrm_audittrail_admin_news_article_trail` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `action_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `action_owner_id` varchar(6),
  `action_owner_name` VARCHAR(300),
  `version_id` int(11) NOT NULL DEFAULT '0',
  `action` varchar(255) NOT NULL,
  `affected_entity_id` varchar(20) NOT NULL DEFAULT '',
  `affected_entity_name` VARCHAR(300) NOT NULL DEFAULT '',
  `action_description` LONGTEXT CHARSET UTF8,
  `app_id` INT DEFAULT NULL,
  `app_name` VARCHAR(1000) DEFAULT NULL,
  `screen_name` VARCHAR(1000) DEFAULT NULL,
  PRIMARY KEY (`id`),
  INDEX audit_admin_news_article_trail_idx_app_id (app_id),
  INDEX audit_admin_news_article_trail_idx_screen_name (screen_name)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

DROP TABLE IF EXISTS `ohrm_audittrail_admin_documents_article_trail`;

CREATE TABLE `ohrm_audittrail_admin_documents_article_trail` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `action_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `action_owner_id` varchar(6),
  `action_owner_name` VARCHAR(300),
  `version_id` int(11) NOT NULL DEFAULT '0',
  `action` varchar(255) NOT NULL,
  `affected_entity_id` varchar(20) NOT NULL DEFAULT '',
  `affected_entity_name` VARCHAR(300) NOT NULL DEFAULT '',
  `action_description` LONGTEXT CHARSET UTF8,
  `app_id` INT DEFAULT NULL,
  `app_name` VARCHAR(1000) DEFAULT NULL,
  `screen_name` VARCHAR(1000) DEFAULT NULL,
  PRIMARY KEY (`id`),
  INDEX audit_admin_documents_article_trail_idx_app_id (app_id),
  INDEX audit_admin_documents_article_trail_idx_screen_name (screen_name)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;


INSERT INTO `ohrm_event_listener` (`event`, `listener_class`, `enabled`, `position`) VALUES
('news.document_article.update', 'DocumentAuditListener', '1', '1'),
('news.document_article.update', 'DocumentPublishListener', '1', '2'),
('news.document_article.delete', 'DocumentAuditListener', '1', '1'),
('news.document_article.create', 'DocumentAuditListener', '1', '1'),
('news.document_article_attachment.create', 'DocumentAuditListener', '1', '1'),
('news.document_article_attachment.delete', 'DocumentAuditListener', '1', '1'),
('news.document_article_attachment.update', 'DocumentAuditListener', '1', '1'),
('news.document_article_status.update', 'DocumentAuditListener', '1', '1'),
('news.news_article.update', 'NewsAuditListener', '1', '1'),
('news.news_article.update', 'NewsPublishListener', '1', '2'),
('news.news_article.delete', 'NewsAuditListener', '1', '1'),
('news.news_article.create', 'NewsAuditListener', '1', '1'),
('news.news_article_attachment.create', 'NewsAuditListener', '1', '1'),
('news.news_article_attachment.delete', 'NewsAuditListener', '1', '1'),
('news.news_article_attachment.update', 'NewsAuditListener', '1', '1'),
('news.news_article_status.update', 'NewsAuditListener', '1', '1');

SET @ohrm_data_group_announcement_document_id =  (SELECT `id`  FROM `ohrm_data_group` WHERE `name` = 'announcement_document');
SET @ohrm_data_group_announcement_news_id =  (SELECT `id`  FROM `ohrm_data_group` WHERE `name` = 'announcement_news');

SET @ohrm_audittrail_section_document_article = (SELECT `id` FROM `ohrm_audittrail_section` WHERE `name`='DocumentArticle');
SET @ohrm_audittrail_section_news_article = (SELECT `id` FROM `ohrm_audittrail_section` WHERE `name`='NewsArticle');

INSERT INTO `ohrm_datagroup_audittrail_section` (`audit_section_id`, `datagroup_id`, `consider_in_notification`)
VALUES
    (@ohrm_audittrail_section_document_article, @ohrm_data_group_announcement_document_id, 0),
    (@ohrm_audittrail_section_news_article, @ohrm_data_group_announcement_news_id, 0);