Class: XmlConv::Util::Transaction

Inherits:
Object
  • Object
show all
Includes:
ODBA::Persistable
Defined in:
lib/xmlconv/util/transaction.rb

Constant Summary collapse

ODBA_SERIALIZABLE =
['@postprocs', '@responses', '@arguments']
ODBA_PREFETCH =
true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransaction

Returns a new instance of Transaction.



22
23
24
# File 'lib/xmlconv/util/transaction.rb', line 22

def initialize
  @postprocs = []
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def arguments
  @arguments
end

#commit_timeObject (readonly)

Returns the value of attribute commit_time.



20
21
22
# File 'lib/xmlconv/util/transaction.rb', line 20

def commit_time
  @commit_time
end

#debug_recipientsObject

Returns the value of attribute debug_recipients.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def debug_recipients
  @debug_recipients
end

#destinationObject

Returns the value of attribute destination.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def destination
  @destination
end

#domainObject

Returns the value of attribute domain.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def domain
  @domain
end

#errorObject

Returns the value of attribute error.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def error
  @error
end

#error_recipientsObject

Returns the value of attribute error_recipients.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def error_recipients
  @error_recipients
end

#inputObject

Returns the value of attribute input.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def input
  @input
end

#input_modelObject (readonly)

Returns the value of attribute input_model.



20
21
22
# File 'lib/xmlconv/util/transaction.rb', line 20

def input_model
  @input_model
end

#modelObject (readonly)

Returns the value of attribute model.



20
21
22
# File 'lib/xmlconv/util/transaction.rb', line 20

def model
  @model
end

#originObject

Returns the value of attribute origin.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def origin
  @origin
end

#outputObject (readonly)

Returns the value of attribute output.



20
21
22
# File 'lib/xmlconv/util/transaction.rb', line 20

def output
  @output
end

#output_modelObject (readonly)

Returns the value of attribute output_model.



20
21
22
# File 'lib/xmlconv/util/transaction.rb', line 20

def output_model
  @output_model
end

#partnerObject

Returns the value of attribute partner.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def partner
  @partner
end

#postprocsObject

Returns the value of attribute postprocs.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def postprocs
  @postprocs
end

#readerObject

Returns the value of attribute reader.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def reader
  @reader
end

#responseObject

Returns the value of attribute response.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def response
  @response
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



20
21
22
# File 'lib/xmlconv/util/transaction.rb', line 20

def start_time
  @start_time
end

#transaction_idObject

Returns the value of attribute transaction_id.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def transaction_id
  @transaction_id
end

#writerObject

Returns the value of attribute writer.



16
17
18
# File 'lib/xmlconv/util/transaction.rb', line 16

def writer
  @writer
end

Instance Method Details

#encode(input_body) ⇒ Object

Assumes its encoding once as ISO-8859-1 (latin1) at here, Because input_body is passed from ruby 1.8.6



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/xmlconv/util/transaction.rb', line 49

def encode(input_body)
  src = input_body.to_s
  begin
    # ISO-8859-1 (latin1) and WINDOWS-1252
    src.encode('ISO-8859-1').force_encoding('UTF-8')
  rescue Encoding::InvalidByteSequenceError,
         Encoding::UndefinedConversionError
    begin
      src.force_encoding('ISO-8859-1').encode('UTF-8')
    rescue
      src
    end
  end
end

#executeObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/xmlconv/util/transaction.rb', line 25

def execute
	reader_instance = Conversion.const_get(@reader)
	writer_instance = Conversion.const_get(@writer)
	@start_time = Time.now
     if !@input.valid_encoding?
       @input = @input.encode("UTF-8", :invalid=>:replace, :replace=>"?")
     else
       @input = @input.encode("UTF-8")
     end
     @input.gsub!(/\t+|\s+/, ' ')
     input_model = reader_instance.parse(@input)
     @arguments ||= []
	@model = reader_instance.convert(input_model, *@arguments)
	output_model = writer_instance.convert(@model, *@arguments)
	@output = output_model.is_a?(Array) ? output_model.join("\n").to_s : output_model.to_s
	@destination.deliver(output_model)
	@commit_time = Time.now
	@output
ensure
	@destination.forget_credentials!
end

#invoice_idsObject



64
65
66
67
68
# File 'lib/xmlconv/util/transaction.rb', line 64

def invoice_ids
  @model.invoices.collect do |inv|
    inv.invoice_id.last.to_s.gsub /^0+/, ''
  end
end

#notifyObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/xmlconv/util/transaction.rb', line 69

def notify
	recipients = [@debug_recipients]
	subject = 'XmlConv2 - Debug-Notification'
	if(@error)
		recipients.push(@error_recipients)
		subject = 'XmlConv2 - Error-Notification'
	end
	recipients.flatten!
	recipients.compact!
	recipients.uniq!
	return if(recipients.empty?)
	body = <<-EOS
Date:   #{@start_time.strftime("%d.%m.%Y")}
Time:   #{@start_time.strftime("%H:%M:%S")}
Status: #{status}
Error:  #{@error}
Link:   http://#{@domain}/de/transaction/transaction_id/#{@transaction_id}

Input:
# input start
#{@input}
# input end

Output:
# output start
#{@output}
# output end
	EOS
     Util::Mail.notify recipients, subject, body
end

#odba_storeObject



99
100
101
102
103
# File 'lib/xmlconv/util/transaction.rb', line 99

def odba_store
  @input.extend(ODBA::Persistable) if(@input)
  @output.extend(ODBA::Persistable) if(@output)
  super
end

#postprocessObject



104
105
106
107
108
109
110
111
112
# File 'lib/xmlconv/util/transaction.rb', line 104

def postprocess
  if(@postprocs.respond_to?(:each))
    @postprocs.each { |klass, *args|
      next if args.empty?
      args.push(self)
      PostProcess.const_get(klass).send(*args)
    }
  end
end

#respond(delivery, response) ⇒ Object



113
114
115
# File 'lib/xmlconv/util/transaction.rb', line 113

def respond delivery, response
  responses[delivery] = response
end

#responsesObject



122
123
124
# File 'lib/xmlconv/util/transaction.rb', line 122

def responses
  @responses ||= []
end

#statusObject



125
126
127
128
129
130
131
132
133
# File 'lib/xmlconv/util/transaction.rb', line 125

def status
	if(@error)
		:error
     elsif(@model.nil? || @model.empty?)
       :empty
	elsif(@destination.respond_to?(:status))
		@destination.status
	end
end

#status=(status) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/xmlconv/util/transaction.rb', line 134

def status=(status)
  if @destination.respond_to?(:status=)
    @destination.status = status
    @destination.odba_store
    @destination.status
  end
end

#status_comparableObject



141
142
143
144
145
# File 'lib/xmlconv/util/transaction.rb', line 141

def status_comparable
	if(@destination.respond_to?(:status_comparable))
		@destination.status_comparable
	end
end

#update_statusObject



146
147
148
149
150
# File 'lib/xmlconv/util/transaction.rb', line 146

def update_status
	if(@destination.respond_to?(:update_status))
		@destination.update_status
	end
end

#uriObject



151
152
153
# File 'lib/xmlconv/util/transaction.rb', line 151

def uri
	@destination.uri if(@destination.respond_to?(:uri))
end

#uri_comparableObject



154
155
156
# File 'lib/xmlconv/util/transaction.rb', line 154

def uri_comparable
	self.uri.to_s
end