/*
 * Copyright (c) 2009-2010 DuraSpace. All rights reserved.
 */
package org.duracloud.account.common.domain;

import java.util.Date;

/**
 * @author: Bill Branan Date: Dec 2, 2010
 */
public class UserInvitation extends BaseDomainData implements Comparable<UserInvitation> {

    private int accountId;
    private String adminUsername;
    private String userEmail;
    private Date creationDate;
    private Date expirationDate;
    private String redemptionCode;

    public UserInvitation(
        int id, int accountId, String adminUsername, String userEmail, int expirationDays,
        String redemptionCode) {
        this(id, accountId, adminUsername, userEmail, expirationDays, redemptionCode, 0);
    }

    public UserInvitation(
        int id, int accountId, String adminUsername, String userEmail, int expirationDays,
        String redemptionCode, int counter) {
        this.id = id;
        this.accountId = accountId;
        this.adminUsername = adminUsername;
        this.userEmail = userEmail;

        this.creationDate = new Date();

        // milliseconds until expiration (days * millis in a day)
        long expMillis = expirationDays * 86400000;
        this.expirationDate = new Date(creationDate.getTime() + expMillis);

        this.redemptionCode = redemptionCode;
        this.counter = counter;
    }

    public UserInvitation(
        int id, int accountId, String adminUsername, String userEmail, Date creationDate,
        Date expirationDate, String redemptionCode, int counter) {
        this.id = id;
        this.accountId = accountId;
        this.adminUsername = adminUsername;
        this.userEmail = userEmail;
        this.creationDate = creationDate;
        this.expirationDate = expirationDate;
        this.redemptionCode = redemptionCode;
        this.counter = counter;
    }

    public int getAccountId() {
        return accountId;
    }

    public String getAdminUsername() {
        return adminUsername;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public Date getCreationDate() {
        return creationDate;
    }

    public Date getExpirationDate() {
        return expirationDate;
    }

    public String getRedemptionCode() {
        return redemptionCode;
    }

    public String getRedemptionURL() {
        return AmaEndpoint.getUrl() + "/users/redeem/" + getRedemptionCode();
    }

    public String getSubject() {
        return "DuraCloud Account Invitation";
    }

    public String getBody() {
        StringBuilder sb = new StringBuilder();

        sb.append("You have been invited to join a DuraCloud account and ");
        sb.append("receive access to a DuraCloud instance. In order to join, ");
        sb.append("please follow these instructions:");
        sb.append("\n");
        sb.append("1. If you do not have a personal user profile with ");
        sb.append("DuraCloud, please create one at: ");
        sb.append("https://manage.duracloud.org/ama/users/new. ");
        sb.append("If you already have a personal user profile with ");
        sb.append("DuraCloud, you may skip this step.");
        sb.append("\n");
        sb.append("2. Click on this link to log in to the DuraCloud ");
        sb.append("Management Console: ");
        sb.append(getRedemptionURL());
        sb.append("\n");
        sb.append("3. After logging in, you should see the details for the ");
        sb.append("DuraCloud account that you now have access to.");
        sb.append("\n");
        sb.append("\n");
        sb.append("If you have any issues accepting the invitation or ");
        sb.append("creating your personal user profile, please contact the ");
        sb.append("DuraCloud team by emailing your issue to ");
        sb.append("support@duracloud.org.");
        sb.append("\n");
        sb.append("\n");
        sb.append("The DuraCloud team");

        return sb.toString();
    }

    @Override
    public int compareTo(UserInvitation invitation) {
        if(this.id == invitation.id) {
            if(this.equals(invitation)) {
               return 0;
            } else {
                return this.getUserEmail().compareTo(invitation.getUserEmail());
            }
        } else if(this.id > invitation.id) {
            return 1;
        } else {
            return -1;
        }
    }

    /*
    * Generated by IntelliJ
    */
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        UserInvitation that = (UserInvitation) o;

        if (accountId != that.accountId) {
            return false;
        }
        if (adminUsername != that.adminUsername) {
            return false;
        }
        if (creationDate != null ? !creationDate.equals(that.creationDate) :
            that.creationDate != null) {
            return false;
        }
        if (expirationDate != null ? !expirationDate
            .equals(that.expirationDate) : that.expirationDate != null) {
            return false;
        }
        if (redemptionCode != null ? !redemptionCode
            .equals(that.redemptionCode) : that.redemptionCode != null) {
            return false;
        }
        if (userEmail != null ? !userEmail.equals(that.userEmail) :
            that.userEmail != null) {
            return false;
        }

        return true;
    }

    /*
     * Generated by IntelliJ
     */    
    @Override
    public int hashCode() {
        int result = accountId;
        result = 31 * result + (adminUsername != null ? adminUsername.hashCode() : 0);
        result = 31 * result + (userEmail != null ? userEmail.hashCode() : 0);
        result =
            31 * result + (creationDate != null ? creationDate.hashCode() : 0);
        result = 31 * result +
            (expirationDate != null ? expirationDate.hashCode() : 0);
        result = 31 * result +
            (redemptionCode != null ? redemptionCode.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "UserInvitation[id="
            + id + ", accountId=" + accountId + ", adminUsername=" + adminUsername
            + ", userEmail=" + userEmail
            + ", creationDate=" + creationDate + ", expirationDate="
            + expirationDate + ", redemptionCode=" + redemptionCode
            + ", counter=" + counter + "]";

    }
}

@@ -4333,24 +4333,25 @@
 %0A    /*%0A    
+ 
 * Generated 
@@ -4358,24 +4358,25 @@
 by IntelliJ%0A
+ 
     */%0A    @
@@ -4737,26 +4737,97 @@
  != 
-that.adminUsername
+null ? !adminUsername.equals(that.adminUsername) :%0A            that.adminUsername != null
 ) %7B%0A
@@ -5556,20 +5556,16 @@
 %0A     */
-    
 %0A    @Ov

Fixes the equals() method in UserInvitation