summaryrefslogtreecommitdiff
path: root/core/src/Model/Project.cpp
blob: 3cb3cb4fe0cf11fd2aa73777b53702277ebfa0f0 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "Project.hpp"

#include "Model/Workflow/Workflow.hpp"
#include "Utils/Macros.hpp"

#include <json/reader.h>
#include <json/value.h>
#include <json/writer.h>
#include <filesystem>
#include <fstream>
#include <stdexcept>
#include <utility>

namespace fs = std::filesystem;

template <class T>
void ReadItemList(ItemList<T>& list, const fs::path& filePath)
{
	std::ifstream ifs(filePath);
	if (ifs) {
		Json::Value root;
		ifs >> root;

		list = ItemList<T>(root);
	}
}

Project::Project(fs::path rootPath)
	: mRootPath{ std::move(rootPath) }
	, mRootPathString{ mRootPath.string() }
	, Database(*this)
{
	// TODO better diagnostic
	const char* kInvalidFormatErr = "Failed to load project: invalid format.";

	std::ifstream ifs(mRootPath / "cplt_project.json");
	if (!ifs) {
		std::string message;
		message += "Failed to load project file at '";
		message += mRootPath.string();
		message += "'.";
		throw std::runtime_error(message);
	}

	{
		Json::Value root;
		ifs >> root;

		const auto& croot = root; // Use const reference so that accessors default to returning a null if not found, instead of silently creating new elements
		if (!croot.isObject()) {
			throw std::runtime_error(kInvalidFormatErr);
		}

		if (auto& name = croot["Name"]; name.isString()) {
			mName = name.asString();
		} else {
			throw std::runtime_error(kInvalidFormatErr);
		}
	}

	auto itemsDir = mRootPath / "items";
	ReadItemList(Products, itemsDir / "products.json");
	ReadItemList(Factories, itemsDir / "factories.json");
	ReadItemList(Customers, itemsDir / "customers.json");

	auto workflowsDir = mRootPath / "workflows";
	fs::create_directories(workflowsDir);

	for (auto& entry : fs::directory_iterator(workflowsDir)) {
		if (!entry.is_regular_file()) continue;
		auto& path = entry.path();
		if (path.extension() != ".cplt-workflow") continue;

		auto name = path.stem().string();
		auto [it, DISCARD] = mWorkflows.insert(name, WorkflowInfo{});
		auto& info = it.value();

		info.Name = std::move(name);
		info.PathStringCache = path.string();
		info.Path = path;
	}
}

Project::Project(fs::path rootPath, std::string name)
	: mRootPath{ std::move(rootPath) }
	, mRootPathString{ mRootPath.string() }
	, mName{ std::move(name) }
	, Database(*this)
{
}

const fs::path& Project::GetPath() const
{
	return mRootPath;
}

const std::string& Project::GetPathString() const
{
	return mRootPathString;
}

fs::path Project::GetDatabasesDirectory() const
{
	return mRootPath / "databases";
}

fs::path Project::GetItemsDirectory() const
{
	return mRootPath / "items";
}

fs::path Project::GetWorkflowsDirectory() const
{
	return mRootPath / "workflows";
}

fs::path Project::GetWorkflowPath(std::string_view name) const
{
	return (mRootPath / "workflows" / name).concat(".cplt-workflow");
}

fs::path Project::GetTemplatesDirectory() const
{
	return mRootPath / "templates";
}

fs::path Project::GetTemplatePath(std::string_view name) const
{
	return (mRootPath / "templates" / name).concat(".cplt-template");
}

const std::string& Project::GetName() const
{
	return mName;
}

void Project::SetName(std::string name)
{
	mName = std::move(name);
}

const tsl::array_map<char, WorkflowInfo>& Project::GetWorkflows() const
{
	return mWorkflows;
}

std::unique_ptr<Workflow> Project::LoadWorkflow(std::string_view name)
{
	auto iter = mWorkflows.find(name);
	if (iter == mWorkflows.end()) {
		return iter.value().LoadFromDisk();
	} else {
		return nullptr;
	}
}

std::unique_ptr<Workflow> Project::CreateWorkflow(std::string_view name)
{
	if (mWorkflows.find(name) != mWorkflows.end()) {
		// Workflow with name already exists
		return nullptr;
	}

	auto workflow = std::make_unique<Workflow>();
	auto [it, DISCARD] = mWorkflows.insert(
		name,
		WorkflowInfo{
			.Path = GetWorkflowPath(name),
			.Name = std::string(name),
		});

	return workflow;
}

bool Project::RemoveWorkflow(std::string_view name)
{
	auto iter = mWorkflows.find(name);
	if (iter == mWorkflows.end()) {
		return false;
	}
	auto& info = iter.value();

	fs::remove(info.Path);
	mWorkflows.erase(iter);

	return true;
}

bool Project::RenameWorkflow(std::string_view oldName, std::string_view newName)
{
	auto iter = mWorkflows.find(oldName);
	if (iter == mWorkflows.end()) return false;

	auto info = std::move(iter.value());

	auto& oldPath = info.Path;
	auto newPath = GetWorkflowPath(newName);
	fs::rename(oldPath, newPath);
	info.Path = std::move(newPath);

	mWorkflows.insert(newName, std::move(info));
	mWorkflows.erase(iter);

	return true;
}

const tsl::array_map<char, TemplateInfo>& Project::GetTemplates() const
{
	return mTemplates;
}

std::unique_ptr<Template> Project::LoadTemplate(std::string_view name)
{
	auto iter = mTemplates.find(name);
	if (iter == mTemplates.end()) {
		return iter.value().LoadFromDisk();
	} else {
		return nullptr;
	}
}

TemplateInfo* Project::InsertTemplate(std::string_view name, TemplateInfo info)
{
	auto [it, inserted] = mTemplates.insert(name, info);
	if (inserted) {
		return &it.value();
	} else {
		return nullptr;
	}
}

bool Project::RemoveTemplate(std::string_view name)
{
	auto iter = mTemplates.find(name);
	if (iter == mTemplates.end()) {
		return false;
	}
	auto& info = iter.value();

	fs::remove(info.Path);
	mTemplates.erase(iter);

	return true;
}

bool Project::RenameTemplate(std::string_view oldName, std::string_view newName)
{
	auto iter = mTemplates.find(oldName);
	if (iter == mTemplates.end()) return false;

	auto info = std::move(iter.value());

	auto& oldPath = info.Path;
	auto newPath = GetTemplatePath(newName);
	fs::rename(oldPath, newPath);
	info.Path = std::move(newPath);

	mTemplates.insert(newName, std::move(info));
	mTemplates.erase(iter);

	return true;
}

Json::Value Project::Serialize()
{
	Json::Value root(Json::objectValue);

	root["Name"] = mName;

	return root;
}

template <class T>
static void WriteItemList(ItemList<T>& list, const fs::path& filePath)
{
	std::ofstream ofs(filePath);
	ofs << list.Serialize();
}

void Project::WriteToDisk()
{
	std::ofstream ofs(mRootPath / "cplt_project.json");
	ofs << this->Serialize();

	auto itemsDir = mRootPath / "items";
	fs::create_directories(itemsDir);

	WriteItemList(Products, itemsDir / "products.json");
	WriteItemList(Factories, itemsDir / "factories.json");
	WriteItemList(Customers, itemsDir / "customers.json");
}