1 |
joko |
1.1 |
############################################## |
2 |
|
|
# |
3 |
|
|
# $Id: Common.pm,v 1.1 2002/10/10 03:26:00 cvsjoko Exp $ |
4 |
|
|
# |
5 |
|
|
# $Log: Common.pm,v $ |
6 |
|
|
# Revision 1.1 2002/10/10 03:26:00 cvsjoko |
7 |
|
|
# + new |
8 |
|
|
# |
9 |
|
|
############################################## |
10 |
|
|
|
11 |
|
|
|
12 |
|
|
package Data::Transform::OO; |
13 |
|
|
|
14 |
|
|
use strict; |
15 |
|
|
use warnings; |
16 |
|
|
|
17 |
|
|
require Exporter; |
18 |
|
|
our @ISA = qw( Exporter ); |
19 |
|
|
our @EXPORT_OK = qw( &object2hash &hash2object ); |
20 |
|
|
|
21 |
|
|
use Data::Dumper; |
22 |
|
|
use Data::Transform::Deep qw( getStructSlotByStringyAddress ); |
23 |
|
|
|
24 |
|
|
sub object2hash { |
25 |
|
|
my $object = shift; |
26 |
|
|
my $options = shift; |
27 |
|
|
my $deref = var_deref($object); |
28 |
|
|
var2utf8($deref) if ($options->{utf8}); |
29 |
|
|
return $deref; |
30 |
|
|
} |
31 |
|
|
|
32 |
|
|
# todo: maybe do diff(s) here sometimes!? |
33 |
|
|
sub hash2object { |
34 |
|
|
my $object = shift; |
35 |
|
|
my $data = shift; |
36 |
|
|
my $options = shift; |
37 |
|
|
|
38 |
|
|
# "patch" needed 'cause php passes numerical-indexed-arrays as hash-tables, |
39 |
|
|
# this method corrects this |
40 |
|
|
var_NumericalHashToArray($data) if ($options->{php}); |
41 |
|
|
|
42 |
|
|
# utf8-conversion/-encoding (essential for I18N) |
43 |
|
|
var_utf2iso($data) if ($options->{utf8}); |
44 |
|
|
|
45 |
|
|
# get fresh object from database |
46 |
|
|
# todo: |
47 |
|
|
# - handle "locked" objects !!! |
48 |
|
|
# - check if already another update occoured (object-revisioning needed!) |
49 |
|
|
#my $obj = $self->getObject($oid); |
50 |
|
|
|
51 |
|
|
# mix changes into fresh object and save it back |
52 |
|
|
hash2object_traverse_mixin($object, $data); |
53 |
|
|
|
54 |
|
|
# done in core mixin function? |
55 |
|
|
#$self->{storage}->update($obj); |
56 |
|
|
|
57 |
|
|
# we should "undef" some objects here to destroy them in memory and enforce reloading when used next time |
58 |
|
|
# simulate: |
59 |
|
|
#$self->{storage}->disconnect(); |
60 |
|
|
|
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
# ------------------------------------ |
64 |
|
|
# core mixin function |
65 |
|
|
# TODO-FEATURE: make this possible in a reverse way: object is empty (no fields) and gets filled up by mixin-data |
66 |
|
|
|
67 |
|
|
# remember keys of structures we are traversing |
68 |
|
|
# this is a HACK: |
69 |
|
|
# - don't (!!!) "packageglobal" @indexstack |
70 |
|
|
# - it will lead to problems with parallelism! |
71 |
|
|
my @indexstack; |
72 |
|
|
|
73 |
|
|
# traverse a deeply nested structure, mix in values from given hash, update underlying tangram-object |
74 |
|
|
sub hash2object_traverse_mixin { |
75 |
|
|
my $object = shift; |
76 |
|
|
my $data = shift; |
77 |
|
|
my $bool_recursion = shift; |
78 |
|
|
|
79 |
|
|
# clear our key - stack if we are called from user-code (non-recursively) |
80 |
|
|
@indexstack = () if (!$bool_recursion); |
81 |
|
|
|
82 |
|
|
my $classname = ref $object; |
83 |
|
|
# if ($classname) { |
84 |
|
|
# print STDERR "*****************", Dumper(Class::Tangram::attribute_types($classname)), "\n"; |
85 |
|
|
# } |
86 |
|
|
|
87 |
|
|
# parser: detected OBJECT (a Tangram one?) (reftype == HASH) (assume) |
88 |
|
|
# what's exactly done here? hmmm.... it works ;) |
89 |
|
|
# maybe a HACK: please try not to use "IntrHash"es, maybe this cannot handle them!!! |
90 |
|
|
# extend! check! |
91 |
|
|
if ((attributes::reftype($object) eq 'HASH') && (ref($object) ne 'HASH') && (ref($object) ne 'ARRAY')) { |
92 |
|
|
|
93 |
|
|
# print STDERR "===", "reftype: ", attributes::reftype($obj), "\n"; |
94 |
|
|
|
95 |
|
|
# loop through fields of object (Tangram-object) |
96 |
|
|
foreach (keys %{$object}) { |
97 |
|
|
push @indexstack, $_; |
98 |
|
|
|
99 |
|
|
# determine type of object |
100 |
|
|
my $ref = ref $object->{$_}; |
101 |
|
|
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
102 |
|
|
if ($ref) { |
103 |
|
|
hash2object_traverse_mixin($object->{$_}, $data, 1); |
104 |
|
|
} else { |
105 |
|
|
my $val = getStructSlotByStringyAddress($data, \@indexstack); |
106 |
|
|
$object->{$_} = $val; |
107 |
|
|
} |
108 |
|
|
pop @indexstack; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
# save object to database ... |
112 |
|
|
# ... do an update if it already exists, do an insert if it doesn't |
113 |
|
|
# my $objectId = $self->{storage}->id($obj); |
114 |
|
|
# $logger->debug( __PACKAGE__ . "->saveObjectFromHash_traverse_mixin_update( object $obj objectId $objectId )" ); |
115 |
|
|
# if ($objectId) { |
116 |
|
|
# $self->{storage}->update($obj); |
117 |
|
|
|
118 |
|
|
#print __PACKAGE__ . ":", "\n"; |
119 |
|
|
#print Dumper($object); |
120 |
|
|
|
121 |
|
|
# } else { |
122 |
|
|
# $self->{storage}->insert($obj); |
123 |
|
|
# } |
124 |
|
|
|
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
#&& ( ref($obj) ne 'HASH' ) |
128 |
|
|
|
129 |
|
|
# loop through entries of array (IntrArray, isn't it?) |
130 |
|
|
if ((ref $object) eq 'ARRAY') { |
131 |
|
|
# print STDERR "===", "refttype ", attributes::reftype($obj), "\n"; |
132 |
|
|
my $i = 0; |
133 |
|
|
foreach (@{$object}) { |
134 |
|
|
push @indexstack, $i; |
135 |
|
|
my $ref = ref $_; |
136 |
|
|
# print STDERR "attrname: $_ ATTRref: $ref", "\n"; |
137 |
|
|
if ($ref && $_) { |
138 |
|
|
hash2object_traverse_mixin($_, $data, 1); |
139 |
|
|
} else { |
140 |
|
|
$object->[$i] = $_; |
141 |
|
|
} |
142 |
|
|
pop @indexstack; |
143 |
|
|
$i++; |
144 |
|
|
} |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
1; |