Git Repository Public Repository

namibia

URLs

Copy to Clipboard
 
1074a2d61ec9d93a0ac7e0181875a2202b1225c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
namespace Workspace\Amf;



/**
 * Device AMF Workspace.
 * @author andre.fourie
 */
class Workspace
{

	/**
	 * @var boolean
	 */
	protected $authenticated = false;
	/**
	 * @var \User\Service\Authentication
	 */
	protected $authDetails;
	/**
	 * @var \Zend\ServiceManager\ServiceLocatorInterface
	 */
    protected $serviceLocator;



    /**
     * Summoning.
     * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator
     */
	public function __construct(\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator)
	{
		$this->serviceLocator = $serviceLocator;
		\Utility\Registry::setEntityManager(
				$this->serviceLocator->get('doctrine.entitymanager.orm_default')
				);
	}

	/**
	 * Set authentication session.
	 * @param unknown $token
	 */
	protected function setAuth($token)
	{
		\Utility\Registry::setSession($token);
		$this->authenticated = true;
		$this->authDetails = \Utility\Registry::getAuthData();
	}

	/**
	 * Authenticate user.
	 * @param string $email
	 * @param string $pin
	 * @return struct
	 */
	public function login($email, $pin)
	{
		$input = new \Workspace\Utility\ServiceInput('ParamSet', array(
				'Login' => array(
						'email' => $email,
						'pin'   => $pin
				)
		));
		$serviceInput = new \Workspace\Utility\ServiceInput('ServiceInput', array(
				'data' => $input->pack()
		));
		$profileService = $this->serviceLocator->get('User.Service.Profile');
		$response = $profileService->deviceLogin($serviceInput->pack());
		return array(
				'Meta' => array_merge(
						array('Task' => __METHOD__),
						$response['Meta']
						),
				'Data' => $response['Data']
		);
	}

	/**
	 * Claim a job item from queue.
	 * @param string $authToken
	 * @param string $datasetName
	 * @param integer $id
	 * @return struct
	 */
	public function claimJob($authToken, $datasetName, $id)
	{
		#-> Session.
		if (!$this->setAuth($authToken))
		{
			return array(
					'Meta' => array(
							'Task'         => __METHOD__,
							'Status'       => 'Exception',
							'StatusReason' => 'Invalid session token.'
					),
					'Data' => array()
			);
		}

		switch ($datasetName)
		{
			case 'valuations':
				$entityName = '\Stock\Entity\Valuation';
				break;
			default:
				return array(
						'Meta' => array(
								'Task'         => __METHOD__,
								'Status'       => 'Exception',
								'StatusReason' => 'Invalid dataset requested.'
						),
						'Data' => array()
				);
				break;
		}
		$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
		$record = $em->getRepository($entityName)
			->find($id);
		if (is_null($record) || 1 != $record->queueStatus)
		{
			return array(
					'Meta' => array(
							'Task'         => __METHOD__,
							'Status'       => 'Fail',
							'StatusReason' => 'Job not available.',
							'DatasetName'  => $datasetName
					),
					'Data' => $record->toArray()
			);
		}
		$record->queueStatus = 2;
		$em->flush();
		return array(
				'Meta' => array(
						'Task'        => __METHOD__,
						'Status'      => 'Success',
						'DatasetName' => $datasetName
				),
				'Data' => $record->toArray()
		);
	}

	/**
	 * Retrieve a dataset.
	 * @param string $authToken
	 * @param string $datasetName
	 * @return struct
	 */
	public function getDataset($authToken, $datasetName)
	{
		#-> Session.
		if (!$this->setAuth($authToken))
		{
			return array(
					'Meta' => array(
							'Task'         => __METHOD__,
							'Status'       => 'Exception',
							'StatusReason' => 'Invalid session token.'
					),
					'Data' => array()
			);
		}

		$datasetName = $this->request['datasetName'];
		switch ($datasetName)
		{
			#-> Location
			case 'countries': $entityName = '\Location\Entity\Country'; break;
			case 'regions': $entityName = '\Location\Entity\Region'; break;
			case 'towns': $entityName = '\Location\Entity\Town'; break;
			#-> Stock
			case 'valuations': $entityName = '\Stock\Entity\Valuation'; break;
			case 'accessories': $entityName = '\Stock\Entity\Accessory'; break;
			case 'upholstery': $entityName = '\Stock\Entity\Upholstery'; break;
			case 'condition': $entityName = '\Stock\Entity\Condition'; break;
			case 'exteriorColours': $entityName = '\Stock\Entity\ExteriorColour'; break;
			case 'interiorColours': $entityName = '\Stock\Entity\InteriorColour'; break;
			case 'fuelTypes': $entityName = '\Stock\Entity\FuelType'; break;
			case 'transmissionTypes': $entityName = '\Stock\Entity\TransmissionType'; break;
			case 'natis': $entityName = '\Stock\Entity\Natis'; break;
			case 'papers': $entityName = '\Stock\Entity\Paper'; break;
			case 'years': $entityName = '\Stock\Entity\Year'; break;
			case 'categories': $entityName = '\Stock\Entity\Category'; break;
			case 'makes': $entityName = '\Stock\Entity\Make'; break;
			case 'models': $entityName = '\Stock\Entity\Model'; break;
			case 'types': $entityName = '\Stock\Entity\Type'; break;
			default:
				return array(
						'Meta' => array(
								'Task'         => __METHOD__,
								'Status'       => 'Fail',
								'StatusReason' => 'Unknown dataset requested.',
								'DatasetName'  => $datasetName
						),
						'Data' => array()
				);
				break;
		}
		$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
		if ('Build' == $entityName::PULL_SYNCH_STRATEGY)
		{
			$timeStart = 0;
			$timeEnd   = time();
			$numPages  = 1;
			$page      = 1;
			$timestamp = time();
			$filter = 'WHERE 1 = 1 ';
			$filter .= defined($entityName . '::JOB_QUEUE')
				? 'AND a.queueStatus = 1'
				: '';
			$filter .= $entityName::ARCHIVE
				? 'AND a.archived = 0'
				: '';
			$query = $em->createQuery(
					"SELECT a FROM $entityName a $filter ORDER BY a.id ASC"
			);
		}
		else
		{
			$page = isset($this->request['page'])
				? (int) $this->request['page']
				: 1;
			$timeStart = isset($this->request['timeStart'])
				? (int) $this->request['timeStart']
				: 0;
			$timeEnd = isset($this->request['timeEnd'])
				? (int) $this->request['timeEnd']
				: time();
			$recs = 10;
			$filter = '';
			$filter .= defined($entityName . '::JOB_QUEUE')
				? 'AND a.queueStatus = 1'
				: '';
			$filter .= $entityName::ARCHIVE
				? 'AND a.archived = 0'
				: '';
			$query = $em->createQuery(
					"SELECT a FROM $entityName a "
					. "WHERE a.archived = :archived"
					. " $filter"
					. " AND (a.created BETWEEN :timeStart AND :timeEnd"
					. "      OR a.updated BETWEEN :timeStart AND :timeEnd)"
					. " ORDER BY a.id ASC"
					)
				->setParameter('archived', false)
				->setParameter('timeStart', date(\Utility\Definitions\Locale::getDateTimeFormat(), $timeStart))
				->setParameter('timeEnd', date(\Utility\Definitions\Locale::getDateTimeFormat(), $timeEnd));
			$numRecsRes = $em->createQuery(
					"SELECT COUNT(a.id) AS total "
					. "FROM $entityName a "
					. "WHERE a.archived = :archived"
					. " $filter"
					. " AND (a.created BETWEEN :timeStart AND :timeEnd"
					. "      OR a.updated BETWEEN :timeStart AND :timeEnd)"
					)
				->setParameter('archived', false)
				->setParameter('timeStart', date(\Utility\Definitions\Locale::getDateTimeFormat(), $timeStart))
				->setParameter('timeEnd', date(\Utility\Definitions\Locale::getDateTimeFormat(), $timeEnd))
				->getSingleResult();
			$numRecs = $numRecsRes['total'];
			error_log("num records: $numRecs");
			$numPages = (0 < $numRecs)
				? ceil($numRecs / $recs)
				: 0;
			$query->setFirstResult(($page -1) * $recs)
				->setMaxResults($recs);
		}
		$dataset = $query->getResult();

		if (defined($entityName . '::JOB_QUEUE'))
		{
			foreach ($dataset as $rowId => $record)
			{
				$dataset[$rowId] = $record->toQueueArray();
			}
		}
		else
		{
			foreach ($dataset as $rowId => $record)
			{
				$dataset[$rowId] = $record->toSynchArray();
			}
		}
		return array(
				'Meta' => array(
					'Task'        => __METHOD__,
					'Status'      => 'Success',
					'DatasetName' => $datasetName,
					'Strategy'    => $entityName::PULL_SYNCH_STRATEGY,
					'DataPages'   => $numPages,
					'Page'        => $page,
					'StartTime'   => $timeStart,
					'EndTime'     => $timeEnd
				),
				'Data' => $dataset
		);
	}

}

Commits for namibia/module/Workspace/src/Workspace/Amf/Workspace.php

Diff revisions: vs.
Revision Author Commited Message
df0489 ... Mark Fri 14 Oct, 2016 10:01:00 +0000

initial commit