Created
February 24, 2015 19:52
-
-
Save mauriciofierrom/0601268997c462a87730 to your computer and use it in GitHub Desktop.
Upload files to Alfresco setting Name, Description, Title and Tags using CMIS, REST and Database Access.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rest_client' | |
require 'cmis' | |
require 'rika' | |
#require 'pg' | |
require 'java' | |
require 'postgresql.jar' | |
def description_tags(uuid, cur_number, provider, description, value, month, area, tags) | |
begin | |
#Java::com.postgresql.jdbc.Driver | |
Java::JavaClass.for_name "org.postgresql.Driver" | |
#userurl = "jdbc:postgresql://192.168.5.250:5432/alfresco" | |
userurl = "jdbc:postgresql://192.168.5.61:5434/alfresco" | |
connSelect = java.sql.DriverManager.get_connection(userurl, "postgres", "cwlcfresco") | |
stmtSelect = connSelect.create_statement | |
puts uuid | |
uuid = uuid.to_s[uuid.to_s.rindex('/')+1..uuid.to_s.rindex(';')-1] | |
selectquery = "SELECT id FROM alf_node WHERE uuid='#{uuid}'" | |
rsS = stmtSelect.execute_query(selectquery) | |
id = nil | |
while(rsS.next) do | |
id = rsS.getObject('id') | |
end | |
stmtInsert = connSelect.create_statement | |
# SAVE TITLE AND DESCRIPTION | |
#puts "INSERT INTO alf_node_properties VALUES (#{id},6,6,false,0,0,0,'" + ("#{provider} #{description} #{value} #{month} #{area}").tr(',','').tr(':', '').tr('.','')+"', null, 31, -1, 3)" | |
desc = "#{provider} #{description} #{value} #{month} #{area}" | |
puts "INSERT INTO alf_node_properties VALUES (#{id},6,6,false,0,0,0, #{desc}, null, 31, -1, 3)" | |
rsI = stmtInsert.execute_update("INSERT INTO alf_node_properties VALUES (#{id},6,6,false,0,0,0, '#{desc}', null, 31, -1, 3)") | |
rsI = stmtInsert.execute_update("INSERT INTO alf_node_properties VALUES (#{id},6,6,false,0,0,0,'CUR #{cur_number}', null, 30, -1, 3)") | |
rsI = stmtInsert.execute_update("INSERT INTO alf_node_properties VALUES (#{id},0,0,false,0,0,0,null,null,150,-1,1)") | |
#rsI = stmtInsert.execute_update("INSERT INTO alf_node_properties VALUES (#{id},6,6,false,0,0,0,'#{provider}',null,29,-1,1)") | |
#rsI = stmtInsert.execute_update("INSERT INTO alf_node_properties VALUES (#{id},6,6,false,0,0,0,'#{area}',null,29,-1,1)") | |
hash = JSON.parse RestClient.get 'http://192.168.5.61:8081/alfresco/service/api/login.json?u=admin&pw=cwlcfresco' | |
ticket = hash['data']['ticket'] | |
puts ticket | |
tags = nil | |
if not provider.empty? and not area.empty? then | |
tags = "['#{provider}', '#{area}']" | |
elsif not provider.empty? and area.empty? then | |
tags = "['#{provider}']" | |
elsif provider.empty? and not area.empty? then | |
tags = "['#{area}']" | |
end | |
puts tags | |
set_tags = RestClient.post("http://192.168.5.61:8081/alfresco/s/api/node/workspace/SpacesStore/#{uuid}/tags?alf_ticket="+ticket, tags, :content_type => :json, :accept => :json) | |
end | |
end | |
#atom_url = "http://192.168.5.250:8080/alfresco/cmisatom" | |
atom_url = "http://192.168.5.61:8081/alfresco/cmisatom" | |
user = "admin" | |
password = "cwlcfresco" | |
@session = CMIS::create_session(atom_url, user, password) | |
#root = @session.root_folder | |
root = @session.get_object('workspace://SpacesStore/0cfd75e3-eaa9-43bd-8a29-5cf2f32d775c') | |
start_dir = '/home/mauricio/Desktop/RESPALDO' | |
folders_created = [] | |
#directories = Dir.entries(start_dir).select { |entry| File.directory? File.join(start_dir,entry) } | |
File.open('/home/mauricio/Desktop/base_financiero.csv').each do |line| | |
# Get matrix information | |
folder_number = line.split(';')[1] | |
cur_number = line.split(';')[2] | |
provider = line.split(';')[3] | |
description = line.split(';')[4] | |
value = line.split(';')[5] | |
month = line.split(';')[7] | |
area = line.split(';')[8] | |
tags = [] | |
tags << provider | |
tags << line.split(';')[8] | |
puts "Folder number: #{folder_number}" | |
puts "Cur number: #{cur_number}" | |
if Dir.exists?("/home/mauricio/Desktop/RESPALDO/CP " + folder_number) then | |
# CREATE FOLDER BASED ON THE NUMBER | |
if folders_created[folder_number.to_i].nil? then | |
doc_folder = root.create_cmis_folder(folder_number) | |
folders_created[folder_number.to_i] = doc_folder.id | |
else | |
doc_folder = @session.get_object(folders_created[folder_number.to_i]) | |
end | |
sub_directories = Dir.entries(start_dir+"/CP "+folder_number).select { |entry| File.directory? File.join(start_dir+"/CP "+folder_number,entry) and !(entry =='.' || entry == '..')} | |
if sub_directories.count.eql? 0 then | |
puts "NO SUBDIRECTORIES" | |
files = Dir["#{start_dir}/CP #{folder_number}/*.pdf"] | |
else | |
puts "THERE ARE SUBDIRECTORIES" | |
files = Dir["#{start_dir}/CP #{folder_number}/#{cur_number}/*.pdf"] | |
end | |
parts = [] | |
if files.count.eql? 1 then | |
puts "FILE COUNT EQUALS 1" | |
#upload file | |
content_stream = CMIS::create_content_stream("#{files[0]}", @session) | |
props = {CMIS::PropertyIds::OBJECT_TYPE_ID => "cmis:document", CMIS::PropertyIds::NAME => "#{cur_number}.pdf", CMIS::PropertyIds::DESCRIPTION => "Una descripcion de ejemplo."} | |
id = doc_folder.create_document(props, content_stream, CMIS::VersioningState::MAJOR) | |
description_tags(id, cur_number, provider, description, value, month, area, tags) | |
else | |
puts "THERE ARE MORE THAN 1 FILE IN THE FOLDER" | |
files.each do |file| | |
if file.include? cur_number then | |
parts << file | |
end | |
end | |
puts parts | |
if parts.count>1 then | |
merge = "pdftk " | |
parts.each do |part| | |
merge = merge + "'#{part}' " | |
end | |
merge = merge + "cat output #{cur_number}.pdf" | |
#puts merge | |
puts "SAVING #{cur_number}.pdf" | |
system(merge) | |
# UPLOAD FILE | |
content_stream = CMIS::create_content_stream("#{cur_number}.pdf", @session) | |
puts CMIS::PropertyIds | |
props = {CMIS::PropertyIds::OBJECT_TYPE_ID => "cmis:document", CMIS::PropertyIds::NAME => "#{cur_number}.pdf", CMIS::PropertyIds::DESCRIPTION => "Una descripcion de ejemplo." } | |
id = doc_folder.create_document(props, content_stream, CMIS::VersioningState::MAJOR) | |
description_tags(id, cur_number, provider, description, value, month, area, tags) | |
elsif parts.count.eql? 1 then | |
content_stream = CMIS::create_content_stream("#{parts[0]}", @session) | |
props = {CMIS::PropertyIds::OBJECT_TYPE_ID => "cmis:document", CMIS::PropertyIds::NAME => "#{cur_number}.pdf", CMIS::PropertyIds::DESCRIPTION => "Una descripcion de ejemplo."} | |
id = doc_folder.create_document(props, content_stream, CMIS::VersioningState::MAJOR) | |
description_tags(id, cur_number, provider, description, value, month, area, tags) | |
else | |
# REPORT FAULTY DIRECTORY | |
end | |
parts = [] | |
end | |
else | |
puts "NO EXISTE EL DIRECTORIO EN LOS DATOS ACTUALES" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment