File indexing completed on 2024-12-22 05:06:06

0001 /*
0002  * Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 
0020 #include <QCoreApplication>
0021 #include <QDebug>
0022 #include <QObject> // tr()
0023 #include <QModelIndex>
0024 #include <QTime>
0025 
0026 #include "common/resource.h"
0027 #include "common/storage.h"
0028 #include "common/resourceconfig.h"
0029 #include "common/log.h"
0030 #include "common/storage.h"
0031 #include "common/definitions.h"
0032 #include "common/propertyparser.h"
0033 
0034 #include "sinksh_utils.h"
0035 #include "state.h"
0036 #include "syntaxtree.h"
0037 
0038 using namespace Sink;
0039 
0040 namespace SinkCreate
0041 {
0042 
0043 Syntax::List syntax();
0044 
0045 bool create(const QStringList &allArgs, State &state)
0046 {
0047     if (allArgs.count() < 2) {
0048         state.printError(syntax()[0].usage());
0049         return false;
0050     }
0051 
0052     auto args = allArgs;
0053     auto type = args.takeFirst();
0054     auto &store = SinkshUtils::getStore(type);
0055     ApplicationDomain::ApplicationDomainType::Ptr object;
0056     auto resource = SinkshUtils::parseUid(args.takeFirst().toLatin1());
0057     object = store.getObject(resource);
0058 
0059     auto map = SinkshUtils::keyValueMapFromArgs(args);
0060     for (auto i = map.begin(); i != map.end(); ++i) {
0061         const auto property = i.key().toLatin1();
0062         object->setProperty(property, Sink::PropertyParser::parse(type.toLatin1(), property, i.value()));
0063     }
0064 
0065     auto result = store.create(*object).exec();
0066     result.waitForFinished();
0067     if (result.errorCode()) {
0068         state.printError(QObject::tr("An error occurred while creating the entity: %1").arg(result.errorMessage()),
0069                          "sink_create_e" + QString::number(result.errorCode()));
0070     }
0071 
0072     return true;
0073 }
0074 
0075 bool resource(const QStringList &args, State &state)
0076 {
0077     if (args.isEmpty()) {
0078         state.printError(QObject::tr("A resource can not be created without a type"), "sinkcreate/01");
0079         return false;
0080     }
0081 
0082     auto &store = SinkshUtils::getStore("resource");
0083 
0084     const auto resourceType = args.at(0);
0085 
0086     auto map = SinkshUtils::keyValueMapFromArgs(args);
0087 
0088     const auto identifier = SinkshUtils::parseUid(map.take("identifier").toLatin1());
0089 
0090     auto object = ApplicationDomain::ApplicationDomainType::createEntity<ApplicationDomain::SinkResource>("", identifier);
0091     object.setResourceType(resourceType.toLatin1());
0092 
0093     for (auto i = map.begin(); i != map.end(); ++i) {
0094         //FIXME we need a generic way to convert the value to the right type
0095         if (i.key() == ApplicationDomain::SinkResource::Account::name) {
0096             object.setAccount(SinkshUtils::parseUid(i.value().toUtf8()));
0097         } else {
0098             object.setProperty(i.key().toLatin1(), i.value());
0099         }
0100     }
0101 
0102     auto result = store.create(object).exec();
0103     result.waitForFinished();
0104     if (result.errorCode()) {
0105         state.printError(QObject::tr("An error occurred while creating the entity: %1").arg(result.errorMessage()),
0106                          "sink_create_e" + QString::number(result.errorCode()));
0107     }
0108 
0109     return true;
0110 }
0111 
0112 bool account(const QStringList &args, State &state)
0113 {
0114     if (args.isEmpty()) {
0115         state.printError(QObject::tr("An account can not be created without a type"), "sinkcreate/01");
0116         return false;
0117     }
0118 
0119     auto &store = SinkshUtils::getStore("account");
0120 
0121     auto type = args.at(0);
0122 
0123     auto map = SinkshUtils::keyValueMapFromArgs(args);
0124 
0125     auto identifier = map.take("identifier").toLatin1();
0126 
0127     auto object = ApplicationDomain::ApplicationDomainType::createEntity<ApplicationDomain::SinkAccount>("", identifier);
0128     object.setAccountType(type);
0129 
0130     for (auto i = map.begin(); i != map.end(); ++i) {
0131         object.setProperty(i.key().toLatin1(), i.value());
0132     }
0133 
0134     auto result = store.create(object).exec();
0135     result.waitForFinished();
0136     if (result.errorCode()) {
0137         state.printError(QObject::tr("An error occurred while creating the entity: %1").arg(result.errorMessage()),
0138                          "sink_create_e" + QString::number(result.errorCode()));
0139     }
0140 
0141     return true;
0142 }
0143 
0144 bool identity(const QStringList &args, State &state)
0145 {
0146     auto &store = SinkshUtils::getStore("identity");
0147 
0148     auto map = SinkshUtils::keyValueMapFromArgs(args);
0149 
0150     auto identifier = map.take("identifier").toLatin1();
0151 
0152     auto object = ApplicationDomain::ApplicationDomainType::createEntity<ApplicationDomain::Identity>("", identifier);
0153 
0154     for (auto i = map.begin(); i != map.end(); ++i) {
0155         //FIXME we need a generic way to convert the value to the right type
0156         if (i.key() == ApplicationDomain::Identity::Account::name) {
0157             object.setAccount(SinkshUtils::parseUid(i.value().toUtf8()));
0158         } else {
0159             object.setProperty(i.key().toLatin1(), i.value());
0160         }
0161     }
0162 
0163     auto result = store.create(object).exec();
0164     result.waitForFinished();
0165     if (result.errorCode()) {
0166         state.printError(QObject::tr("An error occurred while creating the entity: %1").arg(result.errorMessage()),
0167                          "sink_create_e" + QString::number(result.errorCode()));
0168     }
0169 
0170     return true;
0171 }
0172 
0173 Syntax::List syntax()
0174 {
0175     Syntax::List syntax;
0176 
0177     Syntax create("create", QObject::tr("Create items in a resource"), &SinkCreate::create);
0178     create.addPositionalArgument({"type", "The type of entity to create (mail, event, etc.)"});
0179     create.addPositionalArgument({"resourceId", "The ID of the resource that will contain the new entity"});
0180     create.addPositionalArgument({"key value", "Content of the entity", false, true});
0181 
0182     Syntax resource("resource", QObject::tr("Creates a new resource"), &SinkCreate::resource);
0183     resource.addPositionalArgument({"type", "The type of resource to create" });
0184     resource.addPositionalArgument({"key value", "Content of the resource", false, true});
0185 
0186     Syntax account("account", QObject::tr("Creates a new account"), &SinkCreate::account);
0187     account.addPositionalArgument({"type", "The type of account to create" });
0188     account.addPositionalArgument({"key value", "Content of the account", false, true});
0189 
0190     Syntax identity("identity", QObject::tr("Creates a new identity"), &SinkCreate::identity);
0191     identity.addPositionalArgument({"key value", "Content of the identity", false, true});
0192 
0193     create.children << resource;
0194     create.children << account;
0195     create.children << identity;
0196 
0197     syntax << create;
0198     return syntax;
0199 }
0200 
0201 REGISTER_SYNTAX(SinkCreate)
0202 
0203 }