summaryrefslogtreecommitdiff
path: root/project2/console/consoleAppEngine.cpp
blob: d46d0911836c61032fc978904b35146bfd642ac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "consoleAppEngine.h"
#include "consoleEnvironment.h"
#include "../iterate.h"
#include <stdexcept>
#include <libxml/xinclude.h>

class ConsoleSession : public Session {
	public:
		ConsoleSession() : expiry(0)
		{
		}
		virtual ~ConsoleSession()
		{
		}

		virtual Glib::ustring GetValue(const Glib::ustring & name) const
		{
			Values::const_iterator i = vars.find(name);
			if (i == vars.end()) {
				throw Session::VariableNotFound();
			}
			return i->second;
		}
		virtual Values GetValuesCopy() const
		{
			return vars;
		}
		virtual void SetValue(const Glib::ustring & name, const Glib::ustring & value)
		{
			vars[name] = value;
		}
		virtual void ClearValue(const Glib::ustring & name)
		{
			vars.erase(name);
		}
		virtual time_t ExpiryTime() const
		{
			return expiry;
		}

	protected:
		virtual void ExpiryTime(time_t t)
		{
			expiry = t;
		}
	private:
		time_t expiry;
		Values vars;
};

ConsoleApplicationEngine::ConsoleApplicationEngine(const ConsoleEnvironment * env, const boost::filesystem::path & f) :
	ApplicationEngine(),
	_env(env),
	runtime(new ConsoleSession())
{
	xmlpp::DomParser request(f.string());
	while (xmlXIncludeProcessFlags(request.get_document()->cobj(), XML_PARSE_NOXINCNODE) > 0);
	xmlpp::Element * requestRoot = request.get_document()->get_root_node();

	LoaderBase loader;
	loader.supportedStorers.insert(Storer::into(&this->datasources));
	loader.supportedStorers.insert(Storer::into(&parameterChecks));
	loader.supportedStorers.insert(Storer::into(&tasks));
	loader.collectAll("project2", requestRoot, true, true);
}

ConsoleApplicationEngine::~ConsoleApplicationEngine()
{
}

void
ConsoleApplicationEngine::process() const
{
	BOOST_FOREACH(OrderedParamCheckers::value_type pc, parameterChecks) {
		if (!pc.second->performCheck()) {
			throw std::runtime_error("Check failed");
		}
	}
	try {
		BOOST_FOREACH(NoOutputExecutes::value_type t, tasks) {
			t.second->execute();
		}
		// Commit data source transactions (without invoking a connection)
		BOOST_FOREACH(DataSources::value_type ds, this->datasources) {
			ds.second->commit();
		}
	}
	catch (...) {
		// Do something about the error
		BOOST_FOREACH(DataSources::value_type ds, this->datasources) {
			ds.second->rollback();
		}
		throw;
	}
}

const Environment *
ConsoleApplicationEngine::env() const
{
	return _env;
}

SessionPtr
ConsoleApplicationEngine::session() const
{
	return runtime;
}

PresenterPtr
ConsoleApplicationEngine::getPresenter(const std::string & group, const std::string & id) const
{
	return PresenterPtr(new ConsolePresenter(this, group, id));
}

ConsoleApplicationEngine::ConsolePresenter::ConsolePresenter(const ConsoleApplicationEngine *,
		const std::string & group, const std::string & id) :
	Presenter(group, id)
{
}

ConsoleApplicationEngine::ConsolePresenter::~ConsolePresenter()
{
}

Presenter::XmlDocumentPtr
ConsoleApplicationEngine::ConsolePresenter::getDataDocument() const
{
	return Presenter::getDataDocument();
}