1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #include "MultiTask.h"
23 
24 #include <U2Core/AppContext.h>
25 #include <U2Core/ProjectModel.h>
26 #include <U2Core/U2SafePoints.h>
27 
28 namespace U2 {
29 
MultiTask(const QString & name,const QList<Task * > & taskz,bool withLock,TaskFlags f)30 MultiTask::MultiTask(const QString &name, const QList<Task *> &taskz, bool withLock, TaskFlags f)
31     : Task(name, f), l(nullptr), tasks(taskz) {
32     setMaxParallelSubtasks(1);
33     SAFE_POINT(!taskz.empty(), "No tasks provided to multitask", );
34 
35     foreach (Task *t, taskz) {
36         addSubTask(t);
37     }
38     if (withLock) {
39         SAFE_POINT(AppContext::getProject() != nullptr, "MultiTask::no project", );
40         l = new StateLock(getTaskName(), StateLockFlag_LiveLock);
41         AppContext::getProject()->lockState(l);
42     }
43 }
44 
getTasks() const45 QList<Task *> MultiTask::getTasks() const {
46     return tasks;
47 }
48 
report()49 Task::ReportResult MultiTask::report() {
50     Project *p = AppContext::getProject();
51     if (l != nullptr && p != nullptr) {
52         p->unlockState(l);
53         delete l;
54         l = nullptr;
55     }
56     foreach (Task *t, tasks) {
57         CHECK_CONTINUE(t->isConcatenateChildrenErrors());
58 
59         setReportingSupported(true);
60         setReportingEnabled(true);
61         break;
62     }
63     return Task::ReportResult_Finished;
64 }
65 
generateReport() const66 QString MultiTask::generateReport() const {
67     QString report = "<hr><br>";
68     foreach (Task *subtask, tasks) {
69         if (subtask->hasFlags(TaskFlags(TaskFlag_ReportingIsSupported | TaskFlag_ReportingIsEnabled))) {
70             report += QString("Subtask <b>'%1'</b>:<br><br>").arg(subtask->getTaskName());
71             report += subtask->generateReport();
72             report += "<br><hr><br>";
73         }
74     }
75     return report;
76 }
77 
78 //////////////////////////////////////////////////////////////////////////
79 // SequentialMultiTask
80 
SequentialMultiTask(const QString & name,const QList<Task * > & taskz,TaskFlags f)81 SequentialMultiTask::SequentialMultiTask(const QString &name, const QList<Task *> &taskz, TaskFlags f)
82     : Task(name, f), tasks(taskz) {
83     setMaxParallelSubtasks(1);
84 }
85 
prepare()86 void SequentialMultiTask::prepare() {
87     // run the first task
88     if (tasks.size() > 0) {
89         addSubTask(tasks.first());
90     }
91 }
92 
onSubTaskFinished(Task * subTask)93 QList<Task *> SequentialMultiTask::onSubTaskFinished(Task *subTask) {
94     QList<Task *> res;
95 
96     int idx = tasks.indexOf(subTask);
97     if ((idx != -1) && (idx + 1 < tasks.size())) {
98         res.append(tasks.at(idx + 1));
99     }
100 
101     return res;
102 }
103 
getTasks() const104 QList<Task *> SequentialMultiTask::getTasks() const {
105     return tasks;
106 }
107 
108 }  // namespace U2
109