1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: failurereason.py 56295 2015-06-09 14:29:55Z vboxsync $
|
---|
3 |
|
---|
4 | """
|
---|
5 | Test Manager - Failure Reasons.
|
---|
6 | """
|
---|
7 |
|
---|
8 | __copyright__ = \
|
---|
9 | """
|
---|
10 | Copyright (C) 2012-2015 Oracle Corporation
|
---|
11 |
|
---|
12 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
13 | available from http://www.virtualbox.org. This file is free software;
|
---|
14 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
15 | General Public License (GPL) as published by the Free Software
|
---|
16 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
17 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
18 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 |
|
---|
20 | The contents of this file may alternatively be used under the terms
|
---|
21 | of the Common Development and Distribution License Version 1.0
|
---|
22 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
23 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
24 | CDDL are applicable instead of those of the GPL.
|
---|
25 |
|
---|
26 | You may elect to license modified versions of this file under the
|
---|
27 | terms and conditions of either the GPL or the CDDL or both.
|
---|
28 | """
|
---|
29 | __version__ = "$Revision: 56295 $"
|
---|
30 |
|
---|
31 |
|
---|
32 | # Validation Kit imports.
|
---|
33 | from testmanager.core.base import ModelDataBase, ModelLogicBase, TMExceptionBase
|
---|
34 |
|
---|
35 |
|
---|
36 | class FailureReasonData(ModelDataBase):
|
---|
37 | """
|
---|
38 | Failure Reason Data.
|
---|
39 | """
|
---|
40 |
|
---|
41 | ksIdAttr = 'idFailureReason';
|
---|
42 |
|
---|
43 | ksParam_idFailureReason = 'FailureReasonData_idFailureReason'
|
---|
44 | ksParam_tsEffective = 'FailureReasonData_tsEffective'
|
---|
45 | ksParam_tsExpire = 'FailureReasonData_tsExpire'
|
---|
46 | ksParam_uidAuthor = 'FailureReasonData_uidAuthor'
|
---|
47 | ksParam_idFailureCategory = 'FailureReasonData_idFailureCategory'
|
---|
48 | ksParam_sShort = 'FailureReasonData_sShort'
|
---|
49 | ksParam_sFull = 'FailureReasonData_sFull'
|
---|
50 | ksParam_iTicket = 'FailureReasonData_iTicket'
|
---|
51 | ksParam_asUrls = 'FailureReasonData_asUrls'
|
---|
52 |
|
---|
53 | kasAllowNullAttributes = [ 'idFailureReason', 'tsEffective', 'tsExpire',
|
---|
54 | 'uidAuthor', 'iTicket', 'asUrls' ]
|
---|
55 |
|
---|
56 | def __init__(self):
|
---|
57 | ModelDataBase.__init__(self);
|
---|
58 |
|
---|
59 | #
|
---|
60 | # Initialize with defaults.
|
---|
61 | # See the database for explanations of each of these fields.
|
---|
62 | #
|
---|
63 |
|
---|
64 | self.idFailureReason = None
|
---|
65 | self.tsEffective = None
|
---|
66 | self.tsExpire = None
|
---|
67 | self.uidAuthor = None
|
---|
68 | self.idFailureCategory = None
|
---|
69 | self.sShort = None
|
---|
70 | self.sFull = None
|
---|
71 | self.iTicket = None
|
---|
72 | self.asUrls = None
|
---|
73 |
|
---|
74 | def initFromDbRow(self, aoRow):
|
---|
75 | """
|
---|
76 | Re-initializes the data with a row from a SELECT * FROM FailureReasons.
|
---|
77 |
|
---|
78 | Returns self. Raises exception if the row is None or otherwise invalid.
|
---|
79 | """
|
---|
80 |
|
---|
81 | if aoRow is None:
|
---|
82 | raise TMExceptionBase('Failure Reason not found.');
|
---|
83 |
|
---|
84 | self.idFailureReason = aoRow[0]
|
---|
85 | self.tsEffective = aoRow[1]
|
---|
86 | self.tsExpire = aoRow[2]
|
---|
87 | self.uidAuthor = aoRow[3]
|
---|
88 | self.idFailureCategory = aoRow[4]
|
---|
89 | self.sShort = aoRow[5]
|
---|
90 | self.sFull = aoRow[6]
|
---|
91 | self.iTicket = aoRow[7]
|
---|
92 | self.asUrls = aoRow[8]
|
---|
93 |
|
---|
94 | return self
|
---|
95 |
|
---|
96 |
|
---|
97 | class FailureReasonLogic(ModelLogicBase): # pylint: disable=R0903
|
---|
98 | """
|
---|
99 | Failure Reason logic.
|
---|
100 | """
|
---|
101 |
|
---|
102 | def fetchForListing(self, iStart, cMaxRows, tsNow):
|
---|
103 | """
|
---|
104 | Fetches Failure Category records.
|
---|
105 |
|
---|
106 | Returns an array (list) of FailureReasonData items, empty list if none.
|
---|
107 | Raises exception on error.
|
---|
108 | """
|
---|
109 |
|
---|
110 | if tsNow is None:
|
---|
111 | self._oDb.execute('SELECT *\n'
|
---|
112 | 'FROM FailureReasons\n'
|
---|
113 | 'WHERE tsExpire = \'infinity\'::TIMESTAMP\n'
|
---|
114 | 'ORDER BY idFailureReason DESC\n'
|
---|
115 | 'LIMIT %s OFFSET %s\n'
|
---|
116 | , (cMaxRows, iStart,));
|
---|
117 | else:
|
---|
118 | self._oDb.execute('SELECT *\n'
|
---|
119 | 'FROM FailureReasons\n'
|
---|
120 | 'WHERE tsExpire > %s\n'
|
---|
121 | ' AND tsEffective <= %s\n'
|
---|
122 | 'ORDER BY idFailureReason DESC\n'
|
---|
123 | 'LIMIT %s OFFSET %s\n'
|
---|
124 | , (tsNow, tsNow, cMaxRows, iStart,));
|
---|
125 |
|
---|
126 | aoRows = []
|
---|
127 | for aoRow in self._oDb.fetchAll():
|
---|
128 | aoRows.append(FailureReasonData().initFromDbRow(aoRow))
|
---|
129 | return aoRows
|
---|
130 |
|
---|
131 | def fetchForCombo(self, tsEffective = None):
|
---|
132 | """
|
---|
133 | Gets the list of Failure Reasons for a combo box.
|
---|
134 | Returns an array of (value [idFailureReason], drop-down-name [sShort],
|
---|
135 | hover-text [sFull]) tuples.
|
---|
136 | """
|
---|
137 | if tsEffective is None:
|
---|
138 | self._oDb.execute('SELECT idFailureReason, sShort, sFull\n'
|
---|
139 | 'FROM FailureReasons\n'
|
---|
140 | 'WHERE tsExpire = \'infinity\'::TIMESTAMP\n'
|
---|
141 | 'ORDER BY sShort')
|
---|
142 | else:
|
---|
143 | self._oDb.execute('SELECT idFailureReason, sShort, sFull\n'
|
---|
144 | 'FROM FailureReasons\n'
|
---|
145 | 'WHERE tsExpire > %s\n'
|
---|
146 | ' AND tsEffective <= %s\n'
|
---|
147 | 'ORDER BY sShort'
|
---|
148 | , (tsEffective, tsEffective))
|
---|
149 | return self._oDb.fetchAll()
|
---|
150 |
|
---|
151 | def getById(self, idFailureReason):
|
---|
152 | """Get Failure Reason data by idFailureReason"""
|
---|
153 |
|
---|
154 | self._oDb.execute('SELECT *\n'
|
---|
155 | 'FROM FailureReasons\n'
|
---|
156 | 'WHERE tsExpire = \'infinity\'::timestamp\n'
|
---|
157 | ' AND idFailureReason = %s;', (idFailureReason,))
|
---|
158 | aRows = self._oDb.fetchAll()
|
---|
159 | if len(aRows) not in (0, 1):
|
---|
160 | raise self._oDb.integrityException(
|
---|
161 | 'Found more than one failure reasons with the same credentials. Database structure is corrupted.')
|
---|
162 | try:
|
---|
163 | return FailureReasonData().initFromDbRow(aRows[0])
|
---|
164 | except IndexError:
|
---|
165 | return None
|
---|
166 |
|
---|
167 | def getIdsByCategory(self, idFailureCategory, tsEffective=None):
|
---|
168 | """
|
---|
169 | Gets the list of Failure Ressons IDs,
|
---|
170 | all the items belong to @param idFailureCategory
|
---|
171 | """
|
---|
172 | if tsEffective is None:
|
---|
173 | self._oDb.execute('SELECT idFailureReason\n'
|
---|
174 | 'FROM FailureReasons\n'
|
---|
175 | 'WHERE tsExpire = \'infinity\'::TIMESTAMP\n'
|
---|
176 | ' AND idFailureCategory = %s\n'
|
---|
177 | 'ORDER BY idFailureReason DESC'
|
---|
178 | , (idFailureCategory,))
|
---|
179 | else:
|
---|
180 | self._oDb.execute('SELECT idFailureReason\n'
|
---|
181 | 'FROM FailureReasons\n'
|
---|
182 | 'WHERE tsExpire > %s\n'
|
---|
183 | ' AND tsEffective <= %s\n'
|
---|
184 | ' AND idFailureCategory = %s\n'
|
---|
185 | 'ORDER BY idFailureReason DESC'
|
---|
186 | , (tsEffective, tsEffective, idFailureCategory))
|
---|
187 | return self._oDb.fetchAll()
|
---|
188 |
|
---|
189 | def getAll(self, tsEffective=None):
|
---|
190 | """
|
---|
191 | Gets the list of all Failure Reasons.
|
---|
192 | Returns an array of FailureReasonData instances.
|
---|
193 | """
|
---|
194 | if tsEffective is None:
|
---|
195 | self._oDb.execute('SELECT *\n'
|
---|
196 | 'FROM FailureReasons\n'
|
---|
197 | 'WHERE tsExpire = \'infinity\'::TIMESTAMP\n'
|
---|
198 | 'ORDER BY idFailureReason DESC')
|
---|
199 | else:
|
---|
200 | self._oDb.execute('SELECT *\n'
|
---|
201 | 'FROM FailureReasons\n'
|
---|
202 | 'WHERE tsExpire > %s\n'
|
---|
203 | ' AND tsEffective <= %s\n'
|
---|
204 | 'ORDER BY idFailureReason DESC'
|
---|
205 | , (tsEffective, tsEffective))
|
---|
206 | aoRet = []
|
---|
207 | for aoRow in self._oDb.fetchAll():
|
---|
208 | aoRet.append(FailureReasonData().initFromDbRow(aoRow))
|
---|
209 | return aoRet
|
---|
210 |
|
---|
211 | def addEntry(self, oFailureReasonData, uidAuthor, fCommit=True):
|
---|
212 | """Add record to database"""
|
---|
213 |
|
---|
214 | # Check if record with the same sShort fiels is already exists
|
---|
215 | self._oDb.execute('SELECT *\n'
|
---|
216 | 'FROM FailureReasons\n'
|
---|
217 | 'WHERE tsExpire = \'infinity\'::TIMESTAMP\n'
|
---|
218 | ' AND sShort = %s\n',
|
---|
219 | (oFailureReasonData.sShort,))
|
---|
220 | if len(self._oDb.fetchAll()) != 0:
|
---|
221 | raise Exception('Record already exist')
|
---|
222 |
|
---|
223 | # Add record
|
---|
224 | self._oDb.execute('INSERT INTO FailureReasons (\n'
|
---|
225 | ' uidAuthor, idFailureCategory,'
|
---|
226 | ' sShort, sFull, iTicket, asUrls'
|
---|
227 | ')\n'
|
---|
228 | 'VALUES (%s, %s, %s, %s, %s, %s)',
|
---|
229 | (uidAuthor,
|
---|
230 | oFailureReasonData.idFailureCategory,
|
---|
231 | oFailureReasonData.sShort,
|
---|
232 | oFailureReasonData.sFull,
|
---|
233 | oFailureReasonData.iTicket,
|
---|
234 | oFailureReasonData.asUrls))
|
---|
235 | if fCommit:
|
---|
236 | self._oDb.commit()
|
---|
237 |
|
---|
238 | return True
|
---|
239 |
|
---|
240 | def remove(self, uidAuthor, idFailureReason, fNeedCommit=True):
|
---|
241 | """
|
---|
242 | Historize record
|
---|
243 | """
|
---|
244 | self._oDb.execute('UPDATE FailureReasons\n'
|
---|
245 | 'SET tsExpire = CURRENT_TIMESTAMP,\n'
|
---|
246 | ' uidAuthor = %s\n'
|
---|
247 | 'WHERE idFailureReason = %s\n'
|
---|
248 | ' AND tsExpire = \'infinity\'::TIMESTAMP\n',
|
---|
249 | (uidAuthor, idFailureReason))
|
---|
250 |
|
---|
251 | # Also historize Black List records
|
---|
252 | self._oDb.execute('UPDATE BuildBlackList\n'
|
---|
253 | 'SET tsExpire = CURRENT_TIMESTAMP,\n'
|
---|
254 | ' uidAuthor = %s\n'
|
---|
255 | 'WHERE idFailureReason = %s\n'
|
---|
256 | ' AND tsExpire = \'infinity\'::TIMESTAMP\n',
|
---|
257 | (uidAuthor, idFailureReason))
|
---|
258 |
|
---|
259 | if fNeedCommit:
|
---|
260 | self._oDb.commit()
|
---|
261 |
|
---|
262 | return True
|
---|
263 |
|
---|
264 | def editEntry(self, oFailureReasonData, uidAuthor, fCommit=True):
|
---|
265 | """Modify database record"""
|
---|
266 |
|
---|
267 | # Check if record exists
|
---|
268 | oFailureReasonDataOld = self.getById(oFailureReasonData.idFailureReason)
|
---|
269 | if oFailureReasonDataOld is None:
|
---|
270 | raise TMExceptionBase(
|
---|
271 | 'Failure Reason (id: %d) does not exist'
|
---|
272 | % oFailureReasonData.idFailureReason)
|
---|
273 |
|
---|
274 | # Check if anything has been changed
|
---|
275 | if oFailureReasonData.isEqual(oFailureReasonDataOld):
|
---|
276 | return True
|
---|
277 |
|
---|
278 | # Historize record
|
---|
279 | self.remove(
|
---|
280 | uidAuthor, oFailureReasonData.idFailureReason, fNeedCommit=False)
|
---|
281 |
|
---|
282 |
|
---|
283 | # Add new record (keeping its ID)
|
---|
284 | self._oDb.execute('INSERT INTO FailureReasons (\n'
|
---|
285 | ' idFailureReason,'
|
---|
286 | ' uidAuthor,'
|
---|
287 | ' idFailureCategory,'
|
---|
288 | ' sShort,'
|
---|
289 | ' sFull,'
|
---|
290 | ' iTicket,'
|
---|
291 | ' asUrls'
|
---|
292 | ')\n'
|
---|
293 | 'VALUES (%s, %s, %s, %s, %s, %s, %s)',
|
---|
294 | (oFailureReasonData.idFailureReason,
|
---|
295 | uidAuthor,
|
---|
296 | oFailureReasonData.idFailureCategory,
|
---|
297 | oFailureReasonData.sShort,
|
---|
298 | oFailureReasonData.sFull,
|
---|
299 | oFailureReasonData.iTicket,
|
---|
300 | oFailureReasonData.asUrls
|
---|
301 | ))
|
---|
302 |
|
---|
303 | if fCommit:
|
---|
304 | self._oDb.commit()
|
---|
305 |
|
---|
306 | return True
|
---|