MDL-45774 fix trailing whitespace
[moodle.git] / lib / alfresco / Service / Session.php
blob9204d7da5743d6422f90d07aa586b3cd924bf6ad
1 <?php
2 /*
3 * Copyright (C) 2005-2010 Alfresco Software Limited.
5 * This file is part of Alfresco
7 * Alfresco is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * Alfresco is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
21 require_once $CFG->libdir.'/alfresco/Service/Store.php';
22 require_once $CFG->libdir.'/alfresco/Service/Node.php';
23 require_once $CFG->libdir.'/alfresco/Service/WebService/WebServiceFactory.php';
25 class Session extends BaseObject
27 public $authenticationService;
28 public $repositoryService;
29 public $contentService;
31 private $_repository;
32 private $_ticket;
33 private $_stores;
34 private $_namespaceMap;
36 private $nodeCache;
37 private $idCount = 0;
39 /**
40 * Constructor
42 * @param userName the user name
43 * @param ticket the currenlty authenticated users ticket
45 public function __construct($repository, $ticket)
47 $this->nodeCache = array();
49 $this->_repository = $repository;
50 $this->_ticket = $ticket;
52 $this->repositoryService = WebServiceFactory::getRepositoryService($this->_repository->connectionUrl, $this->_ticket);
53 $this->contentService = WebServiceFactory::getContentService($this->_repository->connectionUrl, $this->_ticket);
56 /**
57 * Creates a new store in the current respository
59 * @param $address the address of the new store
60 * @param $scheme the scheme of the new store, default value of 'workspace'
61 * @return Store the new store
63 public function createStore($address, $scheme="workspace")
65 // Create the store
66 $result = $this->repositoryService->createStore(array(
67 "scheme" => $scheme,
68 "address" => $address));
69 $store = new Store($this, $result->createStoreReturn->address, $result->createStoreReturn->scheme);
71 // Add to the cached list if its been populated
72 if (isset($this->_stores) == true)
74 $this->_stores[] = $store;
77 // Return the newly created store
78 return $store;
81 /**
82 * Get the store
84 * @param $address the address of the store
85 * @param $scheme the scheme of the store. The default it 'workspace'
86 * @return Store the store
88 public function getStore($address, $scheme="workspace")
90 return new Store($this, $address, $scheme);
93 /**
94 * Get the store from it string representation (eg: workspace://SpacesStore)
96 * @param $value the stores string representation
97 * @return Store the store
99 public function getStoreFromString($value)
101 list($scheme, $address) = explode("://", $value);
102 return new Store($this, $address, $scheme);
105 public function getNode($store, $id)
107 $node = $this->getNodeImpl($store, $id);
108 if ($node == null)
110 $node = new Node($this, $store, $id);
111 $this->addNode($node);
113 return $node;
116 public function getNodeFromString($value)
118 // TODO
119 throw new Exception("getNode($value) not yet implemented");
123 * Adds a new node to the session.
125 public function addNode($node)
127 $this->nodeCache[$node->__toString()] = $node;
130 private function getNodeImpl($store, $id)
132 $result = null;
133 $nodeRef = $store->scheme . "://" . $store->address . "/" . $id;
134 if (array_key_exists($nodeRef, $this->nodeCache) == true)
136 $result = $this->nodeCache[$nodeRef];
138 return $result;
142 * Commits all unsaved changes to the repository
144 public function save($debug=false)
146 // Build the update statements from the node cache
147 $statements = array();
148 foreach ($this->nodeCache as $node)
150 $node->onBeforeSave($statements);
153 if ($debug == true)
155 var_dump($statements);
156 echo ("<br><br>");
159 if (count($statements) > 0)
161 // Make the web service call
162 $result = $this->repositoryService->update(array("statements" => $statements));
163 //var_dump($result);
165 // Update the state of the updated nodes
166 foreach ($this->nodeCache as $node)
168 $node->onAfterSave($this->getIdMap($result));
174 * Clears the current session by emptying the node cache.
176 * WARNING: all unsaved changes will be lost when clearing the session.
178 public function clear()
180 // Clear the node cache
181 $this->nodeCache = array();
184 private function getIdMap($result)
186 $return = array();
187 $statements = $result->updateReturn;
188 if (is_array($statements) == true)
190 foreach ($statements as $statement)
192 if ($statement->statement == "create")
194 $id = $statement->sourceId;
195 $uuid = $statement->destination->uuid;
196 $return[$id] = $uuid;
200 else
202 if ($statements->statement == "create")
204 $id = $statements->sourceId;
205 $uuid = $statements->destination->uuid;
206 $return[$id] = $uuid;
209 return $return;
212 public function query($store, $query, $language='lucene')
214 // TODO need to support paged queries
215 $result = $this->repositoryService->query(array(
216 "store" => $store->__toArray(),
217 "query" => array(
218 "language" => $language,
219 "statement" => $query),
220 "includeMetaData" => false));
222 // TODO for now do nothing with the score and the returned data
223 $resultSet = $result->queryReturn->resultSet;
224 return $this->resultSetToNodes($this, $store, $resultSet);
227 public function getTicket()
229 return $this->_ticket;
232 public function getRepository()
234 return $this->_repository;
237 public function getNamespaceMap()
239 if ($this->_namespaceMap == null)
241 $this->_namespaceMap = new NamespaceMap();
243 return $this->_namespaceMap;
246 public function getStores()
248 if (isset ($this->_stores) == false)
250 $this->_stores = array ();
251 $results = $this->repositoryService->getStores();
253 foreach ($results->getStoresReturn as $result)
255 $this->_stores[] = new Store($this, $result->address, $result->scheme);
259 return $this->_stores;
262 /** Want these methods to be package scope some how! **/
264 public function nextSessionId()
266 $sessionId = "session".$this->_ticket.$this->idCount;
267 $this->idCount ++;
268 return $sessionId;