2012-02-24 11:07:47 +00:00
# encoding: utf-8
2012-03-06 08:41:06 +00:00
# require 'ruby-debug'
2012-05-15 06:03:54 +00:00
require " #{ Rails . root } /config/initializers/middle_site_connection "
2012-02-24 11:07:47 +00:00
namespace :mid_site do
2012-05-08 11:20:53 +00:00
desc " mid_site Rake task "
2012-03-06 08:41:06 +00:00
attr_from_mid = %w{ nccu_id psn_nam ut_cod up_ut_cod eml_adr off_tel_ext sta_num }
2012-02-24 11:07:47 +00:00
officer_posgrp_code = %w{ 02 06 10 05 } #from RSS2
admin_role = nil
sub_role = nil
2012-04-23 02:13:44 +00:00
test_account_ldap_id = '139716'
2012-05-15 03:28:24 +00:00
MiddleSiteConnection . establish
2012-02-24 11:07:47 +00:00
task :sync = > :environment do
2012-03-06 08:41:06 +00:00
info_profile = Info . first ( conditions : { :key = > 'profile' } )
def find_or_create_sub_role ( role , ut_query = [ ] , key = '' )
sub_role = role . sub_roles . get_sub_role_from_key ( key )
if sub_role . nil?
ut_data = ut_query . find { | ut_data | ut_data [ " ut_cod " ] == key }
# debugger
i18n_for_new_sub_role = I18nVariable . create! ( :document_class = > 'SubRole' , :key = > key , :en = > ( ut_data [ " ut_eng_m " ] rescue '' ) , :zh_tw = > ( ut_data [ " ut_chi_m " ] rescue '' ) , :parent_id = > role . i18n_variable . id )
sub_role = role . sub_roles . create! ( :key = > key )
sub_role . i18n_variable = i18n_for_new_sub_role
sub_role . save!
p " Created SubRole(key: #{ key } ): EN: #{ sub_role . i18n_variable . en } , CH: #{ sub_role . i18n_variable . zh_tw } ParentRole( #{ role . i18n_variable . key } ): #{ role . i18n_variable . en } "
end
sub_role
end
2012-05-15 03:28:24 +00:00
users_from_mid = $mid_site_connection . query ( " SELECT #{ attr_from_mid . join ( ',' ) } FROM rss_pautlst_ut WHERE posgrp_cod IN ( #{ officer_posgrp_code . join ( ',' ) } ) " )
ut_data_from_mid = $mid_site_connection . query ( " SELECT ut_odr, ut_cod, up_ut_cod, ut_chi_m, ut_eng_m FROM rss_paunit WHERE ut_tpe = '1' AND ut_grp != '3' AND up_ut_cod != 'F00' ORDER BY ut_odr, ut_cod " )
2012-02-24 11:07:47 +00:00
remote_list = users_from_mid . collect { | t | t [ " nccu_id " ] }
#remove delete user sho has been deleted at remote first
local_need_remove = User . all . collect { | t | t . nccu_id rescue nil } . uniq . delete_if { | x | x == nil } - remote_list
2012-03-06 08:41:06 +00:00
desc " Going to delete User with IDs: #{ local_need_remove . inspect } "
2012-05-24 09:01:00 +00:00
local_need_remove = User . excludes ( nccu_ldap_uid : test_account_ldap_id ) . collect { | t | t . nccu_ldap_uid rescue nil } . uniq . delete_if { | x | x == nil } - remote_list
2012-07-03 02:51:26 +00:00
AppManager . all . collect { | am | am if am . user . nil? } . delete_if { | x | x == nil } . each { | am | am . destroy }
2012-03-06 08:41:06 +00:00
desc " Deletion completed! \n "
2012-02-24 11:07:47 +00:00
#starting update user
2012-03-06 08:41:06 +00:00
desc " Updating and Newing Users "
2012-05-24 09:01:00 +00:00
2012-03-06 08:41:06 +00:00
users_from_mid . each do | mid_user |
ut_item = ut_data_from_mid . find { | ut_data | ut_data [ " ut_cod " ] == mid_user [ " ut_cod " ] }
up_ut_item = ut_data_from_mid . find { | ut_data | ut_data [ " ut_cod " ] == mid_user [ " up_ut_cod " ] }
local_user = User . find_or_initialize_by ( :nccu_ldap_uid = > mid_user [ " nccu_id " ] )
2012-05-24 09:01:00 +00:00
users_name_from_mid = $mid_site_connection . query ( " SELECT eng_nam_l,eng_nam_f,chi_nam_l,chi_nam_f FROM rss_zzperson_view WHERE nccu_id = #{ mid_user [ " nccu_id " ] } " ) . first
first_name = AttributeValue . find_or_create_by ( :user_id = > local_user . id , :attribute_field_id = > info_profile . attribute_fields [ 0 ] . id , :key = > 'first_name' )
last_name = AttributeValue . find_or_create_by ( :user_id = > local_user . id , :attribute_field_id = > info_profile . attribute_fields [ 1 ] . id , :key = > 'last_name' )
2012-03-06 08:41:06 +00:00
2012-05-24 09:01:00 +00:00
first_name [ 'en' ] = users_name_from_mid [ " eng_nam_f " ]
first_name [ 'zh_tw' ] = users_name_from_mid [ " chi_nam_f " ]
first_name . save
last_name [ 'en' ] = users_name_from_mid [ " eng_nam_l " ]
last_name [ 'zh_tw' ] = users_name_from_mid [ " chi_nam_l " ]
last_name . save
2012-03-06 08:41:06 +00:00
user_model_mapper = Hash [ :ut_cod = > mid_user [ " ut_cod " ] , :up_ut_cod = > mid_user [ " up_ut_cod " ] , :email = > mid_user [ " eml_adr " ] , :off_tel_ext = > mid_user [ " off_tel_ext " ] , :sta_num = > mid_user [ " sta_num " ] ]
user_model_mapper [ :admin ] = false
local_user . update_attributes ( user_model_mapper )
i18n_department_admin = I18nVariable . find_or_create_by ( :document_class = > 'Role' , :key = > 'department_admin' , :en = > 'Department Admin' , :zh_tw = > '系所使用者' )
local_user . role = Role . find_or_create_by ( :key = > " department_admin " , :build_in = > true )
local_user . role . i18n_variable = i18n_department_admin
unless local_user . ut_cod == local_user . up_ut_cod #if the ut_cod has additions info for up ut
local_user . sub_roles << find_or_create_sub_role ( local_user . role , ut_data_from_mid , local_user . up_ut_cod )
end
local_user . sub_roles << find_or_create_sub_role ( local_user . role , ut_data_from_mid , local_user . ut_cod )
2012-02-24 11:07:47 +00:00
local_user . save!
end
2012-07-03 02:51:26 +00:00
2012-02-24 11:07:47 +00:00
end
2012-05-24 09:01:00 +00:00
2012-02-24 11:07:47 +00:00
task :install_admin = > :before_instll_admin do
admins_nccu_id = '2772'
2012-03-06 08:41:06 +00:00
info_profile = Info . first ( conditions : { :key = > 'profile' } )
2012-05-24 09:01:00 +00:00
2012-05-15 03:28:24 +00:00
user_from_mid = $mid_site_connection . query ( " SELECT #{ attr_from_mid . join ( ',' ) } FROM rss_pautlst_ut WHERE posgrp_cod IN ( #{ officer_posgrp_code . join ( ',' ) } ) AND nccu_id = ' #{ admins_nccu_id } ' limit 1 " )
2012-02-24 11:07:47 +00:00
admin_at_mid = user_from_mid . first
2012-05-24 09:01:00 +00:00
local_user = User . find_or_initialize_by ( :nccu_ldap_uid = > admin_at_mid [ " nccu_id " ] )
2012-02-24 11:07:47 +00:00
local_user . update_attributes ( :email = > admin_at_mid [ " eml_adr " ] , :admin = > true , :role_id = > admin_role . id , :sub_role_ids = > [ sub_role . id ] )
2012-05-24 09:01:00 +00:00
users_name_from_mid = $mid_site_connection . query ( " SELECT eng_nam_l,eng_nam_f,chi_nam_l,chi_nam_f FROM rss_zzperson_view WHERE nccu_id = #{ admin_at_mid [ " nccu_id " ] } " ) . first
2012-02-24 11:07:47 +00:00
2012-05-24 09:01:00 +00:00
first_name = AttributeValue . find_or_create_by ( :user_id = > local_user . id , :attribute_field_id = > info_profile . attribute_fields [ 0 ] . id , :key = > 'first_name' )
last_name = AttributeValue . find_or_create_by ( :user_id = > local_user . id , :attribute_field_id = > info_profile . attribute_fields [ 1 ] . id , :key = > 'last_name' )
first_name [ 'en' ] = users_name_from_mid [ " eng_nam_f " ]
first_name [ 'zh_tw' ] = users_name_from_mid [ " chi_nam_f " ]
first_name . save
last_name [ 'en' ] = users_name_from_mid [ " eng_nam_l " ]
last_name [ 'zh_tw' ] = users_name_from_mid [ " chi_nam_l " ]
last_name . save
2012-02-24 11:07:47 +00:00
end
task :install_test = > :before_instll_admin do
admins_nccu_id = '139716'
# admin_role = Role.find_or_create_by( key: 'administrator')
# sub_role = admin_role.sub_roles.find_or_create_by(:key => 'computer_center')
info_profile = Info . first ( conditions : { :key = > 'profile' } )
2012-05-15 03:28:24 +00:00
user_from_mid = $mid_site_connection . query ( " SELECT #{ attr_from_mid . join ( ',' ) } FROM rss_pautlst_ut WHERE nccu_id = ' #{ admins_nccu_id } ' limit 1 " )
2012-02-24 11:07:47 +00:00
admin_at_mid = user_from_mid . first
user_first_name = admin_at_mid [ " psn_nam " ] . size > 3 ? admin_at_mid [ " psn_nam " ] [ 3 .. - 1 ] : admin_at_mid [ " psn_nam " ] [ 1 .. - 1 ]
user_last_name = admin_at_mid [ " psn_nam " ] . size > 3 ? admin_at_mid [ " psn_nam " ] [ 1 .. 2 ] : admin_at_mid [ " psn_nam " ] [ 0 ]
2012-05-24 09:01:00 +00:00
local_user = User . find_or_initialize_by ( :nccu_ldap_uid = > admin_at_mid [ " nccu_id " ] )
2012-02-24 11:07:47 +00:00
local_user . update_attributes ( :email = > admin_at_mid [ " eml_adr " ] , :admin = > true , :role_id = > admin_role . id , :sub_role_ids = > [ sub_role . id ] )
2012-03-06 08:41:06 +00:00
AttributeValue . create ( :user_id = > local_user . id , :attribute_field_id = > info_profile . attribute_fields [ 0 ] . id , :key = > 'first_name' , :en = > user_first_name , :zh_tw = > user_first_name )
2012-02-24 11:07:47 +00:00
AttributeValue . create ( :user_id = > local_user . id , :attribute_field_id = > info_profile . attribute_fields [ 1 ] . id , :key = > 'last_name' , :en = > user_last_name , :zh_tw = > user_last_name )
end
task :before_instll_admin = > :environment do
var_1 = I18nVariable . find_or_create_by ( :document_class = > 'Role' , :key = > 'administrator' , :en = > 'Administrator' , :zh_tw = > '管理員' )
var_1_1 = I18nVariable . find_or_create_by ( :document_class = > 'SubRole' , :key = > 'computer_center' , :en = > 'Computer Center' , :zh_tw = > '計算機中心' , :parent_id = > var_1 . id )
admin_role = Role . find_or_create_by ( :key = > 'administrator' , :built_in = > true )
admin_role . i18n_variable = var_1
admin_role . save!
sub_role = admin_role . sub_roles . find_or_create_by ( :key = > 'computer_center' , :built_in = > true )
sub_role . i18n_variable = var_1_1
sub_role . save!
#var_1_1_1 = I18nVariable.create!( :document_class => 'Attribute', :key => 'field', :en => 'Field', :zh_tw => '領域', :parent_id => var_1_1.id )
#var_1_1_2 = I18nVariable.create!( :document_class => 'Attribute', :key => 'department', :en => 'Department', :zh_tw => '學系', :parent_id => var_1_1.id )
end
task :clean_local_account = > :environment do
User . remote_account . each { | user | user . destroy }
end
task :claen_cc = > [ :clean_i18n_vars , :clean_admin_role_and_sub_role ] do
end
task :clean_i18n_vars = > :environment do
i18ns = I18nVariable . any_in ( key : [ 'administrator' , 'computer_center' ] )
i18ns . each { | var | var . destroy }
end
2012-03-06 08:41:06 +00:00
task :clean_ldap_users = > :environment do
2012-05-24 09:01:00 +00:00
User . where ( :nccu_ldap_uid . ne = > nil ) . each { | t | t . destroy }
2012-03-06 08:41:06 +00:00
end
2012-02-24 11:07:47 +00:00
task :clean_admin_role_and_sub_role = > :environment do
data = Role . any_in ( key : [ 'administrator' , 'computer_center' ] )
data . each { | var | var . destroy }
data = SubRole . any_in ( key : [ 'administrator' , 'computer_center' ] )
data . each { | var | var . destroy }
end
end