update EVENT set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update DEGREE_INFO set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update EXAM_COORDINATOR set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update EXECUTION_DEGREE set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update EXECUTION_PERIOD set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update ACADEMIC_SERVICE_REQUEST set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update PAYMENT_CODE set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update PAYMENT_PLAN set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update SHIFT_DISTRIBUTION set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update EXPECTATION_EVALUATION_GROUP set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update TRANSACTION set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update INSURANCE_VALUE set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update REGISTRATION_DATA_BY_EXECUTION_YEAR set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update STUDENT_DATA_BY_EXECUTION_YEAR set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update REGISTRATION set KEY_REGISTRATION_YEAR = KEY_REGISTRATION_YEAR + 1000 where KEY_REGISTRATION_YEAR is not null;
update TEACHER_PERSONAL_EXPECTATION set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update TEACHER_PERSONAL_EXPECTATION_PERIOD set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update VIGILANT_GROUP set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update VIGILANT set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update YEAR_STUDENT_SPECIAL_SEASON_CODE set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;
update DELEGATE_ELECTION set KEY_EXECUTION_YEAR = KEY_EXECUTION_YEAR + 1000 where KEY_EXECUTION_YEAR is not null;

rename table EXECUTION_PERIOD to ACADEMIC_PERIOD;

alter table ACADEMIC_PERIOD ADD COLUMN OJB_CONCRETE_CLASS VARCHAR(255) NOT NULL;
alter table ACADEMIC_PERIOD add column KEY_INSURANCE_VALUE int(11) unsigned default NULL;
alter table ACADEMIC_PERIOD add column KEY_SHIFT_DISTRIBUTION int(11) default NULL;

alter table ACADEMIC_PERIOD add index (KEY_INSURANCE_VALUE); 
alter table ACADEMIC_PERIOD add index (KEY_SHIFT_DISTRIBUTION);

alter table ACADEMIC_PERIOD change column KEY_EXECUTION_YEAR KEY_EXECUTION_YEAR int(11) default null;
alter table ACADEMIC_PERIOD change column KEY_INQUIRY_RESPONSE_PERIOD KEY_INQUIRY_RESPONSE_PERIOD int(11) default null;

update ACADEMIC_PERIOD set OJB_CONCRETE_CLASS = 'net.sourceforge.fenixedu.domain.ExecutionPeriod';

insert into ACADEMIC_PERIOD (ID_INTERNAL, NAME, STATE, KEY_INSURANCE_VALUE, KEY_SHIFT_DISTRIBUTION, EXECUTION_INTERVAL, OJB_CONCRETE_CLASS) select EXECUTION_YEAR.ID_INTERNAL + 1000, EXECUTION_YEAR.YEAR, EXECUTION_YEAR.STATE, EXECUTION_YEAR.KEY_INSURANCE_VALUE, EXECUTION_YEAR.KEY_SHIFT_DISTRIBUTION, EXECUTION_YEAR.EXECUTION_INTERVAL, "net.sourceforge.fenixedu.domain.ExecutionYear" from  EXECUTION_YEAR;

alter table ACADEMIC_PERIOD add column KEY_ROOT_DOMAIN_OBJECT_FOR_EXECUTION_PERIOD int(11) default NULL;
alter table ACADEMIC_PERIOD add column KEY_ROOT_DOMAIN_OBJECT_FOR_EXECUTION_YEAR int(11) default NULL;
update ACADEMIC_PERIOD set KEY_ROOT_DOMAIN_OBJECT_FOR_EXECUTION_PERIOD = 1 where OJB_CONCRETE_CLASS like '%ExecutionPeriod%';
update ACADEMIC_PERIOD set KEY_ROOT_DOMAIN_OBJECT_FOR_EXECUTION_YEAR = 1 where OJB_CONCRETE_CLASS like '%ExecutionYear%';

drop table EXECUTION_YEAR;
