AssociateController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
13 use \Exception;
14 
20 
21 /**
22  * AssociateController is used to (dis-)associates Node instances,
23  * e.g. in a parent/child association.
24  *
25  * The controller supports the following actions:
26  *
27  * <div class="controller-action">
28  * <div> __Action__ associate </div>
29  * <div>
30  * Connect two Node instances.
31  * | Parameter | Description
32  * |-----------------------|-------------------------
33  * | _in_ `sourceOid` | The object id of the association's source Node
34  * | _in_ `targetOid` | The object id of the association's target Node
35  * | _in_ `role` | The role of the target Node in the source Node
36  * | __Response Actions__ | |
37  * | `ok` | In all cases
38  * </div>
39  * </div>
40  *
41  * <div class="controller-action">
42  * <div> __Action__ disassociate </div>
43  * <div>
44  * Disconnect two Node instances.
45  * | Parameter | Description
46  * |-----------------------|-------------------------
47  * | _in_ `sourceOid` | The object id of the association's source Node
48  * | _in_ `targetOid` | The object id of the association's target Node
49  * | _in_ `role` | The role of the target Node in the source Node
50  * | __Response Actions__ | |
51  * | `ok` | In all cases
52  * </div>
53  * </div>
54  *
55  * @author ingo herwig <ingo@wemove.com>
56  */
58 
59  /**
60  * @see Controller::validate()
61  */
62  protected function validate() {
63  $request = $this->getRequest();
64  $response = $this->getResponse();
65 
66  // check object id validity
67  $sourceOid = ObjectId::parse($request->getValue('sourceOid'));
68  if(!$sourceOid) {
69  $response->addError(ApplicationError::get('OID_INVALID',
70  array('invalidOids' => array($request->getValue('sourceOid')))));
71  return false;
72  }
73  $targetOid = ObjectId::parse($request->getValue('targetOid'));
74  if(!$targetOid) {
75  $response->addError(ApplicationError::get('OID_INVALID',
76  array('invalidOids' => array($request->getValue('targetOid')))));
77  return false;
78  }
79 
80  // check association
81  $persistenceFacade = $this->getPersistenceFacade();
82  $mapper = $persistenceFacade->getMapper($sourceOid->getType());
83  // try role
84  if ($request->hasValue('role')) {
85  $relationDesc = $mapper->getRelation($request->getValue('role'));
86  if ($relationDesc == null) {
87  $response->addError(ApplicationError::get('ROLE_INVALID'));
88  return false;
89  }
90  }
91  // try type
92  else {
93  $relationDesc = $mapper->getRelation($targetOid->getType());
94  if ($relationDesc == null) {
95  $response->addError(ApplicationError::get('ASSOCIATION_INVALID'));
96  return false;
97  }
98  }
99  return true;
100  }
101 
102  /**
103  * @see Controller::doExecute()
104  */
105  protected function doExecute() {
106  $request = $this->getRequest();
107  $response = $this->getResponse();
108  $persistenceFacade = $this->getPersistenceFacade();
109 
110  $transaction = $persistenceFacade->getTransaction();
111  $transaction->begin();
112  try {
113  // get the source node
114  $sourceOID = ObjectId::parse($request->getValue('sourceOid'));
115  $sourceNode = $persistenceFacade->load($sourceOID, BuildDepth::SINGLE);
116 
117  // get the target node
118  $targetOID = ObjectId::parse($request->getValue('targetOid'));
119  $targetNode = $persistenceFacade->load($targetOID, BuildDepth::SINGLE);
120 
121  // get the role
122  $role = null;
123  if ($request->hasValue('role')) {
124  $role = $request->getValue('role');
125  }
126 
127  if ($sourceNode != null && $targetNode != null) {
128  // process actions
129  if ($request->getAction() == 'associate') {
130  try {
131  $sourceNode->addNode($targetNode, $role);
132  }
133  catch (Exception $ex) {
134  throw new ApplicationException($request, $response,
135  ApplicationError::get('ASSOCIATION_INVALID'));
136  }
137  }
138  elseif ($request->getAction() == 'disassociate') {
139  try {
140  $sourceNode->deleteNode($targetNode, $role);
141  }
142  catch (Exception $ex) {
143  throw new ApplicationException($request, $response,
144  ApplicationError::get('ASSOCIATION_INVALID'));
145  }
146  }
147  }
148  else {
149  if ($sourceNode == null) {
150  $response->addError(ApplicationError::get('OID_INVALID',
151  array('invalidOids' => array('sourceOid'))));
152  }
153  if ($targetNode == null) {
154  $response->addError(ApplicationError::get('OID_INVALID',
155  array('invalidOids' => array('targetOid'))));
156  }
157  }
158  $transaction->commit();
159  }
160  catch (Exception $ex) {
161  $this->getLogger()->error($ex);
162  $response->addError(ApplicationError::fromException($ex));
163  $transaction->rollback();
164  }
165 
166  $response->setAction('ok');
167  }
168 }
169 ?>
getRequest()
Get the Request instance.
Definition: Controller.php:190
Controller is the base class of all controllers.
Definition: Controller.php:48
static fromException(\Exception $ex)
Factory method for transforming an exception into an ApplicationError instance.
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:144
Application controllers.
Definition: namespaces.php:3
static get($code, $data=null)
Factory method for retrieving a predefind error instance.
ApplicationException signals a general application exception.
getLogger()
Get the Logger instance.
Definition: Controller.php:206
AssociateController is used to (dis-)associates Node instances, e.g.
getResponse()
Get the Response instance.
Definition: Controller.php:198
getPersistenceFacade()
Get the PersistenceFacade instance.
Definition: Controller.php:222