Changeset View
Changeset View
Standalone View
Standalone View
sinksh/syntax_modules/sink_modify.cpp
Show All 32 Lines | |||||
33 | 33 | | |||
34 | #include "sinksh_utils.h" | 34 | #include "sinksh_utils.h" | ||
35 | #include "state.h" | 35 | #include "state.h" | ||
36 | #include "syntaxtree.h" | 36 | #include "syntaxtree.h" | ||
37 | 37 | | |||
38 | namespace SinkModify | 38 | namespace SinkModify | ||
39 | { | 39 | { | ||
40 | 40 | | |||
41 | Syntax::List syntax(); | ||||
42 | | ||||
41 | bool modify(const QStringList &args, State &state) | 43 | bool modify(const QStringList &args, State &state) | ||
42 | { | 44 | { | ||
43 | if (args.isEmpty()) { | | |||
44 | state.printError(QObject::tr("A type is required"), "sink_modify/02"); | | |||
45 | return false; | | |||
46 | } | | |||
47 | | ||||
48 | if (args.count() < 2) { | | |||
49 | state.printError(QObject::tr("A resource ID is required to remove items"), "sink_modify/03"); | | |||
50 | return false; | | |||
51 | } | | |||
52 | | ||||
53 | if (args.count() < 3) { | 45 | if (args.count() < 3) { | ||
54 | state.printError(QObject::tr("An object ID is required to remove items"), "sink_modify/03"); | 46 | state.printError(syntax()[0].usage()); | ||
55 | return false; | 47 | return false; | ||
56 | } | 48 | } | ||
57 | 49 | | |||
58 | auto type = args[0]; | 50 | auto type = args[0]; | ||
59 | auto resourceId = args[1]; | 51 | auto resourceId = args[1]; | ||
60 | auto identifier = args[2]; | 52 | auto identifier = args[2]; | ||
61 | 53 | | |||
62 | auto &store = SinkshUtils::getStore(type); | 54 | auto &store = SinkshUtils::getStore(type); | ||
Show All 13 Lines | |||||
76 | } | 68 | } | ||
77 | 69 | | |||
78 | return true; | 70 | return true; | ||
79 | } | 71 | } | ||
80 | 72 | | |||
81 | bool resource(const QStringList &args, State &state) | 73 | bool resource(const QStringList &args, State &state) | ||
82 | { | 74 | { | ||
83 | if (args.isEmpty()) { | 75 | if (args.isEmpty()) { | ||
76 | // TODO: pass the syntax as parameter | ||||
84 | state.printError(QObject::tr("A resource can not be modified without an id"), "sink_modify/01"); | 77 | state.printError(QObject::tr("A resource can not be modified without an id"), "sink_modify/01"); | ||
85 | } | 78 | } | ||
86 | 79 | | |||
87 | auto &store = SinkshUtils::getStore("resource"); | 80 | auto &store = SinkshUtils::getStore("resource"); | ||
88 | 81 | | |||
89 | auto resourceId = args.at(0); | 82 | auto resourceId = args.at(0); | ||
90 | Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("", resourceId.toLatin1()); | 83 | Sink::ApplicationDomain::ApplicationDomainType::Ptr object = store.getObject("", resourceId.toLatin1()); | ||
91 | 84 | | |||
92 | auto map = SinkshUtils::keyValueMapFromArgs(args); | 85 | auto map = SinkshUtils::keyValueMapFromArgs(args); | ||
93 | for (auto i = map.begin(); i != map.end(); ++i) { | 86 | for (auto i = map.begin(); i != map.end(); ++i) { | ||
94 | const auto property = i.key().toLatin1(); | 87 | const auto property = i.key().toLatin1(); | ||
95 | object->setProperty(property, Sink::PropertyParser::parse("resource", property, i.value())); | 88 | object->setProperty(property, Sink::PropertyParser::parse("resource", property, i.value())); | ||
96 | } | 89 | } | ||
97 | 90 | | |||
98 | auto result = store.modify(*object).exec(); | 91 | auto result = store.modify(*object).exec(); | ||
99 | result.waitForFinished(); | 92 | result.waitForFinished(); | ||
100 | if (result.errorCode()) { | 93 | if (result.errorCode()) { | ||
101 | state.printError(QObject::tr("An error occurred while modifying the resource %1: %2").arg(resourceId).arg(result.errorMessage()), | 94 | state.printError(QObject::tr("An error occurred while modifying the resource %1: %2").arg(resourceId).arg(result.errorMessage()), | ||
102 | "akonaid_modify_e" + QString::number(result.errorCode())); | 95 | "akonaid_modify_e" + QString::number(result.errorCode())); | ||
103 | } | 96 | } | ||
104 | 97 | | |||
105 | return true; | 98 | return true; | ||
106 | } | 99 | } | ||
107 | 100 | | |||
108 | | ||||
109 | Syntax::List syntax() | 101 | Syntax::List syntax() | ||
110 | { | 102 | { | ||
111 | Syntax modify("modify", QObject::tr("Modify items in a resource"), &SinkModify::modify); | 103 | Syntax modify("modify", QObject::tr("Modify items in a resource"), &SinkModify::modify); | ||
104 | modify.addPositionalArgument({ .name = "type", .help = "The type of entity to modify (mail, event, etc.)" }); | ||||
105 | modify.addPositionalArgument({ .name = "resourceId", .help = "The ID of the resource containing the entity" }); | ||||
106 | modify.addPositionalArgument({ .name = "objectId", .help = "The ID of the entity" }); | ||||
107 | modify.addPositionalArgument( | ||||
108 | { .name = "key value", .help = "Attributes and values to modify", .required = false, .variadic = true }); | ||||
109 | | ||||
112 | Syntax resource("resource", QObject::tr("Modify a resource"), &SinkModify::resource);//, Syntax::EventDriven); | 110 | Syntax resource("resource", QObject::tr("Modify a resource"), &SinkModify::resource);//, Syntax::EventDriven); | ||
111 | | ||||
112 | resource.addPositionalArgument({ .name = "id", .help = "The ID of the resource" }); | ||||
113 | resource.addPositionalArgument( | ||||
114 | { .name = "key value", .help = "Attributes and values to modify", .required = false, .variadic = true }); | ||||
115 | | ||||
113 | resource.completer = &SinkshUtils::resourceOrTypeCompleter; | 116 | resource.completer = &SinkshUtils::resourceOrTypeCompleter; | ||
114 | modify.children << resource; | 117 | modify.children << resource; | ||
115 | 118 | | |||
116 | return Syntax::List() << modify; | 119 | return Syntax::List() << modify; | ||
117 | } | 120 | } | ||
118 | 121 | | |||
119 | REGISTER_SYNTAX(SinkModify) | 122 | REGISTER_SYNTAX(SinkModify) | ||
120 | 123 | | |||
121 | } | 124 | } |