aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/models/CategoryModel.java
blob: 77b08bfae2abfc112c9fd8cfd0e1ae6df75cb107 (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
package org.wordpress.android.models;

import android.content.ContentValues;
import android.database.Cursor;

/**
 * Represents WordPress post Category data and handles local database (de)serialization.
 */
public class CategoryModel {
    // Categories table column names
    public static final String ID_COLUMN_NAME = "ID";
    public static final String NAME_COLUMN_NAME = "name";
    public static final String SLUG_COLUMN_NAME = "slug";
    public static final String DESC_COLUMN_NAME = "description";
    public static final String PARENT_ID_COLUMN_NAME = "parent";
    public static final String POST_COUNT_COLUMN_NAME = "post_count";

    public int id;
    public String name;
    public String slug;
    public String description;
    public int parentId;
    public int postCount;
    public boolean isInLocalTable;

    public CategoryModel() {
        id = -1;
        name = "";
        slug = "";
        description = "";
        parentId = -1;
        postCount = 0;
        isInLocalTable = false;
    }

    /**
     * Sets data from a local database {@link Cursor}.
     */
    public void deserializeFromDatabase(Cursor cursor) {
        if (cursor == null) return;

        id = cursor.getInt(cursor.getColumnIndex(ID_COLUMN_NAME));
        name = cursor.getString(cursor.getColumnIndex(NAME_COLUMN_NAME));
        slug = cursor.getString(cursor.getColumnIndex(SLUG_COLUMN_NAME));
        description = cursor.getString(cursor.getColumnIndex(DESC_COLUMN_NAME));
        parentId = cursor.getInt(cursor.getColumnIndex(PARENT_ID_COLUMN_NAME));
        postCount = cursor.getInt(cursor.getColumnIndex(POST_COUNT_COLUMN_NAME));
        isInLocalTable = true;
    }

    /**
     * Creates the {@link ContentValues} object to store this category data in a local database.
     */
    public ContentValues serializeToDatabase() {
        ContentValues values = new ContentValues();
        values.put(ID_COLUMN_NAME, id);
        values.put(NAME_COLUMN_NAME, name);
        values.put(SLUG_COLUMN_NAME, slug);
        values.put(DESC_COLUMN_NAME, description);
        values.put(PARENT_ID_COLUMN_NAME, parentId);
        values.put(POST_COUNT_COLUMN_NAME, postCount);

        return values;
    }
}