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;
34 private $_namespaceMap;
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
);
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")
66 $result = $this->repositoryService
->createStore(array(
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
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);
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);
110 $node = new Node($this, $store, $id);
111 $this->addNode($node);
116 public function getNodeFromString($value)
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)
133 $nodeRef = $store->scheme
. "://" . $store->address
. "/" . $id;
134 if (array_key_exists($nodeRef, $this->nodeCache
) == true)
136 $result = $this->nodeCache
[$nodeRef];
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);
155 var_dump($statements);
159 if (count($statements) > 0)
161 // Make the web service call
162 $result = $this->repositoryService
->update(array("statements" => $statements));
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)
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;
202 if ($statements->statement
== "create")
204 $id = $statements->sourceId
;
205 $uuid = $statements->destination
->uuid
;
206 $return[$id] = $uuid;
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(),
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
;