bool MailerGenerator::generate() const { if (actionList.isEmpty()) { qCritical("Bad parameter: Action name empty"); return false; } QStringList files; FileWriter fwh(dstDir.filePath(mailerName.toLower() + "mailer.h")); FileWriter fws(dstDir.filePath(mailerName.toLower() + "mailer.cpp")); // Generates a mailer header file QString act; for (QStringListIterator i(actionList); i.hasNext(); ) { act.append(" void ").append(i.next()).append("();\n"); } QString code = QString(MAILER_HEADER_FILE_TEMPLATE).arg(mailerName.toUpper(), mailerName, act); fwh.write(code, false); files << fwh.fileName(); // Generates a mailer source code QString actimpl; for (QStringListIterator i(actionList); i.hasNext(); ) { actimpl.append("void ").append(mailerName).append("Mailer::").append(i.next()).append("()\n{\n //\n // write codes\n //\n\n deliver(\"mail\");\n}\n\n"); } code = QString(MAILER_SOURCE_FILE_TEMPLATE).arg(mailerName.toLower(), actimpl); fws.write(code, false); files << fws.fileName(); // Updates a project file ProjectFileGenerator progen(dstDir.filePath("controllers.pro")); return progen.add(files); }
bool ControllerGenerator::generate(const QString &dstDir) const { // Reserved word check if (ngCtlrName()->contains(tableName.toLower())) { qCritical("Reserved word error. Please use another word. Controller name: %s", qPrintable(tableName)); return false; } QDir dir(dstDir); QStringList files; FileWriter fwh(dir.filePath(controllerName.toLower() + "controller.h")); FileWriter fws(dir.filePath(controllerName.toLower() + "controller.cpp")); if (actionList.isEmpty()) { if (fieldList.isEmpty()) { qCritical("Incorrect parameters."); return false; } QPair<QString, QVariant::Type> pair; if (primaryKeyIndex >= 0) pair = fieldList[primaryKeyIndex]; // Generates a controller source code QString sessInsertStr; QString sessGetStr; QString revStr; QString varName = enumNameToVariableName(controllerName); // Generates a controller header file QString code = QString(CONTROLLER_HEADER_FILE_TEMPLATE).arg(controllerName.toUpper(), controllerName, fieldNameToVariableName(pair.first)); fwh.write(code, false); files << fwh.fileName(); if (lockRevIndex >= 0) { sessInsertStr = QString(" session().insert(\"%1_lockRevision\", model.lockRevision());\n").arg(varName); sessGetStr = QString(" int rev = session().value(\"%1_lockRevision\").toInt();\n").arg(varName); revStr = QLatin1String(", rev"); } code = QString(CONTROLLER_SOURCE_FILE_TEMPLATE).arg(controllerName.toLower(), controllerName, varName, convMethod()->value(pair.second).arg(fieldNameToVariableName(pair.first)), sessInsertStr, sessGetStr, revStr, fieldNameToVariableName(pair.first)); fws.write(code, false); files << fws.fileName(); } else { // Generates a controller header file QString actions; for (QStringListIterator i(actionList); i.hasNext(); ) { actions.append(" void ").append(i.next()).append("();\n"); } QString code = QString(CONTROLLER_TINY_HEADER_FILE_TEMPLATE).arg(controllerName.toUpper(), controllerName, actions); fwh.write(code, false); files << fwh.fileName(); // Generates a controller source code QString actimpl; for (QStringListIterator i(actionList); i.hasNext(); ) { actimpl.append("void ").append(controllerName).append("Controller::").append(i.next()).append("()\n{\n // write code\n}\n\n"); } code = QString(CONTROLLER_TINY_SOURCE_FILE_TEMPLATE).arg(controllerName.toLower(), controllerName, actimpl); fws.write(code, false); files << fws.fileName(); } // Generates a project file ProjectFileGenerator progen(dir.filePath("controllers.pro")); return progen.add(files); }