ConcurrencyController.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 
26 
27 /**
28  * ConcurrencyController is used to lock/unlock objects.
29  *
30  * The controller supports the following actions:
31  *
32  * <div class="controller-action">
33  * <div> __Action__ lock </div>
34  * <div>
35  * Lock an object.
36  * | Parameter | Description
37  * |-----------------------|-------------------------
38  * | _in_ / _out_ `oid` | The object id of the object to lock
39  * | _in_ / _out_ `type` | The lock type (_optimistic_ or _pessimistic_) (optional, default: _optimistic_)
40  * | __Response Actions__ | |
41  * | `ok` | In all cases
42  * </div>
43  * </div>
44  *
45  * <div class="controller-action">
46  * <div> __Action__ unlock </div>
47  * <div>
48  * Unlock an object.
49  * | Parameter | Description
50  * |-----------------------|-------------------------
51  * | _in_ / _out_ `oid` | The object id of the object to unlock
52  * | _in_ / _out_ `type` | The lock type (_optimistic_ or _pessimistic_) (optional, default: _optimistic_)
53  * | __Response Actions__ | |
54  * | `ok` | In all cases
55  * </div>
56  * </div>
57  *
58  * @note If the user already holds a pessimistic lock, and tries to aquire an
59  * optimistic lock, the returned lock type is still pessimistic.
60  *
61  * @author ingo herwig <ingo@wemove.com>
62  */
64 
65  private $_concurrencyManager = null;
66 
67  /**
68  * Constructor
69  * @param $session
70  * @param $persistenceFacade
71  * @param $permissionManager
72  * @param $actionMapper
73  * @param $localization
74  * @param $message
75  * @param $configuration
76  * @param $concurrencyManager
77  */
78  public function __construct(Session $session,
79  PersistenceFacade $persistenceFacade,
80  PermissionManager $permissionManager,
81  ActionMapper $actionMapper,
82  Localization $localization,
83  Message $message,
84  Configuration $configuration,
85  ConcurrencyManager $concurrencyManager) {
86  parent::__construct($session, $persistenceFacade, $permissionManager,
87  $actionMapper, $localization, $message, $configuration);
88  $this->_concurrencyManager = $concurrencyManager;
89  }
90 
91  /**
92  * @see Controller::validate()
93  */
94  protected function validate() {
95  $request = $this->getRequest();
96  $response = $this->getResponse();
97  $oid = ObjectId::parse($request->getValue('oid'));
98  if(!$oid) {
99  $response->addError(ApplicationError::get('OID_INVALID',
100  array('invalidOids' => array($request->getValue('oid')))));
101  return false;
102  }
103  $lockType = $request->getValue('type', Lock::TYPE_OPTIMISTIC);
104  if (!in_array($lockType, array(Lock::TYPE_OPTIMISTIC, Lock::TYPE_PESSIMISTIC))) {
105  $response->addError(ApplicationError::get('PARAMETER_INVALID',
106  array('invalidParameters' => array('type'))));
107  }
108  return true;
109  }
110 
111  /**
112  * @see Controller::doExecute()
113  */
114  protected function doExecute() {
115  $request = $this->getRequest();
116  $response = $this->getResponse();
117  $oid = ObjectId::parse($request->getValue('oid'));
118  $lockType = $request->getValue('type', Lock::TYPE_OPTIMISTIC);
119 
120  // process actions
121  try {
122  if ($request->getAction() == 'lock') {
123  $this->_concurrencyManager->aquireLock($oid, $lockType);
124  $lock = $this->_concurrencyManager->getLock($oid);
125  $response->setValue('type', $lock->getType());
126  }
127  elseif ($request->getAction() == 'unlock') {
128  $this->_concurrencyManager->releaseLock($oid, $lockType);
129  }
130  }
131  catch (PessimisticLockException $ex) {
132  $response->addError(ApplicationError::get('OBJECT_IS_LOCKED',
133  array('lockedOids' => array($oid->__toString()))));
134  }
135 
136  $response->setValue('oid', $oid);
137  $response->setAction('ok');
138  }
139 }
140 ?>
getRequest()
Get the Request instance.
Definition: Controller.php:190
Localization defines the interface for storing localized entity instances and retrieving them back...
ConcurrencyController is used to lock/unlock objects.
Controller is the base class of all controllers.
Definition: Controller.php:48
ConcurrencyManager is used to handle concurrency for objects.
Message is used to get localized messages to be used in the user interface.
Definition: Message.php:23
Session is the interface for session implementations and defines access to session variables...
Definition: Session.php:21
static parse($oid)
Parse a serialized object id string into an ObjectId instance.
Definition: ObjectId.php:144
PermissionManager implementations are used to handle all authorization requests.
Implementations of Configuration give access to the application configuration.
Application controllers.
Definition: namespaces.php:3
__construct(Session $session, PersistenceFacade $persistenceFacade, PermissionManager $permissionManager, ActionMapper $actionMapper, Localization $localization, Message $message, Configuration $configuration, ConcurrencyManager $concurrencyManager)
Constructor.
ActionMapper implementations are responsible for instantiating and executing Controllers based on the...
static get($code, $data=null)
Factory method for retrieving a predefind error instance.
PessimisticLockException signals an exception when trying to create an pessimistic lock...
PersistenceFacade defines the interface for PersistenceFacade implementations.
getResponse()
Get the Response instance.
Definition: Controller.php:198