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 "ScriptWorkerSerializer.h"
23 
24 #include <QDomElement>
25 
26 #include <U2Lang/BaseTypes.h>
27 #include <U2Lang/IncludedProtoFactory.h>
28 #include <U2Lang/WorkflowEnv.h>
29 
30 #define WORKFLOW_DOC "GB2WORKFLOW"
31 #define ACTOR_ELEMENT "Actor"
32 #define INPUT_PORT_ELEMENT "Input-port"
33 #define OUTPUT_PORT_ELEMENT "Output-port"
34 #define ATTRIBUTE_ELEMENT "Attributes"
35 #define IN_SLOT_ELEMENT "In-Slots"
36 #define OUT_SLOT_ELEMENT "Out-Slots"
37 #define SLOT_ID "Slot"
38 #define ATTR_ELEMENT "Attribute"
39 #define NAME_ID "Name"
40 #define TYPE_ID "Type"
41 #define NAME_ELEMENT "Element-name"
42 #define DESCR_ELEMENT "Element-description"
43 #define DESCR_ID "Description"
44 
45 namespace U2 {
46 
string2actor(const QString data,const QString actorName,QString & error,const QString actorFilePath)47 Workflow::ActorPrototype *ScriptWorkerSerializer::string2actor(const QString data, const QString actorName, QString &error, const QString actorFilePath) {
48     QDomDocument xml;
49     xml.setContent(data, false, &error);
50     if (!error.isEmpty()) {
51         return nullptr;
52     }
53     QDomElement doc = xml.documentElement();
54     DataTypeRegistry *dtr = Workflow::WorkflowEnv::getDataTypeRegistry();
55     assert(dtr);
56 
57     QDomNodeList inputs = doc.elementsByTagName(IN_SLOT_ELEMENT);
58     QList<DataTypePtr> inputTypes;
59     for (int i = 0; i < inputs.size(); i++) {
60         QDomElement slot = inputs.item(i).toElement();
61         QString id = slot.attribute(SLOT_ID);
62         inputTypes << dtr->getById(id);
63     }
64 
65     QDomNodeList outputs = doc.elementsByTagName(OUT_SLOT_ELEMENT);
66     QList<DataTypePtr> outputTypes;
67     for (int i = 0; i < outputs.size(); i++) {
68         QDomElement slot = outputs.item(i).toElement();
69         QString id = slot.attribute(SLOT_ID);
70         outputTypes << dtr->getById(id);
71     }
72 
73     QDomNodeList attributes = doc.elementsByTagName(ATTR_ELEMENT);
74     QList<Attribute *> attrs;
75     for (int i = 0; i < attributes.size(); i++) {
76         QDomElement attr = attributes.item(i).toElement();
77         QString typeId = attr.attribute(TYPE_ID);
78         QString name = attr.attribute(NAME_ID);
79 
80         DataTypePtr ptr = dtr->getById(typeId);
81         Descriptor desc(name, name, ptr->getDisplayName());
82         if (ptr == BaseTypes::BOOL_TYPE()) {
83             attrs << new Attribute(desc, ptr, false, QVariant(false));
84         } else {
85             attrs << new Attribute(desc, ptr);
86         }
87     }
88 
89     QString newActorName = actorName;
90     if (newActorName.isEmpty()) {
91         QDomElement name = doc.elementsByTagName(NAME_ELEMENT).item(0).toElement();
92         newActorName = name.attribute(NAME_ID);
93     }
94 
95     QDomElement descr = doc.elementsByTagName(DESCR_ELEMENT).item(0).toElement();
96     QString actorDesc = descr.attribute(DESCR_ID);
97 
98     // if actorName is not set then it is not alias name
99     bool isAliasName = !actorName.isEmpty();
100 
101     Workflow::ActorPrototype *proto = Workflow::IncludedProtoFactory::getScriptProto(inputTypes, outputTypes, attrs, newActorName, actorDesc, actorFilePath, isAliasName);
102 
103     if (nullptr == proto) {
104         error = QObject::tr("UGENE external error. Please, try again");
105         return nullptr;
106     }
107 
108     return proto;
109 }
110 
111 }  // namespace U2
112