14
14
public bool isDeleted {get; set;}
16
16
public IBackend backend {get; set;}
17
public abstract int instanceOf { get; }
20
// public signal void created ( bool status );
21
// public signal void deleted ( bool status );
22
// public signal void updated ( bool status );
18
private CouchDB.StructField _annotations;
24
19
protected CouchDB.Document document;
25
this.name = "Untitled Item";
32
27
this.createdAt = Utils.DateTime.newTimeNow();
33
28
this.updatedAt = Utils.DateTime.newTimeNow();
35
30
/*temporary stored in memory*/
36
31
this.isDeleted = false;
47
42
public CouchDBItem (IBackend aBackend, CouchDB.Session couchdb){
48
43
this.backend = aBackend;
49
44
this.document = new CouchDB.Document(couchdb);
52
48
public CouchDBItem.fromDocument (IBackend aBackend, CouchDB.Document aDocument){
53
49
this.backend = aBackend;
54
50
this.document = aDocument;
55
CouchDBBackend.fillItemFieldsFromDocument(this, aDocument);
51
this.syncItemFromDocument();
58
public abstract void push();
54
/* sync Item using document at source */
55
public void syncItemFromDocument(){
56
this.id = this.document.get_id();
58
if (document.has_field(CouchDBBackend.FIELD_NAME)) {
59
this.name = this.document.get_string_field(CouchDBBackend.FIELD_NAME);
62
if (document.has_field(CouchDBBackend.FIELD_TEXT)) {
63
this.text = this.document.get_string_field(CouchDBBackend.FIELD_TEXT);
66
if (document.has_field(CouchDBBackend.FIELD_CREATED_AT)) {
67
this.createdAt = Utils.DateTime.parseTime(
68
this.document.get_string_field(CouchDBBackend.FIELD_CREATED_AT),
72
if (document.has_field(CouchDBBackend.FIELD_UPDATED_AT)) {
73
this.updatedAt = Utils.DateTime.parseTime(
74
this.document.get_string_field(CouchDBBackend.FIELD_UPDATED_AT),
77
if (this.getAnnotations().has_field(CouchDBBackend.FIELD_WIDTH)) {
78
this.width = this.getAnnotations().get_int_field(
79
CouchDBBackend.FIELD_WIDTH);
82
if (this.getAnnotations().has_field(CouchDBBackend.FIELD_HEIGHT)) {
83
this.height = this.getAnnotations().get_int_field(
84
CouchDBBackend.FIELD_HEIGHT);
88
/* sync document using item at source */
89
public void syncDocumentFromItem(){
91
this.document.set_string_field(
92
CouchDBBackend.FIELD_NAME, this.name);
93
this.document.set_string_field(
94
CouchDBBackend.FIELD_TEXT, this.text);
96
this.document.set_string_field(
97
CouchDBBackend.FIELD_UPDATED_AT,
98
this.updatedAt.format("%Y-%m-%dT%H:%M:%S"));
99
this.document.set_string_field(
100
CouchDBBackend.FIELD_CREATED_AT,
101
this.createdAt.format("%Y-%m-%dT%H:%M:%S"));
103
this.getAnnotations().set_int_field(
104
CouchDBBackend.FIELD_WIDTH, this.width);
105
this.getAnnotations().set_int_field(
106
CouchDBBackend.FIELD_HEIGHT, this.height);
109
/* Deletes this item from the database */
60
110
public void delete (){
70
120
this.isDeleted = true;
71
121
this.updated(true);
75
public void update (){
125
/* Stores the item in the database */
126
public virtual void push() {
76
129
this.updatedAt = Utils.DateTime.newTimeNow();
130
this.syncDocumentFromItem();
132
string database = "UNKNOWN DATABASE";
136
database = CouchDBBackend.DB_NOTES;
137
} else if (this is ITask) {
138
database = CouchDBBackend.DB_NOTES;
139
} else if (this is IProject) {
140
database = CouchDBBackend.DB_LUCRURI;
141
} else if (this is ICategory) {
142
database = CouchDBBackend.DB_LUCRURI;
144
this.log.debug("Don't know where should I put this document.");
147
result = document.put(database);
148
} catch (GLib.Error e) {
149
this.log.debug("Failed to put document in %s: %s".printf(database, e.message));
154
if (this.id == null){
155
this.id = this.document.get_id();
161
if (this.id == null){
169
public CouchDB.StructField getAnnotations() {
171
if (this._annotations != null) {
172
return this._annotations;
174
CouchDB.StructField application_annotations;
176
application_annotations = this.document.get_struct_field(
177
CouchDBBackend.FIELD_APPLICATION_ANNOTATIONS);
178
if (application_annotations == null) {
179
application_annotations = new CouchDB.StructField.from_string(
180
CouchDBBackend.STRING_ANNOTATIONS);
181
this.document.set_struct_field(
182
CouchDBBackend.FIELD_APPLICATION_ANNOTATIONS,
183
application_annotations);
186
CouchDB.StructField lucruri_annotations;
187
lucruri_annotations = application_annotations.get_struct_field(
188
CouchDBBackend.FIELD_LUCRURI);
189
if (lucruri_annotations == null) {
190
lucruri_annotations = new CouchDB.StructField();
191
application_annotations.set_struct_field(
192
CouchDBBackend.FIELD_LUCRURI,
193
lucruri_annotations);
195
this._annotations = lucruri_annotations;
196
return this._annotations;
199
// FIXME delete this from IItem
200
public void update() {