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 "CloneAssemblyWithReferenceToDbiTask.h"
23 
24 #include <QScopedPointer>
25 
26 #include <U2Core/AssemblyObject.h>
27 #include <U2Core/CloneObjectTask.h>
28 #include <U2Core/DNASequenceObject.h>
29 #include <U2Core/DocumentModel.h>
30 #include <U2Core/U2AssemblyDbi.h>
31 #include <U2Core/U2DbiUtils.h>
32 #include <U2Core/U2ObjectDbi.h>
33 #include <U2Core/U2SafePoints.h>
34 #include <U2Core/U2SequenceDbi.h>
35 
36 namespace U2 {
37 
CloneAssemblyWithReferenceToDbiTask(const U2Assembly & assembly,const U2Sequence & reference,const U2DbiRef & srcDbiRef,const U2DbiRef & dstDbiRef,const QVariantMap & hints)38 CloneAssemblyWithReferenceToDbiTask::CloneAssemblyWithReferenceToDbiTask(const U2Assembly &assembly,
39                                                                          const U2Sequence &reference,
40                                                                          const U2DbiRef &srcDbiRef,
41                                                                          const U2DbiRef &dstDbiRef,
42                                                                          const QVariantMap &hints)
43     : Task(tr("Clone assembly object to the destination database"), TaskFlags_FOSE_COSC),
44       assembly(assembly),
45       reference(reference),
46       srcDbiRef(srcDbiRef),
47       dstDbiRef(dstDbiRef),
48       dstFolder(hints.value(DocumentFormat::DBI_FOLDER_HINT, U2ObjectDbi::ROOT_FOLDER).toString()),
49       cloneAssemblyTask(nullptr),
50       cloneReferenceTask(nullptr) {
51     SAFE_POINT_EXT(assembly.hasValidId(), setError(tr("Invalid assembly ID")), );
52     SAFE_POINT_EXT(reference.hasValidId(), setError(tr("Invalid assembly ID")), );
53     SAFE_POINT_EXT(srcDbiRef.isValid(), setError(tr("Invalid source database reference")), );
54     SAFE_POINT_EXT(dstDbiRef.isValid(), setError(tr("Invalid destination database reference")), );
55 }
56 
prepare()57 void CloneAssemblyWithReferenceToDbiTask::prepare() {
58     AssemblyObject *assemblyObject = new AssemblyObject(assembly.visualName, U2EntityRef(srcDbiRef, assembly.id));
59     cloneAssemblyTask = new CloneObjectTask(assemblyObject, dstDbiRef, dstFolder);
60     addSubTask(cloneAssemblyTask);
61 
62     U2SequenceObject *sequenceObject = new U2SequenceObject(reference.visualName, U2EntityRef(srcDbiRef, reference.id));
63     cloneReferenceTask = new CloneObjectTask(sequenceObject, dstDbiRef, dstFolder);
64     addSubTask(cloneReferenceTask);
65 }
66 
run()67 void CloneAssemblyWithReferenceToDbiTask::run() {
68     delete cloneAssemblyTask->getSourceObject();
69     delete cloneReferenceTask->getSourceObject();
70 
71     QScopedPointer<GObject> clonedObject(cloneAssemblyTask->takeResult());
72     SAFE_POINT_EXT(nullptr != clonedObject, setError(tr("Can't get the cloned object")), );
73     QScopedPointer<AssemblyObject> clonedAssemblyObject(qobject_cast<AssemblyObject *>(clonedObject.data()));
74     SAFE_POINT_EXT(nullptr != clonedAssemblyObject, setError(tr("Unexpected result object: expect AssemblyObject, got %1 object").arg(clonedObject->getGObjectType())), );
75     clonedObject.take();
76 
77     clonedObject.reset(cloneReferenceTask->takeResult());
78     SAFE_POINT_EXT(nullptr != clonedObject, setError(tr("Can't get the cloned object")), );
79     QScopedPointer<U2SequenceObject> clonedSequenceObject(qobject_cast<U2SequenceObject *>(clonedObject.data()));
80     SAFE_POINT_EXT(nullptr != clonedSequenceObject, setError(tr("Unexpected result object: expect U2SequenceObject, got %1 object").arg(clonedObject->getGObjectType())), );
81     clonedObject.take();
82 
83     DbiConnection con(dstDbiRef, stateInfo);
84     CHECK_OP(stateInfo, );
85 
86     U2Assembly clonedAssembly = con.dbi->getAssemblyDbi()->getAssemblyObject(clonedAssemblyObject->getEntityRef().entityId, stateInfo);
87     CHECK_OP(stateInfo, );
88     U2Sequence clonedSequence = con.dbi->getSequenceDbi()->getSequenceObject(clonedSequenceObject->getEntityRef().entityId, stateInfo);
89     CHECK_OP(stateInfo, );
90 
91     clonedAssembly.referenceId = clonedSequence.id;
92     con.dbi->getAssemblyDbi()->updateAssemblyObject(clonedAssembly, stateInfo);
93 }
94 
95 }  // namespace U2
96