#!/usr/bin/env ruby

require_relative 'database'
require_relative 'vocabularies'
require_relative 'migrants'

output = File.open(File.join('data', 'works.ttl'), 'w')

output.puts prefixes(:rdfs, :tm, :person, :location, :organisation, :work)
output.puts

DB[:work].each do |row|
  id = row[:IDWork]
  props = ["a tm:WorkRecord"]

  props << "tm:person person:#{row[:IDPerson]}"                        unless row[:IDPerson].to_s.empty?
  props << "tm:location location:#{row[:IDLocation]}"                  unless row[:IDLocation].to_s.empty?
  props << "tm:organisation organisation:#{row[:IDOrganisation]}"      unless row[:IDOrganisation].to_s.empty?
  props << "tm:organisation organisation:#{row[:IDOrganisation2]}"     unless row[:IDOrganisation2].to_s.empty?
  props << "tm:dateStartFuzzy #{ttl_literal(row[:DateStart_Fuzzy])}"   unless row[:DateStart_Fuzzy].to_s.empty?
  props << "tm:dateStartMin #{ttl_literal(row[:DateStart_Min])}"       if row[:DateStart_Min]
  props << "tm:dateStartMax #{ttl_literal(row[:DateStart_Max])}"       if row[:DateStart_Max]
  props << "tm:dateEndFuzzy #{ttl_literal(row[:DateEnd_Fuzzy])}"       unless row[:DateEnd_Fuzzy].to_s.empty?
  props << "tm:dateEndMin #{ttl_literal(row[:DateEnd_Min])}"           if row[:DateEnd_Min]
  props << "tm:dateEndMax #{ttl_literal(row[:DateEnd_Max])}"           if row[:DateEnd_Max]
  props << "tm:employment #{ttl_literal(row[:Employment])}"            unless row[:Employment].to_s.empty?
  props << "tm:profession #{ttl_literal(row[:Profession])}"            unless row[:Profession].to_s.empty?
  props << "tm:profession2 #{ttl_literal(row[:Profession2])}"          unless row[:Profession2].to_s.empty?
  props << "tm:profession3 #{ttl_literal(row[:Profession3])}"          unless row[:Profession3].to_s.empty?
  props << "tm:employmentType #{ttl_literal(row[:EmploymentType])}"    unless row[:EmploymentType].to_s.empty?
  props << "rdfs:comment #{ttl_literal(row[:comment])}"                unless row[:comment].to_s.empty?

  output.puts "work:#{id} #{props.first} ;"
  props[1..-2].each { |p| output.puts "  #{p} ;" }
  output.puts "  #{props.last} ."
  output.puts
end

output.close
