# Path: reggata/logic/worker_threads.py
# class DeleteGroupOfItemsThread(AbstractWorkerThread):
#     def __init__(self, parent, repo, item_ids, user_login):
#         super(DeleteGroupOfItemsThread, self).__init__(parent)
#         self.repo = repo
#         self.item_ids = item_ids
#         self.user_login = user_login
#         self.errors = 0
#         self.detailed_message = None
# 
#     def doWork(self):
#         self.errors = 0
#         self.detailed_message = ""
#         uow = self.repo.createUnitOfWork()
#         try:
#             i = 0
#             for itemId in self.item_ids:
#                 try:
#                     cmd = cmds.DeleteItemCommand(itemId, self.user_login)
#                     uow.executeCommand(cmd)
#                 except errors.AccessError as ex:
#                     # User self.user_login has no permissions to delete this item
#                     self.errors += 1
#                     self.detailed_message += str(ex) + os.linesep
# 
#                 i = i + 1
#                 self.emit(QtCore.SIGNAL("progress"), int(100.0*float(i)/len(self.item_ids)))
# 
#             uow.executeCommand(cmds.DeleteHangingTagsCommand())
#             uow.executeCommand(cmds.DeleteHangingFieldsCommand())
#         finally:
#             uow.close()
# 
# Path: reggata/tests/abstract_test_cases.py
# class AbstractTestCaseWithRepo(unittest.TestCase):
# 
#     def setUp(self):
#         self.repoBasePath = tests_context.TEST_REPO_BASE_PATH
#         self.copyOfRepoBasePath = tests_context.COPY_OF_TEST_REPO_BASE_PATH
# 
#         if (os.path.exists(self.copyOfRepoBasePath)):
#             shutil.rmtree(self.copyOfRepoBasePath)
#         shutil.copytree(self.repoBasePath, self.copyOfRepoBasePath)
# 
#         self.repo = RepoMgr(self.copyOfRepoBasePath)
# 
#     def tearDown(self):
#         self.repo = None
#         shutil.rmtree(self.copyOfRepoBasePath)
# 
#     def getExistingItem(self, itemId):
#         try:
#             uow = self.repo.createUnitOfWork()
#             item = uow.executeCommand(GetExpungedItemCommand(itemId))
#             self.assertIsNotNone(item)
#         finally:
#             uow.close()
#         return item
# 
#     def getDataRef(self, url):
#         # In the case when this DataRef is a file, url should be a relative path
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRef = uow._session.query(DataRef) \
#                     .filter(DataRef.url_raw==helpers.to_db_format(url)).first()
#             # NOTE: dataRef could be None
#         finally:
#             uow.close()
#         return dataRef
# 
# 
#     def getDataRefById(self, objId):
#         '''
#             Returns a DataRef object with given id or None, if DataRef object not found.
#         '''
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRef = uow._session.query(DataRef).filter(DataRef.id==objId).first()
#         finally:
#             uow.close()
#         return dataRef
# 
#     
#     def getDataRefsFromDir(self, dirRelPath):
#         dataRefs = []
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRefs = uow._session.query(DataRef).filter(
#                 DataRef.url_raw.like(helpers.to_db_format(dirRelPath) + "/%")).all()
#         finally:
#             uow.close()
#         return dataRefs
# 
# 
#     def updateExistingItem(self, detachedItem, newSrcAbsPath, newDstRelPath, user_login):
#         item = None
#         try:
#             uow = self.repo.createUnitOfWork()
#             cmd = UpdateExistingItemCommand(detachedItem, newSrcAbsPath, newDstRelPath, user_login)
#             item = uow.executeCommand(cmd)
#         finally:
#             uow.close()
#         return item
# 
# Path: reggata/data/commands.py
# class GetExpungedItemCommand(AbstractCommand):
#     '''
#         Returns expunged (detached) object of Item class from database with given id.
#     '''
#     def __init__(self, itemId):
#         self.__itemId = itemId
# 
#     def _execute(self, uow):
#         self._session = uow.session
#         item = self._session.query(db.Item)\
#             .options(joinedload_all('data_ref'))\
#             .options(joinedload_all('item_tags.tag'))\
#             .options(joinedload_all('item_fields.field'))\
#             .get(self.__itemId)
# 
#         if item is None:
#             raise err.NotFoundError()
# 
#         self._session.expunge(item)
#         return item


from reggata.logic.worker_threads import DeleteGroupOfItemsThread
from reggata.tests.abstract_test_cases import AbstractTestCaseWithRepo
from reggata.data.commands import GetExpungedItemCommand
'''
Created on 25.01.2012
@author: vvolkov
'''

class DeleteGroupOfItemsThreadTest(AbstractTestCaseWithRepo):

    def testName(self):
        thread = DeleteGroupOfItemsThread(parent=None,
                                          repo=self.repo,
                                          item_ids=[1, 2, 3],
                                          user_login="user")
        thread.run()

        try:
            uow = self.repo.createUnitOfWork()

# Path: reggata/logic/worker_threads.py
# class DeleteGroupOfItemsThread(AbstractWorkerThread):
#     def __init__(self, parent, repo, item_ids, user_login):
#         super(DeleteGroupOfItemsThread, self).__init__(parent)
#         self.repo = repo
#         self.item_ids = item_ids
#         self.user_login = user_login
#         self.errors = 0
#         self.detailed_message = None
# 
#     def doWork(self):
#         self.errors = 0
#         self.detailed_message = ""
#         uow = self.repo.createUnitOfWork()
#         try:
#             i = 0
#             for itemId in self.item_ids:
#                 try:
#                     cmd = cmds.DeleteItemCommand(itemId, self.user_login)
#                     uow.executeCommand(cmd)
#                 except errors.AccessError as ex:
#                     # User self.user_login has no permissions to delete this item
#                     self.errors += 1
#                     self.detailed_message += str(ex) + os.linesep
# 
#                 i = i + 1
#                 self.emit(QtCore.SIGNAL("progress"), int(100.0*float(i)/len(self.item_ids)))
# 
#             uow.executeCommand(cmds.DeleteHangingTagsCommand())
#             uow.executeCommand(cmds.DeleteHangingFieldsCommand())
#         finally:
#             uow.close()
# 
# Path: reggata/tests/abstract_test_cases.py
# class AbstractTestCaseWithRepo(unittest.TestCase):
# 
#     def setUp(self):
#         self.repoBasePath = tests_context.TEST_REPO_BASE_PATH
#         self.copyOfRepoBasePath = tests_context.COPY_OF_TEST_REPO_BASE_PATH
# 
#         if (os.path.exists(self.copyOfRepoBasePath)):
#             shutil.rmtree(self.copyOfRepoBasePath)
#         shutil.copytree(self.repoBasePath, self.copyOfRepoBasePath)
# 
#         self.repo = RepoMgr(self.copyOfRepoBasePath)
# 
#     def tearDown(self):
#         self.repo = None
#         shutil.rmtree(self.copyOfRepoBasePath)
# 
#     def getExistingItem(self, itemId):
#         try:
#             uow = self.repo.createUnitOfWork()
#             item = uow.executeCommand(GetExpungedItemCommand(itemId))
#             self.assertIsNotNone(item)
#         finally:
#             uow.close()
#         return item
# 
#     def getDataRef(self, url):
#         # In the case when this DataRef is a file, url should be a relative path
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRef = uow._session.query(DataRef) \
#                     .filter(DataRef.url_raw==helpers.to_db_format(url)).first()
#             # NOTE: dataRef could be None
#         finally:
#             uow.close()
#         return dataRef
# 
# 
#     def getDataRefById(self, objId):
#         '''
#             Returns a DataRef object with given id or None, if DataRef object not found.
#         '''
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRef = uow._session.query(DataRef).filter(DataRef.id==objId).first()
#         finally:
#             uow.close()
#         return dataRef
# 
#     
#     def getDataRefsFromDir(self, dirRelPath):
#         dataRefs = []
#         try:
#             uow = self.repo.createUnitOfWork()
#             dataRefs = uow._session.query(DataRef).filter(
#                 DataRef.url_raw.like(helpers.to_db_format(dirRelPath) + "/%")).all()
#         finally:
#             uow.close()
#         return dataRefs
# 
# 
#     def updateExistingItem(self, detachedItem, newSrcAbsPath, newDstRelPath, user_login):
#         item = None
#         try:
#             uow = self.repo.createUnitOfWork()
#             cmd = UpdateExistingItemCommand(detachedItem, newSrcAbsPath, newDstRelPath, user_login)
#             item = uow.executeCommand(cmd)
#         finally:
#             uow.close()
#         return item
# 
# Path: reggata/data/commands.py
# class GetExpungedItemCommand(AbstractCommand):
#     '''
#         Returns expunged (detached) object of Item class from database with given id.
#     '''
#     def __init__(self, itemId):
#         self.__itemId = itemId
# 
#     def _execute(self, uow):
#         self._session = uow.session
#         item = self._session.query(db.Item)\
#             .options(joinedload_all('data_ref'))\
#             .options(joinedload_all('item_tags.tag'))\
#             .options(joinedload_all('item_fields.field'))\
#             .get(self.__itemId)
# 
#         if item is None:
#             raise err.NotFoundError()
# 
#         self._session.expunge(item)
#         return item


# Path: reggata/tests/test_worker_threads.py
from reggata.logic.worker_threads import DeleteGroupOfItemsThread
from reggata.tests.abstract_test_cases import AbstractTestCaseWithRepo
from reggata.data.commands import GetExpungedItemCommand

'''
Created on 25.01.2012
@author: vvolkov
'''

class DeleteGroupOfItemsThreadTest(AbstractTestCaseWithRepo):

    def testName(self):
        thread = DeleteGroupOfItemsThread(parent=None,
                                          repo=self.repo,
                                          item_ids=[1, 2, 3],
                                          user_login="user")
        thread.run()

        try:
            uow = self.repo.createUnitOfWork()

            self.assertFalse(uow.executeCommand(GetExpungedItemCommand(1)).alive)