~adiroiban/lucruri/trunk

« back to all changes in this revision

Viewing changes to src/CouchDBItem.vala

  • Committer: Adi Roiban
  • Date: 2010-04-11 18:03:06 UTC
  • Revision ID: adi@roiban.ro-20100411180306-qy5k3e2aq4i95nyz
Initial refactor for using CouchDBItem.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
public abstract class CouchDBItem : GLib.Object , IItem {
 
2
 
2
3
    /* properties */
3
 
 
4
4
    public string id {get; set;}
5
5
    public string name {get; set;}
6
6
    public string text {get; set;}
14
14
    public bool isDeleted {get; set;}
15
15
 
16
16
    public IBackend backend  {get; set;}
17
 
    public abstract int instanceOf { get; }
18
 
 
19
 
        /* signals */
20
 
//    public signal void created ( bool status );
21
 
//    public signal void deleted ( bool status );
22
 
//    public signal void updated ( bool status );
23
 
 
 
17
 
 
18
    private CouchDB.StructField _annotations;
24
19
    protected CouchDB.Document document;
25
 
 
26
 
    private Logger log;
 
20
    protected Logger log;
27
21
 
28
22
    construct {
29
23
        /*stored in db*/
30
 
        this.id = null;
 
24
        this.id = "";
 
25
        this.name = "Untitled Item";
31
26
        this.text = "";
32
27
        this.createdAt = Utils.DateTime.newTimeNow();
33
28
        this.updatedAt = Utils.DateTime.newTimeNow();
34
 
 
 
29
        
35
30
        /*temporary stored in memory*/
36
31
                this.isDeleted = false;
37
32
        this.height = 350;
47
42
    public CouchDBItem (IBackend aBackend, CouchDB.Session couchdb){
48
43
        this.backend = aBackend;
49
44
        this.document = new CouchDB.Document(couchdb);
 
45
        
50
46
        }
51
47
 
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();
56
52
        }
57
53
 
58
 
        public abstract void push();
59
 
 
 
54
    /* sync Item using document at source */
 
55
    public void syncItemFromDocument(){
 
56
        this.id = this.document.get_id();
 
57
 
 
58
        if (document.has_field(CouchDBBackend.FIELD_NAME)) {
 
59
            this.name = this.document.get_string_field(CouchDBBackend.FIELD_NAME);
 
60
        }
 
61
 
 
62
        if (document.has_field(CouchDBBackend.FIELD_TEXT)) {
 
63
            this.text = this.document.get_string_field(CouchDBBackend.FIELD_TEXT);
 
64
        }
 
65
 
 
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),
 
69
                "%Y-%m-%dT%T");
 
70
        }
 
71
 
 
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),
 
75
                "%Y-%m-%dT%T");
 
76
        }
 
77
        if (this.getAnnotations().has_field(CouchDBBackend.FIELD_WIDTH)) {
 
78
            this.width = this.getAnnotations().get_int_field(
 
79
                CouchDBBackend.FIELD_WIDTH);
 
80
        }
 
81
 
 
82
        if (this.getAnnotations().has_field(CouchDBBackend.FIELD_HEIGHT)) {
 
83
            this.height = this.getAnnotations().get_int_field(
 
84
                CouchDBBackend.FIELD_HEIGHT);
 
85
        }
 
86
    }
 
87
 
 
88
    /* sync document using item at source */
 
89
    public void syncDocumentFromItem(){
 
90
 
 
91
        this.document.set_string_field(
 
92
            CouchDBBackend.FIELD_NAME, this.name);
 
93
        this.document.set_string_field(
 
94
            CouchDBBackend.FIELD_TEXT, this.text);
 
95
 
 
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"));
 
102
 
 
103
        this.getAnnotations().set_int_field(
 
104
            CouchDBBackend.FIELD_WIDTH, this.width);
 
105
        this.getAnnotations().set_int_field(
 
106
            CouchDBBackend.FIELD_HEIGHT, this.height);
 
107
    }
 
108
 
 
109
    /* Deletes this item from the database */
60
110
    public void delete (){
61
111
        bool result;
62
112
        try {
69
119
        if (result) {
70
120
                    this.isDeleted = true;
71
121
                    this.updated(true);
72
 
                }    
 
122
                }
73
123
        }
74
124
 
75
 
    public void update (){
 
125
    /* Stores the item in the database */
 
126
    public virtual void push() {
 
127
        bool result;
 
128
 
76
129
        this.updatedAt = Utils.DateTime.newTimeNow();
 
130
        this.syncDocumentFromItem();
 
131
 
 
132
        string database = "UNKNOWN DATABASE";
 
133
        try {
 
134
 
 
135
            if (this is INote) {
 
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;
 
143
            } else {
 
144
                this.log.debug("Don't know where should I put this document.");
 
145
                return; 
 
146
            }
 
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));
 
150
            result = false;
 
151
        }
 
152
 
 
153
            if (result){
 
154
                if (this.id == null){
 
155
                    this.id = this.document.get_id();
 
156
                    this.created(true);
 
157
                } else {
 
158
                    this.updated(true);
 
159
               }
 
160
            } else {
 
161
                if (this.id == null){
 
162
                    this.created(false);
 
163
                } else {
 
164
                    this.updated(false);
 
165
                }
 
166
            }
 
167
        }
 
168
 
 
169
    public CouchDB.StructField getAnnotations() {
 
170
 
 
171
        if (this._annotations != null) {
 
172
            return this._annotations;
 
173
        }
 
174
        CouchDB.StructField application_annotations;
 
175
 
 
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);
 
184
        }
 
185
 
 
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);
 
194
        }
 
195
        this._annotations = lucruri_annotations;
 
196
        return this._annotations;
 
197
    }
 
198
 
 
199
    // FIXME delete this from IItem
 
200
    public void update() {
77
201
        this.push();
78
 
        }
 
202
    }
79
203
 
80
204
}