| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # $Id: HWObj.pm,v 1.3 2003/01/16 14:29:10 eserte Exp $ | ||
| 5 | # Author: Slaven Rezic | ||
| 6 | # | ||
| 7 | # Copyright (C) 2001 Online Office Berlin. All rights reserved. | ||
| 8 | # Copyright (C) 2002 Slaven Rezic. | ||
| 9 | # This is free software; you can redistribute it and/or modify it under the | ||
| 10 | # terms of the GNU General Public License, see the file COPYING. | ||
| 11 | |||
| 12 | # | ||
| 13 | # Mail: slaven@rezic.de | ||
| 14 | # WWW: http://we-framework.sourceforge.net | ||
| 15 | # | ||
| 16 | |||
| 17 | package WE::DB::HWObj; | ||
| 18 | |||
| 19 | 1 | ▶ | use base qw/WE::DB::Obj/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use WE::Util::LangString; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | |||
| 22 | __PACKAGE__->mk_accessors(qw/HWHost HWPort HWRoot HW/); | ||
| 23 | |||
| 24 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 25 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/); | ||
| 27 | |||
| 28 | 0 | ▶ | use HyperWave::CSP; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 29 | |||
| 30 | {package HyperWave::CSP; | ||
| 31 | use vars qw($DEBUG); $DEBUG=0; | ||
| 32 | |||
| 33 | # | ||
| 34 | # Reads up to the number of bytes from the socket | ||
| 35 | # returns 0 on failure, otherwise the buffer read | ||
| 36 | # | ||
| 37 | sub _hw_read { | ||
| 38 | my $socket = shift; | ||
| 39 | my $length_to_read = shift; | ||
| 40 | |||
| 41 | warn "_hw_read\n" if $DEBUG > 2; | ||
| 42 | |||
| 43 | my $buff1 = "0.02"; | ||
| 44 | my $tries_remaining = 20; | ||
| 45 | |||
| 46 | # loop until it's all read, or we timeout | ||
| 47 | if (!defined(sysread($socket, $buff1, $length_to_read))) { | ||
| 48 | warn "_hw_read: sysread: $!"; | ||
| 49 | } | ||
| 50 | $length_to_read -= length($buff1); | ||
| 51 | my $buffer = $buff1; | ||
| 52 | while ($length_to_read && $tries_remaining) { | ||
| 53 | select(undef,undef,undef,0.01); | ||
| 54 | #sleep(5); | ||
| 55 | $tries_remaining--; | ||
| 56 | $buff1 = "0.02"; | ||
| 57 | if (!defined(sysread($socket, $buff1, $length_to_read))) { | ||
| 58 | warn "_hw_read: sysread: $!"; | ||
| 59 | } | ||
| 60 | $length_to_read -= length($buff1); | ||
| 61 | $buffer .= $buff1; | ||
| 62 | warn "_hw_read: read = \"0.02\" of " . | ||
| 63 | $length_to_read . "\n" if $DEBUG > 2; | ||
| 64 | } | ||
| 65 | |||
| 66 | if (!$tries_remaining) { | ||
| 67 | warn "_hw_read: ran out of tries!\n"; | ||
| 68 | return 0; | ||
| 69 | } | ||
| 70 | |||
| 71 | warn "_hw_read: returning = '$buffer'\n" if $DEBUG > 2; | ||
| 72 | return $buffer; | ||
| 73 | |||
| 74 | } | ||
| 75 | } | ||
| 76 | |||
| 77 | sub new { | ||
| 78 | my($class, $root, %hwargs) = @_; | ||
| 79 | my $self = {}; | ||
| 80 | bless $self, $class; | ||
| 81 | $self->Root($root); | ||
| 82 | $self->HWHost($hwargs{-host}); | ||
| 83 | $self->HWPort($hwargs{-port}); | ||
| 84 | $hwargs{-rootcollection} = "rootcollection" | ||
| 85 | if (!defined $hwargs{-rootcollection}); | ||
| 86 | $self->HWRoot($hwargs{-rootcollection}); | ||
| 87 | $self; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub root_object { | ||
| 91 | my $self = shift; | ||
| 92 | my $rootid = $self->HW->get_objnum_by_name($self->HWRoot); | ||
| 93 | if (defined $rootid) { | ||
| 94 | my(%a) = $self->HW->get_attributes_hash($rootid); | ||
| 95 | $self->_change_attributes(\%a); | ||
| 96 | return WE::Obj::Site->new(%a); | ||
| 97 | } else { | ||
| 98 | undef; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | sub get_object { | ||
| 103 | my($self, $obj_id) = @_; | ||
| 104 | my(%a) = $self->HW->get_attributes_hash($obj_id); | ||
| 105 | $self->_change_attributes(\%a); | ||
| 106 | |||
| 107 | if ($a{'DocumentType'} eq 'collection') { | ||
| 108 | WE::Obj::FolderObj->new(%a); | ||
| 109 | } elsif (scalar keys %a) { | ||
| 110 | WE::Obj::DocObj->new(%a); | ||
| 111 | } else { | ||
| 112 | undef; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | sub exists { | ||
| 117 | my($self, $obj_id) = @_; | ||
| 118 | defined $self->HW->get_attributes($obj_id); | ||
| 119 | } | ||
| 120 | |||
| 121 | sub children_ids { | ||
| 122 | my($self, $obj_id) = @_; | ||
| 123 | $self->idify_params($obj_id); | ||
| 124 | my $o = $self->HW->get_children($obj_id); | ||
| 125 | $o ? split /\s+/, $o : (); | ||
| 126 | } | ||
| 127 | |||
| 128 | sub parent_ids { | ||
| 129 | my($self, $obj_id) = @_; | ||
| 130 | $self->idify_params($obj_id); | ||
| 131 | my $o = $self->HW->get_parents($obj_id); | ||
| 132 | $o ? split /\s+/, $o : (); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub version_ids { die "NYI" } | ||
| 136 | |||
| 137 | sub idify_params { | ||
| 138 | my $self = shift; | ||
| 139 | foreach (@_) { | ||
| 140 | if (UNIVERSAL::isa($_, "WE::Obj")) { | ||
| 141 | $_ = hex $_->{ObjectID}; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | sub _change_attributes { | ||
| 147 | my($self, $aref) = @_; | ||
| 148 | my @titles; | ||
| 149 | while(my($name, $value) = each %$aref) { | ||
| 150 | if ($name eq 'Author') { $name = 'Owner' } | ||
| 151 | elsif ($name =~ /^Time(Created|Modified)$/) { $value = hwdate2isodate($value) } | ||
| 152 | elsif ($name eq 'MimeType') { $name = 'ContentType' } | ||
| 153 | elsif ($name eq 'Title') { push @titles, $value; next } | ||
| 154 | elsif ($name eq 'ObjectID') { $aref->{Id} = hex $value } | ||
| 155 | $aref->{$name} = $value; | ||
| 156 | } | ||
| 157 | if (@titles) { | ||
| 158 | $aref->{Title} = hwtitle2langstr(@titles); | ||
| 159 | } | ||
| 160 | $aref; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub hwdate2isodate { | ||
| 164 | my $hwdate = shift; | ||
| 165 | $hwdate =~ s/^(\d+)\/(\d+)\/(\d+)/$1-$2-$3/; | ||
| 166 | $hwdate; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub hwtitle2langstr { | ||
| 170 | my(@titles) = @_; | ||
| 171 | my %t; | ||
| 172 | foreach (@titles) { | ||
| 173 | if (/^([^:]+):(.*)$/) { | ||
| 174 | $t{$1 eq 'ge' ? 'de' : $1} = $2; | ||
| 175 | } else { | ||
| 176 | warn "Can't parse title $_"; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | new WE::Util::LangString %t; | ||
| 180 | } | ||
| 181 | |||
| 182 | 1; | ||
| 183 | |||
| 184 | __END__ | ||