/**
 * Copyright (c) 2012-2015 Nord Trading Network.
 *
 * http://www.nordpos.mobi
 *
 * 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 mobi.nordpos.dao.model;

import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.openbravo.pos.util.Hashcypher;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;
import java.util.UUID;

/**
 * @author Andrey Svininykh <svininykh@gmail.com>
 */
public class Customer {

    public static final String ID = "ID";
    public static final String NAME = "NAME";
    public static final String SEARCHKEY = "SEARCHKEY";
    public static final String TAXCATEGORY = "TAXCATEGORY";
    public static final String EMAIL = "EMAIL";
    public static final String ATTRIBUTES = "ATTRIBUTES";
    
    private static String LOGIN_PASSWORD_KEY = "customer.login.password";

    @DatabaseField(generatedId = true, columnName = ID)
    private UUID id;

    @DatabaseField(columnName = NAME, canBeNull = false)
    private String name;

    @DatabaseField(columnName = SEARCHKEY, canBeNull = false)
    private String searchkey;

    @DatabaseField(foreign = true,
            columnName = TAXCATEGORY,
            foreignColumnName = TaxCategory.ID,
            foreignAutoRefresh = true,
            canBeNull = true)
    private TaxCategory taxCategory;

    @DatabaseField(columnName = EMAIL, canBeNull = true)
    private String email;

    @DatabaseField(columnName = ATTRIBUTES, dataType = DataType.BYTE_ARRAY)
    byte[] attributes;

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSearchkey() {
        return searchkey;
    }

    public void setSearchkey(String searchkey) {
        this.searchkey = searchkey;
    }

    public TaxCategory getTaxCategory() {
        return taxCategory;
    }

    public void setTaxCategory(TaxCategory taxCategory) {
        this.taxCategory = taxCategory;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public byte[] getAttributes() {
        return attributes;
    }

    public void setAttributes(byte[] attributes) {
        this.attributes = attributes;
    }

    public Properties getProperties() throws IOException {
        Properties properties = new Properties();
        if (this.attributes != null) {
            properties.loadFromXML(new ByteArrayInputStream(this.attributes));
        }
        return properties;
    }

    public void setProperties(Properties properties) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        properties.storeToXML(outputStream, "Customer attributes", "UTF-8");
        setAttributes(outputStream.toByteArray());
    }

    public String getPassword() throws IOException {
        return getProperties().getProperty(LOGIN_PASSWORD_KEY);
    }

    public void setPassword(String password) throws UnsupportedEncodingException, NoSuchAlgorithmException, IOException {
        Properties properties = getProperties();
        properties.setProperty(LOGIN_PASSWORD_KEY, Hashcypher.hashString(password));
        setProperties(properties);
    }

    public boolean isAuthentication(String password) throws UnsupportedEncodingException, NoSuchAlgorithmException, IOException {
        return Hashcypher.authenticate(password, getPassword());
    }

    @Override
    public int hashCode() {
        return name.hashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == null || other.getClass() != getClass()) {
            return false;
        }
        return name.equals(((Customer) other).name);
    }

}

@@ -752,16 +752,61 @@
 eField;%0A
+import com.j256.ormlite.table.DatabaseTable;%0A
 import c
@@ -1145,16 +1145,56 @@
 om%3E%0A */%0A
+@DatabaseTable(tableName = %22CUSTOMERS%22)%0A
 public c

Set table name for Customer model.