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 "WriteSequenceValidator.h"
23 
24 #include <U2Core/AppContext.h>
25 #include <U2Core/DocumentModel.h>
26 #include <U2Core/U2SafePoints.h>
27 
28 #include <U2Lang/BaseAttributes.h>
29 #include <U2Lang/BaseSlots.h>
30 #include <U2Lang/SupportClass.h>
31 
32 namespace U2 {
33 namespace Workflow {
34 
WriteSequenceValidator(const QString & attr,const QString & port,const QString & slot)35 WriteSequenceValidator::WriteSequenceValidator(const QString &attr, const QString &port, const QString &slot)
36     : ScreenedParamValidator(attr, port, slot) {
37 }
38 
validate(const Configuration * cfg,NotificationsList & notificationList) const39 bool WriteSequenceValidator::validate(const Configuration *cfg, NotificationsList &notificationList) const {
40     const Actor *actor = dynamic_cast<const Actor *>(cfg);
41     SAFE_POINT(nullptr != actor, "NULL actor", false);
42     if (!isAnnotationsBinded(actor)) {
43         return true;
44     }
45 
46     DocumentFormat *format = getFormatSafe(actor);
47     CHECK(nullptr != format, true);
48     if (!isAnnotationsSupported(format)) {
49         QString warning = QObject::tr("The format %1 does not support annotations").arg(format->getFormatId().toUpper());
50         notificationList << WorkflowNotification(warning, "", WorkflowNotification::U2_WARNING);
51         cmdLog.trace(warning);
52     }
53 
54     return true;
55 }
56 
getFormatSafe(const Actor * actor)57 DocumentFormat *WriteSequenceValidator::getFormatSafe(const Actor *actor) {
58     Attribute *attr = actor->getParameter(BaseAttributes::DOCUMENT_FORMAT_ATTRIBUTE().getId());
59     SAFE_POINT(nullptr != attr, "NULL format attribute", nullptr);
60     CHECK(actor->isAttributeVisible(attr), nullptr);
61     QString formatId = attr->getAttributePureValue().toString();
62     return AppContext::getDocumentFormatRegistry()->getFormatById(formatId);
63 }
64 
isAnnotationsBinded(const Actor * actor) const65 bool WriteSequenceValidator::isAnnotationsBinded(const Actor *actor) const {
66     Port *p = actor->getPort(port);
67     SAFE_POINT(nullptr != p, "NULL port", false);
68     Attribute *attr = p->getParameter(IntegralBusPort::BUS_MAP_ATTR_ID);
69     SAFE_POINT(nullptr != attr, "NULL busmap attribute", false);
70     StrStrMap busMap = attr->getAttributeValueWithoutScript<StrStrMap>();
71     QString bindData = busMap.value(BaseSlots::ANNOTATION_TABLE_SLOT().getId(), "");
72     return !bindData.isEmpty();
73 }
74 
isAnnotationsSupported(const DocumentFormat * format)75 bool WriteSequenceValidator::isAnnotationsSupported(const DocumentFormat *format) {
76     return format->getSupportedObjectTypes().contains(GObjectTypes::ANNOTATION_TABLE);
77 }
78 
validate(const IntegralBusPort * port,NotificationsList & notificationList) const79 bool WriteSequencePortValidator::validate(const IntegralBusPort *port, NotificationsList &notificationList) const {
80     bool result = true;
81     Actor *actor = port->owner();
82 
83     QStringList screenedSlots(BaseSlots::URL_SLOT().getId());
84 
85     if (!isBinded(port, BaseSlots::ANNOTATION_TABLE_SLOT().getId())) {
86         DocumentFormat *format = WriteSequenceValidator::getFormatSafe(actor);
87         CHECK(nullptr != format, result);
88         if (!WriteSequenceValidator::isAnnotationsSupported(format)) {
89             screenedSlots << BaseSlots::ANNOTATION_TABLE_SLOT().getId();
90         }
91     }
92     result &= ScreenedSlotValidator::validate(screenedSlots, port, notificationList);
93     return result;
94 }
95 
96 }  // namespace Workflow
97 }  // namespace U2
98