53 $request->
setValue(
'oid', $oid->__toString());
58 $request->
setValue(
'sourceOid', $sourceOid->__toString());
65 $request->
setValue(
'targetOid', $targetOid->__toString());
67 $request->
setValue(
'oid', $targetOid->__toString());
69 parent::initialize($request, $response);
78 if ($request->hasValue(
'className') &&
82 array(
'invalidParameters' => array(
'className'))));
85 if ($request->hasHeader(
'Position')) {
86 $position = $request->getHeader(
'Position');
87 if (!preg_match(
'/^before /', $position) && !preg_match(
'/^last$/', $position)) {
89 array(
'invalidParameters' => array(
'Position'))));
94 return parent::validate();
103 switch ($request->getMethod()) {
120 $response->setAction(
'ok');
145 if ($request->getBooleanValue(
'collection') ===
false) {
153 $object = $subResponse->getValue(
'object');
154 $response->clearValues();
155 if ($object != null) {
156 $response->setValue($object->getOID()->__toString(), $object);
173 if ($request->hasHeader(
'Range')) {
174 if (preg_match(
'/^items=([\-]?[0-9]+)-([\-]?[0-9]+)$/', $request->getHeader(
'Range'), $matches)) {
175 $offset = intval($matches[1]);
176 $limit = intval($matches[2])-$offset+1;
177 $request->setValue(
'offset', $offset);
178 $request->setValue(
'limit', $limit);
183 foreach ($request->getValues() as $key => $value) {
185 if (preg_match(
'/^sort\(([^\)]+)\)$|sortBy=([.]+)$/', $key, $matches)) {
186 $sortDefs = preg_split(
'/,/', $matches[1]);
188 $sortDef = $sortDefs[0];
189 $sortFieldName = substr($sortDef, 1);
190 $sortDirection = preg_match(
'/^-/', $sortDef) ?
'desc' :
'asc';
191 $request->setValue(
'sortFieldName', $sortFieldName);
192 $request->setValue(
'sortDirection', $sortDirection);
196 if (preg_match(
'/^limit\(([^\)]+)\)$/', $key, $matches)) {
197 $rangeDefs = preg_split(
'/,/', $matches[1]);
198 $limit = intval(array_pop($rangeDefs));
199 $offset =
sizeof($rangeDefs) > 0 ? intval($rangeDefs[0]) : 0;
200 $request->setValue(
'offset', $offset);
201 $request->setValue(
'limit', $limit);
207 if ($request->hasValue(
'className')) {
208 $operatorMap = array(
'eq' =>
'=',
'ne' =>
'!=',
'lt' =>
'<',
'lte' =>
'<=',
209 'gt' =>
'>',
'gte' =>
'>=',
'in' =>
'in',
'match' =>
'regexp');
210 $mapper = $persistenceFacade->getMapper($request->getValue(
'className'));
211 $type = $mapper->getType();
212 $simpleType = $persistenceFacade->getSimpleType($type);
214 foreach ($request->getValues() as $name => $value) {
215 if (strpos($name,
'.') > 0) {
217 list($typeInName, $attributeInName) = preg_split(
'/\.+(?=[^\.]+$)/', $name);
218 if (($typeInName == $type || $typeInName == $simpleType) &&
219 $mapper->hasAttribute($attributeInName)) {
220 $queryTemplate = $objectQuery->getObjectTemplate($type);
222 $value = strtolower($value) ==
'null' ? null : $value;
224 $parts = explode(
'=', $value);
226 if (
sizeof($parts) > 0 && isset($operatorMap[$op])) {
227 $operator = $operatorMap[$op];
233 $queryTemplate->setValue($attributeInName,
Criteria::asValue($operator, $value));
237 $query = $objectQuery->getQueryCondition();
238 if (strlen($query) > 0) {
239 $request->setValue(
'query', $query);
244 if ($request->hasValue(
"relation") && $request->hasValue(
'sourceOid')) {
245 $relationName = $request->getValue(
"relation");
249 $sourceNode = $persistenceFacade->load($sourceOid);
252 $request->setValue(
'query', $query);
255 $mapper = $sourceNode->getMapper();
256 $relation = $mapper->getRelation($relationName);
257 $otherType = $relation->getOtherType();
258 $request->setValue(
'className', $otherType);
261 $otherMapper = $persistenceFacade->getMapper($otherType);
262 $sortkeyDef = $otherMapper->getSortkey($relation->getThisRole());
263 if ($sortkeyDef != null) {
264 $request->setValue(
'sortFieldName', $sortkeyDef[
'sortFieldName']);
265 $request->setValue(
'sortDirection', $sortkeyDef[
'sortDirection']);
274 $objects = $subResponse->getValue(
'list');
275 $response->clearValues();
276 $response->setValues($objects);
279 $size =
sizeof($objects);
280 $limit = $size == 0 ? $offset : $offset+$size-1;
281 $total = $subResponse->getValue(
'totalCount');
282 $response->setHeader(
'Content-Range',
'items '.$offset.
'-'.$limit.
'/'.$total);
290 if (
sizeof($this->
getResponse()->getErrors()) > 0) {
291 parent::assignResponseDefaults();
312 if ($request->hasValue(
'relation') && $request->hasValue(
'sourceOid')) {
315 $relatedType = $this->
getRelatedType($sourceOid, $request->getValue(
'relation'));
316 $request->setValue(
'className', $relatedType);
319 $targetOid = $subResponseCreate->getValue(
'oid');
320 $targetOidStr = $targetOid->__toString();
323 $request->setValue(
'targetOid', $targetOidStr);
324 $request->setValue(
'role', $request->getValue(
'relation'));
329 $targetObj = $persistenceFacade->load($targetOid);
330 $subResponse->setValue(
'oid', $targetOid);
331 $subResponse->setValue($targetOidStr, $targetObj);
339 $oidStr = $subResponse->hasValue(
'oid') ? $subResponse->getValue(
'oid')->__toString() :
'';
340 if (!$subResponse->hasErrors()) {
341 $response->clearValues();
342 if ($subResponse->hasValue($oidStr)) {
343 $object = $subResponse->getValue($oidStr);
344 $response->setValue($oidStr, $object);
351 $response->setValues($subResponse->getValues());
375 $orderReferenceOid = null;
376 if ($request->hasHeader(
'Position')) {
377 $position = $request->getHeader(
'Position');
378 if ($position ==
'last') {
379 $orderReferenceOid =
'ORDER_BOTTOM';
382 list($ignore, $orderReferenceIdStr) = preg_split(
'/ /', $position);
383 if ($request->hasValue(
'relation') && $request->hasValue(
'sourceOid')) {
386 $relatedType = $this->
getRelatedType($sourceOid, $request->getValue(
'relation'));
387 $orderReferenceOid =
new ObjectId($relatedType, $orderReferenceIdStr);
391 $orderReferenceOid =
new ObjectId($request->getValue(
'className'), $orderReferenceIdStr);
396 if ($request->hasValue(
'relation') && $request->hasValue(
'sourceOid') &&
397 $request->hasValue(
'targetOid')) {
398 if ($orderReferenceOid != null) {
400 $request->setValue(
'containerOid', $request->getValue(
'sourceOid'));
401 $request->setValue(
'insertOid', $request->getValue(
'targetOid'));
402 $request->setValue(
'referenceOid', $orderReferenceOid);
403 $request->setValue(
'role', $request->getValue(
'relation'));
408 $request->setValue(
'role', $request->getValue(
'relation'));
418 $targetOidStr = $request->getValue(
'targetOid');
421 $targetObj = $persistenceFacade->load($targetOid);
422 $subResponse->setValue(
'oid', $targetOid);
423 $subResponse->setValue($targetOidStr, $targetObj);
427 if ($orderReferenceOid != null) {
430 $request->setValue(
'referenceOid', $orderReferenceOid);
438 $targetObj = $persistenceFacade->load($targetOid);
439 $subResponse->setValue(
'oid', $targetOid);
440 $subResponse->setValue($targetOidStr, $targetObj);
452 if (!$subResponse->hasErrors()) {
453 $response->clearValues();
454 if ($subResponse->hasValue($oidStr)) {
455 $object = $subResponse->getValue($oidStr);
456 $response->setValue($oidStr, $object);
463 $response->setValues($subResponse->getValues());
482 if ($request->hasValue(
'relation') && $request->hasValue(
'sourceOid') &&
483 $request->hasValue(
'targetOid')) {
485 $request->setValue(
'role', $request->getValue(
'relation'));
493 $response->setValues($subResponse->getValues());
495 if (!$subResponse->hasErrors()) {
513 $response->setHeader(
'Location', $oid->__toString());
523 foreach ($request->getValues() as $key => $value) {
540 $sourceMapper = $persistenceFacade->getMapper($sourceOid->
getType());
541 $relation = $sourceMapper->getRelation($role);
542 return $relation->getOtherType();
Response holds the response values that are used as output from Controller instances.
getRequest()
Get the Request instance.
RESTController handles REST requests.
setLocationHeaderFromOid($oid)
Set the location response header according to the given object id.
static getRelationQueryCondition($node, $otherRole)
Get the query condition used to select all related Nodes of a given role.
initialize(Request $request, Response $response)
Controller is the base class of all controllers.
getFirstRequestOid()
Get the first oid from the request.
getRelatedType(ObjectId $sourceOid, $role)
Get the type that is used in the given role related to the given source object.
handleDelete()
Handle a DELETE request (delete an object of a given type)
ObjectQuery implements a template based object query.
static asValue($operator, $value)
Factory method for constructing a Critera that may be used as value on a PersistentObject's attribute...
ObjectId is the unique identifier of an object.
handlePut()
Handle a PUT request (update an object of a given type)
Request holds the request values that are used as input to Controller instances.
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
hasValue($name)
Check for existance of a value.
executeSubAction($action)
Delegate the current request to another action.
setValue($name, $value)
Set a value.
static isValid($oid)
Check if a serialized ObjectId has a valid syntax, the type is known and if the number of primary key...
handlePost()
Handle a POST request (create an object of a given type)
static get($code, $data=null)
Factory method for retrieving a predefind error instance.
handleGet()
Handle a GET request (read object(s) of a given type)
getType()
Get the type (including namespace)
getResponse()
Get the Response instance.
getValue($name, $default=null, $filter=null, $options=null)
Get a value.
getPersistenceFacade()
Get the PersistenceFacade instance.