com.github.leonardoxh.temporeal.app.model.User.java Source code

Java tutorial

Introduction

Here is the source code for com.github.leonardoxh.temporeal.app.model.User.java

Source

/*
 * Copyright 2014 Leonardo Rossetto
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.leonardoxh.temporeal.app.model;

import android.os.Parcel;
import android.os.Parcelable;

import com.fasterxml.jackson.annotation.JsonProperty;

import com.google.android.gms.plus.model.people.Person;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * Temos de espelhar a mesma classe com as mesmas propertys que estao no servidor
 * como estamos trabalhando com um modelo restful temos que fazer isso
 * para que os dois lados se comuniquem corretamente.
 * Vamos implementar tambem a interface Parcelable para podermos passar o nosso usuario
 * atraves de nossos servicos e activity's mais facilmente
 */
@DatabaseTable(tableName = "User")
public class User extends Model implements Parcelable {

    /**
     * Esta constante e requerida pela interface parcelable para
     * podermos resgatar os valores de nosso modelo novamente
     */
    public static final Creator<User> CREATOR = new Creator<User>() {

        @Override
        public User createFromParcel(Parcel parcel) {
            return new User(parcel);
        }

        @Override
        public User[] newArray(int i) {
            return new User[i];
        }

    };

    public static final String COLUMN_NAME_PLUS_ID = "plus_id";
    public static final String COLUMN_NAME_DISPLAY_NAME = "display_name";
    public static final String COLUMN_NAME_PROFILE_URL = "profile_url";
    public static final String COLUMN_NAME_PROFILE_IMAGE = "profile_image";

    @JsonProperty("email")
    @DatabaseField(columnName = COLUMN_NAME_PLUS_ID, canBeNull = false)
    private String plusId;

    @JsonProperty("name")
    @DatabaseField(columnName = COLUMN_NAME_DISPLAY_NAME, canBeNull = false)
    private String displayName;

    @JsonProperty("profileUrl")
    @DatabaseField(columnName = COLUMN_NAME_PROFILE_URL, canBeNull = false)
    private String profileUrl;

    @JsonProperty("profilePicture")
    @DatabaseField(columnName = COLUMN_NAME_PROFILE_IMAGE, canBeNull = false)
    private String profileImage;

    /**
     * Este construtor vazio e para o uso do ORMLite
     */
    public User() {

    }

    /**
     * E tambem temos este construtor para ja iniciarmos a
     * pessoa com o Person dela do Google Plus
     * @param person o objeto que e retornado do Google Plus apos a autenticacao
     */
    public User(Person person) {
        setPlusId(person.getId());
        setDisplayName(person.getDisplayName());
        setProfileUrl(person.getUrl());
        setProfileImage(person.getImage().getUrl());
    }

    /**
     * Este construtor vai resgatar o usuario de um parcelable para nos
     * @param parcel o parcelable para resgatarmos os valores
     */
    public User(Parcel parcel) {
        setId(parcel.readLong());
        setServerId(parcel.readLong());
        setPlusId(parcel.readString());
        setDisplayName(parcel.readString());
        setProfileUrl(parcel.readString());
        setProfileImage(parcel.readString());
    }

    public void setPlusId(String plusId) {
        this.plusId = plusId;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public void setProfileUrl(String profileUrl) {
        this.profileUrl = profileUrl;
    }

    public void setProfileImage(String profileImage) {
        this.profileImage = profileImage;
    }

    public String getPlusId() {
        return plusId;
    }

    public String getDisplayName() {
        return displayName;
    }

    public String getProfileUrl() {
        return profileUrl;
    }

    public String getProfileImage() {
        return profileImage;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int flags) {
        parcel.writeLong(getId() == null ? 0L : getId());
        parcel.writeLong(getServerId() == null ? 0L : getServerId());
        parcel.writeString(getPlusId());
        parcel.writeString(getDisplayName());
        parcel.writeString(getProfileUrl());
        parcel.writeString(getProfileImage());
    }

}