-
Notifications
You must be signed in to change notification settings - Fork 6
/
fedora_api.datastream.inc
180 lines (153 loc) · 5.44 KB
/
fedora_api.datastream.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
include_once('fedora_api.item.inc');
class FedoraDatastream extends PropertyObject {
private $_parent = NULL;
public $dsID = '';
//protected $_populated = FALSE; // If the getDatastream call has been made to populate this object's fields
private $_dsLabel;
private $_dsVersionID;
private $_dsCreateDate;
private $_dsState;
private $_dsMIME;
private $_dsFormatURI;
private $_dsSize;
private $_dsVersionable;
private $_dsInfoType;
private $_dsLocation;
private $_dsLocatoinType;
private $_dsChecksumType;
private $_dsChecksum;
/**
*
* @param type $parent FedoraItem that this item is a part of
* @param type $dsID The Datastreams DSID
* @param type $label Datastream's label, if creating a minimal datastream
* @param type $mimeType The Datastream's MIME Type, if creating a minimal datastream
*/
function __construct($parent, $dsID, $label = NULL, $mimeType = NULL) {
$this->_parent = $parent;
$this->_dsID = $dsID;
$this->_populated = FALSE;
if ($label == NULL && $mimeType == NULL) {
$this->_refresh();
}
else {
// Create a minimal datastream. Getters may invoke getDatastreamInfo to access other fields.
$this->_dsLabel = $label;
$this->_dsMIME = $mimeType;
}
}
protected function get_dsID() {
return $this->_dsID;
}
protected function get_content() {
$resp = $this->parent->client->api->getDatastreamDissemination($this->parent->pid, $this->_dsID);
if ($resp->code != 200) {
throw new FedoraRestException("Error getting datastream contents $dsID . $resp->error");
}
return $resp->data;
}
protected function set_content($content) {
$resp = $this->parent->client->api->modifyDatastream($this->parent->pid, $this->_dsID, NULL, $content);
if ($resp->code != 200) {
throw new FedoraRestException("Unable to set datastream contents. " . $resp->error);
}
}
protected function get_dsLabel() {
return $this->_dsLabel;
}
protected function set_dsLabel($label) {
$resp = $this->parent->client->api->modifyDatastream($this->parent->pid, $this->_dsID, NULL, NULL, array('dsLabel' => $label));
if ($resp->code != 200) {
throw new FedoraRestException("Unable to set label, $resp->error");
}
$this->_dsLabel = $label;
}
protected function get_dsVersionID() {
return $this->_dsVersionID;
}
protected function get_dsCreateDate() {
return $this->_dsCreateDate;
}
protected function get_dsState() {
return $this->_dsState;
}
protected function set_dsState($state) {
$state = strtoupper($state);
if (!in_array($state, array('A', 'I', 'D'))) {
throw new FedoraRestException("State must be one of 'A', 'I', or 'D'");
}
$resp = $this->parent->client->api->modifyDatastream($this->parent->pid, $this->_dsID, NULL, NULL, array('dsState' => $state));
if ($resp->code != 200) {
throw new FedoraRestException("Unable to set label, $resp->error");
}
$this->_dsState = $state;
}
protected function get_dsMIME() {
return $this->_dsMIME;
}
protected function get_dsFormatURI() {
return $this->_dsFormatURI;
}
protected function get_dsSize() {
return $this->_dsSize;
}
protected function get_dsVersionable() {
return $this->_dsVersionable;
}
protected function set_dsVersionable($versionable) {
$versionable = $versionable == TRUE ? 'true' : 'false';
$resp = $this->parent->client->api->modifyDatastream($this->parent->pid, $this->_dsID, NULL, NULL, array('versionable' => $versionable));
if ($resp->code != 200) {
throw new FedoraRestException("Unable to set dsVersionable, $resp->error");
}
$this->_dsVersionable = $versionable;
}
protected function get_dsInfoType() {
return $this->_dsInfoType;
}
protected function get_dsLocation() {
return $this->_dsLocation;
}
protected function get_dsLocationType() {
return $this->_dsLocationType;
}
protected function get_dsChecksumType() {
return $this->_dsChecksumType;
}
protected function get_dsChecksum() {
return $this->_dsChecksum;
}
protected function get_parent() {
return $this->_parent;
}
private function _decodeDatastreamXML($xml) {
$ds_info = new SimpleXMLElement($xml);
$this->_dsLabel = (string) $ds_info->dsLabel;
$this->_dsVersionID = (string) $ds_info->dsVersionID;
$this->_dsCreateDate = new DateTime((string) $ds_info->createdDate);
$this->_dsState = (string) $ds_info->dsState;
$this->_dsMIME = (string) $ds_info->dsMIME;
$this->_dsFormatURI = (string) $ds_info->dsFormatURI;
$this->_dsSize = (int) (string) $ds_info->dsSize;
$this->_dsVersionable = (boolean) (string) $ds_info->dsVersionable;
$this->_dsInfoType = (string) $ds_info->dsInfoType;
$this->_dsLocation = (string) $ds_info->dsLocation;
$this->_dsLocationType = (string) $ds_info->dsLocationType;
$this->_dsChecksumType = (string) $ds_info->dsChecksumType;
$this->_dsChecksum = (string) $ds_info->dsChecksum;
}
/**
* Call out to Fedora to populate this Datastream object's fields.
*/
protected function _refresh() {
$resp = $this->_parent->client->api->getDatastream($this->_parent->pid, $this->_dsID);
if ($resp->code == 200) {
$this->_decodeDatastreamXML($resp->data);
$this->_populated = TRUE;
}
else {
throw new FedoraRestException("Unable to create datastream for $this->_dsID. $resp->error.");
}
}
}