Skip to content

Commit

Permalink
Initial filestore realizations. #24
Browse files Browse the repository at this point in the history
  • Loading branch information
rostgaard committed Feb 23, 2016
1 parent 12ab61b commit b2627d9
Show file tree
Hide file tree
Showing 5 changed files with 659 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/filestore.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* This file is part of OpenReception
Copyright (C) 2016-, BitStackers K/S
This is free software; you can redistribute it and/or modify it
under terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version. This software is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License along with
this program; see the file COPYING3. If not, see http://www.gnu.org/licenses.
*/

library openreception.filestore;

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:logging/logging.dart';

import 'model.dart' as model;
import 'storage.dart' as storage;

part 'filestore/filestore-git_engine.dart';
part 'filestore/filestore-ivr.dart';
part 'filestore/filestore-reception_dialplan.dart';

const String libraryName = 'openreception.filestore';

final JsonEncoder _jsonpp = new JsonEncoder.withIndent(' ');

final model.User _systemUser = new model.User.empty()
..name = 'System'
..address = 'root@localhost';

/**
* Generate an author string.
*/
String _authorString(model.User user) =>
new HtmlEscape(HtmlEscapeMode.ATTRIBUTE).convert('${user.name}') +
' <${user.address}>';
122 changes: 122 additions & 0 deletions lib/filestore/filestore-calendar.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* This file is part of OpenReception
Copyright (C) 2016-, BitStackers K/S
This is free software; you can redistribute it and/or modify it
under terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version. This software is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the GNU General Public License along with
this program; see the file COPYING3. If not, see http://www.gnu.org/licenses.
*/

part of openreception.filestore;

class Calendar implements storage.Calendar {
final Logger _log = new Logger('$libraryName.Calendar');
final String path;
GitEngine _git;

Future get initialized => _git.initialized;
Future get ready => _git.whenReady;

/**
*
*/
Ivr({String this.path: 'json-data/ivr'}) {
_git = new GitEngine(path);
_git.init();
}

/**
*
*/
Future<model.IvrMenu> create(model.IvrMenu menu, [model.User user]) async {
final File file = new File('$path/${menu.name}.json');

if (file.existsSync()) {
throw new storage.ClientError(
'File already exists, please update instead');
}

/// Set the user
if (user == null) {
user = _systemUser;
}

file.writeAsStringSync(_jsonpp.convert(menu));

await _git.add(file, 'Added ${menu.name}', _authorString(user));

return menu;
}

/**
*
*/
Future<model.IvrMenu> get(String menuName) async {
final File file = new File('$path/${menuName}.json');

if (!file.existsSync()) {
throw new storage.NotFound('No file with name ${menuName}');
}

try {
final model.IvrMenu menu =
model.IvrMenu.decode(JSON.decode(file.readAsStringSync()));
return menu;
} catch (e) {
throw e;
}
}

/**
*
*/
Future<Iterable<model.IvrMenu>> list() async => new Directory(path)
.listSync()
.where((fse) => fse is File && fse.path.endsWith('.json'))
.map((File fse) =>
model.IvrMenu.decode(JSON.decode(fse.readAsStringSync())));

/**
*
*/
Future<model.IvrMenu> update(model.IvrMenu menu, [model.User user]) async {
final File file = new File('$path/${menu.name}.json');

if (!file.existsSync()) {
throw new storage.NotFound();
}

/// Set the user
if (user == null) {
user = _systemUser;
}

file.writeAsStringSync(_jsonpp.convert(menu));

await _git._commit('Updated ${menu.name}', _authorString(user));

return menu;
}

/**
*
*/
Future remove(String menuName, [model.User user]) async {
final File file = new File('$path/${menuName}.json');

if (!file.existsSync()) {
throw new storage.NotFound();
}

/// Set the user
if (user == null) {
user = _systemUser;
}

await _git.remove(file, 'Removed $menuName', _authorString(user));
}
}
Loading

0 comments on commit b2627d9

Please sign in to comment.