SELECT @campus_id:=ID_INTERNAL FROM RESOURCE WHERE RESOURCE.OJB_CONCRETE_CLASS LIKE '%CAMPUS';
SELECT @degree_admin_office_id:=ID_INTERNAL FROM ADMINISTRATIVE_OFFICE WHERE ADMINISTRATIVE_OFFICE_TYPE = 'DEGREE';
SELECT @master_degree_admin_office_id:=ID_INTERNAL FROM ADMINISTRATIVE_OFFICE WHERE ADMINISTRATIVE_OFFICE_TYPE = 'MASTER_DEGREE';

INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION_GROUP (KEY_CAMPUS, KEY_ADMINISTRATIVE_OFFICE, KEY_ROOT_DOMAIN_OBJECT) VALUES (@campus_id, @degree_admin_office_id, 1), (@campus_id, @master_degree_admin_office_id, 1);

SELECT @admin_user_person_id:=ID_INTERNAL FROM PARTY WHERE PARTY.PARTY_NAME LIKE '%Fenix System Administrator';
SELECT @degree_admin_office_unit_id:=KEY_UNIT FROM ADMINISTRATIVE_OFFICE WHERE ADMINISTRATIVE_OFFICE_TYPE = 'DEGREE';
SELECT @master_degree_admin_office_unit_id:=KEY_UNIT FROM ADMINISTRATIVE_OFFICE WHERE ADMINISTRATIVE_OFFICE_TYPE = 'MASTER_DEGREE';
SELECT @degree_admin_person_id:=KEY_CHILD_PARTY FROM ACCOUNTABILITY WHERE KEY_PARENT_PARTY = @degree_admin_office_unit_id AND KEY_PARENT_PARTY <> @admin_user_person_id LIMIT 1;
SELECT @master_degree_admin_person_id:=KEY_CHILD_PARTY FROM ACCOUNTABILITY WHERE KEY_PARENT_PARTY = @master_degree_admin_office_unit_id AND KEY_PARENT_PARTY <> @admin_user_person_id LIMIT 1;


INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION (PERMISSION_TYPE, KEY_ROOT_DOMAIN_OBJECT, PERMISSION_MEMBERS_GROUP, KEY_ADMINISTRATIVE_OFFICE_PERMISSION_GROUP, OJB_CONCRETE_CLASS) VALUES ("MANAGE_CONCLUSION", 1, CONCAT("fixed(", @master_degree_admin_person_id, ")"), (SELECT ADMINISTRATIVE_OFFICE_PERMISSION_GROUP.ID_INTERNAL FROM ADMINISTRATIVE_OFFICE_PERMISSION_GROUP WHERE KEY_CAMPUS = @campus_id AND KEY_ADMINISTRATIVE_OFFICE = @master_degree_admin_office_id), 'net.sourceforge.fenixedu.domain.accessControl.academicAdminOffice.GenericAdministrativeOfficePermission');
INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION (PERMISSION_TYPE, KEY_ROOT_DOMAIN_OBJECT, PERMISSION_MEMBERS_GROUP, KEY_ADMINISTRATIVE_OFFICE_PERMISSION_GROUP, OJB_CONCRETE_CLASS) VALUES ("MANAGE_MARKSHEETS", 1, CONCAT("fixed(", @master_degree_admin_person_id, ")"), (SELECT ADMINISTRATIVE_OFFICE_PERMISSION_GROUP.ID_INTERNAL FROM ADMINISTRATIVE_OFFICE_PERMISSION_GROUP WHERE KEY_CAMPUS = @campus_id AND KEY_ADMINISTRATIVE_OFFICE = @master_degree_admin_office_id), 'net.sourceforge.fenixedu.domain.accessControl.academicAdminOffice.GenericAdministrativeOfficePermission');
INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION (PERMISSION_TYPE, KEY_ROOT_DOMAIN_OBJECT, PERMISSION_MEMBERS_GROUP, KEY_ADMINISTRATIVE_OFFICE_PERMISSION_GROUP, OJB_CONCRETE_CLASS) VALUES ("RECTIFICATION_MARKSHEETS", 1, CONCAT("fixed(", @master_degree_admin_person_id, ")"), (SELECT ADMINISTRATIVE_OFFICE_PERMISSION_GROUP.ID_INTERNAL FROM ADMINISTRATIVE_OFFICE_PERMISSION_GROUP WHERE KEY_CAMPUS = @campus_id AND KEY_ADMINISTRATIVE_OFFICE = @master_degree_admin_office_id), 'net.sourceforge.fenixedu.domain.accessControl.academicAdminOffice.GenericAdministrativeOfficePermission');
INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION (PERMISSION_TYPE, KEY_ROOT_DOMAIN_OBJECT, PERMISSION_MEMBERS_GROUP, KEY_ADMINISTRATIVE_OFFICE_PERMISSION_GROUP, OJB_CONCRETE_CLASS) VALUES ("DISSERTATION_MARKSHEETS", 1, CONCAT("fixed(", @master_degree_admin_person_id, ")"), (SELECT ADMINISTRATIVE_OFFICE_PERMISSION_GROUP.ID_INTERNAL FROM ADMINISTRATIVE_OFFICE_PERMISSION_GROUP WHERE KEY_CAMPUS = @campus_id AND KEY_ADMINISTRATIVE_OFFICE = @master_degree_admin_office_id), 'net.sourceforge.fenixedu.domain.accessControl.academicAdminOffice.GenericAdministrativeOfficePermission');
INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION (PERMISSION_TYPE, KEY_ROOT_DOMAIN_OBJECT, PERMISSION_MEMBERS_GROUP, KEY_ADMINISTRATIVE_OFFICE_PERMISSION_GROUP, OJB_CONCRETE_CLASS) VALUES ("UPDATE_REGISTRATION_AFTER_CONCLUSION", 1, CONCAT("fixed(", @master_degree_admin_person_id, ")"), (SELECT ADMINISTRATIVE_OFFICE_PERMISSION_GROUP.ID_INTERNAL FROM ADMINISTRATIVE_OFFICE_PERMISSION_GROUP WHERE KEY_CAMPUS = @campus_id AND KEY_ADMINISTRATIVE_OFFICE = @master_degree_admin_office_id), 'net.sourceforge.fenixedu.domain.accessControl.academicAdminOffice.UpdateRegistrationAfterConclusionPermission');
INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION (PERMISSION_TYPE, KEY_ROOT_DOMAIN_OBJECT, PERMISSION_MEMBERS_GROUP, KEY_ADMINISTRATIVE_OFFICE_PERMISSION_GROUP, OJB_CONCRETE_CLASS) VALUES ("ENROLMENT_WITHOUT_RULES", 1, CONCAT("fixed(", @master_degree_admin_person_id, ")"), (SELECT ADMINISTRATIVE_OFFICE_PERMISSION_GROUP.ID_INTERNAL FROM ADMINISTRATIVE_OFFICE_PERMISSION_GROUP WHERE KEY_CAMPUS = @campus_id AND KEY_ADMINISTRATIVE_OFFICE = @master_degree_admin_office_id), 'net.sourceforge.fenixedu.domain.accessControl.academicAdminOffice.GenericAdministrativeOfficePermission');

INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION (PERMISSION_TYPE, KEY_ROOT_DOMAIN_OBJECT, PERMISSION_MEMBERS_GROUP, KEY_ADMINISTRATIVE_OFFICE_PERMISSION_GROUP, OJB_CONCRETE_CLASS) VALUES ("MANAGE_CONCLUSION", 1, CONCAT("fixed(", @degree_admin_person_id, ")"), (SELECT ADMINISTRATIVE_OFFICE_PERMISSION_GROUP.ID_INTERNAL FROM ADMINISTRATIVE_OFFICE_PERMISSION_GROUP WHERE KEY_CAMPUS = @campus_id AND KEY_ADMINISTRATIVE_OFFICE = @degree_admin_office_id), 'net.sourceforge.fenixedu.domain.accessControl.academicAdminOffice.GenericAdministrativeOfficePermission');
INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION (PERMISSION_TYPE, KEY_ROOT_DOMAIN_OBJECT, PERMISSION_MEMBERS_GROUP, KEY_ADMINISTRATIVE_OFFICE_PERMISSION_GROUP, OJB_CONCRETE_CLASS) VALUES ("MANAGE_MARKSHEETS", 1, CONCAT("fixed(", @degree_admin_person_id, ")"), (SELECT ADMINISTRATIVE_OFFICE_PERMISSION_GROUP.ID_INTERNAL FROM ADMINISTRATIVE_OFFICE_PERMISSION_GROUP WHERE KEY_CAMPUS = @campus_id AND KEY_ADMINISTRATIVE_OFFICE = @degree_admin_office_id), 'net.sourceforge.fenixedu.domain.accessControl.academicAdminOffice.GenericAdministrativeOfficePermission');
INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION (PERMISSION_TYPE, KEY_ROOT_DOMAIN_OBJECT, PERMISSION_MEMBERS_GROUP, KEY_ADMINISTRATIVE_OFFICE_PERMISSION_GROUP, OJB_CONCRETE_CLASS) VALUES ("RECTIFICATION_MARKSHEETS", 1, CONCAT("fixed(", @degree_admin_person_id, ")"), (SELECT ADMINISTRATIVE_OFFICE_PERMISSION_GROUP.ID_INTERNAL FROM ADMINISTRATIVE_OFFICE_PERMISSION_GROUP WHERE KEY_CAMPUS = @campus_id AND KEY_ADMINISTRATIVE_OFFICE = @degree_admin_office_id), 'net.sourceforge.fenixedu.domain.accessControl.academicAdminOffice.GenericAdministrativeOfficePermission');
INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION (PERMISSION_TYPE, KEY_ROOT_DOMAIN_OBJECT, PERMISSION_MEMBERS_GROUP, KEY_ADMINISTRATIVE_OFFICE_PERMISSION_GROUP, OJB_CONCRETE_CLASS) VALUES ("UPDATE_REGISTRATION_AFTER_CONCLUSION", 1, CONCAT("fixed(", @degree_admin_person_id, ")"), (SELECT ADMINISTRATIVE_OFFICE_PERMISSION_GROUP.ID_INTERNAL FROM ADMINISTRATIVE_OFFICE_PERMISSION_GROUP WHERE KEY_CAMPUS = @campus_id AND KEY_ADMINISTRATIVE_OFFICE = @degree_admin_office_id), 'net.sourceforge.fenixedu.domain.accessControl.academicAdminOffice.UpdateRegistrationAfterConclusionPermission');
INSERT INTO ADMINISTRATIVE_OFFICE_PERMISSION (PERMISSION_TYPE, KEY_ROOT_DOMAIN_OBJECT, PERMISSION_MEMBERS_GROUP, KEY_ADMINISTRATIVE_OFFICE_PERMISSION_GROUP, OJB_CONCRETE_CLASS) VALUES ("ENROLMENT_WITHOUT_RULES", 1, CONCAT("fixed(", @degree_admin_person_id, ")"), (SELECT ADMINISTRATIVE_OFFICE_PERMISSION_GROUP.ID_INTERNAL FROM ADMINISTRATIVE_OFFICE_PERMISSION_GROUP WHERE KEY_CAMPUS = @campus_id AND KEY_ADMINISTRATIVE_OFFICE = @degree_admin_office_id), 'net.sourceforge.fenixedu.domain.accessControl.academicAdminOffice.GenericAdministrativeOfficePermission');